Skip to content

Commit 2b85556

Browse files
committed
resolve: Feed the def_kind query immediately on DefId creation
1 parent b4c4664 commit 2b85556

File tree

15 files changed

+162
-173
lines changed

15 files changed

+162
-173
lines changed

compiler/rustc_ast_lowering/src/asm.rs

+1
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
228228
parent_def_id.def_id,
229229
node_id,
230230
DefPathData::AnonConst,
231+
DefKind::AnonConst,
231232
*op_sp,
232233
);
233234
let anon_const = AnonConst { id: node_id, value: P(expr) };

compiler/rustc_ast_lowering/src/expr.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use rustc_ast::ptr::P as AstP;
1212
use rustc_ast::*;
1313
use rustc_data_structures::stack::ensure_sufficient_stack;
1414
use rustc_hir as hir;
15-
use rustc_hir::def::Res;
15+
use rustc_hir::def::{DefKind, Res};
1616
use rustc_hir::definitions::DefPathData;
1717
use rustc_session::errors::report_lit_error;
1818
use rustc_span::source_map::{respan, Spanned};
@@ -395,7 +395,13 @@ impl<'hir> LoweringContext<'_, 'hir> {
395395
let node_id = self.next_node_id();
396396

397397
// Add a definition for the in-band const def.
398-
self.create_def(parent_def_id.def_id, node_id, DefPathData::AnonConst, f.span);
398+
self.create_def(
399+
parent_def_id.def_id,
400+
node_id,
401+
DefPathData::AnonConst,
402+
DefKind::AnonConst,
403+
f.span,
404+
);
399405

400406
let anon_const = AnonConst { id: node_id, value: arg };
401407
generic_args.push(AngleBracketedArg::Arg(GenericArg::Const(anon_const)));

compiler/rustc_ast_lowering/src/item.rs

+11-3
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
480480
}
481481
ItemKind::MacroDef(MacroDef { body, macro_rules }) => {
482482
let body = P(self.lower_delim_args(body));
483-
let macro_kind = self.resolver.decl_macro_kind(self.local_def_id(id));
483+
let DefKind::Macro(macro_kind) = self.tcx.def_kind(self.local_def_id(id)) else {
484+
unreachable!()
485+
};
484486
let macro_def = self.arena.alloc(ast::MacroDef { body, macro_rules: *macro_rules });
485487
hir::ItemKind::Macro(macro_def, macro_kind)
486488
}
@@ -1399,6 +1401,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
13991401
self.local_def_id(parent_node_id),
14001402
param_node_id,
14011403
DefPathData::TypeNs(sym::host),
1404+
DefKind::ConstParam,
14021405
span,
14031406
);
14041407
self.host_param_id = Some(def_id);
@@ -1457,8 +1460,13 @@ impl<'hir> LoweringContext<'_, 'hir> {
14571460

14581461
if let Some((span, hir_id, def_id)) = host_param_parts {
14591462
let const_node_id = self.next_node_id();
1460-
let anon_const: LocalDefId =
1461-
self.create_def(def_id, const_node_id, DefPathData::AnonConst, span);
1463+
let anon_const = self.create_def(
1464+
def_id,
1465+
const_node_id,
1466+
DefPathData::AnonConst,
1467+
DefKind::AnonConst,
1468+
span,
1469+
);
14621470

14631471
let const_id = self.next_id();
14641472
let const_expr_id = self.next_id();

compiler/rustc_ast_lowering/src/lib.rs

+14-9
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ use rustc_middle::{
6767
ty::{ResolverAstLowering, TyCtxt},
6868
};
6969
use rustc_session::parse::{add_feature_diagnostics, feature_err};
70-
use rustc_span::hygiene::MacroKind;
7170
use rustc_span::symbol::{kw, sym, Ident, Symbol};
7271
use rustc_span::{DesugaringKind, Span, DUMMY_SP};
7372
use smallvec::SmallVec;
@@ -153,7 +152,6 @@ trait ResolverAstLoweringExt {
153152
fn get_lifetime_res(&self, id: NodeId) -> Option<LifetimeRes>;
154153
fn take_extra_lifetime_params(&mut self, id: NodeId) -> Vec<(Ident, NodeId, LifetimeRes)>;
155154
fn remap_extra_lifetime_params(&mut self, from: NodeId, to: NodeId);
156-
fn decl_macro_kind(&self, def_id: LocalDefId) -> MacroKind;
157155
}
158156

159157
impl ResolverAstLoweringExt for ResolverAstLowering {
@@ -217,10 +215,6 @@ impl ResolverAstLoweringExt for ResolverAstLowering {
217215
let lifetimes = self.extra_lifetime_params_map.remove(&from).unwrap_or_default();
218216
self.extra_lifetime_params_map.insert(to, lifetimes);
219217
}
220-
221-
fn decl_macro_kind(&self, def_id: LocalDefId) -> MacroKind {
222-
self.builtin_macro_kinds.get(&def_id).copied().unwrap_or(MacroKind::Bang)
223-
}
224218
}
225219

226220
/// Context of `impl Trait` in code, which determines whether it is allowed in an HIR subtree,
@@ -467,6 +461,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
467461
parent: LocalDefId,
468462
node_id: ast::NodeId,
469463
data: DefPathData,
464+
def_kind: DefKind,
470465
span: Span,
471466
) -> LocalDefId {
472467
debug_assert_ne!(node_id, ast::DUMMY_NODE_ID);
@@ -478,7 +473,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
478473
self.tcx.hir().def_key(self.local_def_id(node_id)),
479474
);
480475

481-
let def_id = self.tcx.at(span).create_def(parent, data).def_id();
476+
let def_id = self.tcx.at(span).create_def(parent, data, def_kind).def_id();
482477

483478
debug!("create_def: def_id_to_node_id[{:?}] <-> {:?}", def_id, node_id);
484479
self.resolver.node_id_to_def_id.insert(node_id, def_id);
@@ -780,6 +775,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
780775
self.current_hir_id_owner.def_id,
781776
param,
782777
DefPathData::LifetimeNs(kw::UnderscoreLifetime),
778+
DefKind::LifetimeParam,
783779
ident.span,
784780
);
785781
debug!(?_def_id);
@@ -1192,6 +1188,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
11921188
parent_def_id.def_id,
11931189
node_id,
11941190
DefPathData::AnonConst,
1191+
DefKind::AnonConst,
11951192
span,
11961193
);
11971194

@@ -1429,6 +1426,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
14291426
self.current_hir_id_owner.def_id,
14301427
*def_node_id,
14311428
DefPathData::TypeNs(ident.name),
1429+
DefKind::TyParam,
14321430
span,
14331431
);
14341432
let (param, bounds, path) = self.lower_universal_param_and_bounds(
@@ -1582,6 +1580,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
15821580
self.current_hir_id_owner.def_id,
15831581
opaque_ty_node_id,
15841582
DefPathData::ImplTrait,
1583+
DefKind::OpaqueTy,
15851584
opaque_ty_span,
15861585
);
15871586
debug!(?opaque_ty_def_id);
@@ -1636,6 +1635,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
16361635
opaque_ty_def_id,
16371636
duplicated_lifetime_node_id,
16381637
DefPathData::LifetimeNs(lifetime.ident.name),
1638+
DefKind::LifetimeParam,
16391639
lifetime.ident.span,
16401640
);
16411641
captured_to_synthesized_mapping.insert(old_def_id, duplicated_lifetime_def_id);
@@ -2505,8 +2505,13 @@ impl<'hir> GenericArgsCtor<'hir> {
25052505
});
25062506
lcx.attrs.insert(hir_id.local_id, std::slice::from_ref(attr));
25072507

2508-
let def_id =
2509-
lcx.create_def(lcx.current_hir_id_owner.def_id, id, DefPathData::AnonConst, span);
2508+
let def_id = lcx.create_def(
2509+
lcx.current_hir_id_owner.def_id,
2510+
id,
2511+
DefPathData::AnonConst,
2512+
DefKind::AnonConst,
2513+
span,
2514+
);
25102515
lcx.children.push((def_id, hir::MaybeOwner::NonOwner(hir_id)));
25112516
self.args.push(hir::GenericArg::Const(hir::ConstArg {
25122517
value: hir::AnonConst { def_id, hir_id, body },

compiler/rustc_interface/src/queries.rs

+4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use rustc_codegen_ssa::CodegenResults;
88
use rustc_data_structures::steal::Steal;
99
use rustc_data_structures::svh::Svh;
1010
use rustc_data_structures::sync::{AppendOnlyIndexVec, FreezeLock, OnceLock, WorkerLocal};
11+
use rustc_hir::def::DefKind;
1112
use rustc_hir::def_id::{StableCrateId, CRATE_DEF_ID, LOCAL_CRATE};
1213
use rustc_hir::definitions::Definitions;
1314
use rustc_incremental::setup_dep_graph;
@@ -171,6 +172,9 @@ impl<'tcx> Queries<'tcx> {
171172
)));
172173
feed.crate_for_resolver(tcx.arena.alloc(Steal::new((krate, pre_configured_attrs))));
173174
feed.output_filenames(Arc::new(outputs));
175+
176+
let feed = tcx.feed_local_def_id(CRATE_DEF_ID);
177+
feed.def_kind(DefKind::Mod);
174178
});
175179
Ok(qcx)
176180
})

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

+1-93
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use rustc_data_structures::svh::Svh;
99
use rustc_data_structures::sync::{par_for_each_in, try_par_for_each_in, DynSend, DynSync};
1010
use rustc_hir::def::{DefKind, Res};
1111
use rustc_hir::def_id::{DefId, LocalDefId, LocalModDefId, LOCAL_CRATE};
12-
use rustc_hir::definitions::{DefKey, DefPath, DefPathData, DefPathHash};
12+
use rustc_hir::definitions::{DefKey, DefPath, DefPathHash};
1313
use rustc_hir::intravisit::{self, Visitor};
1414
use rustc_hir::*;
1515
use rustc_index::Idx;
@@ -168,98 +168,6 @@ impl<'hir> Map<'hir> {
168168
self.tcx.definitions_untracked().def_path_hash(def_id)
169169
}
170170

171-
/// Do not call this function directly. The query should be called.
172-
pub(super) fn def_kind(self, local_def_id: LocalDefId) -> DefKind {
173-
let hir_id = self.tcx.local_def_id_to_hir_id(local_def_id);
174-
let node = match self.find(hir_id) {
175-
Some(node) => node,
176-
None => match self.def_key(local_def_id).disambiguated_data.data {
177-
// FIXME: Some anonymous constants produced by `#[rustc_legacy_const_generics]`
178-
// do not have corresponding HIR nodes, but they are still anonymous constants.
179-
DefPathData::AnonConst => return DefKind::AnonConst,
180-
_ => bug!("no HIR node for def id {local_def_id:?}"),
181-
},
182-
};
183-
match node {
184-
Node::Item(item) => match item.kind {
185-
ItemKind::Static(_, mt, _) => DefKind::Static(mt),
186-
ItemKind::Const(..) => DefKind::Const,
187-
ItemKind::Fn(..) => DefKind::Fn,
188-
ItemKind::Macro(_, macro_kind) => DefKind::Macro(macro_kind),
189-
ItemKind::Mod(..) => DefKind::Mod,
190-
ItemKind::OpaqueTy(..) => DefKind::OpaqueTy,
191-
ItemKind::TyAlias(..) => DefKind::TyAlias,
192-
ItemKind::Enum(..) => DefKind::Enum,
193-
ItemKind::Struct(..) => DefKind::Struct,
194-
ItemKind::Union(..) => DefKind::Union,
195-
ItemKind::Trait(..) => DefKind::Trait,
196-
ItemKind::TraitAlias(..) => DefKind::TraitAlias,
197-
ItemKind::ExternCrate(_) => DefKind::ExternCrate,
198-
ItemKind::Use(..) => DefKind::Use,
199-
ItemKind::ForeignMod { .. } => DefKind::ForeignMod,
200-
ItemKind::GlobalAsm(..) => DefKind::GlobalAsm,
201-
ItemKind::Impl(impl_) => DefKind::Impl { of_trait: impl_.of_trait.is_some() },
202-
},
203-
Node::ForeignItem(item) => match item.kind {
204-
ForeignItemKind::Fn(..) => DefKind::Fn,
205-
ForeignItemKind::Static(_, mt) => DefKind::Static(mt),
206-
ForeignItemKind::Type => DefKind::ForeignTy,
207-
},
208-
Node::TraitItem(item) => match item.kind {
209-
TraitItemKind::Const(..) => DefKind::AssocConst,
210-
TraitItemKind::Fn(..) => DefKind::AssocFn,
211-
TraitItemKind::Type(..) => DefKind::AssocTy,
212-
},
213-
Node::ImplItem(item) => match item.kind {
214-
ImplItemKind::Const(..) => DefKind::AssocConst,
215-
ImplItemKind::Fn(..) => DefKind::AssocFn,
216-
ImplItemKind::Type(..) => DefKind::AssocTy,
217-
},
218-
Node::Variant(_) => DefKind::Variant,
219-
Node::Ctor(variant_data) => {
220-
let ctor_of = match self.find_parent(hir_id) {
221-
Some(Node::Item(..)) => def::CtorOf::Struct,
222-
Some(Node::Variant(..)) => def::CtorOf::Variant,
223-
_ => unreachable!(),
224-
};
225-
match variant_data.ctor_kind() {
226-
Some(kind) => DefKind::Ctor(ctor_of, kind),
227-
None => bug!("constructor node without a constructor"),
228-
}
229-
}
230-
Node::AnonConst(_) => DefKind::AnonConst,
231-
Node::ConstBlock(_) => DefKind::InlineConst,
232-
Node::Field(_) => DefKind::Field,
233-
Node::Expr(expr) => match expr.kind {
234-
ExprKind::Closure(_) => DefKind::Closure,
235-
_ => bug!("def_kind: unsupported node: {}", self.node_to_string(hir_id)),
236-
},
237-
Node::GenericParam(param) => match param.kind {
238-
GenericParamKind::Lifetime { .. } => DefKind::LifetimeParam,
239-
GenericParamKind::Type { .. } => DefKind::TyParam,
240-
GenericParamKind::Const { .. } => DefKind::ConstParam,
241-
},
242-
Node::Crate(_) => DefKind::Mod,
243-
Node::Stmt(_)
244-
| Node::PathSegment(_)
245-
| Node::Ty(_)
246-
| Node::TypeBinding(_)
247-
| Node::Infer(_)
248-
| Node::TraitRef(_)
249-
| Node::Pat(_)
250-
| Node::PatField(_)
251-
| Node::ExprField(_)
252-
| Node::Local(_)
253-
| Node::Param(_)
254-
| Node::Arm(_)
255-
| Node::Lifetime(_)
256-
| Node::Block(_) => span_bug!(
257-
self.span(hir_id),
258-
"unexpected node with def id {local_def_id:?}: {node:?}"
259-
),
260-
}
261-
}
262-
263171
/// Finds the id of the parent node to this one.
264172
///
265173
/// If calling repeatedly and iterating over parents, prefer [`Map::parent_iter`].

compiler/rustc_middle/src/hir/mod.rs

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

compiler/rustc_middle/src/ty/context.rs

+5
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,9 @@ impl<'tcx> TyCtxt<'tcx> {
501501
pub fn feed_local_crate(self) -> TyCtxtFeed<'tcx, CrateNum> {
502502
TyCtxtFeed { tcx: self, key: LOCAL_CRATE }
503503
}
504+
pub fn feed_local_def_id(self, key: LocalDefId) -> TyCtxtFeed<'tcx, LocalDefId> {
505+
TyCtxtFeed { tcx: self, key }
506+
}
504507

505508
/// In order to break cycles involving `AnonConst`, we need to set the expected type by side
506509
/// effect. However, we do not want this as a general capability, so this interface restricts
@@ -973,6 +976,7 @@ impl<'tcx> TyCtxtAt<'tcx> {
973976
self,
974977
parent: LocalDefId,
975978
data: hir::definitions::DefPathData,
979+
def_kind: DefKind,
976980
) -> TyCtxtFeed<'tcx, LocalDefId> {
977981
// This function modifies `self.definitions` using a side-effect.
978982
// We need to ensure that these side effects are re-run by the incr. comp. engine.
@@ -997,6 +1001,7 @@ impl<'tcx> TyCtxtAt<'tcx> {
9971001
let key = self.untracked.definitions.write().create_def(parent, data);
9981002

9991003
let feed = TyCtxtFeed { tcx: self.tcx, key };
1004+
feed.def_kind(def_kind);
10001005
feed.def_span(self.span);
10011006
feed
10021007
}

compiler/rustc_middle/src/ty/mod.rs

-3
Original file line numberDiff line numberDiff line change
@@ -202,9 +202,6 @@ pub struct ResolverAstLowering {
202202
pub def_id_to_node_id: IndexVec<LocalDefId, ast::NodeId>,
203203

204204
pub trait_map: NodeMap<Vec<hir::TraitCandidate>>,
205-
/// A small map keeping true kinds of built-in macros that appear to be fn-like on
206-
/// the surface (`macro` items in libcore), but are actually attributes or derives.
207-
pub builtin_macro_kinds: FxHashMap<LocalDefId, MacroKind>,
208205
/// List functions and methods for which lifetime elision was successful.
209206
pub lifetime_elision_allowed: FxHashSet<ast::NodeId>,
210207

0 commit comments

Comments
 (0)