Skip to content

Commit 3ea2972

Browse files
committed
Revert "resolve: Feed the def_kind query immediately on DefId creation"
This reverts commit f0dc906.
1 parent 90d29fc commit 3ea2972

File tree

15 files changed

+173
-162
lines changed

15 files changed

+173
-162
lines changed

compiler/rustc_ast_lowering/src/asm.rs

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

compiler/rustc_ast_lowering/src/expr.rs

+2-8
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::{DefKind, Res};
15+
use rustc_hir::def::Res;
1616
use rustc_hir::definitions::DefPathData;
1717
use rustc_session::errors::report_lit_error;
1818
use rustc_span::source_map::{respan, Spanned};
@@ -395,13 +395,7 @@ 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(
399-
parent_def_id.def_id,
400-
node_id,
401-
DefPathData::AnonConst,
402-
DefKind::AnonConst,
403-
f.span,
404-
);
398+
self.create_def(parent_def_id.def_id, node_id, DefPathData::AnonConst, f.span);
405399

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

compiler/rustc_ast_lowering/src/item.rs

+3-11
Original file line numberDiff line numberDiff line change
@@ -480,9 +480,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
480480
}
481481
ItemKind::MacroDef(MacroDef { body, macro_rules }) => {
482482
let body = P(self.lower_delim_args(body));
483-
let DefKind::Macro(macro_kind) = self.tcx.def_kind(self.local_def_id(id)) else {
484-
unreachable!()
485-
};
483+
let macro_kind = self.resolver.decl_macro_kind(self.local_def_id(id));
486484
let macro_def = self.arena.alloc(ast::MacroDef { body, macro_rules: *macro_rules });
487485
hir::ItemKind::Macro(macro_def, macro_kind)
488486
}
@@ -1401,7 +1399,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
14011399
self.local_def_id(parent_node_id),
14021400
param_node_id,
14031401
DefPathData::TypeNs(sym::host),
1404-
DefKind::ConstParam,
14051402
span,
14061403
);
14071404
self.host_param_id = Some(def_id);
@@ -1460,13 +1457,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
14601457

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

14711463
let const_id = self.next_id();
14721464
let const_expr_id = self.next_id();

compiler/rustc_ast_lowering/src/lib.rs

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

157159
impl ResolverAstLoweringExt for ResolverAstLowering {
@@ -215,6 +217,10 @@ impl ResolverAstLoweringExt for ResolverAstLowering {
215217
let lifetimes = self.extra_lifetime_params_map.remove(&from).unwrap_or_default();
216218
self.extra_lifetime_params_map.insert(to, lifetimes);
217219
}
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+
}
218224
}
219225

220226
/// Context of `impl Trait` in code, which determines whether it is allowed in an HIR subtree,
@@ -461,7 +467,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
461467
parent: LocalDefId,
462468
node_id: ast::NodeId,
463469
data: DefPathData,
464-
def_kind: DefKind,
465470
span: Span,
466471
) -> LocalDefId {
467472
debug_assert_ne!(node_id, ast::DUMMY_NODE_ID);
@@ -473,7 +478,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
473478
self.tcx.hir().def_key(self.local_def_id(node_id)),
474479
);
475480

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

478483
debug!("create_def: def_id_to_node_id[{:?}] <-> {:?}", def_id, node_id);
479484
self.resolver.node_id_to_def_id.insert(node_id, def_id);
@@ -775,7 +780,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
775780
self.current_hir_id_owner.def_id,
776781
param,
777782
DefPathData::LifetimeNs(kw::UnderscoreLifetime),
778-
DefKind::LifetimeParam,
779783
ident.span,
780784
);
781785
debug!(?_def_id);
@@ -1188,7 +1192,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
11881192
parent_def_id.def_id,
11891193
node_id,
11901194
DefPathData::AnonConst,
1191-
DefKind::AnonConst,
11921195
span,
11931196
);
11941197

@@ -1426,7 +1429,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
14261429
self.current_hir_id_owner.def_id,
14271430
*def_node_id,
14281431
DefPathData::TypeNs(ident.name),
1429-
DefKind::TyParam,
14301432
span,
14311433
);
14321434
let (param, bounds, path) = self.lower_universal_param_and_bounds(
@@ -1580,7 +1582,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
15801582
self.current_hir_id_owner.def_id,
15811583
opaque_ty_node_id,
15821584
DefPathData::ImplTrait,
1583-
DefKind::OpaqueTy,
15841585
opaque_ty_span,
15851586
);
15861587
debug!(?opaque_ty_def_id);
@@ -1635,7 +1636,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
16351636
opaque_ty_def_id,
16361637
duplicated_lifetime_node_id,
16371638
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,13 +2505,8 @@ impl<'hir> GenericArgsCtor<'hir> {
25052505
});
25062506
lcx.attrs.insert(hir_id.local_id, std::slice::from_ref(attr));
25072507

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-
);
2508+
let def_id =
2509+
lcx.create_def(lcx.current_hir_id_owner.def_id, id, DefPathData::AnonConst, span);
25152510
lcx.children.push((def_id, hir::MaybeOwner::NonOwner(hir_id)));
25162511
self.args.push(hir::GenericArg::Const(hir::ConstArg {
25172512
value: hir::AnonConst { def_id, hir_id, body },

compiler/rustc_interface/src/queries.rs

-4
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ 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;
1211
use rustc_hir::def_id::{StableCrateId, CRATE_DEF_ID, LOCAL_CRATE};
1312
use rustc_hir::definitions::Definitions;
1413
use rustc_incremental::setup_dep_graph;
@@ -172,9 +171,6 @@ impl<'tcx> Queries<'tcx> {
172171
)));
173172
feed.crate_for_resolver(tcx.arena.alloc(Steal::new((krate, pre_configured_attrs))));
174173
feed.output_filenames(Arc::new(outputs));
175-
176-
let feed = tcx.feed_local_def_id(CRATE_DEF_ID);
177-
feed.def_kind(DefKind::Mod);
178174
});
179175
Ok(qcx)
180176
})

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

+93-1
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, DefPathHash};
12+
use rustc_hir::definitions::{DefKey, DefPath, DefPathData, DefPathHash};
1313
use rustc_hir::intravisit::{self, Visitor};
1414
use rustc_hir::*;
1515
use rustc_index::Idx;
@@ -168,6 +168,98 @@ 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+
171263
/// Finds the id of the parent node to this one.
172264
///
173265
/// 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,6 +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.def_kind = |tcx, def_id| tcx.hir().def_kind(def_id);
205206
providers.all_local_trait_impls = |tcx, ()| &tcx.resolutions(()).trait_impls;
206207
providers.expn_that_defined =
207208
|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,9 +501,6 @@ 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-
}
507504

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

1003999
let feed = TyCtxtFeed { tcx: self.tcx, key };
1004-
feed.def_kind(def_kind);
10051000
feed.def_span(self.span);
10061001
feed
10071002
}

compiler/rustc_middle/src/ty/mod.rs

+3
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,9 @@ 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>,
205208
/// List functions and methods for which lifetime elision was successful.
206209
pub lifetime_elision_allowed: FxHashSet<ast::NodeId>,
207210

0 commit comments

Comments
 (0)