Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit f0dc906

Browse files
committedNov 28, 2023
resolve: Feed the def_kind query immediately on DefId creation
1 parent 46a24ed commit f0dc906

File tree

15 files changed

+162
-173
lines changed

15 files changed

+162
-173
lines changed
 

‎compiler/rustc_ast_lowering/src/asm.rs

Lines changed: 1 addition & 0 deletions
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

Lines changed: 8 additions & 2 deletions
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

Lines changed: 11 additions & 3 deletions
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

Lines changed: 14 additions & 9 deletions
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

Lines changed: 4 additions & 0 deletions
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

Lines changed: 1 addition & 93 deletions
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

Lines changed: 0 additions & 1 deletion
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

Lines changed: 5 additions & 0 deletions
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

Lines changed: 0 additions & 3 deletions
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

‎compiler/rustc_resolve/src/def_collector.rs

Lines changed: 108 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use crate::{ImplTraitContext, Resolver};
22
use rustc_ast::visit::{self, FnKind};
33
use rustc_ast::*;
44
use rustc_expand::expand::AstFragment;
5+
use rustc_hir::def::{CtorKind, CtorOf, DefKind};
56
use rustc_hir::def_id::LocalDefId;
67
use rustc_hir::definitions::*;
78
use rustc_span::hygiene::LocalExpnId;
@@ -26,13 +27,20 @@ struct DefCollector<'a, 'b, 'tcx> {
2627
}
2728

2829
impl<'a, 'b, 'tcx> DefCollector<'a, 'b, 'tcx> {
29-
fn create_def(&mut self, node_id: NodeId, data: DefPathData, span: Span) -> LocalDefId {
30+
fn create_def(
31+
&mut self,
32+
node_id: NodeId,
33+
data: DefPathData,
34+
def_kind: DefKind,
35+
span: Span,
36+
) -> LocalDefId {
3037
let parent_def = self.parent_def;
3138
debug!("create_def(node_id={:?}, data={:?}, parent_def={:?})", node_id, data, parent_def);
3239
self.resolver.create_def(
3340
parent_def,
3441
node_id,
3542
data,
43+
def_kind,
3644
self.expansion.to_expn_id(),
3745
span.with_parent(None),
3846
)
@@ -68,7 +76,8 @@ impl<'a, 'b, 'tcx> DefCollector<'a, 'b, 'tcx> {
6876
self.visit_macro_invoc(field.id);
6977
} else {
7078
let name = field.ident.map_or_else(|| sym::integer(index(self)), |ident| ident.name);
71-
let def = self.create_def(field.id, DefPathData::ValueNs(name), field.span);
79+
let def =
80+
self.create_def(field.id, DefPathData::ValueNs(name), DefKind::Field, field.span);
7281
self.with_parent(def, |this| visit::walk_field_def(this, field));
7382
}
7483
}
@@ -87,34 +96,43 @@ impl<'a, 'b, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'b, 'tcx> {
8796

8897
// Pick the def data. This need not be unique, but the more
8998
// information we encapsulate into, the better
90-
let def_data = match &i.kind {
91-
ItemKind::Impl { .. } => DefPathData::Impl,
92-
ItemKind::ForeignMod(..) => DefPathData::ForeignMod,
93-
ItemKind::Mod(..)
94-
| ItemKind::Trait(..)
95-
| ItemKind::TraitAlias(..)
96-
| ItemKind::Enum(..)
97-
| ItemKind::Struct(..)
98-
| ItemKind::Union(..)
99-
| ItemKind::ExternCrate(..)
100-
| ItemKind::TyAlias(..) => DefPathData::TypeNs(i.ident.name),
101-
ItemKind::Static(..) | ItemKind::Const(..) | ItemKind::Fn(..) => {
102-
DefPathData::ValueNs(i.ident.name)
99+
let mut opt_macro_data = None;
100+
let ty_data = DefPathData::TypeNs(i.ident.name);
101+
let value_data = DefPathData::ValueNs(i.ident.name);
102+
let (def_data, def_kind) = match &i.kind {
103+
ItemKind::Impl(i) => {
104+
(DefPathData::Impl, DefKind::Impl { of_trait: i.of_trait.is_some() })
105+
}
106+
ItemKind::ForeignMod(..) => (DefPathData::ForeignMod, DefKind::ForeignMod),
107+
ItemKind::Mod(..) => (ty_data, DefKind::Mod),
108+
ItemKind::Trait(..) => (ty_data, DefKind::Trait),
109+
ItemKind::TraitAlias(..) => (ty_data, DefKind::TraitAlias),
110+
ItemKind::Enum(..) => (ty_data, DefKind::Enum),
111+
ItemKind::Struct(..) => (ty_data, DefKind::Struct),
112+
ItemKind::Union(..) => (ty_data, DefKind::Union),
113+
ItemKind::ExternCrate(..) => (ty_data, DefKind::ExternCrate),
114+
ItemKind::TyAlias(..) => (ty_data, DefKind::TyAlias),
115+
ItemKind::Static(s) => (value_data, DefKind::Static(s.mutability)),
116+
ItemKind::Const(..) => (value_data, DefKind::Const),
117+
ItemKind::Fn(..) => (value_data, DefKind::Fn),
118+
ItemKind::MacroDef(..) => {
119+
let macro_data = self.resolver.compile_macro(i, self.resolver.tcx.sess.edition());
120+
let macro_kind = macro_data.ext.macro_kind();
121+
opt_macro_data = Some(macro_data);
122+
(DefPathData::MacroNs(i.ident.name), DefKind::Macro(macro_kind))
103123
}
104-
ItemKind::MacroDef(..) => DefPathData::MacroNs(i.ident.name),
105124
ItemKind::MacCall(..) => {
106125
visit::walk_item(self, i);
107126
return self.visit_macro_invoc(i.id);
108127
}
109-
ItemKind::GlobalAsm(..) => DefPathData::GlobalAsm,
128+
ItemKind::GlobalAsm(..) => (DefPathData::GlobalAsm, DefKind::GlobalAsm),
110129
ItemKind::Use(..) => {
111130
return visit::walk_item(self, i);
112131
}
113132
};
114-
let def_id = self.create_def(i.id, def_data, i.span);
133+
let def_id = self.create_def(i.id, def_data, def_kind, i.span);
115134

116-
if let ItemKind::MacroDef(..) = i.kind {
117-
let macro_data = self.resolver.compile_macro(i, self.resolver.tcx.sess.edition());
135+
if let Some(macro_data) = opt_macro_data {
118136
self.resolver.macro_map.insert(def_id.to_def_id(), macro_data);
119137
}
120138

@@ -123,8 +141,13 @@ impl<'a, 'b, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'b, 'tcx> {
123141
match i.kind {
124142
ItemKind::Struct(ref struct_def, _) | ItemKind::Union(ref struct_def, _) => {
125143
// If this is a unit or tuple-like struct, register the constructor.
126-
if let Some(ctor_node_id) = struct_def.ctor_node_id() {
127-
this.create_def(ctor_node_id, DefPathData::Ctor, i.span);
144+
if let Some((ctor_kind, ctor_node_id)) = CtorKind::from_ast(struct_def) {
145+
this.create_def(
146+
ctor_node_id,
147+
DefPathData::Ctor,
148+
DefKind::Ctor(CtorOf::Struct, ctor_kind),
149+
i.span,
150+
);
128151
}
129152
}
130153
_ => {}
@@ -151,7 +174,12 @@ impl<'a, 'b, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'b, 'tcx> {
151174
// then the closure_def will never be used, and we should avoid generating a
152175
// def-id for it.
153176
if let Some(body) = body {
154-
let closure_def = self.create_def(closure_id, DefPathData::ClosureExpr, span);
177+
let closure_def = self.create_def(
178+
closure_id,
179+
DefPathData::ClosureExpr,
180+
DefKind::Closure,
181+
span,
182+
);
155183
self.with_parent(closure_def, |this| this.visit_block(body));
156184
}
157185
return;
@@ -162,18 +190,23 @@ impl<'a, 'b, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'b, 'tcx> {
162190
}
163191

164192
fn visit_use_tree(&mut self, use_tree: &'a UseTree, id: NodeId, _nested: bool) {
165-
self.create_def(id, DefPathData::Use, use_tree.span);
193+
self.create_def(id, DefPathData::Use, DefKind::Use, use_tree.span);
166194
visit::walk_use_tree(self, use_tree, id);
167195
}
168196

169197
fn visit_foreign_item(&mut self, foreign_item: &'a ForeignItem) {
170-
if let ForeignItemKind::MacCall(_) = foreign_item.kind {
171-
return self.visit_macro_invoc(foreign_item.id);
172-
}
198+
let def_kind = match foreign_item.kind {
199+
ForeignItemKind::Static(_, mt, _) => DefKind::Static(mt),
200+
ForeignItemKind::Fn(_) => DefKind::Fn,
201+
ForeignItemKind::TyAlias(_) => DefKind::ForeignTy,
202+
ForeignItemKind::MacCall(_) => return self.visit_macro_invoc(foreign_item.id),
203+
};
173204

205+
// FIXME: The namespace is incorrect for foreign types.
174206
let def = self.create_def(
175207
foreign_item.id,
176208
DefPathData::ValueNs(foreign_item.ident.name),
209+
def_kind,
177210
foreign_item.span,
178211
);
179212

@@ -186,10 +219,16 @@ impl<'a, 'b, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'b, 'tcx> {
186219
if v.is_placeholder {
187220
return self.visit_macro_invoc(v.id);
188221
}
189-
let def = self.create_def(v.id, DefPathData::TypeNs(v.ident.name), v.span);
222+
let def =
223+
self.create_def(v.id, DefPathData::TypeNs(v.ident.name), DefKind::Variant, v.span);
190224
self.with_parent(def, |this| {
191-
if let Some(ctor_node_id) = v.data.ctor_node_id() {
192-
this.create_def(ctor_node_id, DefPathData::Ctor, v.span);
225+
if let Some((ctor_kind, ctor_node_id)) = CtorKind::from_ast(&v.data) {
226+
this.create_def(
227+
ctor_node_id,
228+
DefPathData::Ctor,
229+
DefKind::Ctor(CtorOf::Variant, ctor_kind),
230+
v.span,
231+
);
193232
}
194233
visit::walk_variant(this, v)
195234
});
@@ -210,12 +249,14 @@ impl<'a, 'b, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'b, 'tcx> {
210249
return;
211250
}
212251
let name = param.ident.name;
213-
let def_path_data = match param.kind {
214-
GenericParamKind::Lifetime { .. } => DefPathData::LifetimeNs(name),
215-
GenericParamKind::Type { .. } => DefPathData::TypeNs(name),
216-
GenericParamKind::Const { .. } => DefPathData::ValueNs(name),
252+
let (def_path_data, def_kind) = match param.kind {
253+
GenericParamKind::Lifetime { .. } => {
254+
(DefPathData::LifetimeNs(name), DefKind::LifetimeParam)
255+
}
256+
GenericParamKind::Type { .. } => (DefPathData::TypeNs(name), DefKind::TyParam),
257+
GenericParamKind::Const { .. } => (DefPathData::ValueNs(name), DefKind::ConstParam),
217258
};
218-
self.create_def(param.id, def_path_data, param.ident.span);
259+
self.create_def(param.id, def_path_data, def_kind, param.ident.span);
219260

220261
// impl-Trait can happen inside generic parameters, like
221262
// ```
@@ -229,13 +270,14 @@ impl<'a, 'b, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'b, 'tcx> {
229270
}
230271

231272
fn visit_assoc_item(&mut self, i: &'a AssocItem, ctxt: visit::AssocCtxt) {
232-
let def_data = match &i.kind {
233-
AssocItemKind::Fn(..) | AssocItemKind::Const(..) => DefPathData::ValueNs(i.ident.name),
234-
AssocItemKind::Type(..) => DefPathData::TypeNs(i.ident.name),
273+
let (def_data, def_kind) = match &i.kind {
274+
AssocItemKind::Fn(..) => (DefPathData::ValueNs(i.ident.name), DefKind::AssocFn),
275+
AssocItemKind::Const(..) => (DefPathData::ValueNs(i.ident.name), DefKind::AssocConst),
276+
AssocItemKind::Type(..) => (DefPathData::TypeNs(i.ident.name), DefKind::AssocTy),
235277
AssocItemKind::MacCall(..) => return self.visit_macro_invoc(i.id),
236278
};
237279

238-
let def = self.create_def(i.id, def_data, i.span);
280+
let def = self.create_def(i.id, def_data, def_kind, i.span);
239281
self.with_parent(def, |this| visit::walk_assoc_item(this, i, ctxt));
240282
}
241283

@@ -247,7 +289,12 @@ impl<'a, 'b, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'b, 'tcx> {
247289
}
248290

249291
fn visit_anon_const(&mut self, constant: &'a AnonConst) {
250-
let def = self.create_def(constant.id, DefPathData::AnonConst, constant.value.span);
292+
let def = self.create_def(
293+
constant.id,
294+
DefPathData::AnonConst,
295+
DefKind::AnonConst,
296+
constant.value.span,
297+
);
251298
self.with_parent(def, |this| visit::walk_anon_const(this, constant));
252299
}
253300

@@ -257,15 +304,31 @@ impl<'a, 'b, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'b, 'tcx> {
257304
ExprKind::Closure(ref closure) => {
258305
// Async closures desugar to closures inside of closures, so
259306
// we must create two defs.
260-
let closure_def = self.create_def(expr.id, DefPathData::ClosureExpr, expr.span);
307+
let closure_def =
308+
self.create_def(expr.id, DefPathData::ClosureExpr, DefKind::Closure, expr.span);
261309
match closure.asyncness {
262-
Async::Yes { closure_id, .. } => {
263-
self.create_def(closure_id, DefPathData::ClosureExpr, expr.span)
264-
}
310+
Async::Yes { closure_id, .. } => self.create_def(
311+
closure_id,
312+
DefPathData::ClosureExpr,
313+
DefKind::Closure,
314+
expr.span,
315+
),
265316
Async::No => closure_def,
266317
}
267318
}
268-
ExprKind::Gen(_, _, _) => self.create_def(expr.id, DefPathData::ClosureExpr, expr.span),
319+
ExprKind::Gen(_, _, _) => {
320+
self.create_def(expr.id, DefPathData::ClosureExpr, DefKind::Closure, expr.span)
321+
}
322+
ExprKind::ConstBlock(ref constant) => {
323+
let def = self.create_def(
324+
constant.id,
325+
DefPathData::AnonConst,
326+
DefKind::InlineConst,
327+
constant.value.span,
328+
);
329+
self.with_parent(def, |this| visit::walk_anon_const(this, constant));
330+
return;
331+
}
269332
_ => self.parent_def,
270333
};
271334

‎compiler/rustc_resolve/src/lib.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1034,9 +1034,6 @@ pub struct Resolver<'a, 'tcx> {
10341034
used_extern_options: FxHashSet<Symbol>,
10351035
macro_names: FxHashSet<Ident>,
10361036
builtin_macros: FxHashMap<Symbol, BuiltinMacroState>,
1037-
/// A small map keeping true kinds of built-in macros that appear to be fn-like on
1038-
/// the surface (`macro` items in libcore), but are actually attributes or derives.
1039-
builtin_macro_kinds: FxHashMap<LocalDefId, MacroKind>,
10401037
registered_tools: &'tcx RegisteredTools,
10411038
macro_use_prelude: FxHashMap<Symbol, NameBinding<'a>>,
10421039
macro_map: FxHashMap<DefId, MacroData>,
@@ -1216,6 +1213,7 @@ impl<'tcx> Resolver<'_, 'tcx> {
12161213
parent: LocalDefId,
12171214
node_id: ast::NodeId,
12181215
data: DefPathData,
1216+
def_kind: DefKind,
12191217
expn_id: ExpnId,
12201218
span: Span,
12211219
) -> LocalDefId {
@@ -1230,6 +1228,9 @@ impl<'tcx> Resolver<'_, 'tcx> {
12301228
// FIXME: remove `def_span` body, pass in the right spans here and call `tcx.at().create_def()`
12311229
let def_id = self.tcx.untracked().definitions.write().create_def(parent, data);
12321230

1231+
let feed = self.tcx.feed_local_def_id(def_id);
1232+
feed.def_kind(def_kind);
1233+
12331234
// Create the definition.
12341235
if expn_id != ExpnId::root() {
12351236
self.expn_that_defined.insert(def_id, expn_id);
@@ -1403,7 +1404,6 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
14031404
used_extern_options: Default::default(),
14041405
macro_names: FxHashSet::default(),
14051406
builtin_macros: Default::default(),
1406-
builtin_macro_kinds: Default::default(),
14071407
registered_tools,
14081408
macro_use_prelude: FxHashMap::default(),
14091409
macro_map: FxHashMap::default(),
@@ -1542,7 +1542,6 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
15421542
node_id_to_def_id: self.node_id_to_def_id,
15431543
def_id_to_node_id: self.def_id_to_node_id,
15441544
trait_map: self.trait_map,
1545-
builtin_macro_kinds: self.builtin_macro_kinds,
15461545
lifetime_elision_allowed: self.lifetime_elision_allowed,
15471546
lint_buffer: Steal::new(self.lint_buffer),
15481547
};

‎compiler/rustc_resolve/src/macros.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -950,10 +950,6 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
950950
BuiltinMacroState::NotYetSeen(builtin_ext) => {
951951
ext.kind = builtin_ext;
952952
rule_spans = Vec::new();
953-
if item.id != ast::DUMMY_NODE_ID {
954-
self.builtin_macro_kinds
955-
.insert(self.local_def_id(item.id), ext.macro_kind());
956-
}
957953
}
958954
BuiltinMacroState::AlreadySeen(span) => {
959955
struct_span_err!(

‎compiler/rustc_ty_utils/src/assoc.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -254,13 +254,12 @@ fn associated_type_for_impl_trait_in_trait(
254254
assert_eq!(tcx.def_kind(trait_def_id), DefKind::Trait);
255255

256256
let span = tcx.def_span(opaque_ty_def_id);
257-
let trait_assoc_ty = tcx.at(span).create_def(trait_def_id, DefPathData::ImplTraitAssocTy);
257+
let trait_assoc_ty =
258+
tcx.at(span).create_def(trait_def_id, DefPathData::ImplTraitAssocTy, DefKind::AssocTy);
258259

259260
let local_def_id = trait_assoc_ty.def_id();
260261
let def_id = local_def_id.to_def_id();
261262

262-
trait_assoc_ty.def_kind(DefKind::AssocTy);
263-
264263
// There's no HIR associated with this new synthesized `def_id`, so feed
265264
// `opt_local_def_id_to_hir_id` with `None`.
266265
trait_assoc_ty.opt_local_def_id_to_hir_id(None);
@@ -357,13 +356,12 @@ fn associated_type_for_impl_trait_in_impl(
357356
hir::FnRetTy::DefaultReturn(_) => tcx.def_span(impl_fn_def_id),
358357
hir::FnRetTy::Return(ty) => ty.span,
359358
};
360-
let impl_assoc_ty = tcx.at(span).create_def(impl_local_def_id, DefPathData::ImplTraitAssocTy);
359+
let impl_assoc_ty =
360+
tcx.at(span).create_def(impl_local_def_id, DefPathData::ImplTraitAssocTy, DefKind::AssocTy);
361361

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.def_kind(DefKind::AssocTy);
366-
367365
// There's no HIR associated with this new synthesized `def_id`, so feed
368366
// `opt_local_def_id_to_hir_id` with `None`.
369367
impl_assoc_ty.opt_local_def_id_to_hir_id(None);

‎tests/ui/dep-graph/dep-graph-trait-impl-two-traits.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ mod x {
2929
mod y {
3030
use {Foo, Bar};
3131

32-
#[rustc_then_this_would_need(typeck)] //~ ERROR OK
32+
#[rustc_then_this_would_need(typeck)] //~ ERROR no path
3333
pub fn call_bar() {
3434
char::bar('a');
3535
}

‎tests/ui/dep-graph/dep-graph-trait-impl-two-traits.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: OK
1+
error: no path from `x::<impl Foo for char>` to `typeck`
22
--> $DIR/dep-graph-trait-impl-two-traits.rs:32:5
33
|
44
LL | #[rustc_then_this_would_need(typeck)]

0 commit comments

Comments
 (0)
Please sign in to comment.