-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Description
Problem
Type inference error (E0283) occurs when using if/else if/else
conditional expressions for attribute values in the rsx!
macro
when reqwest
is included as a dependency.
The compiler reports:
error[E0283]: type annotations needed
cannot infer type
= note: cannot satisfy : IntoAttributeValue<>
This happens because both bytes::Bytes
and reqwest::Body
implement From<&'static str>
, creating an ambiguity during the
rsx!
macro expansion that the compiler cannot resolve.
Steps To Reproduce
- Add dependencies to
Cargo.toml
:
[dependencies]
dioxus = { version = "0.6", features = ["web"] }
reqwest = { version = "0.12", features = ["json"] }
- Create a component with conditional class attribute using if/else if/else:
use dioxus::prelude::*;
fn App() -> Element {
let is_recording = use_signal(|| false);
let is_transcribing = use_signal(|| false);
rsx! {
div {
button {
class: if is_recording() {
"bg-red-600"
} else if is_transcribing() {
"bg-gray-600"
} else {
"bg-gray-700"
},
"Click me"
}
}
}
}
- Build for WASM: dx build --platform web
- Compilation fails with E0283 error
08:24:32 [cargo] error[E0283]: type annotations needed
--> src/main.rs:67:5
|
67 | / rsx! {
68 | | div {
69 | | button {
70 | | class: if is_recording() {
... |
80 | | }
| |_____^ cannot infer type
|
= note: cannot satisfy `_: IntoAttributeValue<_>`
= help: the following types implement trait `IntoAttributeValue<T>`:
&str
Arguments<'_>
AttributeValue
MappedSignal<O, S>
Memo<T>
Option<T>
Rc<(dyn AnyValue + 'static)>
ReadOnlySignal<T>
and 18 others
Expected behavior
Conditional if/else if/else expressions should work for attribute values without type inference errors. The macro should be able to infer that string literals should be converted to IntoAttributeValue without ambiguity, even when multiple crates in the
dependency tree implement From<&str> for different types.
Environment:
- Dioxus version: v0.6
- Rust version: 1.83.0 (stable)
- OS info: macOS 14.4 (also occurs on Linux)
- App platform: web (wasm32-unknown-unknown)
Workarounds
Two workarounds exist:
- Wrap conditional in format!():
class: format!("{}",
if is_recording() { "bg-red-600" }
else if is_transcribing() { "bg-gray-600" }
else { "bg-gray-700" }
)
- Assign to a variable first:
let class_name = if is_recording() { "bg-red-600" }
else if is_transcribing() { "bg-gray-600" }
else { "bg-gray-700" };
rsx! {
button { class: class_name, "Click me" }
}
Root Cause Analysis
The issue stems from:
- reqwest depends on bytes crate (transitive dependency)
- Both bytes::Bytes and reqwest::Body implement From<&'static str>
- The rsx! macro expansion with conditional expressions creates a context where Rust cannot determine which From impl to use
- Simple if/else may work, but if/else if/else consistently triggers the error
Questionnaire
I'm interested in fixing this myself but don't know where to start.