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

no attributes allowed in this position

Error message

no attributes allowed in this position

What it means

Attributes (e.g. `#[cfg(...)]`, `#[test]`) are not permitted on the fn_extra match expression itself nor on its individual arms. expect_empty_attrs() is called on both the match and each arm and rejects any attributes present (parse.rs:170, 190, 214-224).

Source

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

        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(());
    }

    let e = syn::Error::new(
        attrs.first().unwrap().span(),
        "no attributes allowed in this position",
    );
    Err(e)
}

/// Extract a named field from a map, raising an error if it doesn't exist.
fn expect_field(v: &mut Vec<Mapping>, name: &str) -> syn::Result<Expr> {
    let pos = v.iter().position(|v| v.name == name).ok_or_else(|| {
        syn::Error::new(
            Span::call_site(),
            format!("missing expected field `{name}`"),
        )
    })?;

    Ok(v.remove(pos).expr)
}

View on GitHub (pinned to 7088e4b63a)

Solutions

  1. Remove the attribute from the match/arm.
  2. Control which functions are included via the top-level attributes, skip, or only fields instead.

Example fix

// before
fn_extra: #[cfg(test)]
    match MACRO_FN_NAME {
        _ => |x| x,
    },
// after
fn_extra: match MACRO_FN_NAME {
    _ => |x| x,
},
Defensive patterns

Strategy: validation

Prevention

When it happens

Trigger: `fn_extra: #[cfg(test)] match MACRO_FN_NAME { ... }` or an arm like `#[attr] foo => ...`.

Common situations: Trying to conditionally compile arms; pasting attributes that are legal on normal match arms but not here.

Related errors


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