yewstack/yew · error · syn::Error

this `while` expression has a condition, but no block

Error message

this `while` expression has a condition, but no block

What it means

After parsing the condition, `HtmlWhile::parse` checks `input.is_empty()` (html_while.rs:39-45); if the input ends immediately after the condition, the required brace-delimited body is missing and the error points at the condition span. `while` inside `html!` must always carry its `{ … }` body, just like plain Rust.

Source

Thrown at packages/yew-macro/src/html_tree/html_while.rs:41

        (ident == "while").then_some(())
    }
}

impl Parse for HtmlWhile {
    fn parse(input: ParseStream) -> syn::Result<Self> {
        While::parse(input)?;
        let cond = Box::new(input.call(Expr::parse_without_eager_brace)?);
        match &*cond {
            Expr::Block(syn::ExprBlock { block, .. }) if block.stmts.is_empty() => {
                return Err(syn::Error::new(
                    cond.span(),
                    "missing condition for `while` expression",
                ));
            }
            _ => {}
        }
        if input.is_empty() {
            return Err(syn::Error::new(
                cond.span(),
                "this `while` expression has a condition, but no block",
            ));
        }

        let body_stream;
        braced!(body_stream in input);

        let (stmts, body, deprecations) = parse_loop_body(&body_stream, "while")?;

        Ok(Self {
            cond,
            stmts,
            body,
            deprecations,
        })
    }
}

View on GitHub (pinned to 0e4a05472f)

Solutions

  1. Add the body block: `while loading { <Spinner /> }`
  2. Compute repeated children in plain Rust and render the result once
  3. Double-check the construct — for a one-shot conditional, `if` is likely what was meant

Example fix

// before
html! { while loading }

// after
html! { while loading { <Spinner /> } }
Defensive patterns

Strategy: validation

Prevention

When it happens

Trigger: `html! { while loading }` — a condition present but nothing after it (end of macro input).

Common situations: Truncated templates while sketching loading spinners, forgetting the body block, or expecting following sibling children to serve as the body without braces.

Related errors


AI-assisted analysis of yewstack/yew@0e4a05472f (2026-08-22). Data as JSON: /api/errors/6b2803058e075c71. Report an issue: GitHub.