From f0dc9063196d56b43c56b04c9f94aa416c83bbd5 Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Tue, 21 Nov 2023 23:40:23 +0300 Subject: [PATCH 1/2] resolve: Feed the `def_kind` query immediately on `DefId` creation --- compiler/rustc_ast_lowering/src/asm.rs | 1 + compiler/rustc_ast_lowering/src/expr.rs | 10 +- compiler/rustc_ast_lowering/src/item.rs | 14 +- compiler/rustc_ast_lowering/src/lib.rs | 23 +-- compiler/rustc_interface/src/queries.rs | 4 + compiler/rustc_middle/src/hir/map/mod.rs | 94 +---------- compiler/rustc_middle/src/hir/mod.rs | 1 - compiler/rustc_middle/src/ty/context.rs | 5 + compiler/rustc_middle/src/ty/mod.rs | 3 - compiler/rustc_resolve/src/def_collector.rs | 153 ++++++++++++------ compiler/rustc_resolve/src/lib.rs | 9 +- compiler/rustc_resolve/src/macros.rs | 4 - compiler/rustc_ty_utils/src/assoc.rs | 10 +- .../dep-graph-trait-impl-two-traits.rs | 2 +- .../dep-graph-trait-impl-two-traits.stderr | 2 +- 15 files changed, 162 insertions(+), 173 deletions(-) diff --git a/compiler/rustc_ast_lowering/src/asm.rs b/compiler/rustc_ast_lowering/src/asm.rs index a1e626996800f..951bf41d9d769 100644 --- a/compiler/rustc_ast_lowering/src/asm.rs +++ b/compiler/rustc_ast_lowering/src/asm.rs @@ -228,6 +228,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { parent_def_id.def_id, node_id, DefPathData::AnonConst, + DefKind::AnonConst, *op_sp, ); let anon_const = AnonConst { id: node_id, value: P(expr) }; diff --git a/compiler/rustc_ast_lowering/src/expr.rs b/compiler/rustc_ast_lowering/src/expr.rs index 6f2f9787e7727..180459efef920 100644 --- a/compiler/rustc_ast_lowering/src/expr.rs +++ b/compiler/rustc_ast_lowering/src/expr.rs @@ -12,7 +12,7 @@ use rustc_ast::ptr::P as AstP; use rustc_ast::*; use rustc_data_structures::stack::ensure_sufficient_stack; use rustc_hir as hir; -use rustc_hir::def::Res; +use rustc_hir::def::{DefKind, Res}; use rustc_hir::definitions::DefPathData; use rustc_session::errors::report_lit_error; use rustc_span::source_map::{respan, Spanned}; @@ -395,7 +395,13 @@ impl<'hir> LoweringContext<'_, 'hir> { let node_id = self.next_node_id(); // Add a definition for the in-band const def. - self.create_def(parent_def_id.def_id, node_id, DefPathData::AnonConst, f.span); + self.create_def( + parent_def_id.def_id, + node_id, + DefPathData::AnonConst, + DefKind::AnonConst, + f.span, + ); let anon_const = AnonConst { id: node_id, value: arg }; generic_args.push(AngleBracketedArg::Arg(GenericArg::Const(anon_const))); diff --git a/compiler/rustc_ast_lowering/src/item.rs b/compiler/rustc_ast_lowering/src/item.rs index c4c08096b8bb4..fb75471ede3bc 100644 --- a/compiler/rustc_ast_lowering/src/item.rs +++ b/compiler/rustc_ast_lowering/src/item.rs @@ -480,7 +480,9 @@ impl<'hir> LoweringContext<'_, 'hir> { } ItemKind::MacroDef(MacroDef { body, macro_rules }) => { let body = P(self.lower_delim_args(body)); - let macro_kind = self.resolver.decl_macro_kind(self.local_def_id(id)); + let DefKind::Macro(macro_kind) = self.tcx.def_kind(self.local_def_id(id)) else { + unreachable!() + }; let macro_def = self.arena.alloc(ast::MacroDef { body, macro_rules: *macro_rules }); hir::ItemKind::Macro(macro_def, macro_kind) } @@ -1399,6 +1401,7 @@ impl<'hir> LoweringContext<'_, 'hir> { self.local_def_id(parent_node_id), param_node_id, DefPathData::TypeNs(sym::host), + DefKind::ConstParam, span, ); self.host_param_id = Some(def_id); @@ -1457,8 +1460,13 @@ impl<'hir> LoweringContext<'_, 'hir> { if let Some((span, hir_id, def_id)) = host_param_parts { let const_node_id = self.next_node_id(); - let anon_const: LocalDefId = - self.create_def(def_id, const_node_id, DefPathData::AnonConst, span); + let anon_const = self.create_def( + def_id, + const_node_id, + DefPathData::AnonConst, + DefKind::AnonConst, + span, + ); let const_id = self.next_id(); let const_expr_id = self.next_id(); diff --git a/compiler/rustc_ast_lowering/src/lib.rs b/compiler/rustc_ast_lowering/src/lib.rs index e9e842559225c..4c7b9b4155fdf 100644 --- a/compiler/rustc_ast_lowering/src/lib.rs +++ b/compiler/rustc_ast_lowering/src/lib.rs @@ -67,7 +67,6 @@ use rustc_middle::{ ty::{ResolverAstLowering, TyCtxt}, }; use rustc_session::parse::{add_feature_diagnostics, feature_err}; -use rustc_span::hygiene::MacroKind; use rustc_span::symbol::{kw, sym, Ident, Symbol}; use rustc_span::{DesugaringKind, Span, DUMMY_SP}; use smallvec::SmallVec; @@ -153,7 +152,6 @@ trait ResolverAstLoweringExt { fn get_lifetime_res(&self, id: NodeId) -> Option; fn take_extra_lifetime_params(&mut self, id: NodeId) -> Vec<(Ident, NodeId, LifetimeRes)>; fn remap_extra_lifetime_params(&mut self, from: NodeId, to: NodeId); - fn decl_macro_kind(&self, def_id: LocalDefId) -> MacroKind; } impl ResolverAstLoweringExt for ResolverAstLowering { @@ -217,10 +215,6 @@ impl ResolverAstLoweringExt for ResolverAstLowering { let lifetimes = self.extra_lifetime_params_map.remove(&from).unwrap_or_default(); self.extra_lifetime_params_map.insert(to, lifetimes); } - - fn decl_macro_kind(&self, def_id: LocalDefId) -> MacroKind { - self.builtin_macro_kinds.get(&def_id).copied().unwrap_or(MacroKind::Bang) - } } /// 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> { parent: LocalDefId, node_id: ast::NodeId, data: DefPathData, + def_kind: DefKind, span: Span, ) -> LocalDefId { debug_assert_ne!(node_id, ast::DUMMY_NODE_ID); @@ -478,7 +473,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { self.tcx.hir().def_key(self.local_def_id(node_id)), ); - let def_id = self.tcx.at(span).create_def(parent, data).def_id(); + let def_id = self.tcx.at(span).create_def(parent, data, def_kind).def_id(); debug!("create_def: def_id_to_node_id[{:?}] <-> {:?}", def_id, node_id); self.resolver.node_id_to_def_id.insert(node_id, def_id); @@ -780,6 +775,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { self.current_hir_id_owner.def_id, param, DefPathData::LifetimeNs(kw::UnderscoreLifetime), + DefKind::LifetimeParam, ident.span, ); debug!(?_def_id); @@ -1192,6 +1188,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { parent_def_id.def_id, node_id, DefPathData::AnonConst, + DefKind::AnonConst, span, ); @@ -1429,6 +1426,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { self.current_hir_id_owner.def_id, *def_node_id, DefPathData::TypeNs(ident.name), + DefKind::TyParam, span, ); let (param, bounds, path) = self.lower_universal_param_and_bounds( @@ -1582,6 +1580,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { self.current_hir_id_owner.def_id, opaque_ty_node_id, DefPathData::ImplTrait, + DefKind::OpaqueTy, opaque_ty_span, ); debug!(?opaque_ty_def_id); @@ -1636,6 +1635,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { opaque_ty_def_id, duplicated_lifetime_node_id, DefPathData::LifetimeNs(lifetime.ident.name), + DefKind::LifetimeParam, lifetime.ident.span, ); captured_to_synthesized_mapping.insert(old_def_id, duplicated_lifetime_def_id); @@ -2505,8 +2505,13 @@ impl<'hir> GenericArgsCtor<'hir> { }); lcx.attrs.insert(hir_id.local_id, std::slice::from_ref(attr)); - let def_id = - lcx.create_def(lcx.current_hir_id_owner.def_id, id, DefPathData::AnonConst, span); + let def_id = lcx.create_def( + lcx.current_hir_id_owner.def_id, + id, + DefPathData::AnonConst, + DefKind::AnonConst, + span, + ); lcx.children.push((def_id, hir::MaybeOwner::NonOwner(hir_id))); self.args.push(hir::GenericArg::Const(hir::ConstArg { value: hir::AnonConst { def_id, hir_id, body }, diff --git a/compiler/rustc_interface/src/queries.rs b/compiler/rustc_interface/src/queries.rs index b7cd5468a00dc..e81c3f42a166b 100644 --- a/compiler/rustc_interface/src/queries.rs +++ b/compiler/rustc_interface/src/queries.rs @@ -8,6 +8,7 @@ use rustc_codegen_ssa::CodegenResults; use rustc_data_structures::steal::Steal; use rustc_data_structures::svh::Svh; use rustc_data_structures::sync::{AppendOnlyIndexVec, FreezeLock, OnceLock, WorkerLocal}; +use rustc_hir::def::DefKind; use rustc_hir::def_id::{StableCrateId, CRATE_DEF_ID, LOCAL_CRATE}; use rustc_hir::definitions::Definitions; use rustc_incremental::setup_dep_graph; @@ -171,6 +172,9 @@ impl<'tcx> Queries<'tcx> { ))); feed.crate_for_resolver(tcx.arena.alloc(Steal::new((krate, pre_configured_attrs)))); feed.output_filenames(Arc::new(outputs)); + + let feed = tcx.feed_local_def_id(CRATE_DEF_ID); + feed.def_kind(DefKind::Mod); }); Ok(qcx) }) diff --git a/compiler/rustc_middle/src/hir/map/mod.rs b/compiler/rustc_middle/src/hir/map/mod.rs index 7bf0da2f2f3b4..f407d30ac82a3 100644 --- a/compiler/rustc_middle/src/hir/map/mod.rs +++ b/compiler/rustc_middle/src/hir/map/mod.rs @@ -9,7 +9,7 @@ use rustc_data_structures::svh::Svh; use rustc_data_structures::sync::{par_for_each_in, try_par_for_each_in, DynSend, DynSync}; use rustc_hir::def::{DefKind, Res}; use rustc_hir::def_id::{DefId, LocalDefId, LocalModDefId, LOCAL_CRATE}; -use rustc_hir::definitions::{DefKey, DefPath, DefPathData, DefPathHash}; +use rustc_hir::definitions::{DefKey, DefPath, DefPathHash}; use rustc_hir::intravisit::{self, Visitor}; use rustc_hir::*; use rustc_index::Idx; @@ -168,98 +168,6 @@ impl<'hir> Map<'hir> { self.tcx.definitions_untracked().def_path_hash(def_id) } - /// Do not call this function directly. The query should be called. - pub(super) fn def_kind(self, local_def_id: LocalDefId) -> DefKind { - let hir_id = self.tcx.local_def_id_to_hir_id(local_def_id); - let node = match self.find(hir_id) { - Some(node) => node, - None => match self.def_key(local_def_id).disambiguated_data.data { - // FIXME: Some anonymous constants produced by `#[rustc_legacy_const_generics]` - // do not have corresponding HIR nodes, but they are still anonymous constants. - DefPathData::AnonConst => return DefKind::AnonConst, - _ => bug!("no HIR node for def id {local_def_id:?}"), - }, - }; - match node { - Node::Item(item) => match item.kind { - ItemKind::Static(_, mt, _) => DefKind::Static(mt), - ItemKind::Const(..) => DefKind::Const, - ItemKind::Fn(..) => DefKind::Fn, - ItemKind::Macro(_, macro_kind) => DefKind::Macro(macro_kind), - ItemKind::Mod(..) => DefKind::Mod, - ItemKind::OpaqueTy(..) => DefKind::OpaqueTy, - ItemKind::TyAlias(..) => DefKind::TyAlias, - ItemKind::Enum(..) => DefKind::Enum, - ItemKind::Struct(..) => DefKind::Struct, - ItemKind::Union(..) => DefKind::Union, - ItemKind::Trait(..) => DefKind::Trait, - ItemKind::TraitAlias(..) => DefKind::TraitAlias, - ItemKind::ExternCrate(_) => DefKind::ExternCrate, - ItemKind::Use(..) => DefKind::Use, - ItemKind::ForeignMod { .. } => DefKind::ForeignMod, - ItemKind::GlobalAsm(..) => DefKind::GlobalAsm, - ItemKind::Impl(impl_) => DefKind::Impl { of_trait: impl_.of_trait.is_some() }, - }, - Node::ForeignItem(item) => match item.kind { - ForeignItemKind::Fn(..) => DefKind::Fn, - ForeignItemKind::Static(_, mt) => DefKind::Static(mt), - ForeignItemKind::Type => DefKind::ForeignTy, - }, - Node::TraitItem(item) => match item.kind { - TraitItemKind::Const(..) => DefKind::AssocConst, - TraitItemKind::Fn(..) => DefKind::AssocFn, - TraitItemKind::Type(..) => DefKind::AssocTy, - }, - Node::ImplItem(item) => match item.kind { - ImplItemKind::Const(..) => DefKind::AssocConst, - ImplItemKind::Fn(..) => DefKind::AssocFn, - ImplItemKind::Type(..) => DefKind::AssocTy, - }, - Node::Variant(_) => DefKind::Variant, - Node::Ctor(variant_data) => { - let ctor_of = match self.find_parent(hir_id) { - Some(Node::Item(..)) => def::CtorOf::Struct, - Some(Node::Variant(..)) => def::CtorOf::Variant, - _ => unreachable!(), - }; - match variant_data.ctor_kind() { - Some(kind) => DefKind::Ctor(ctor_of, kind), - None => bug!("constructor node without a constructor"), - } - } - Node::AnonConst(_) => DefKind::AnonConst, - Node::ConstBlock(_) => DefKind::InlineConst, - Node::Field(_) => DefKind::Field, - Node::Expr(expr) => match expr.kind { - ExprKind::Closure(_) => DefKind::Closure, - _ => bug!("def_kind: unsupported node: {}", self.node_to_string(hir_id)), - }, - Node::GenericParam(param) => match param.kind { - GenericParamKind::Lifetime { .. } => DefKind::LifetimeParam, - GenericParamKind::Type { .. } => DefKind::TyParam, - GenericParamKind::Const { .. } => DefKind::ConstParam, - }, - Node::Crate(_) => DefKind::Mod, - Node::Stmt(_) - | Node::PathSegment(_) - | Node::Ty(_) - | Node::TypeBinding(_) - | Node::Infer(_) - | Node::TraitRef(_) - | Node::Pat(_) - | Node::PatField(_) - | Node::ExprField(_) - | Node::Local(_) - | Node::Param(_) - | Node::Arm(_) - | Node::Lifetime(_) - | Node::Block(_) => span_bug!( - self.span(hir_id), - "unexpected node with def id {local_def_id:?}: {node:?}" - ), - } - } - /// Finds the id of the parent node to this one. /// /// If calling repeatedly and iterating over parents, prefer [`Map::parent_iter`]. diff --git a/compiler/rustc_middle/src/hir/mod.rs b/compiler/rustc_middle/src/hir/mod.rs index bf652fc277ab4..a16317a0c670b 100644 --- a/compiler/rustc_middle/src/hir/mod.rs +++ b/compiler/rustc_middle/src/hir/mod.rs @@ -202,7 +202,6 @@ pub fn provide(providers: &mut Providers) { span_bug!(hir.span(hir_id), "fn_arg_names: unexpected item {:?}", def_id); } }; - providers.def_kind = |tcx, def_id| tcx.hir().def_kind(def_id); providers.all_local_trait_impls = |tcx, ()| &tcx.resolutions(()).trait_impls; providers.expn_that_defined = |tcx, id| tcx.resolutions(()).expn_that_defined.get(&id).copied().unwrap_or(ExpnId::root()); diff --git a/compiler/rustc_middle/src/ty/context.rs b/compiler/rustc_middle/src/ty/context.rs index 4ded61daa5cfb..bb02c6e0c7856 100644 --- a/compiler/rustc_middle/src/ty/context.rs +++ b/compiler/rustc_middle/src/ty/context.rs @@ -501,6 +501,9 @@ impl<'tcx> TyCtxt<'tcx> { pub fn feed_local_crate(self) -> TyCtxtFeed<'tcx, CrateNum> { TyCtxtFeed { tcx: self, key: LOCAL_CRATE } } + pub fn feed_local_def_id(self, key: LocalDefId) -> TyCtxtFeed<'tcx, LocalDefId> { + TyCtxtFeed { tcx: self, key } + } /// In order to break cycles involving `AnonConst`, we need to set the expected type by side /// effect. However, we do not want this as a general capability, so this interface restricts @@ -973,6 +976,7 @@ impl<'tcx> TyCtxtAt<'tcx> { self, parent: LocalDefId, data: hir::definitions::DefPathData, + def_kind: DefKind, ) -> TyCtxtFeed<'tcx, LocalDefId> { // This function modifies `self.definitions` using a side-effect. // We need to ensure that these side effects are re-run by the incr. comp. engine. @@ -997,6 +1001,7 @@ impl<'tcx> TyCtxtAt<'tcx> { let key = self.untracked.definitions.write().create_def(parent, data); let feed = TyCtxtFeed { tcx: self.tcx, key }; + feed.def_kind(def_kind); feed.def_span(self.span); feed } diff --git a/compiler/rustc_middle/src/ty/mod.rs b/compiler/rustc_middle/src/ty/mod.rs index da93f7f8ae653..24cba913bb891 100644 --- a/compiler/rustc_middle/src/ty/mod.rs +++ b/compiler/rustc_middle/src/ty/mod.rs @@ -202,9 +202,6 @@ pub struct ResolverAstLowering { pub def_id_to_node_id: IndexVec, pub trait_map: NodeMap>, - /// A small map keeping true kinds of built-in macros that appear to be fn-like on - /// the surface (`macro` items in libcore), but are actually attributes or derives. - pub builtin_macro_kinds: FxHashMap, /// List functions and methods for which lifetime elision was successful. pub lifetime_elision_allowed: FxHashSet, diff --git a/compiler/rustc_resolve/src/def_collector.rs b/compiler/rustc_resolve/src/def_collector.rs index c52450e11d642..cf3e787d839b9 100644 --- a/compiler/rustc_resolve/src/def_collector.rs +++ b/compiler/rustc_resolve/src/def_collector.rs @@ -2,6 +2,7 @@ use crate::{ImplTraitContext, Resolver}; use rustc_ast::visit::{self, FnKind}; use rustc_ast::*; use rustc_expand::expand::AstFragment; +use rustc_hir::def::{CtorKind, CtorOf, DefKind}; use rustc_hir::def_id::LocalDefId; use rustc_hir::definitions::*; use rustc_span::hygiene::LocalExpnId; @@ -26,13 +27,20 @@ struct DefCollector<'a, 'b, 'tcx> { } impl<'a, 'b, 'tcx> DefCollector<'a, 'b, 'tcx> { - fn create_def(&mut self, node_id: NodeId, data: DefPathData, span: Span) -> LocalDefId { + fn create_def( + &mut self, + node_id: NodeId, + data: DefPathData, + def_kind: DefKind, + span: Span, + ) -> LocalDefId { let parent_def = self.parent_def; debug!("create_def(node_id={:?}, data={:?}, parent_def={:?})", node_id, data, parent_def); self.resolver.create_def( parent_def, node_id, data, + def_kind, self.expansion.to_expn_id(), span.with_parent(None), ) @@ -68,7 +76,8 @@ impl<'a, 'b, 'tcx> DefCollector<'a, 'b, 'tcx> { self.visit_macro_invoc(field.id); } else { let name = field.ident.map_or_else(|| sym::integer(index(self)), |ident| ident.name); - let def = self.create_def(field.id, DefPathData::ValueNs(name), field.span); + let def = + self.create_def(field.id, DefPathData::ValueNs(name), DefKind::Field, field.span); self.with_parent(def, |this| visit::walk_field_def(this, field)); } } @@ -87,34 +96,43 @@ impl<'a, 'b, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'b, 'tcx> { // Pick the def data. This need not be unique, but the more // information we encapsulate into, the better - let def_data = match &i.kind { - ItemKind::Impl { .. } => DefPathData::Impl, - ItemKind::ForeignMod(..) => DefPathData::ForeignMod, - ItemKind::Mod(..) - | ItemKind::Trait(..) - | ItemKind::TraitAlias(..) - | ItemKind::Enum(..) - | ItemKind::Struct(..) - | ItemKind::Union(..) - | ItemKind::ExternCrate(..) - | ItemKind::TyAlias(..) => DefPathData::TypeNs(i.ident.name), - ItemKind::Static(..) | ItemKind::Const(..) | ItemKind::Fn(..) => { - DefPathData::ValueNs(i.ident.name) + let mut opt_macro_data = None; + let ty_data = DefPathData::TypeNs(i.ident.name); + let value_data = DefPathData::ValueNs(i.ident.name); + let (def_data, def_kind) = match &i.kind { + ItemKind::Impl(i) => { + (DefPathData::Impl, DefKind::Impl { of_trait: i.of_trait.is_some() }) + } + ItemKind::ForeignMod(..) => (DefPathData::ForeignMod, DefKind::ForeignMod), + ItemKind::Mod(..) => (ty_data, DefKind::Mod), + ItemKind::Trait(..) => (ty_data, DefKind::Trait), + ItemKind::TraitAlias(..) => (ty_data, DefKind::TraitAlias), + ItemKind::Enum(..) => (ty_data, DefKind::Enum), + ItemKind::Struct(..) => (ty_data, DefKind::Struct), + ItemKind::Union(..) => (ty_data, DefKind::Union), + ItemKind::ExternCrate(..) => (ty_data, DefKind::ExternCrate), + ItemKind::TyAlias(..) => (ty_data, DefKind::TyAlias), + ItemKind::Static(s) => (value_data, DefKind::Static(s.mutability)), + ItemKind::Const(..) => (value_data, DefKind::Const), + ItemKind::Fn(..) => (value_data, DefKind::Fn), + ItemKind::MacroDef(..) => { + let macro_data = self.resolver.compile_macro(i, self.resolver.tcx.sess.edition()); + let macro_kind = macro_data.ext.macro_kind(); + opt_macro_data = Some(macro_data); + (DefPathData::MacroNs(i.ident.name), DefKind::Macro(macro_kind)) } - ItemKind::MacroDef(..) => DefPathData::MacroNs(i.ident.name), ItemKind::MacCall(..) => { visit::walk_item(self, i); return self.visit_macro_invoc(i.id); } - ItemKind::GlobalAsm(..) => DefPathData::GlobalAsm, + ItemKind::GlobalAsm(..) => (DefPathData::GlobalAsm, DefKind::GlobalAsm), ItemKind::Use(..) => { return visit::walk_item(self, i); } }; - let def_id = self.create_def(i.id, def_data, i.span); + let def_id = self.create_def(i.id, def_data, def_kind, i.span); - if let ItemKind::MacroDef(..) = i.kind { - let macro_data = self.resolver.compile_macro(i, self.resolver.tcx.sess.edition()); + if let Some(macro_data) = opt_macro_data { self.resolver.macro_map.insert(def_id.to_def_id(), macro_data); } @@ -123,8 +141,13 @@ impl<'a, 'b, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'b, 'tcx> { match i.kind { ItemKind::Struct(ref struct_def, _) | ItemKind::Union(ref struct_def, _) => { // If this is a unit or tuple-like struct, register the constructor. - if let Some(ctor_node_id) = struct_def.ctor_node_id() { - this.create_def(ctor_node_id, DefPathData::Ctor, i.span); + if let Some((ctor_kind, ctor_node_id)) = CtorKind::from_ast(struct_def) { + this.create_def( + ctor_node_id, + DefPathData::Ctor, + DefKind::Ctor(CtorOf::Struct, ctor_kind), + i.span, + ); } } _ => {} @@ -151,7 +174,12 @@ impl<'a, 'b, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'b, 'tcx> { // then the closure_def will never be used, and we should avoid generating a // def-id for it. if let Some(body) = body { - let closure_def = self.create_def(closure_id, DefPathData::ClosureExpr, span); + let closure_def = self.create_def( + closure_id, + DefPathData::ClosureExpr, + DefKind::Closure, + span, + ); self.with_parent(closure_def, |this| this.visit_block(body)); } return; @@ -162,18 +190,23 @@ impl<'a, 'b, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'b, 'tcx> { } fn visit_use_tree(&mut self, use_tree: &'a UseTree, id: NodeId, _nested: bool) { - self.create_def(id, DefPathData::Use, use_tree.span); + self.create_def(id, DefPathData::Use, DefKind::Use, use_tree.span); visit::walk_use_tree(self, use_tree, id); } fn visit_foreign_item(&mut self, foreign_item: &'a ForeignItem) { - if let ForeignItemKind::MacCall(_) = foreign_item.kind { - return self.visit_macro_invoc(foreign_item.id); - } + let def_kind = match foreign_item.kind { + ForeignItemKind::Static(_, mt, _) => DefKind::Static(mt), + ForeignItemKind::Fn(_) => DefKind::Fn, + ForeignItemKind::TyAlias(_) => DefKind::ForeignTy, + ForeignItemKind::MacCall(_) => return self.visit_macro_invoc(foreign_item.id), + }; + // FIXME: The namespace is incorrect for foreign types. let def = self.create_def( foreign_item.id, DefPathData::ValueNs(foreign_item.ident.name), + def_kind, foreign_item.span, ); @@ -186,10 +219,16 @@ impl<'a, 'b, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'b, 'tcx> { if v.is_placeholder { return self.visit_macro_invoc(v.id); } - let def = self.create_def(v.id, DefPathData::TypeNs(v.ident.name), v.span); + let def = + self.create_def(v.id, DefPathData::TypeNs(v.ident.name), DefKind::Variant, v.span); self.with_parent(def, |this| { - if let Some(ctor_node_id) = v.data.ctor_node_id() { - this.create_def(ctor_node_id, DefPathData::Ctor, v.span); + if let Some((ctor_kind, ctor_node_id)) = CtorKind::from_ast(&v.data) { + this.create_def( + ctor_node_id, + DefPathData::Ctor, + DefKind::Ctor(CtorOf::Variant, ctor_kind), + v.span, + ); } visit::walk_variant(this, v) }); @@ -210,12 +249,14 @@ impl<'a, 'b, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'b, 'tcx> { return; } let name = param.ident.name; - let def_path_data = match param.kind { - GenericParamKind::Lifetime { .. } => DefPathData::LifetimeNs(name), - GenericParamKind::Type { .. } => DefPathData::TypeNs(name), - GenericParamKind::Const { .. } => DefPathData::ValueNs(name), + let (def_path_data, def_kind) = match param.kind { + GenericParamKind::Lifetime { .. } => { + (DefPathData::LifetimeNs(name), DefKind::LifetimeParam) + } + GenericParamKind::Type { .. } => (DefPathData::TypeNs(name), DefKind::TyParam), + GenericParamKind::Const { .. } => (DefPathData::ValueNs(name), DefKind::ConstParam), }; - self.create_def(param.id, def_path_data, param.ident.span); + self.create_def(param.id, def_path_data, def_kind, param.ident.span); // impl-Trait can happen inside generic parameters, like // ``` @@ -229,13 +270,14 @@ impl<'a, 'b, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'b, 'tcx> { } fn visit_assoc_item(&mut self, i: &'a AssocItem, ctxt: visit::AssocCtxt) { - let def_data = match &i.kind { - AssocItemKind::Fn(..) | AssocItemKind::Const(..) => DefPathData::ValueNs(i.ident.name), - AssocItemKind::Type(..) => DefPathData::TypeNs(i.ident.name), + let (def_data, def_kind) = match &i.kind { + AssocItemKind::Fn(..) => (DefPathData::ValueNs(i.ident.name), DefKind::AssocFn), + AssocItemKind::Const(..) => (DefPathData::ValueNs(i.ident.name), DefKind::AssocConst), + AssocItemKind::Type(..) => (DefPathData::TypeNs(i.ident.name), DefKind::AssocTy), AssocItemKind::MacCall(..) => return self.visit_macro_invoc(i.id), }; - let def = self.create_def(i.id, def_data, i.span); + let def = self.create_def(i.id, def_data, def_kind, i.span); self.with_parent(def, |this| visit::walk_assoc_item(this, i, ctxt)); } @@ -247,7 +289,12 @@ impl<'a, 'b, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'b, 'tcx> { } fn visit_anon_const(&mut self, constant: &'a AnonConst) { - let def = self.create_def(constant.id, DefPathData::AnonConst, constant.value.span); + let def = self.create_def( + constant.id, + DefPathData::AnonConst, + DefKind::AnonConst, + constant.value.span, + ); self.with_parent(def, |this| visit::walk_anon_const(this, constant)); } @@ -257,15 +304,31 @@ impl<'a, 'b, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'b, 'tcx> { ExprKind::Closure(ref closure) => { // Async closures desugar to closures inside of closures, so // we must create two defs. - let closure_def = self.create_def(expr.id, DefPathData::ClosureExpr, expr.span); + let closure_def = + self.create_def(expr.id, DefPathData::ClosureExpr, DefKind::Closure, expr.span); match closure.asyncness { - Async::Yes { closure_id, .. } => { - self.create_def(closure_id, DefPathData::ClosureExpr, expr.span) - } + Async::Yes { closure_id, .. } => self.create_def( + closure_id, + DefPathData::ClosureExpr, + DefKind::Closure, + expr.span, + ), Async::No => closure_def, } } - ExprKind::Gen(_, _, _) => self.create_def(expr.id, DefPathData::ClosureExpr, expr.span), + ExprKind::Gen(_, _, _) => { + self.create_def(expr.id, DefPathData::ClosureExpr, DefKind::Closure, expr.span) + } + ExprKind::ConstBlock(ref constant) => { + let def = self.create_def( + constant.id, + DefPathData::AnonConst, + DefKind::InlineConst, + constant.value.span, + ); + self.with_parent(def, |this| visit::walk_anon_const(this, constant)); + return; + } _ => self.parent_def, }; diff --git a/compiler/rustc_resolve/src/lib.rs b/compiler/rustc_resolve/src/lib.rs index d15e245868325..208391cc0199f 100644 --- a/compiler/rustc_resolve/src/lib.rs +++ b/compiler/rustc_resolve/src/lib.rs @@ -1034,9 +1034,6 @@ pub struct Resolver<'a, 'tcx> { used_extern_options: FxHashSet, macro_names: FxHashSet, builtin_macros: FxHashMap, - /// A small map keeping true kinds of built-in macros that appear to be fn-like on - /// the surface (`macro` items in libcore), but are actually attributes or derives. - builtin_macro_kinds: FxHashMap, registered_tools: &'tcx RegisteredTools, macro_use_prelude: FxHashMap>, macro_map: FxHashMap, @@ -1216,6 +1213,7 @@ impl<'tcx> Resolver<'_, 'tcx> { parent: LocalDefId, node_id: ast::NodeId, data: DefPathData, + def_kind: DefKind, expn_id: ExpnId, span: Span, ) -> LocalDefId { @@ -1230,6 +1228,9 @@ impl<'tcx> Resolver<'_, 'tcx> { // FIXME: remove `def_span` body, pass in the right spans here and call `tcx.at().create_def()` let def_id = self.tcx.untracked().definitions.write().create_def(parent, data); + let feed = self.tcx.feed_local_def_id(def_id); + feed.def_kind(def_kind); + // Create the definition. if expn_id != ExpnId::root() { self.expn_that_defined.insert(def_id, expn_id); @@ -1403,7 +1404,6 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> { used_extern_options: Default::default(), macro_names: FxHashSet::default(), builtin_macros: Default::default(), - builtin_macro_kinds: Default::default(), registered_tools, macro_use_prelude: FxHashMap::default(), macro_map: FxHashMap::default(), @@ -1542,7 +1542,6 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> { node_id_to_def_id: self.node_id_to_def_id, def_id_to_node_id: self.def_id_to_node_id, trait_map: self.trait_map, - builtin_macro_kinds: self.builtin_macro_kinds, lifetime_elision_allowed: self.lifetime_elision_allowed, lint_buffer: Steal::new(self.lint_buffer), }; diff --git a/compiler/rustc_resolve/src/macros.rs b/compiler/rustc_resolve/src/macros.rs index f8274e7d7d0df..5f21741223bc0 100644 --- a/compiler/rustc_resolve/src/macros.rs +++ b/compiler/rustc_resolve/src/macros.rs @@ -950,10 +950,6 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> { BuiltinMacroState::NotYetSeen(builtin_ext) => { ext.kind = builtin_ext; rule_spans = Vec::new(); - if item.id != ast::DUMMY_NODE_ID { - self.builtin_macro_kinds - .insert(self.local_def_id(item.id), ext.macro_kind()); - } } BuiltinMacroState::AlreadySeen(span) => { struct_span_err!( diff --git a/compiler/rustc_ty_utils/src/assoc.rs b/compiler/rustc_ty_utils/src/assoc.rs index 612123e79015e..82cd0cc50d21a 100644 --- a/compiler/rustc_ty_utils/src/assoc.rs +++ b/compiler/rustc_ty_utils/src/assoc.rs @@ -254,13 +254,12 @@ fn associated_type_for_impl_trait_in_trait( assert_eq!(tcx.def_kind(trait_def_id), DefKind::Trait); let span = tcx.def_span(opaque_ty_def_id); - let trait_assoc_ty = tcx.at(span).create_def(trait_def_id, DefPathData::ImplTraitAssocTy); + let trait_assoc_ty = + tcx.at(span).create_def(trait_def_id, DefPathData::ImplTraitAssocTy, DefKind::AssocTy); let local_def_id = trait_assoc_ty.def_id(); let def_id = local_def_id.to_def_id(); - trait_assoc_ty.def_kind(DefKind::AssocTy); - // There's no HIR associated with this new synthesized `def_id`, so feed // `opt_local_def_id_to_hir_id` with `None`. trait_assoc_ty.opt_local_def_id_to_hir_id(None); @@ -357,13 +356,12 @@ fn associated_type_for_impl_trait_in_impl( hir::FnRetTy::DefaultReturn(_) => tcx.def_span(impl_fn_def_id), hir::FnRetTy::Return(ty) => ty.span, }; - let impl_assoc_ty = tcx.at(span).create_def(impl_local_def_id, DefPathData::ImplTraitAssocTy); + let impl_assoc_ty = + tcx.at(span).create_def(impl_local_def_id, DefPathData::ImplTraitAssocTy, DefKind::AssocTy); let local_def_id = impl_assoc_ty.def_id(); let def_id = local_def_id.to_def_id(); - impl_assoc_ty.def_kind(DefKind::AssocTy); - // There's no HIR associated with this new synthesized `def_id`, so feed // `opt_local_def_id_to_hir_id` with `None`. impl_assoc_ty.opt_local_def_id_to_hir_id(None); diff --git a/tests/ui/dep-graph/dep-graph-trait-impl-two-traits.rs b/tests/ui/dep-graph/dep-graph-trait-impl-two-traits.rs index 590475fa03a3e..0331e75b2fe8d 100644 --- a/tests/ui/dep-graph/dep-graph-trait-impl-two-traits.rs +++ b/tests/ui/dep-graph/dep-graph-trait-impl-two-traits.rs @@ -29,7 +29,7 @@ mod x { mod y { use {Foo, Bar}; - #[rustc_then_this_would_need(typeck)] //~ ERROR OK + #[rustc_then_this_would_need(typeck)] //~ ERROR no path pub fn call_bar() { char::bar('a'); } diff --git a/tests/ui/dep-graph/dep-graph-trait-impl-two-traits.stderr b/tests/ui/dep-graph/dep-graph-trait-impl-two-traits.stderr index 4e10437362c6b..08f382cc024c7 100644 --- a/tests/ui/dep-graph/dep-graph-trait-impl-two-traits.stderr +++ b/tests/ui/dep-graph/dep-graph-trait-impl-two-traits.stderr @@ -1,4 +1,4 @@ -error: OK +error: no path from `x::` to `typeck` --> $DIR/dep-graph-trait-impl-two-traits.rs:32:5 | LL | #[rustc_then_this_would_need(typeck)] From 84de641484fcb337351947573c7c8608ee52dfc2 Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Tue, 28 Nov 2023 15:43:07 +0300 Subject: [PATCH 2/2] def collector: Set correct namespace in `DefPathData` for foreign types --- compiler/rustc_resolve/src/def_collector.rs | 26 ++++++++------------- tests/ui/symbol-names/foreign-types.stderr | 2 +- 2 files changed, 11 insertions(+), 17 deletions(-) diff --git a/compiler/rustc_resolve/src/def_collector.rs b/compiler/rustc_resolve/src/def_collector.rs index cf3e787d839b9..224f3f36a3f56 100644 --- a/compiler/rustc_resolve/src/def_collector.rs +++ b/compiler/rustc_resolve/src/def_collector.rs @@ -194,25 +194,19 @@ impl<'a, 'b, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'b, 'tcx> { visit::walk_use_tree(self, use_tree, id); } - fn visit_foreign_item(&mut self, foreign_item: &'a ForeignItem) { - let def_kind = match foreign_item.kind { - ForeignItemKind::Static(_, mt, _) => DefKind::Static(mt), - ForeignItemKind::Fn(_) => DefKind::Fn, - ForeignItemKind::TyAlias(_) => DefKind::ForeignTy, - ForeignItemKind::MacCall(_) => return self.visit_macro_invoc(foreign_item.id), + fn visit_foreign_item(&mut self, fi: &'a ForeignItem) { + let (def_data, def_kind) = match fi.kind { + ForeignItemKind::Static(_, mt, _) => { + (DefPathData::ValueNs(fi.ident.name), DefKind::Static(mt)) + } + ForeignItemKind::Fn(_) => (DefPathData::ValueNs(fi.ident.name), DefKind::Fn), + ForeignItemKind::TyAlias(_) => (DefPathData::TypeNs(fi.ident.name), DefKind::ForeignTy), + ForeignItemKind::MacCall(_) => return self.visit_macro_invoc(fi.id), }; - // FIXME: The namespace is incorrect for foreign types. - let def = self.create_def( - foreign_item.id, - DefPathData::ValueNs(foreign_item.ident.name), - def_kind, - foreign_item.span, - ); + let def = self.create_def(fi.id, def_data, def_kind, fi.span); - self.with_parent(def, |this| { - visit::walk_foreign_item(this, foreign_item); - }); + self.with_parent(def, |this| visit::walk_foreign_item(this, fi)); } fn visit_variant(&mut self, v: &'a Variant) { diff --git a/tests/ui/symbol-names/foreign-types.stderr b/tests/ui/symbol-names/foreign-types.stderr index 9c8633742b249..6304499148526 100644 --- a/tests/ui/symbol-names/foreign-types.stderr +++ b/tests/ui/symbol-names/foreign-types.stderr @@ -1,4 +1,4 @@ -error: symbol-name(_RMCsCRATE_HASH_13foreign_typesINtB_5CheckNvB_11ForeignTypeE) +error: symbol-name(_RMCsCRATE_HASH_13foreign_typesINtB_5CheckNtB_11ForeignTypeE) --> $DIR/foreign-types.rs:13:1 | LL | #[rustc_symbol_name]