{"id":"0cbf496d18cf4f32","repo":"rust-lang/rust","slug":"not-a-module-node","errorCode":null,"errorMessage":"not a module: {node:?}","messagePattern":"not a module: (.+?)","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"compiler/rustc_middle/src/hir/map.rs","lineNumber":420,"sourceCode":"    }\n\n    /// Gets the attributes on the crate. This is preferable to\n    /// invoking `krate.attrs` because it registers a tighter\n    /// dep-graph access.\n    pub fn hir_krate_attrs(self) -> &'tcx [Attribute] {\n        self.hir_attrs(CRATE_HIR_ID)\n    }\n\n    pub fn hir_rustc_coherence_is_core(self) -> bool {\n        find_attr!(self.hir_krate_attrs(), RustcCoherenceIsCore)\n    }\n\n    pub fn hir_get_module(self, module: LocalModId) -> (&'tcx Mod<'tcx>, Span, HirId) {\n        let hir_id = HirId::make_owner(module.to_local_def_id());\n        match self.hir_owner_node(hir_id.owner) {\n            OwnerNode::Item(&Item { span, kind: ItemKind::Mod(_, m), .. }) => (m, span, hir_id),\n            OwnerNode::Crate(item) => (item, item.spans.inner_span, hir_id),\n            node => panic!(\"not a module: {node:?}\"),\n        }\n    }\n\n    /// Walks the contents of the local crate. See also `visit_all_item_likes_in_crate`.\n    pub fn hir_walk_toplevel_module<V>(self, visitor: &mut V) -> V::Result\n    where\n        V: Visitor<'tcx>,\n    {\n        let (top_mod, span, hir_id) = self.hir_get_module(CRATE_MOD_ID);\n        visitor.visit_mod(top_mod, span, hir_id)\n    }\n\n    /// Walks the attributes in a crate.\n    pub fn hir_walk_attributes<V>(self, visitor: &mut V) -> V::Result\n    where\n        V: Visitor<'tcx>,\n    {\n        let krate = self.hir_crate_items(());","sourceCodeStart":402,"sourceCodeEnd":438,"githubUrl":"https://github.com/rust-lang/rust/blob/22057b88b091743bc0fd8d592a9264f0a6951403/compiler/rustc_middle/src/hir/map.rs#L402-L438","documentation":"Panic in hir_get_module (compiler/rustc_middle/src/hir/map.rs:420). Given a LocalModId, it constructs the owner HirId and expects hir_owner_node to return an OwnerNode::Item of ItemKind::Mod, or OwnerNode::Crate. Any other node shape triggers this, meaning the LocalModId did not refer to an actual module/crate owner.","triggerScenarios":"Calling tcx.hir_get_module(id) with a LocalModId whose owning def is not a mod item (e.g. a block expression, an inline module whose owner is a function, or an out-of-range/corrupted LocalModId). Happens inside rustc passes or custom lints/tools that walk module trees.","commonSituations":"Compiler-internal bug where a non-module owner is registered in the module map; procedural macros or third-party analysis tools that synthesize or pass through HirId/LocalModId values incorrectly; mismatch after a HIR refactor.","solutions":["If this is your own tool/lint, validate that the LocalModId you pass actually points at a mod owner before calling hir_get_module.","Report as a rustc ICE if reached through normal compilation — include the node printed in the message.","Try a clean build or a different nightly to rule out a stale incremental cache holding a bad HIR."],"exampleFix":null,"handlingStrategy":"type-guard","validationCode":"// Before treating a HirId as a module, confirm the HIR node kind\n// is actually a module item.\nuse rustc_hir::{HirId, Node, Item, ItemKind};\nuse rustc_middle::ty::TyCtxt;\n\nfn hir_node_is_module(tcx: TyCtxt<'_>, hir_id: HirId) -> bool {\n    matches!(\n        tcx.hir().get(hir_id),\n        Node::Item(Item { kind: ItemKind::Mod(..), .. })\n    )\n}","typeGuard":"// Narrow a HirId to a known-module node before calling\n// module-specific HIR map methods.\nuse rustc_hir::{HirId, Node, Item, ItemKind};\nuse rustc_middle::ty::TyCtxt;\n\nfn as_module_owner<'tcx>(\n    tcx: TyCtxt<'tcx>,\n    hir_id: HirId,\n) -> Option<&'tcx Item<'tcx>> {\n    if let Node::Item(item) = tcx.hir().get(hir_id) {\n        if matches!(item.kind, ItemKind::Mod(..)) {\n            return Some(item);\n        }\n    }\n    None\n}","tryCatchPattern":null,"preventionTips":["Always check tcx.hir().get(hir_id) against Node::Item with ItemKind::Mod before calling module-only APIs","Do not assume a DefId maps to a module just because it appears in a path segment","When walking HIR from a macro-generated node, verify the kind at every hop rather than skipping checks"],"tags":["rustc","hir","modules","ice"],"analyzedSha":"22057b88b091743bc0fd8d592a9264f0a6951403","analyzedAt":"2026-08-03T08:09:25.915Z","schemaVersion":2}