resolve: Feed the def_kind query immediately on DefId creation

This commit is contained in:
Vadim Petrochenkov 2023-11-21 23:40:23 +03:00
parent 46a24ed2f4
commit f0dc906319
15 changed files with 162 additions and 173 deletions

View File

@ -228,6 +228,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
parent_def_id.def_id, parent_def_id.def_id,
node_id, node_id,
DefPathData::AnonConst, DefPathData::AnonConst,
DefKind::AnonConst,
*op_sp, *op_sp,
); );
let anon_const = AnonConst { id: node_id, value: P(expr) }; let anon_const = AnonConst { id: node_id, value: P(expr) };

View File

@ -12,7 +12,7 @@ use rustc_ast::ptr::P as AstP;
use rustc_ast::*; use rustc_ast::*;
use rustc_data_structures::stack::ensure_sufficient_stack; use rustc_data_structures::stack::ensure_sufficient_stack;
use rustc_hir as hir; use rustc_hir as hir;
use rustc_hir::def::Res; use rustc_hir::def::{DefKind, Res};
use rustc_hir::definitions::DefPathData; use rustc_hir::definitions::DefPathData;
use rustc_session::errors::report_lit_error; use rustc_session::errors::report_lit_error;
use rustc_span::source_map::{respan, Spanned}; use rustc_span::source_map::{respan, Spanned};
@ -395,7 +395,13 @@ impl<'hir> LoweringContext<'_, 'hir> {
let node_id = self.next_node_id(); let node_id = self.next_node_id();
// Add a definition for the in-band const def. // 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 }; let anon_const = AnonConst { id: node_id, value: arg };
generic_args.push(AngleBracketedArg::Arg(GenericArg::Const(anon_const))); generic_args.push(AngleBracketedArg::Arg(GenericArg::Const(anon_const)));

View File

@ -480,7 +480,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
} }
ItemKind::MacroDef(MacroDef { body, macro_rules }) => { ItemKind::MacroDef(MacroDef { body, macro_rules }) => {
let body = P(self.lower_delim_args(body)); 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 }); let macro_def = self.arena.alloc(ast::MacroDef { body, macro_rules: *macro_rules });
hir::ItemKind::Macro(macro_def, macro_kind) hir::ItemKind::Macro(macro_def, macro_kind)
} }
@ -1399,6 +1401,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
self.local_def_id(parent_node_id), self.local_def_id(parent_node_id),
param_node_id, param_node_id,
DefPathData::TypeNs(sym::host), DefPathData::TypeNs(sym::host),
DefKind::ConstParam,
span, span,
); );
self.host_param_id = Some(def_id); 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 { if let Some((span, hir_id, def_id)) = host_param_parts {
let const_node_id = self.next_node_id(); let const_node_id = self.next_node_id();
let anon_const: LocalDefId = let anon_const = self.create_def(
self.create_def(def_id, const_node_id, DefPathData::AnonConst, span); def_id,
const_node_id,
DefPathData::AnonConst,
DefKind::AnonConst,
span,
);
let const_id = self.next_id(); let const_id = self.next_id();
let const_expr_id = self.next_id(); let const_expr_id = self.next_id();

View File

@ -67,7 +67,6 @@ use rustc_middle::{
ty::{ResolverAstLowering, TyCtxt}, ty::{ResolverAstLowering, TyCtxt},
}; };
use rustc_session::parse::{add_feature_diagnostics, feature_err}; 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::symbol::{kw, sym, Ident, Symbol};
use rustc_span::{DesugaringKind, Span, DUMMY_SP}; use rustc_span::{DesugaringKind, Span, DUMMY_SP};
use smallvec::SmallVec; use smallvec::SmallVec;
@ -153,7 +152,6 @@ trait ResolverAstLoweringExt {
fn get_lifetime_res(&self, id: NodeId) -> Option<LifetimeRes>; fn get_lifetime_res(&self, id: NodeId) -> Option<LifetimeRes>;
fn take_extra_lifetime_params(&mut self, id: NodeId) -> Vec<(Ident, NodeId, LifetimeRes)>; 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 remap_extra_lifetime_params(&mut self, from: NodeId, to: NodeId);
fn decl_macro_kind(&self, def_id: LocalDefId) -> MacroKind;
} }
impl ResolverAstLoweringExt for ResolverAstLowering { impl ResolverAstLoweringExt for ResolverAstLowering {
@ -217,10 +215,6 @@ impl ResolverAstLoweringExt for ResolverAstLowering {
let lifetimes = self.extra_lifetime_params_map.remove(&from).unwrap_or_default(); let lifetimes = self.extra_lifetime_params_map.remove(&from).unwrap_or_default();
self.extra_lifetime_params_map.insert(to, lifetimes); 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, /// 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, parent: LocalDefId,
node_id: ast::NodeId, node_id: ast::NodeId,
data: DefPathData, data: DefPathData,
def_kind: DefKind,
span: Span, span: Span,
) -> LocalDefId { ) -> LocalDefId {
debug_assert_ne!(node_id, ast::DUMMY_NODE_ID); 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)), 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); debug!("create_def: def_id_to_node_id[{:?}] <-> {:?}", def_id, node_id);
self.resolver.node_id_to_def_id.insert(node_id, def_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, self.current_hir_id_owner.def_id,
param, param,
DefPathData::LifetimeNs(kw::UnderscoreLifetime), DefPathData::LifetimeNs(kw::UnderscoreLifetime),
DefKind::LifetimeParam,
ident.span, ident.span,
); );
debug!(?_def_id); debug!(?_def_id);
@ -1192,6 +1188,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
parent_def_id.def_id, parent_def_id.def_id,
node_id, node_id,
DefPathData::AnonConst, DefPathData::AnonConst,
DefKind::AnonConst,
span, span,
); );
@ -1429,6 +1426,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
self.current_hir_id_owner.def_id, self.current_hir_id_owner.def_id,
*def_node_id, *def_node_id,
DefPathData::TypeNs(ident.name), DefPathData::TypeNs(ident.name),
DefKind::TyParam,
span, span,
); );
let (param, bounds, path) = self.lower_universal_param_and_bounds( 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, self.current_hir_id_owner.def_id,
opaque_ty_node_id, opaque_ty_node_id,
DefPathData::ImplTrait, DefPathData::ImplTrait,
DefKind::OpaqueTy,
opaque_ty_span, opaque_ty_span,
); );
debug!(?opaque_ty_def_id); debug!(?opaque_ty_def_id);
@ -1636,6 +1635,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
opaque_ty_def_id, opaque_ty_def_id,
duplicated_lifetime_node_id, duplicated_lifetime_node_id,
DefPathData::LifetimeNs(lifetime.ident.name), DefPathData::LifetimeNs(lifetime.ident.name),
DefKind::LifetimeParam,
lifetime.ident.span, lifetime.ident.span,
); );
captured_to_synthesized_mapping.insert(old_def_id, duplicated_lifetime_def_id); 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)); lcx.attrs.insert(hir_id.local_id, std::slice::from_ref(attr));
let def_id = let def_id = lcx.create_def(
lcx.create_def(lcx.current_hir_id_owner.def_id, id, DefPathData::AnonConst, span); lcx.current_hir_id_owner.def_id,
id,
DefPathData::AnonConst,
DefKind::AnonConst,
span,
);
lcx.children.push((def_id, hir::MaybeOwner::NonOwner(hir_id))); lcx.children.push((def_id, hir::MaybeOwner::NonOwner(hir_id)));
self.args.push(hir::GenericArg::Const(hir::ConstArg { self.args.push(hir::GenericArg::Const(hir::ConstArg {
value: hir::AnonConst { def_id, hir_id, body }, value: hir::AnonConst { def_id, hir_id, body },

View File

@ -8,6 +8,7 @@ use rustc_codegen_ssa::CodegenResults;
use rustc_data_structures::steal::Steal; use rustc_data_structures::steal::Steal;
use rustc_data_structures::svh::Svh; use rustc_data_structures::svh::Svh;
use rustc_data_structures::sync::{AppendOnlyIndexVec, FreezeLock, OnceLock, WorkerLocal}; 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::def_id::{StableCrateId, CRATE_DEF_ID, LOCAL_CRATE};
use rustc_hir::definitions::Definitions; use rustc_hir::definitions::Definitions;
use rustc_incremental::setup_dep_graph; 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.crate_for_resolver(tcx.arena.alloc(Steal::new((krate, pre_configured_attrs))));
feed.output_filenames(Arc::new(outputs)); feed.output_filenames(Arc::new(outputs));
let feed = tcx.feed_local_def_id(CRATE_DEF_ID);
feed.def_kind(DefKind::Mod);
}); });
Ok(qcx) Ok(qcx)
}) })

View File

@ -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_data_structures::sync::{par_for_each_in, try_par_for_each_in, DynSend, DynSync};
use rustc_hir::def::{DefKind, Res}; use rustc_hir::def::{DefKind, Res};
use rustc_hir::def_id::{DefId, LocalDefId, LocalModDefId, LOCAL_CRATE}; 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::intravisit::{self, Visitor};
use rustc_hir::*; use rustc_hir::*;
use rustc_index::Idx; use rustc_index::Idx;
@ -168,98 +168,6 @@ impl<'hir> Map<'hir> {
self.tcx.definitions_untracked().def_path_hash(def_id) 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. /// Finds the id of the parent node to this one.
/// ///
/// If calling repeatedly and iterating over parents, prefer [`Map::parent_iter`]. /// If calling repeatedly and iterating over parents, prefer [`Map::parent_iter`].

View File

@ -202,7 +202,6 @@ pub fn provide(providers: &mut Providers) {
span_bug!(hir.span(hir_id), "fn_arg_names: unexpected item {:?}", def_id); 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.all_local_trait_impls = |tcx, ()| &tcx.resolutions(()).trait_impls;
providers.expn_that_defined = providers.expn_that_defined =
|tcx, id| tcx.resolutions(()).expn_that_defined.get(&id).copied().unwrap_or(ExpnId::root()); |tcx, id| tcx.resolutions(()).expn_that_defined.get(&id).copied().unwrap_or(ExpnId::root());

View File

@ -501,6 +501,9 @@ impl<'tcx> TyCtxt<'tcx> {
pub fn feed_local_crate(self) -> TyCtxtFeed<'tcx, CrateNum> { pub fn feed_local_crate(self) -> TyCtxtFeed<'tcx, CrateNum> {
TyCtxtFeed { tcx: self, key: LOCAL_CRATE } 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 /// 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 /// effect. However, we do not want this as a general capability, so this interface restricts
@ -973,6 +976,7 @@ impl<'tcx> TyCtxtAt<'tcx> {
self, self,
parent: LocalDefId, parent: LocalDefId,
data: hir::definitions::DefPathData, data: hir::definitions::DefPathData,
def_kind: DefKind,
) -> TyCtxtFeed<'tcx, LocalDefId> { ) -> TyCtxtFeed<'tcx, LocalDefId> {
// This function modifies `self.definitions` using a side-effect. // 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. // 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 key = self.untracked.definitions.write().create_def(parent, data);
let feed = TyCtxtFeed { tcx: self.tcx, key }; let feed = TyCtxtFeed { tcx: self.tcx, key };
feed.def_kind(def_kind);
feed.def_span(self.span); feed.def_span(self.span);
feed feed
} }

View File

@ -202,9 +202,6 @@ pub struct ResolverAstLowering {
pub def_id_to_node_id: IndexVec<LocalDefId, ast::NodeId>, pub def_id_to_node_id: IndexVec<LocalDefId, ast::NodeId>,
pub trait_map: NodeMap<Vec<hir::TraitCandidate>>, pub trait_map: NodeMap<Vec<hir::TraitCandidate>>,
/// 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<LocalDefId, MacroKind>,
/// List functions and methods for which lifetime elision was successful. /// List functions and methods for which lifetime elision was successful.
pub lifetime_elision_allowed: FxHashSet<ast::NodeId>, pub lifetime_elision_allowed: FxHashSet<ast::NodeId>,

View File

@ -2,6 +2,7 @@ use crate::{ImplTraitContext, Resolver};
use rustc_ast::visit::{self, FnKind}; use rustc_ast::visit::{self, FnKind};
use rustc_ast::*; use rustc_ast::*;
use rustc_expand::expand::AstFragment; use rustc_expand::expand::AstFragment;
use rustc_hir::def::{CtorKind, CtorOf, DefKind};
use rustc_hir::def_id::LocalDefId; use rustc_hir::def_id::LocalDefId;
use rustc_hir::definitions::*; use rustc_hir::definitions::*;
use rustc_span::hygiene::LocalExpnId; use rustc_span::hygiene::LocalExpnId;
@ -26,13 +27,20 @@ struct DefCollector<'a, 'b, 'tcx> {
} }
impl<'a, 'b, 'tcx> 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; let parent_def = self.parent_def;
debug!("create_def(node_id={:?}, data={:?}, parent_def={:?})", node_id, data, parent_def); debug!("create_def(node_id={:?}, data={:?}, parent_def={:?})", node_id, data, parent_def);
self.resolver.create_def( self.resolver.create_def(
parent_def, parent_def,
node_id, node_id,
data, data,
def_kind,
self.expansion.to_expn_id(), self.expansion.to_expn_id(),
span.with_parent(None), span.with_parent(None),
) )
@ -68,7 +76,8 @@ impl<'a, 'b, 'tcx> DefCollector<'a, 'b, 'tcx> {
self.visit_macro_invoc(field.id); self.visit_macro_invoc(field.id);
} else { } else {
let name = field.ident.map_or_else(|| sym::integer(index(self)), |ident| ident.name); 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)); 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 // Pick the def data. This need not be unique, but the more
// information we encapsulate into, the better // information we encapsulate into, the better
let def_data = match &i.kind { let mut opt_macro_data = None;
ItemKind::Impl { .. } => DefPathData::Impl, let ty_data = DefPathData::TypeNs(i.ident.name);
ItemKind::ForeignMod(..) => DefPathData::ForeignMod, let value_data = DefPathData::ValueNs(i.ident.name);
ItemKind::Mod(..) let (def_data, def_kind) = match &i.kind {
| ItemKind::Trait(..) ItemKind::Impl(i) => {
| ItemKind::TraitAlias(..) (DefPathData::Impl, DefKind::Impl { of_trait: i.of_trait.is_some() })
| ItemKind::Enum(..) }
| ItemKind::Struct(..) ItemKind::ForeignMod(..) => (DefPathData::ForeignMod, DefKind::ForeignMod),
| ItemKind::Union(..) ItemKind::Mod(..) => (ty_data, DefKind::Mod),
| ItemKind::ExternCrate(..) ItemKind::Trait(..) => (ty_data, DefKind::Trait),
| ItemKind::TyAlias(..) => DefPathData::TypeNs(i.ident.name), ItemKind::TraitAlias(..) => (ty_data, DefKind::TraitAlias),
ItemKind::Static(..) | ItemKind::Const(..) | ItemKind::Fn(..) => { ItemKind::Enum(..) => (ty_data, DefKind::Enum),
DefPathData::ValueNs(i.ident.name) 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(..) => { ItemKind::MacCall(..) => {
visit::walk_item(self, i); visit::walk_item(self, i);
return self.visit_macro_invoc(i.id); return self.visit_macro_invoc(i.id);
} }
ItemKind::GlobalAsm(..) => DefPathData::GlobalAsm, ItemKind::GlobalAsm(..) => (DefPathData::GlobalAsm, DefKind::GlobalAsm),
ItemKind::Use(..) => { ItemKind::Use(..) => {
return visit::walk_item(self, i); 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 { if let Some(macro_data) = opt_macro_data {
let macro_data = self.resolver.compile_macro(i, self.resolver.tcx.sess.edition());
self.resolver.macro_map.insert(def_id.to_def_id(), 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 { match i.kind {
ItemKind::Struct(ref struct_def, _) | ItemKind::Union(ref struct_def, _) => { ItemKind::Struct(ref struct_def, _) | ItemKind::Union(ref struct_def, _) => {
// If this is a unit or tuple-like struct, register the constructor. // If this is a unit or tuple-like struct, register the constructor.
if let Some(ctor_node_id) = struct_def.ctor_node_id() { if let Some((ctor_kind, ctor_node_id)) = CtorKind::from_ast(struct_def) {
this.create_def(ctor_node_id, DefPathData::Ctor, i.span); 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 // then the closure_def will never be used, and we should avoid generating a
// def-id for it. // def-id for it.
if let Some(body) = body { 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)); self.with_parent(closure_def, |this| this.visit_block(body));
} }
return; 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) { 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); visit::walk_use_tree(self, use_tree, id);
} }
fn visit_foreign_item(&mut self, foreign_item: &'a ForeignItem) { fn visit_foreign_item(&mut self, foreign_item: &'a ForeignItem) {
if let ForeignItemKind::MacCall(_) = foreign_item.kind { let def_kind = match foreign_item.kind {
return self.visit_macro_invoc(foreign_item.id); 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( let def = self.create_def(
foreign_item.id, foreign_item.id,
DefPathData::ValueNs(foreign_item.ident.name), DefPathData::ValueNs(foreign_item.ident.name),
def_kind,
foreign_item.span, foreign_item.span,
); );
@ -186,10 +219,16 @@ impl<'a, 'b, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'b, 'tcx> {
if v.is_placeholder { if v.is_placeholder {
return self.visit_macro_invoc(v.id); 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| { self.with_parent(def, |this| {
if let Some(ctor_node_id) = v.data.ctor_node_id() { if let Some((ctor_kind, ctor_node_id)) = CtorKind::from_ast(&v.data) {
this.create_def(ctor_node_id, DefPathData::Ctor, v.span); this.create_def(
ctor_node_id,
DefPathData::Ctor,
DefKind::Ctor(CtorOf::Variant, ctor_kind),
v.span,
);
} }
visit::walk_variant(this, v) visit::walk_variant(this, v)
}); });
@ -210,12 +249,14 @@ impl<'a, 'b, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'b, 'tcx> {
return; return;
} }
let name = param.ident.name; let name = param.ident.name;
let def_path_data = match param.kind { let (def_path_data, def_kind) = match param.kind {
GenericParamKind::Lifetime { .. } => DefPathData::LifetimeNs(name), GenericParamKind::Lifetime { .. } => {
GenericParamKind::Type { .. } => DefPathData::TypeNs(name), (DefPathData::LifetimeNs(name), DefKind::LifetimeParam)
GenericParamKind::Const { .. } => DefPathData::ValueNs(name), }
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 // 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) { fn visit_assoc_item(&mut self, i: &'a AssocItem, ctxt: visit::AssocCtxt) {
let def_data = match &i.kind { let (def_data, def_kind) = match &i.kind {
AssocItemKind::Fn(..) | AssocItemKind::Const(..) => DefPathData::ValueNs(i.ident.name), AssocItemKind::Fn(..) => (DefPathData::ValueNs(i.ident.name), DefKind::AssocFn),
AssocItemKind::Type(..) => DefPathData::TypeNs(i.ident.name), 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), 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)); 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) { 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)); 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) => { ExprKind::Closure(ref closure) => {
// Async closures desugar to closures inside of closures, so // Async closures desugar to closures inside of closures, so
// we must create two defs. // 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 { match closure.asyncness {
Async::Yes { closure_id, .. } => { Async::Yes { closure_id, .. } => self.create_def(
self.create_def(closure_id, DefPathData::ClosureExpr, expr.span) closure_id,
} DefPathData::ClosureExpr,
DefKind::Closure,
expr.span,
),
Async::No => closure_def, 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, _ => self.parent_def,
}; };

View File

@ -1034,9 +1034,6 @@ pub struct Resolver<'a, 'tcx> {
used_extern_options: FxHashSet<Symbol>, used_extern_options: FxHashSet<Symbol>,
macro_names: FxHashSet<Ident>, macro_names: FxHashSet<Ident>,
builtin_macros: FxHashMap<Symbol, BuiltinMacroState>, builtin_macros: FxHashMap<Symbol, BuiltinMacroState>,
/// 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<LocalDefId, MacroKind>,
registered_tools: &'tcx RegisteredTools, registered_tools: &'tcx RegisteredTools,
macro_use_prelude: FxHashMap<Symbol, NameBinding<'a>>, macro_use_prelude: FxHashMap<Symbol, NameBinding<'a>>,
macro_map: FxHashMap<DefId, MacroData>, macro_map: FxHashMap<DefId, MacroData>,
@ -1216,6 +1213,7 @@ impl<'tcx> Resolver<'_, 'tcx> {
parent: LocalDefId, parent: LocalDefId,
node_id: ast::NodeId, node_id: ast::NodeId,
data: DefPathData, data: DefPathData,
def_kind: DefKind,
expn_id: ExpnId, expn_id: ExpnId,
span: Span, span: Span,
) -> LocalDefId { ) -> 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()` // 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 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. // Create the definition.
if expn_id != ExpnId::root() { if expn_id != ExpnId::root() {
self.expn_that_defined.insert(def_id, expn_id); self.expn_that_defined.insert(def_id, expn_id);
@ -1403,7 +1404,6 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
used_extern_options: Default::default(), used_extern_options: Default::default(),
macro_names: FxHashSet::default(), macro_names: FxHashSet::default(),
builtin_macros: Default::default(), builtin_macros: Default::default(),
builtin_macro_kinds: Default::default(),
registered_tools, registered_tools,
macro_use_prelude: FxHashMap::default(), macro_use_prelude: FxHashMap::default(),
macro_map: 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, node_id_to_def_id: self.node_id_to_def_id,
def_id_to_node_id: self.def_id_to_node_id, def_id_to_node_id: self.def_id_to_node_id,
trait_map: self.trait_map, trait_map: self.trait_map,
builtin_macro_kinds: self.builtin_macro_kinds,
lifetime_elision_allowed: self.lifetime_elision_allowed, lifetime_elision_allowed: self.lifetime_elision_allowed,
lint_buffer: Steal::new(self.lint_buffer), lint_buffer: Steal::new(self.lint_buffer),
}; };

View File

@ -950,10 +950,6 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
BuiltinMacroState::NotYetSeen(builtin_ext) => { BuiltinMacroState::NotYetSeen(builtin_ext) => {
ext.kind = builtin_ext; ext.kind = builtin_ext;
rule_spans = Vec::new(); 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) => { BuiltinMacroState::AlreadySeen(span) => {
struct_span_err!( struct_span_err!(

View File

@ -254,13 +254,12 @@ fn associated_type_for_impl_trait_in_trait(
assert_eq!(tcx.def_kind(trait_def_id), DefKind::Trait); assert_eq!(tcx.def_kind(trait_def_id), DefKind::Trait);
let span = tcx.def_span(opaque_ty_def_id); 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 local_def_id = trait_assoc_ty.def_id();
let def_id = local_def_id.to_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 // There's no HIR associated with this new synthesized `def_id`, so feed
// `opt_local_def_id_to_hir_id` with `None`. // `opt_local_def_id_to_hir_id` with `None`.
trait_assoc_ty.opt_local_def_id_to_hir_id(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::DefaultReturn(_) => tcx.def_span(impl_fn_def_id),
hir::FnRetTy::Return(ty) => ty.span, 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 local_def_id = impl_assoc_ty.def_id();
let def_id = local_def_id.to_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 // There's no HIR associated with this new synthesized `def_id`, so feed
// `opt_local_def_id_to_hir_id` with `None`. // `opt_local_def_id_to_hir_id` with `None`.
impl_assoc_ty.opt_local_def_id_to_hir_id(None); impl_assoc_ty.opt_local_def_id_to_hir_id(None);

View File

@ -29,7 +29,7 @@ mod x {
mod y { mod y {
use {Foo, Bar}; use {Foo, Bar};
#[rustc_then_this_would_need(typeck)] //~ ERROR OK #[rustc_then_this_would_need(typeck)] //~ ERROR no path
pub fn call_bar() { pub fn call_bar() {
char::bar('a'); char::bar('a');
} }

View File

@ -1,4 +1,4 @@
error: OK error: no path from `x::<impl Foo for char>` to `typeck`
--> $DIR/dep-graph-trait-impl-two-traits.rs:32:5 --> $DIR/dep-graph-trait-impl-two-traits.rs:32:5
| |
LL | #[rustc_then_this_would_need(typeck)] LL | #[rustc_then_this_would_need(typeck)]