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
- File a rustc ICE with the minimized #[proc_macro_derive(...)] repro.
- Ensure the derive declaration has a trait name: #[proc_macro_derive(MyTrait)].
- Bisect across nightlies.
- Try stable to check whether it is a recent regression.
Defensive patterns
Strategy: validation
Prevention
- Always write #[proc_macro_derive(TraitName)] with an explicit trait name.
- Treat a panic here as a compiler bug; file an ICE with the minimized declaration.
- Bisect across nightlies with cargo bisect-rustc.
- Try stable to confirm whether it is a regression.
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
- unrecognized type identifier `{ty_name}`
- if `all` is specified, no other type identifiers may be give
- `fn_extra`: no default `_` pattern specified and the followi
- unrecognized meta expression `{s}`
- unexpected fields {map:?}
AI-assisted analysis of rust-lang/rust@7088e4b63a (2026-08-10).
Data as JSON: /api/errors/c6be512ff8b5d5e5.
Report an issue: GitHub.