Skip to content

Commit 2c23386

Browse files
committed
rustc: Make def_kind mandatory for all DefIds
1 parent fad6bb8 commit 2c23386

File tree

13 files changed

+36
-56
lines changed

13 files changed

+36
-56
lines changed

compiler/rustc_metadata/src/rmeta/decoder.rs

+6-9
Original file line numberDiff line numberDiff line change
@@ -850,7 +850,7 @@ impl MetadataBlob {
850850
) -> io::Result<()> {
851851
let root = blob.get_root();
852852

853-
let def_kind = root.tables.opt_def_kind.get(blob, item).unwrap();
853+
let def_kind = root.tables.def_kind.get(blob, item).unwrap();
854854
let def_key = root.tables.def_keys.get(blob, item).unwrap().decode(blob);
855855
let def_name = if item == CRATE_DEF_INDEX {
856856
rustc_span::symbol::kw::Crate
@@ -1001,14 +1001,11 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
10011001
}
10021002

10031003
fn def_kind(self, item_id: DefIndex) -> DefKind {
1004-
self.root.tables.opt_def_kind.get(self, item_id).unwrap_or_else(|| {
1005-
bug!(
1006-
"CrateMetadata::def_kind({:?}): id not found, in crate {:?} with number {}",
1007-
item_id,
1008-
self.root.name(),
1009-
self.cnum,
1010-
)
1011-
})
1004+
self.root
1005+
.tables
1006+
.def_kind
1007+
.get(self, item_id)
1008+
.unwrap_or_else(|| self.missing("def_kind", item_id))
10121009
}
10131010

10141011
fn get_span(self, index: DefIndex, sess: &Session) -> Span {

compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ provide! { tcx, def_id, other, cdata,
231231
lookup_deprecation_entry => { table }
232232
params_in_repr => { table }
233233
unused_generic_params => { cdata.root.tables.unused_generic_params.get(cdata, def_id.index) }
234-
opt_def_kind => { table_direct }
234+
def_kind => { cdata.def_kind(def_id.index) }
235235
impl_parent => { table }
236236
impl_polarity => { table_direct }
237237
defaultness => { table_direct }

compiler/rustc_metadata/src/rmeta/encoder.rs

+12-7
Original file line numberDiff line numberDiff line change
@@ -1354,9 +1354,8 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
13541354

13551355
for local_id in tcx.iter_local_def_id() {
13561356
let def_id = local_id.to_def_id();
1357-
let def_kind = tcx.opt_def_kind(local_id);
1358-
let Some(def_kind) = def_kind else { continue };
1359-
self.tables.opt_def_kind.set_some(def_id.index, def_kind);
1357+
let def_kind = tcx.def_kind(local_id);
1358+
self.tables.def_kind.set_some(def_id.index, def_kind);
13601359
if should_encode_span(def_kind) {
13611360
let def_span = tcx.def_span(local_id);
13621361
record!(self.tables.def_span[def_id] <- def_span);
@@ -1393,7 +1392,13 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
13931392
if should_encode_fn_sig(def_kind) {
13941393
record!(self.tables.fn_sig[def_id] <- tcx.fn_sig(def_id));
13951394
}
1396-
if should_encode_generics(def_kind) {
1395+
// FIXME: Some anonymous constants produced by `#[rustc_legacy_const_generics]`
1396+
// do not have corresponding HIR nodes, so some queries usually making sense for
1397+
// anonymous constants will not work on them and panic. It's not clear whether it
1398+
// can cause any observable issues or not.
1399+
let anon_const_without_hir = def_kind == DefKind::AnonConst
1400+
&& tcx.hir().find(tcx.local_def_id_to_hir_id(local_id)).is_none();
1401+
if should_encode_generics(def_kind) && !anon_const_without_hir {
13971402
let g = tcx.generics_of(def_id);
13981403
record!(self.tables.generics_of[def_id] <- g);
13991404
record!(self.tables.explicit_predicates_of[def_id] <- self.tcx.explicit_predicates_of(def_id));
@@ -1407,7 +1412,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
14071412
}
14081413
}
14091414
}
1410-
if should_encode_type(tcx, local_id, def_kind) {
1415+
if should_encode_type(tcx, local_id, def_kind) && !anon_const_without_hir {
14111416
record!(self.tables.type_of[def_id] <- self.tcx.type_of(def_id));
14121417
}
14131418
if should_encode_constness(def_kind) {
@@ -1785,7 +1790,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
17851790
self.tables.proc_macro_quoted_spans.set_some(i, span);
17861791
}
17871792

1788-
self.tables.opt_def_kind.set_some(LOCAL_CRATE.as_def_id().index, DefKind::Mod);
1793+
self.tables.def_kind.set_some(LOCAL_CRATE.as_def_id().index, DefKind::Mod);
17891794
record!(self.tables.def_span[LOCAL_CRATE.as_def_id()] <- tcx.def_span(LOCAL_CRATE.as_def_id()));
17901795
self.encode_attrs(LOCAL_CRATE.as_def_id().expect_local());
17911796
let vis = tcx.local_visibility(CRATE_DEF_ID).map_id(|def_id| def_id.local_def_index);
@@ -1833,7 +1838,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
18331838
def_key.disambiguated_data.data = DefPathData::MacroNs(name);
18341839

18351840
let def_id = id.to_def_id();
1836-
self.tables.opt_def_kind.set_some(def_id.index, DefKind::Macro(macro_kind));
1841+
self.tables.def_kind.set_some(def_id.index, DefKind::Macro(macro_kind));
18371842
self.tables.proc_macro.set_some(def_id.index, macro_kind);
18381843
self.encode_attrs(id);
18391844
record!(self.tables.def_keys[def_id] <- def_key);

compiler/rustc_metadata/src/rmeta/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ define_tables! {
405405
// so we can take their names, visibilities etc from other encoded tables.
406406
module_children_non_reexports: Table<DefIndex, LazyArray<DefIndex>>,
407407
associated_item_or_field_def_ids: Table<DefIndex, LazyArray<DefIndex>>,
408-
opt_def_kind: Table<DefIndex, DefKind>,
408+
def_kind: Table<DefIndex, DefKind>,
409409
visibility: Table<DefIndex, LazyValue<ty::Visibility<DefIndex>>>,
410410
def_span: Table<DefIndex, LazyValue<Span>>,
411411
def_ident_span: Table<DefIndex, LazyValue<Span>>,

compiler/rustc_middle/src/hir/map/mod.rs

+7-11
Original file line numberDiff line numberDiff line change
@@ -174,21 +174,18 @@ impl<'hir> Map<'hir> {
174174
}
175175

176176
/// Do not call this function directly. The query should be called.
177-
pub(super) fn opt_def_kind(self, local_def_id: LocalDefId) -> Option<DefKind> {
177+
pub(super) fn def_kind(self, local_def_id: LocalDefId) -> DefKind {
178178
let hir_id = self.local_def_id_to_hir_id(local_def_id);
179179
let node = match self.find(hir_id) {
180180
Some(node) => node,
181181
None => match self.def_key(local_def_id).disambiguated_data.data {
182-
// FIXME: Some anonymous constants do not have corresponding HIR nodes,
183-
// so many local queries will panic on their def ids. `None` is currently
184-
// returned here instead of `DefKind::{Anon,Inline}Const` to avoid such panics.
185-
// Ideally all def ids should have `DefKind`s, we need to create the missing
186-
// HIR nodes or feed relevant query results to achieve that.
187-
DefPathData::AnonConst => return None,
182+
// FIXME: Some anonymous constants produced by `#[rustc_legacy_const_generics]`
183+
// do not have corresponding HIR nodes, but they are still anonymous constants.
184+
DefPathData::AnonConst => return DefKind::AnonConst,
188185
_ => bug!("no HIR node for def id {local_def_id:?}"),
189186
},
190187
};
191-
let def_kind = match node {
188+
match node {
192189
Node::Item(item) => match item.kind {
193190
ItemKind::Static(_, mt, _) => DefKind::Static(mt),
194191
ItemKind::Const(..) => DefKind::Const,
@@ -266,8 +263,7 @@ impl<'hir> Map<'hir> {
266263
self.span(hir_id),
267264
"unexpected node with def id {local_def_id:?}: {node:?}"
268265
),
269-
};
270-
Some(def_kind)
266+
}
271267
}
272268

273269
/// Finds the id of the parent node to this one.
@@ -895,7 +891,7 @@ impl<'hir> Map<'hir> {
895891

896892
#[inline]
897893
fn opt_ident(self, id: HirId) -> Option<Ident> {
898-
match self.get(id) {
894+
match self.find(id)? {
899895
Node::Pat(&Pat { kind: PatKind::Binding(_, _, ident, _), .. }) => Some(ident),
900896
// A `Ctor` doesn't have an identifier itself, but its parent
901897
// struct/variant does. Compare with `hir::Map::opt_span`.

compiler/rustc_middle/src/hir/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ pub fn provide(providers: &mut Providers) {
202202
span_bug!(hir.span(hir_id), "fn_arg_names: unexpected item {:?}", def_id);
203203
}
204204
};
205-
providers.opt_def_kind = |tcx, def_id| tcx.hir().opt_def_kind(def_id);
205+
providers.def_kind = |tcx, def_id| tcx.hir().def_kind(def_id);
206206
providers.all_local_trait_impls = |tcx, ()| &tcx.resolutions(()).trait_impls;
207207
providers.expn_that_defined =
208208
|tcx, id| tcx.resolutions(()).expn_that_defined.get(&id).copied().unwrap_or(ExpnId::root());

compiler/rustc_middle/src/query/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1151,7 +1151,7 @@ rustc_queries! {
11511151
cache_on_disk_if { true }
11521152
}
11531153

1154-
query opt_def_kind(def_id: DefId) -> Option<DefKind> {
1154+
query def_kind(def_id: DefId) -> DefKind {
11551155
desc { |tcx| "looking up definition kind of `{}`", tcx.def_path_str(def_id) }
11561156
cache_on_disk_if { def_id.is_local() }
11571157
separate_provide_extern

compiler/rustc_middle/src/query/plumbing.rs

-17
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ use measureme::StringId;
1212
use rustc_data_structures::fx::FxHashMap;
1313
use rustc_data_structures::sync::AtomicU64;
1414
use rustc_data_structures::sync::WorkerLocal;
15-
use rustc_hir::def::DefKind;
1615
use rustc_hir::def_id::{DefId, LocalDefId};
1716
use rustc_hir::hir_id::OwnerId;
1817
use rustc_query_system::dep_graph::DepNodeIndex;
@@ -668,21 +667,5 @@ mod sealed {
668667

669668
pub use sealed::IntoQueryParam;
670669

671-
impl<'tcx> TyCtxt<'tcx> {
672-
pub fn def_kind(self, def_id: impl IntoQueryParam<DefId>) -> DefKind {
673-
let def_id = def_id.into_query_param();
674-
self.opt_def_kind(def_id)
675-
.unwrap_or_else(|| bug!("def_kind: unsupported node: {:?}", def_id))
676-
}
677-
}
678-
679-
impl<'tcx> TyCtxtAt<'tcx> {
680-
pub fn def_kind(self, def_id: impl IntoQueryParam<DefId>) -> DefKind {
681-
let def_id = def_id.into_query_param();
682-
self.opt_def_kind(def_id)
683-
.unwrap_or_else(|| bug!("def_kind: unsupported node: {:?}", def_id))
684-
}
685-
}
686-
687670
#[derive(Copy, Clone, Debug, HashStable)]
688671
pub struct CyclePlaceholder(pub ErrorGuaranteed);

compiler/rustc_privacy/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,7 @@ impl<'tcx> EmbargoVisitor<'tcx> {
500500
}
501501

502502
let macro_module_def_id = self.tcx.local_parent(local_def_id);
503-
if self.tcx.opt_def_kind(macro_module_def_id) != Some(DefKind::Mod) {
503+
if self.tcx.def_kind(macro_module_def_id) != DefKind::Mod {
504504
// The macro's parent doesn't correspond to a `mod`, return early (#63164, #65252).
505505
return;
506506
}

compiler/rustc_query_impl/src/plumbing.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -325,11 +325,11 @@ pub(crate) fn create_query_frame<
325325
Some(key.default_span(tcx))
326326
};
327327
let def_id = key.key_as_def_id();
328-
let def_kind = if kind == dep_graph::dep_kinds::opt_def_kind || with_no_queries() {
328+
let def_kind = if kind == dep_graph::dep_kinds::def_kind || with_no_queries() {
329329
// Try to avoid infinite recursion.
330330
None
331331
} else {
332-
def_id.and_then(|def_id| def_id.as_local()).and_then(|def_id| tcx.opt_def_kind(def_id))
332+
def_id.and_then(|def_id| def_id.as_local()).map(|def_id| tcx.def_kind(def_id))
333333
};
334334
let hash = || {
335335
tcx.with_stable_hashing_context(|mut hcx| {

compiler/rustc_ty_utils/src/assoc.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ fn associated_type_for_impl_trait_in_trait(
259259
let local_def_id = trait_assoc_ty.def_id();
260260
let def_id = local_def_id.to_def_id();
261261

262-
trait_assoc_ty.opt_def_kind(Some(DefKind::AssocTy));
262+
trait_assoc_ty.def_kind(DefKind::AssocTy);
263263

264264
// There's no HIR associated with this new synthesized `def_id`, so feed
265265
// `opt_local_def_id_to_hir_id` with `None`.
@@ -362,7 +362,7 @@ fn associated_type_for_impl_trait_in_impl(
362362
let local_def_id = impl_assoc_ty.def_id();
363363
let def_id = local_def_id.to_def_id();
364364

365-
impl_assoc_ty.opt_def_kind(Some(DefKind::AssocTy));
365+
impl_assoc_ty.def_kind(DefKind::AssocTy);
366366

367367
// There's no HIR associated with this new synthesized `def_id`, so feed
368368
// `opt_local_def_id_to_hir_id` with `None`.

src/tools/clippy/clippy_lints/src/unused_async.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ impl<'tcx> LateLintPass<'tcx> for UnusedAsync {
148148
// statements, so don't lint at all if there are any such paths.
149149
if let Some(def_id) = path.res.opt_def_id()
150150
&& let Some(local_def_id) = def_id.as_local()
151-
&& let Some(DefKind::Fn) = cx.tcx.opt_def_kind(def_id)
151+
&& cx.tcx.def_kind(def_id) == DefKind::Fn
152152
&& cx.tcx.asyncness(def_id).is_async()
153153
&& !is_node_func_call(cx.tcx.hir().get_parent(hir_id), path.span)
154154
{

src/tools/clippy/clippy_lints/src/useless_conversion.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use clippy_utils::sugg::Sugg;
44
use clippy_utils::ty::{is_copy, is_type_diagnostic_item, same_type_and_consts};
55
use clippy_utils::{get_parent_expr, is_trait_method, is_ty_alias, path_to_local};
66
use rustc_errors::Applicability;
7-
use rustc_hir::def::DefKind;
87
use rustc_hir::def_id::DefId;
98
use rustc_hir::{BindingAnnotation, Expr, ExprKind, HirId, MatchSource, Node, PatKind};
109
use rustc_infer::infer::TyCtxtInferExt;
@@ -208,7 +207,7 @@ impl<'tcx> LateLintPass<'tcx> for UselessConversion {
208207
&& let Some(did) = cx.qpath_res(qpath, recv.hir_id).opt_def_id()
209208
// make sure that the path indeed points to a fn-like item, so that
210209
// `fn_sig` does not ICE. (see #11065)
211-
&& cx.tcx.opt_def_kind(did).is_some_and(DefKind::is_fn_like) =>
210+
&& cx.tcx.def_kind(did).is_fn_like() =>
212211
{
213212
Some((
214213
did,

0 commit comments

Comments
 (0)