{"record":{"id":"62e7e057146a0caa","repo":"rust-lang/rust-analyzer","slug":"macro-definition-has-parse-errors","errorCode":null,"errorMessage":"macro definition has parse errors","messagePattern":"macro definition has parse errors","errorType":"error_code","errorClass":"ExpandError","httpStatus":null,"severity":"error","filePath":"crates/hir-expand/src/declarative.rs","lineNumber":42,"sourceCode":"pub struct DeclarativeMacroExpander {\n    pub mac: mbe::DeclarativeMacro,\n    pub transparency: Transparency,\n    edition: Edition,\n}\n\nimpl DeclarativeMacroExpander {\n    pub fn expand(\n        &self,\n        db: &dyn SourceDatabase,\n        tt: &tt::TopSubtree,\n        call_id: MacroCallId,\n        span: Span,\n    ) -> ExpandResult<(tt::TopSubtree, Option<u32>)> {\n        let loc = call_id.loc(db);\n        match self.mac.err() {\n            Some(_) => ExpandResult::new(\n                (tt::TopSubtree::empty(tt::DelimSpan { open: span, close: span }), None),\n                ExpandError::new(span, ExpandErrorKind::MacroDefinition),\n            ),\n            None => self\n                .mac\n                .expand(\n                    db,\n                    tt,\n                    |s| {\n                        s.ctx =\n                            apply_mark(db, s.ctx, call_id.into(), self.transparency, self.edition)\n                    },\n                    loc.kind.call_style(),\n                    span,\n                )\n                .map_err(Into::into),\n        }\n    }\n\n    pub fn expand_unhygienic(","sourceCodeStart":24,"sourceCodeEnd":60,"githubUrl":"https://github.com/rust-lang/rust-analyzer/blob/e8f7e90aa3e7b26aa9a000200f606c1078da99ec/crates/hir-expand/src/declarative.rs#L24-L60","documentation":"Declarative macro expansion (macro_rules!/macro) in hir-expand checks whether the stored macro definition body (self.mac) contains a parse error before expanding. If so, it skips expansion and returns an empty subtree with ExpandErrorKind::MacroDefinition. The caller sees 'macro definition has parse errors' at the call site span.","triggerScenarios":"Invoking a declarative macro whose definition body failed to parse — e.g. `macro_rules!` with unbalanced delimiters, invalid tokens in the definition, or a definition recovered badly by the parser — then calling MacroDef::expand on it.","commonSituations":"Typing a macro_rules! definition with a syntax mistake (missing semicolon, unbalanced braces/parens); user code that doesn't compile but is open in the IDE, so diagnostics show macro-definition errors alongside parser errors; edition/token issues where the parser misreads the definition.","solutions":["Fix the syntax of the macro_rules!/macro definition itself; look for parser errors reported at the definition site.","Check for unbalanced delimiters or stray tokens in the macro body and its match arms.","If the definition looks valid but still errors, check for an unstable-token/edition mismatch and report a rust-analyzer parser bug with a minimal fixture.","In IDE use, look at the definition location (the error is raised at the call site but the cause is at the def)."],"exampleFix":"// before\nmacro_rules! m {\n    () => { 42 }\n    (x) => { x }  // missing semicolon after first arm\n}\n// after\nmacro_rules! m {\n    () => { 42 };\n    (x) => { x };\n}","handlingStrategy":"try-catch","validationCode":"fn macro_def_ok(def: &MacroDef) -> bool { def.err().is_none() } // check before expanding\n","typeGuard":null,"tryCatchPattern":"let res: ExpandResult<_> = def.expand(db, call_id, tt, span);\nif let Err(err) = &res.value.1 {\n    if matches!(err.kind, ExpandErrorKind::MacroDefinition) {\n        // definition has parse errors: surface diagnostics at the def site, skip expansion\n    }\n}","preventionTips":["Keep macro_rules! bodies syntactically valid: balanced delimiters, `;` between arms.","Check IDE/parser errors at the definition site before debugging expansion output.","Compare with rustc to distinguish user error from rust-analyzer parser bugs."],"tags":["rust","macros","parse-error","ide"],"backgroundTag":"macro-definition-parse-error","analyzedSha":"e8f7e90aa3e7b26aa9a000200f606c1078da99ec","analyzedAt":"2026-09-03T21:08:06.959Z","contentChangedAt":"2026-09-03T21:08:06.959Z","schemaVersion":2},"datasetVersion":"2026-09-11T07:07:21.782Z"}