DioxusLabs/dioxus · error · syn::Error

Expected a single statement in the if block

Error message

Expected a single statement in the if block

What it means

An attribute value written as a block `{ ... }` inside an if/else attribute branch contained zero or multiple statements. The RSX parser needs exactly one expression (or format string) per block to build the attribute value.

Solutions

  1. Reduce the if block in the attribute to a single statement or expression; multiple statements are not supported there.
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at packages/rsx/src/attribute.rs:550 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of DioxusLabs/dioxus@24f6a829df (2026-08-23). Data as JSON: /api/errors/6163ff42f263a389. Report an issue: GitHub.

Appendix: source

Thrown at packages/rsx/src/attribute.rs:550

            match expr {
                AttributeValue::IfExpr(if_expr) => if_expr.contains_expression(),
                AttributeValue::AttrLiteral(_) => false,
                _ => true,
            }
        }

        attribute_value_contains_expression(&self.then_value)
            || self
                .else_value
                .as_deref()
                .is_some_and(attribute_value_contains_expression)
    }

    fn parse_attribute_value_from_block(block: &Block) -> syn::Result<Box<AttributeValue>> {
        let stmts = &block.stmts;

        if stmts.len() != 1 {
            return Err(syn::Error::new(
                block.span(),
                "Expected a single statement in the if block",
            ));
        }

        // either an ifmt or an expr in the block
        let stmt = &stmts[0];

        // Either it's a valid ifmt or an expression
        match stmt {
            syn::Stmt::Expr(exp, None) => {
                // Try parsing the statement as an IfmtInput by passing it through tokens
                let value: Result<HotLiteral, syn::Error> = syn::parse2(exp.to_token_stream());
                Ok(match value {
                    Ok(res) => Box::new(AttributeValue::AttrLiteral(res)),
                    Err(_) => Box::new(AttributeValue::AttrExpr(PartialExpr::from_expr(exp))),
                })
            }

View on GitHub (pinned to 24f6a829df)