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

expected an identifier

Error message

expected an identifier

What it means

Thrown by `#[function_enum]` (enums.rs:25) when the single attribute is not an identifier. The attribute must be the name of the `BaseName` enum, so literals, punctuation, or groups are rejected.

Source

Thrown at library/compiler-builtins/crates/libm-macros/src/enums.rs:25

use crate::{ALL_OPERATIONS, base_name};

/// Implement `#[function_enum]`, see documentation in `lib.rs`.
pub fn function_enum(
    mut item: ItemEnum,
    attributes: pm2::TokenStream,
) -> syn::Result<pm2::TokenStream> {
    expect_empty_enum(&item)?;
    let attr_span = attributes.span();
    let mut attr = attributes.into_iter();

    // Attribute should be the identifier of the `BaseName` enum.
    let Some(tt) = attr.next() else {
        return Err(syn::Error::new(attr_span, "expected one attribute"));
    };

    let pm2::TokenTree::Ident(base_enum) = tt else {
        return Err(syn::Error::new(tt.span(), "expected an identifier"));
    };

    if let Some(tt) = attr.next() {
        return Err(syn::Error::new(
            tt.span(),
            "unexpected token after identifier",
        ));
    }

    let enum_name = &item.ident;
    let mut as_str_arms = Vec::new();
    let mut from_str_arms = Vec::new();
    let mut base_arms = Vec::new();

    for func in ALL_OPERATIONS.iter() {
        let fn_name = func.name;
        let ident = Ident::new(&fn_name.to_upper_camel_case(), Span::call_site());
        let bname_ident = Ident::new(&base_name(fn_name).to_upper_camel_case(), Span::call_site());

View on GitHub (pinned to 7088e4b63a)

Solutions

  1. Pass a single bare identifier: `#[function_enum(BaseName)]`.
  2. Avoid paths, strings, or fully-qualified names in the attribute.

Example fix

// before
#[function_enum("BaseName")]
enum Func {}

// after
#[function_enum(BaseName)]
enum Func {}
Defensive patterns

Strategy: validation

Prevention

When it happens

Trigger: Providing a non-identifier token as the attribute, e.g. `#[function_enum("BaseName")]`, `#[function_enum(123)]`, or `#[function_enum(::BaseName)]`.

Common situations: Mis-typing the enum name; attempting to pass a path/string instead of a bare identifier.

Related errors


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