Skip to content

Commit 2e21b54

Browse files
committed
rustc_middle: Document which exactly DefIds don't have DefKinds
1 parent 8a7ca93 commit 2e21b54

File tree

1 file changed

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

1 file changed

+18
-3
lines changed

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;
@@ -180,7 +180,19 @@ impl<'hir> Map<'hir> {
180180
/// Do not call this function directly. The query should be called.
181181
pub(super) fn opt_def_kind(self, local_def_id: LocalDefId) -> Option<DefKind> {
182182
let hir_id = self.local_def_id_to_hir_id(local_def_id);
183-
let def_kind = match self.find(hir_id)? {
183+
let node = match self.find(hir_id) {
184+
Some(node) => node,
185+
None => match self.def_key(local_def_id).disambiguated_data.data {
186+
// FIXME: Some anonymous constants do not have corresponding HIR nodes,
187+
// so many local queries will panic on their def ids. `None` is currently
188+
// returned here instead of `DefKind::{Anon,Inline}Const` to avoid such panics.
189+
// Ideally all def ids should have `DefKind`s, we need to create the missing
190+
// HIR nodes or feed relevant query results to achieve that.
191+
DefPathData::AnonConst => return None,
192+
_ => bug!("no HIR node for def id {local_def_id:?}"),
193+
},
194+
};
195+
let def_kind = match node {
184196
Node::Item(item) => match item.kind {
185197
ItemKind::Static(_, mt, _) => DefKind::Static(mt),
186198
ItemKind::Const(..) => DefKind::Const,
@@ -267,7 +279,10 @@ impl<'hir> Map<'hir> {
267279
| Node::Param(_)
268280
| Node::Arm(_)
269281
| Node::Lifetime(_)
270-
| Node::Block(_) => return None,
282+
| Node::Block(_) => span_bug!(
283+
self.span(hir_id),
284+
"unexpected node with def id {local_def_id:?}: {node:?}"
285+
),
271286
};
272287
Some(def_kind)
273288
}

0 commit comments

Comments
 (0)