Skip to content

Commit 9c24e80

Browse files
committed
Auto merge of rust-lang#118188 - petrochenkov:defeed, r=<try>
[WIP] resolve: Feed the `opt_def_kind` query immediately on `DefId` creation ... for most `DefId`s. Right now the def kind query requires building HIR for no good reason, in this PR I'm trying to change that. In two cases the `DefKind` is not known early enough. - Choosing between `DefKind::Closure` and `DefKind::Coroutine` requires analyzing (expanded) closure body, which cannot be done in def collector. I think `DefKind::Coroutine` should be merged into `DefKind::Closure`, it's more useful to have all `DefKind`s early in resolver (perhaps even merge them with `DefPathData`s) than to make this specific distinction. - `opt_def_kind` should report `None` instead of `Some(DefKind::AnonConst)` for some specific subset of anonymous constants not know in def collector, otherwise code in other parts of the compiler panics. I'll try to get rid of this oddity in a separate PR. Submitting this in a WIP state for preliminary benchmarking.
2 parents 06d1afe + 6a70c75 commit 9c24e80

File tree

15 files changed

+250
-163
lines changed

15 files changed

+250
-163
lines changed

Diff for: 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) };

Diff for: 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)));

Diff for: 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: LocalDefId = 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();

Diff for: compiler/rustc_ast_lowering/src/lib.rs

+14-9
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ use rustc_middle::{
7070
ty::{ResolverAstLowering, TyCtxt},
7171
};
7272
use rustc_session::parse::{add_feature_diagnostics, feature_err};
73-
use rustc_span::hygiene::MacroKind;
7473
use rustc_span::symbol::{kw, sym, Ident, Symbol};
7574
use rustc_span::{DesugaringKind, Span, DUMMY_SP};
7675
use smallvec::SmallVec;
@@ -156,7 +155,6 @@ trait ResolverAstLoweringExt {
156155
fn get_lifetime_res(&self, id: NodeId) -> Option<LifetimeRes>;
157156
fn take_extra_lifetime_params(&mut self, id: NodeId) -> Vec<(Ident, NodeId, LifetimeRes)>;
158157
fn remap_extra_lifetime_params(&mut self, from: NodeId, to: NodeId);
159-
fn decl_macro_kind(&self, def_id: LocalDefId) -> MacroKind;
160158
}
161159

162160
impl ResolverAstLoweringExt for ResolverAstLowering {
@@ -220,10 +218,6 @@ impl ResolverAstLoweringExt for ResolverAstLowering {
220218
let lifetimes = self.extra_lifetime_params_map.remove(&from).unwrap_or_default();
221219
self.extra_lifetime_params_map.insert(to, lifetimes);
222220
}
223-
224-
fn decl_macro_kind(&self, def_id: LocalDefId) -> MacroKind {
225-
self.builtin_macro_kinds.get(&def_id).copied().unwrap_or(MacroKind::Bang)
226-
}
227221
}
228222

229223
/// Context of `impl Trait` in code, which determines whether it is allowed in an HIR subtree,
@@ -470,6 +464,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
470464
parent: LocalDefId,
471465
node_id: ast::NodeId,
472466
data: DefPathData,
467+
def_kind: DefKind,
473468
span: Span,
474469
) -> LocalDefId {
475470
debug_assert_ne!(node_id, ast::DUMMY_NODE_ID);
@@ -481,7 +476,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
481476
self.tcx.hir().def_key(self.local_def_id(node_id)),
482477
);
483478

484-
let def_id = self.tcx.at(span).create_def(parent, data).def_id();
479+
let def_id = self.tcx.at(span).create_def(parent, data, def_kind).def_id();
485480

486481
debug!("create_def: def_id_to_node_id[{:?}] <-> {:?}", def_id, node_id);
487482
self.resolver.node_id_to_def_id.insert(node_id, def_id);
@@ -783,6 +778,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
783778
self.current_hir_id_owner.def_id,
784779
param,
785780
DefPathData::LifetimeNs(kw::UnderscoreLifetime),
781+
DefKind::LifetimeParam,
786782
ident.span,
787783
);
788784
debug!(?_def_id);
@@ -1195,6 +1191,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
11951191
parent_def_id.def_id,
11961192
node_id,
11971193
DefPathData::AnonConst,
1194+
DefKind::AnonConst,
11981195
span,
11991196
);
12001197

@@ -1432,6 +1429,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
14321429
self.current_hir_id_owner.def_id,
14331430
*def_node_id,
14341431
DefPathData::TypeNs(ident.name),
1432+
DefKind::TyParam,
14351433
span,
14361434
);
14371435
let (param, bounds, path) = self.lower_universal_param_and_bounds(
@@ -1585,6 +1583,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
15851583
self.current_hir_id_owner.def_id,
15861584
opaque_ty_node_id,
15871585
DefPathData::ImplTrait,
1586+
DefKind::OpaqueTy,
15881587
opaque_ty_span,
15891588
);
15901589
debug!(?opaque_ty_def_id);
@@ -1639,6 +1638,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
16391638
opaque_ty_def_id,
16401639
duplicated_lifetime_node_id,
16411640
DefPathData::LifetimeNs(lifetime.ident.name),
1641+
DefKind::LifetimeParam,
16421642
lifetime.ident.span,
16431643
);
16441644
captured_to_synthesized_mapping.insert(old_def_id, duplicated_lifetime_def_id);
@@ -2509,8 +2509,13 @@ impl<'hir> GenericArgsCtor<'hir> {
25092509
});
25102510
lcx.attrs.insert(hir_id.local_id, std::slice::from_ref(attr));
25112511

2512-
let def_id =
2513-
lcx.create_def(lcx.current_hir_id_owner.def_id, id, DefPathData::AnonConst, span);
2512+
let def_id = lcx.create_def(
2513+
lcx.current_hir_id_owner.def_id,
2514+
id,
2515+
DefPathData::AnonConst,
2516+
DefKind::AnonConst,
2517+
span,
2518+
);
25142519
lcx.children.push((def_id, hir::MaybeOwner::NonOwner(hir_id)));
25152520
self.args.push(hir::GenericArg::Const(hir::ConstArg {
25162521
value: hir::AnonConst { def_id, hir_id, body },

Diff for: compiler/rustc_interface/src/queries.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@ 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;
1415
use rustc_metadata::creader::CStore;
1516
use rustc_middle::arena::Arena;
1617
use rustc_middle::dep_graph::DepGraph;
17-
use rustc_middle::ty::{GlobalCtxt, TyCtxt};
18+
use rustc_middle::ty::{GlobalCtxt, TyCtxt, TyCtxtFeed};
1819
use rustc_session::config::{self, CrateType, OutputFilenames, OutputType};
1920
use rustc_session::cstore::Untracked;
2021
use rustc_session::output::find_crate_name;
@@ -175,6 +176,9 @@ impl<'tcx> Queries<'tcx> {
175176
crate_name,
176177
)));
177178
feed.crate_for_resolver(tcx.arena.alloc(Steal::new((krate, pre_configured_attrs))));
179+
180+
let feed = TyCtxtFeed { tcx, key: CRATE_DEF_ID };
181+
feed.opt_def_kind(Some(DefKind::Mod));
178182
});
179183
Ok(qcx)
180184
})

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

+5-72
Original file line numberDiff line numberDiff line change
@@ -188,84 +188,17 @@ impl<'hir> Map<'hir> {
188188
_ => bug!("no HIR node for def id {local_def_id:?}"),
189189
},
190190
};
191+
let bug = || {
192+
span_bug!(self.span(hir_id), "unexpected node with def id {local_def_id:?}: {node:?}")
193+
};
191194
let def_kind = match node {
192-
Node::Item(item) => match item.kind {
193-
ItemKind::Static(_, mt, _) => DefKind::Static(mt),
194-
ItemKind::Const(..) => DefKind::Const,
195-
ItemKind::Fn(..) => DefKind::Fn,
196-
ItemKind::Macro(_, macro_kind) => DefKind::Macro(macro_kind),
197-
ItemKind::Mod(..) => DefKind::Mod,
198-
ItemKind::OpaqueTy(..) => DefKind::OpaqueTy,
199-
ItemKind::TyAlias(..) => DefKind::TyAlias,
200-
ItemKind::Enum(..) => DefKind::Enum,
201-
ItemKind::Struct(..) => DefKind::Struct,
202-
ItemKind::Union(..) => DefKind::Union,
203-
ItemKind::Trait(..) => DefKind::Trait,
204-
ItemKind::TraitAlias(..) => DefKind::TraitAlias,
205-
ItemKind::ExternCrate(_) => DefKind::ExternCrate,
206-
ItemKind::Use(..) => DefKind::Use,
207-
ItemKind::ForeignMod { .. } => DefKind::ForeignMod,
208-
ItemKind::GlobalAsm(..) => DefKind::GlobalAsm,
209-
ItemKind::Impl(impl_) => DefKind::Impl { of_trait: impl_.of_trait.is_some() },
210-
},
211-
Node::ForeignItem(item) => match item.kind {
212-
ForeignItemKind::Fn(..) => DefKind::Fn,
213-
ForeignItemKind::Static(_, mt) => DefKind::Static(mt),
214-
ForeignItemKind::Type => DefKind::ForeignTy,
215-
},
216-
Node::TraitItem(item) => match item.kind {
217-
TraitItemKind::Const(..) => DefKind::AssocConst,
218-
TraitItemKind::Fn(..) => DefKind::AssocFn,
219-
TraitItemKind::Type(..) => DefKind::AssocTy,
220-
},
221-
Node::ImplItem(item) => match item.kind {
222-
ImplItemKind::Const(..) => DefKind::AssocConst,
223-
ImplItemKind::Fn(..) => DefKind::AssocFn,
224-
ImplItemKind::Type(..) => DefKind::AssocTy,
225-
},
226-
Node::Variant(_) => DefKind::Variant,
227-
Node::Ctor(variant_data) => {
228-
let ctor_of = match self.find_parent(hir_id) {
229-
Some(Node::Item(..)) => def::CtorOf::Struct,
230-
Some(Node::Variant(..)) => def::CtorOf::Variant,
231-
_ => unreachable!(),
232-
};
233-
match variant_data.ctor_kind() {
234-
Some(kind) => DefKind::Ctor(ctor_of, kind),
235-
None => bug!("constructor node without a constructor"),
236-
}
237-
}
238195
Node::AnonConst(_) => DefKind::AnonConst,
239-
Node::ConstBlock(_) => DefKind::InlineConst,
240-
Node::Field(_) => DefKind::Field,
241196
Node::Expr(expr) => match expr.kind {
242197
ExprKind::Closure(Closure { movability: None, .. }) => DefKind::Closure,
243198
ExprKind::Closure(Closure { movability: Some(_), .. }) => DefKind::Coroutine,
244-
_ => bug!("def_kind: unsupported node: {}", self.node_to_string(hir_id)),
199+
_ => bug(),
245200
},
246-
Node::GenericParam(param) => match param.kind {
247-
GenericParamKind::Lifetime { .. } => DefKind::LifetimeParam,
248-
GenericParamKind::Type { .. } => DefKind::TyParam,
249-
GenericParamKind::Const { .. } => DefKind::ConstParam,
250-
},
251-
Node::Crate(_) => DefKind::Mod,
252-
Node::Stmt(_)
253-
| Node::PathSegment(_)
254-
| Node::Ty(_)
255-
| Node::TypeBinding(_)
256-
| Node::Infer(_)
257-
| Node::TraitRef(_)
258-
| Node::Pat(_)
259-
| Node::PatField(_)
260-
| Node::ExprField(_)
261-
| Node::Local(_)
262-
| Node::Param(_)
263-
| Node::Arm(_)
264-
| Node::Lifetime(_)
265-
| Node::Block(_) => span_bug!(
266-
self.span(hir_id),
267-
"unexpected node with def id {local_def_id:?}: {node:?}"
268-
),
201+
_ => bug(),
269202
};
270203
Some(def_kind)
271204
}

Diff for: compiler/rustc_middle/src/ty/context.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ pub struct FreeRegionInfo {
459459
pub struct TyCtxtFeed<'tcx, KEY: Copy> {
460460
pub tcx: TyCtxt<'tcx>,
461461
// Do not allow direct access, as downstream code must not mutate this field.
462-
key: KEY,
462+
pub key: KEY,
463463
}
464464

465465
impl<'tcx> TyCtxt<'tcx> {
@@ -933,6 +933,7 @@ impl<'tcx> TyCtxtAt<'tcx> {
933933
self,
934934
parent: LocalDefId,
935935
data: hir::definitions::DefPathData,
936+
def_kind: DefKind,
936937
) -> TyCtxtFeed<'tcx, LocalDefId> {
937938
// This function modifies `self.definitions` using a side-effect.
938939
// We need to ensure that these side effects are re-run by the incr. comp. engine.
@@ -957,6 +958,7 @@ impl<'tcx> TyCtxtAt<'tcx> {
957958
let key = self.untracked.definitions.write().create_def(parent, data);
958959

959960
let feed = TyCtxtFeed { tcx: self.tcx, key };
961+
feed.opt_def_kind(Some(def_kind));
960962
feed.def_span(self.span);
961963
feed
962964
}

Diff for: compiler/rustc_middle/src/ty/mod.rs

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

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

0 commit comments

Comments
 (0)