rust-lang/rust · error

Trait name is mandatory, so it is present

Error message

Trait name is mandatory, so it is present

What it means

proc_macro_attrs.rs:38: ProcMacroDeriveParser::convert calls parse_derive_like(cx, args, true) (third arg true => name mandatory) then does trait_name.expect("Trait name is mandatory, so it is present"). The contract: when the mandatory flag is true, parse_derive_like returns Some(trait_name), so the Option is always Some.

Source

Thrown at compiler/rustc_attr_parsing/src/attributes/proc_macro_attrs.rs:38

    const ALLOWED_TARGETS: AllowedTargets<'_> = PROC_MACRO_ALLOWED_TARGETS;
    const STABILITY: AttributeStability = AttributeStability::Stable;
    const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::ProcMacroAttribute;
}

pub(crate) struct ProcMacroDeriveParser;
impl SingleAttributeParser for ProcMacroDeriveParser {
    const PATH: &[Symbol] = &[sym::proc_macro_derive];
    const ALLOWED_TARGETS: AllowedTargets<'_> = PROC_MACRO_ALLOWED_TARGETS;
    const TEMPLATE: AttributeTemplate = template!(
        List: &["TraitName", "TraitName, attributes(name1, name2, ...)"],
        "https://doc.rust-lang.org/reference/procedural-macros.html#derive-macros"
    );
    const STABILITY: AttributeStability = AttributeStability::Stable;

    fn convert(cx: &mut AcceptContext<'_, '_>, args: &ArgParser) -> Option<AttributeKind> {
        let (trait_name, helper_attrs) = parse_derive_like(cx, args, true)?;
        Some(AttributeKind::ProcMacroDerive {
            trait_name: trait_name.expect("Trait name is mandatory, so it is present"),
            helper_attrs,
        })
    }
}

pub(crate) struct RustcBuiltinMacroParser;
impl SingleAttributeParser for RustcBuiltinMacroParser {
    const PATH: &[Symbol] = &[sym::rustc_builtin_macro];
    const ALLOWED_TARGETS: AllowedTargets<'_> =
        AllowedTargets::AllowList(&[Allow(Target::MacroDef)]);
    const TEMPLATE: AttributeTemplate =
        template!(List: &["TraitName", "TraitName, attributes(name1, name2, ...)"]);
    const STABILITY: AttributeStability = unstable!(rustc_attrs);

    fn convert(cx: &mut AcceptContext<'_, '_>, args: &ArgParser) -> Option<AttributeKind> {
        let (builtin_name, helper_attrs) = parse_derive_like(cx, args, false)?;
        Some(AttributeKind::RustcBuiltinMacro { builtin_name, helper_attrs })
    }

View on GitHub (pinned to 7088e4b63a)

Solutions

  1. File a rustc ICE with the minimized #[proc_macro_derive(...)] repro.
  2. Ensure the derive declaration has a trait name: #[proc_macro_derive(MyTrait)].
  3. Bisect across nightlies.
  4. Try stable to check whether it is a recent regression.
Defensive patterns

Strategy: validation

Prevention

When it happens

Trigger: Parsing #[proc_macro_derive(...)] where parse_derive_like returns (None, _) despite the mandatory flag, an internal contract violation in the derive-attribute parser.

Common situations: A regression in the proc_macro_derive attribute parser; not reachable by well-formed #[proc_macro_derive(TraitName)] on a normal toolchain.

Related errors


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