yewstack/yew · error · syn::Error

missing condition for `while` expression

Error message

missing condition for `while` expression

What it means

`HtmlWhile::parse` parses the `while` token and then the condition via `Expr::parse_without_eager_brace` (html_while.rs:30-31); if the parsed condition is an empty block `{}`, no condition was written and the macro errors on the condition span (html_while.rs:32-38). It is the same guard the `if` variant applies, extended to `while` loops in `html!`.

Source

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

    stmts: Vec<Stmt>,
    body: HtmlChildrenTree,
    deprecations: TokenStream,
}

impl PeekValue<()> for HtmlWhile {
    fn peek(cursor: Cursor) -> Option<()> {
        let (ident, _) = cursor.ident()?;
        (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")?;

View on GitHub (pinned to 0e4a05472f)

Solutions

  1. Write the loop condition: `while loading { … }`
  2. If the condition is dynamic, compute a `bool` before `html!` and use that identifier in the loop
  3. For unconditional repetition use `for _ in 0..n` instead

Example fix

// before
html! { while {} { <Spinner /> } }

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

Strategy: validation

Prevention

When it happens

Trigger: `html! { while {} { <Spinner /> } }` — an empty block between `while` and the loop body instead of a boolean expression.

Common situations: A placeholder condition left by a snippet, intending `{}` as a grouping block, or editing away the real condition and leaving the braces behind.

Related errors


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