rust-lang/rust · error · syn::Error

no guards allowed in this position

Error message

no guards allowed in this position

What it means

Match arms in fn_extra dispatch purely on function-name patterns and cannot carry `if` guards (parse.rs:197-200). A guard like `foo if cond => ...` is rejected because the macro needs deterministic name-keyed dispatch.

Source

Thrown at library/compiler-builtins/crates/libm-macros/src/parse.rs:198

    for arm in arms {
        let Arm {
            attrs,
            pat,
            guard,
            fat_arrow_token: _,
            body,
            comma: _,
        } = arm;

        expect_empty_attrs(&attrs)?;

        let keys = match pat {
            syn::Pat::Wild(w) => vec![Ident::new("_", w.span())],
            _ => Parser::parse2(parse_ident_pat, pat.into_token_stream())?,
        };

        if let Some(guard) = guard {
            let e = syn::Error::new(guard.0.span(), "no guards allowed in this position");
            return Err(e);
        }

        for key in keys {
            let inserted = res.insert(key.clone(), *body.clone());
            if inserted.is_some() {
                let e = syn::Error::new(key.span(), format!("key `{key}` specified twice"));
                return Err(e);
            }
        }
    }

    Ok(res)
}

fn expect_empty_attrs(attrs: &[Attribute]) -> syn::Result<()> {
    if attrs.is_empty() {
        return Ok(());

View on GitHub (pinned to 7088e4b63a)

Solutions

  1. Remove the `if` guard from the arm.
  2. Move the conditional logic into the arm body expression (e.g. an if/else inside the body).

Example fix

// before
fn_extra: match MACRO_FN_NAME {
    sin if cfg!(foo) => |x| x.sin(),
    _ => |x| x,
},
// after
fn_extra: match MACRO_FN_NAME {
    sin => |x| if cfg!(foo) { x.sin() } else { x },
    _ => |x| x,
},
Defensive patterns

Strategy: validation

Prevention

When it happens

Trigger: Writing `fn_extra: match MACRO_FN_NAME { foo if some_cond => ... }`.

Common situations: Trying to add conditional logic per function; assuming full Rust match semantics apply.

Related errors


AI-assisted analysis of rust-lang/rust@7088e4b63a (2026-08-10). Data as JSON: /api/errors/ad00f7d065ce8ea4. Report an issue: GitHub.