{"id":"10fec6dd85a8110f","repo":"rust-lang/rust","slug":"file-modules-must-have-an-attribute-to-exclude","errorCode":null,"errorMessage":"file modules must have an attribute to exclude","messagePattern":"file modules must have an attribute to exclude","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"compiler/rustc_parse/src/lib.rs","lineNumber":316,"sourceCode":"    }\n\n    let source = pprust::item_to_string(item);\n    let filename = FileName::macro_expansion_source_code(&source);\n    unwrap_or_emit_fatal(source_str_to_stream(psess, filename, source, Some(item.span)))\n}\n\nfn fake_token_stream_for_file_mod(\n    psess: &ParseSess,\n    item: &ast::Item,\n    attr_to_exclude: Option<&ast::Attribute>,\n) -> Option<TokenStream> {\n    let ast::ItemKind::Mod(_, _, ast::ModKind::Loaded(_, ast::Inline::No { .. }, spans)) =\n        &item.kind\n    else {\n        return None;\n    };\n\n    let attr = attr_to_exclude.expect(\"file modules must have an attribute to exclude\");\n    assert_eq!(attr.style, ast::AttrStyle::Inner);\n\n    let mut body_tts = Vec::new();\n    body_tts.extend(lex_token_trees_for_span(psess, spans.inner_span.until(attr.span))?);\n    body_tts.extend(lex_token_trees_for_span(\n        psess,\n        attr.span.between(spans.inner_span.shrink_to_hi()),\n    )?);\n\n    let mut wrapper_tts = Vec::new();\n    for attr in item.attrs.iter().filter(|attr| attr.style == ast::AttrStyle::Outer) {\n        wrapper_tts.extend(attr.token_trees());\n    }\n    wrapper_tts.extend(lex_token_trees_for_span(psess, item.span)?);\n    let Some(TokenTree::Token(semi, _)) = wrapper_tts.pop() else {\n        return None;\n    };\n    if semi.kind != token::Semi {","sourceCodeStart":298,"sourceCodeEnd":334,"githubUrl":"https://github.com/rust-lang/rust/blob/22057b88b091743bc0fd8d592a9264f0a6951403/compiler/rustc_parse/src/lib.rs#L298-L334","documentation":"Internal panic in `fake_token_stream_for_file_mod`, used to reconstruct a `TokenStream` for a file-based module item (one declared via `mod foo;` and loaded from a separate file, i.e. `ModKind::Loaded(_, Inline::No, ..)`). Such modules are required to carry the inner attribute the caller wants stripped, so `attr_to_exclude` must be `Some`. Passing `None` for a file module violates the function's contract and triggers `.expect`.","triggerScenarios":"The attribute/macros infrastructure calls `fake_token_stream_for_item` (or `fake_token_stream_for_file_mod`) on a file-module item while passing `attr_to_exclude: None`.","commonSituations":"Compiler regression in attribute expansion or cfg-attribute processing applied to `mod foo;` file modules; usually after changes to inner-attribute handling.","solutions":["Report as a rustc ICE with the crate and the specific file module (`mod foo;`) that triggers it","As a workaround, inline the module (`mod foo { ... }`) to take the `Inline::Yes` path that bypasses this function","Bisect nightly toolchains to locate the introducing commit"],"exampleFix":"// before: file module triggers the path\nmod foo;\n// after: inline the module to sidestep file-mod token reconstruction\nmod foo {\n    // ...\n}","handlingStrategy":"validation","validationCode":"use rustc_ast::ast;\nfn module_has_exclude_attr(item: &ast::Item) -> bool {\n    item.attrs.iter().any(|a| {\n        matches!(a.kind, ast::AttrKind::Normal(ref n)\n            if matches!(n.item.path.segments.iter().last().map(|s| s.ident.as_str()), Some(\"no_implicit_prelude\") | Some(\"cfg\")))\n    })\n}\nif !module_has_exclude_attr(&mod_item) { return Err(\"file module lacks exclude attribute\"); }","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Before asking the parser to exclude a file module, attach the exclusion attribute (e.g. #![cfg(...)] / #![no_implicit_prelude]) in the same pass.","Track whether a module came from a `mod.rs`/file path; file modules are the ones that require the attribute.","Keep attribute insertion atomic with module registration so an exclude request never reaches the parser without its attribute.","Reject exclude requests for inline modules early — they don't need the attribute and will trip the assertion."],"tags":["compiler-ice","parser","modules","attributes"],"analyzedSha":"22057b88b091743bc0fd8d592a9264f0a6951403","analyzedAt":"2026-08-03T08:09:25.915Z","schemaVersion":2}