Skip to content

Commit 5ca6e98

Browse files
committed
Auto merge of #109778 - petrochenkov:allkind, r=cjgillot
rustc_middle: Document which exactly `DefId`s don't have `DefKind`s I don't currently have time to investigate when and how to create these missing HIR nodes, but if someone else could do that it would be great.
2 parents 194a0bb + 2e21b54 commit 5ca6e98

File tree

1 file changed

+18
-3
lines changed
  • compiler/rustc_middle/src/hir/map

1 file changed

+18
-3
lines changed

Diff for: compiler/rustc_middle/src/hir/map/mod.rs

+18-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use rustc_data_structures::svh::Svh;
88
use rustc_data_structures::sync::{par_for_each_in, Send, Sync};
99
use rustc_hir::def::{DefKind, Res};
1010
use rustc_hir::def_id::{DefId, LocalDefId, CRATE_DEF_ID, LOCAL_CRATE};
11-
use rustc_hir::definitions::{DefKey, DefPath, DefPathHash};
11+
use rustc_hir::definitions::{DefKey, DefPath, DefPathData, DefPathHash};
1212
use rustc_hir::intravisit::{self, Visitor};
1313
use rustc_hir::*;
1414
use rustc_index::vec::Idx;
@@ -179,7 +179,19 @@ impl<'hir> Map<'hir> {
179179
/// Do not call this function directly. The query should be called.
180180
pub(super) fn opt_def_kind(self, local_def_id: LocalDefId) -> Option<DefKind> {
181181
let hir_id = self.local_def_id_to_hir_id(local_def_id);
182-
let def_kind = match self.find(hir_id)? {
182+
let node = match self.find(hir_id) {
183+
Some(node) => node,
184+
None => match self.def_key(local_def_id).disambiguated_data.data {
185+
// FIXME: Some anonymous constants do not have corresponding HIR nodes,
186+
// so many local queries will panic on their def ids. `None` is currently
187+
// returned here instead of `DefKind::{Anon,Inline}Const` to avoid such panics.
188+
// Ideally all def ids should have `DefKind`s, we need to create the missing
189+
// HIR nodes or feed relevant query results to achieve that.
190+
DefPathData::AnonConst => return None,
191+
_ => bug!("no HIR node for def id {local_def_id:?}"),
192+
},
193+
};
194+
let def_kind = match node {
183195
Node::Item(item) => match item.kind {
184196
ItemKind::Static(_, mt, _) => DefKind::Static(mt),
185197
ItemKind::Const(..) => DefKind::Const,
@@ -266,7 +278,10 @@ impl<'hir> Map<'hir> {
266278
| Node::Param(_)
267279
| Node::Arm(_)
268280
| Node::Lifetime(_)
269-
| Node::Block(_) => return None,
281+
| Node::Block(_) => span_bug!(
282+
self.span(hir_id),
283+
"unexpected node with def id {local_def_id:?}: {node:?}"
284+
),
270285
};
271286
Some(def_kind)
272287
}

0 commit comments

Comments
 (0)