{"id":"44d053b696ff49b1","repo":"rust-lang/rust","slug":"not-a-hir-owner","errorCode":null,"errorMessage":"Not a HIR owner","messagePattern":"Not a HIR owner","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"compiler/rustc_middle/src/hir/mod.rs","lineNumber":448,"sourceCode":"            MaybeOwner::Owner(o) => ProjectedMaybeOwner::Owner(ProjectedOwnerInfo {\n                nodes: &o.nodes,\n                parenting: &o.parenting,\n                trait_map: &o.trait_map,\n                delayed_lints: &o.delayed_lints,\n            }),\n            MaybeOwner::NonOwner(hir_id) => ProjectedMaybeOwner::NonOwner(hir_id),\n        }\n    }\n\n    pub fn as_owner(&self) -> Option<&ProjectedOwnerInfo<'tcx>> {\n        match self {\n            ProjectedMaybeOwner::Owner(i) => Some(i),\n            ProjectedMaybeOwner::NonOwner(_) => None,\n        }\n    }\n\n    pub fn unwrap(&'tcx self) -> &'tcx ProjectedOwnerInfo<'tcx> {\n        self.as_owner().unwrap_or_else(|| panic!(\"Not a HIR owner\"))\n    }\n}\n\npub fn provide(providers: &mut Providers) {\n    providers.hir_crate_items = map::hir_crate_items;\n    providers.crate_hash = map::crate_hash;\n    providers.hir_module_items = map::hir_module_items;\n    providers.hir_attr_map =\n        |tcx, id| tcx.lower_to_hir(id).as_owner().map_or(AttributeMap::EMPTY, |o| &o.attrs);\n    providers.hir_owner = |tcx, def_id| ProjectedMaybeOwner::new(tcx.lower_to_hir(def_id));\n    providers.hir_owner_parent_q = |tcx, owner_id| tcx.hir_owner_parent_impl(owner_id);\n    providers.def_span = |tcx, def_id| tcx.hir_span(tcx.local_def_id_to_hir_id(def_id));\n    providers.def_ident_span = |tcx, def_id| {\n        let hir_id = tcx.local_def_id_to_hir_id(def_id);\n        tcx.hir_opt_ident_span(hir_id)\n    };\n    providers.ty_span = |tcx, def_id| {\n        let node = tcx.hir_node_by_def_id(def_id);","sourceCodeStart":430,"sourceCodeEnd":466,"githubUrl":"https://github.com/rust-lang/rust/blob/22057b88b091743bc0fd8d592a9264f0a6951403/compiler/rustc_middle/src/hir/mod.rs#L430-L466","documentation":"Panic in ProjectedMaybeOwner::unwrap (compiler/rustc_middle/src/hir/mod.rs:448). The method calls as_owner() (returns Some only for ProjectedMaybeOwner::Owner) and otherwise panics with 'Not a HIR owner'. It is the checked accessor for the Owner variant; callers use it when they have already established that the value represents an owning node, and a NonOwner value is a contract violation.","triggerScenarios":"Calling .unwrap() on a ProjectedMaybeOwner (produced by ProjectedMaybeOwner::new(tcx.lower_to_hir(def_id))) when the def_id is a non-owner (e.g. an associated item, a nested item, or anything whose HIR is embedded in a parent owner rather than being an owner itself). The underlying lower_to_hir returned MaybeOwner::NonOwner(hir_id).","commonSituations":"Compiler passes or tools that assume every def_id is an owner; mismatch after HIR owner reclassification (e.g. moving an item from owner to nested); calling providers.hir_owner(...).unwrap() on a non-owner def_id.","solutions":["Use as_owner() / match on ProjectedMaybeOwner::Owner vs NonOwner before unwrapping.","Confirm the def_id you pass actually owns HIR (items, fn/trait/impl bodies that are owners); for non-owners use the parent owner.","If reached during normal compilation, report as an ICE with the owner_id/def_id context."],"exampleFix":"// before\nlet owner = tcx.hir_owner(def_id).unwrap();\n\n// after\nmatch tcx.hir_owner(def_id) {\n    ProjectedMaybeOwner::Owner(o) => { /* use o */ }\n    ProjectedMaybeOwner::NonOwner(hir_id) => { /* handle non-owner */ }\n}","handlingStrategy":"type-guard","validationCode":"// Before calling an API that requires a HIR owner, verify the\n// DefId/HirId actually resolves to an owner.\nuse rustc_hir::def_id::{DefId, LocalDefId};\nuse rustc_middle::ty::TyCtxt;\n\nfn is_hir_owner(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {\n    // hir_owner_nodes returns None for non-owner items\n    tcx.hir_owner_nodes(def_id).is_some()\n}","typeGuard":"// Narrow a LocalDefId to a confirmed HIR owner before\n// invoking owner-scoped queries.\nuse rustc_hir::def_id::LocalDefId;\nuse rustc_middle::ty::TyCtxt;\n\nfn as_hir_owner<'tcx>(\n    tcx: TyCtxt<'tcx>,\n    def_id: LocalDefId,\n) -> Option<LocalDefId> {\n    tcx.hir_owner_nodes(def_id)\n        .map(|_| def_id)\n        // or, if using the older API:\n        .or_else(|| {\n            let map = tcx.hir();\n            map.opt_local_def_id_to_hir_id(def_id)\n                .filter(|hid| map.is_owner(*hid))\n                .map(|_| def_id)\n        })\n}","tryCatchPattern":null,"preventionTips":["Always check tcx.hir_owner_nodes(def_id).is_some() before calling owner-scoped HIR queries","Do not assume a DefId from a foreign crate resolves to a local HIR owner; cross-crate items lack local HIR","For inline or synthetic DefIds generated by macros, verify ownership before traversal","Prefer LocalDefId over HirId for owner-scoped APIs to make the ownership requirement explicit at the type level"],"tags":["rustc","hir","owner","ice"],"analyzedSha":"22057b88b091743bc0fd8d592a9264f0a6951403","analyzedAt":"2026-08-03T08:09:25.915Z","schemaVersion":2}