Skip to content

create_async_runtime: Make cfg checks exhaustive #2937

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion sdk/typespec/typespec_client_core/src/async_runtime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,11 @@ pub fn set_async_runtime(runtime: Arc<dyn AsyncRuntime>) -> crate::Result<()> {
}

fn create_async_runtime() -> Arc<dyn AsyncRuntime> {
#[cfg(all(feature = "wasm_bindgen", feature = "tokio"))]
compile_error!(
"feature \"wasm_bindgen\" and feature \"tokio\" cannot be enabled at the same time"
);

#[cfg(all(target_arch = "wasm32", feature = "wasm_bindgen"))]
{
Arc::new(web_runtime::WasmBindgenRuntime) as Arc<dyn AsyncRuntime>
Expand All @@ -193,7 +198,10 @@ fn create_async_runtime() -> Arc<dyn AsyncRuntime> {
{
Arc::new(tokio_runtime::TokioRuntime) as Arc<dyn AsyncRuntime>
}
#[cfg(not(any(feature = "tokio", feature = "wasm_bindgen")))]
#[cfg(not(any(
feature = "tokio",
all(target_arch = "wasm32", feature = "wasm_bindgen")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this way - with mutually-exclusive features as added above - instead of how I had it? Basically, if you were on wasm but didn't specify wasm_bindgen, you'd get the standard runtime which, yes, would fail on wasm anyway. But it would fail with the compile_error now so nothing has changed except that with how I had it, if - somehow - standard did support wasm (highly doubtful, but imagine a world for std did support this) it would just work.

Is your goal here only to provide a better error message?

Copy link
Contributor Author

@magodo magodo Aug 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@heaths It is a leak of the check here in case you have specified wasm_bindgen but not targeting wasm32. In this case, this function will return a () (instead of one of the runtime instance). By doing this change, this case will be caught by this cfg, and return the standard runtime as expected.

)))]
{
Arc::new(standard_runtime::StdRuntime) as Arc<dyn AsyncRuntime>
}
Expand Down
Loading