Split up `Definitions` and `ResolverAstLowering`.
Split off https://github.com/rust-lang/rust/pull/95573
r? `@michaelwoerister`
"rustc_errors",
"rustc_hir",
"rustc_index",
+ "rustc_middle",
"rustc_query_system",
"rustc_session",
"rustc_span",
"bitflags",
"rustc_arena",
"rustc_ast",
- "rustc_ast_lowering",
"rustc_ast_pretty",
"rustc_attr",
"rustc_data_structures",
rustc_target = { path = "../rustc_target" }
rustc_data_structures = { path = "../rustc_data_structures" }
rustc_index = { path = "../rustc_index" }
+rustc_middle = { path = "../rustc_middle" }
rustc_query_system = { path = "../rustc_query_system" }
rustc_span = { path = "../rustc_span" }
rustc_errors = { path = "../rustc_errors" }
-use crate::{ImplTraitContext, ImplTraitPosition, ParamMode};
+use crate::{ImplTraitContext, ImplTraitPosition, ParamMode, ResolverAstLoweringExt};
use super::LoweringContext;
use rustc_hir::def::{DefKind, Res};
use rustc_hir::definitions::DefPathData;
use rustc_session::parse::feature_err;
-use rustc_span::{sym, ExpnId, Span};
+use rustc_span::{sym, Span};
use rustc_target::asm;
use std::collections::hash_map::Entry;
use std::fmt::Write;
// Wrap the expression in an AnonConst.
let parent_def_id = self.current_hir_id_owner;
- let node_id = self.resolver.next_node_id();
- self.resolver.create_def(
- parent_def_id,
- node_id,
- DefPathData::AnonConst,
- ExpnId::root(),
- *op_sp,
- );
+ let node_id = self.next_node_id();
+ self.create_def(parent_def_id, node_id, DefPathData::AnonConst);
let anon_const = AnonConst { id: node_id, value: P(expr) };
hir::InlineAsmOperand::SymFn {
anon_const: self.lower_anon_const(&anon_const),
-use crate::{FnDeclKind, ImplTraitPosition};
-
+use super::ResolverAstLoweringExt;
use super::{ImplTraitContext, LoweringContext, ParamMode, ParenthesizedGenericArgs};
+use crate::{FnDeclKind, ImplTraitPosition};
use rustc_ast::attr;
use rustc_ast::ptr::P as AstP;
use rustc_hir as hir;
use rustc_hir::def::Res;
use rustc_hir::definitions::DefPathData;
-use rustc_span::hygiene::ExpnId;
use rustc_span::source_map::{respan, DesugaringKind, Span, Spanned};
use rustc_span::symbol::{sym, Ident};
use rustc_span::DUMMY_SP;
for (idx, arg) in args.into_iter().enumerate() {
if legacy_args_idx.contains(&idx) {
let parent_def_id = self.current_hir_id_owner;
- let node_id = self.resolver.next_node_id();
+ let node_id = self.next_node_id();
// Add a definition for the in-band const def.
- self.resolver.create_def(
- parent_def_id,
- node_id,
- DefPathData::AnonConst,
- ExpnId::root(),
- arg.span,
- );
+ self.create_def(parent_def_id, node_id, DefPathData::AnonConst);
let anon_const = AnonConst { id: node_id, value: arg };
generic_args.push(AngleBracketedArg::Arg(GenericArg::Const(anon_const)));
};
// `::std::task::Poll::Ready(result) => break result`
- let loop_node_id = self.resolver.next_node_id();
+ let loop_node_id = self.next_node_id();
let loop_hir_id = self.lower_node_id(loop_node_id);
let ready_arm = {
let x_ident = Ident::with_dummy_span(sym::result);
use rustc_hir::intravisit::{self, Visitor};
use rustc_hir::*;
use rustc_index::vec::{Idx, IndexVec};
+use rustc_middle::span_bug;
use rustc_session::Session;
use rustc_span::source_map::SourceMap;
use rustc_span::{Span, DUMMY_SP};
// owner of that node.
if cfg!(debug_assertions) {
if hir_id.owner != self.owner {
- panic!(
+ span_bug!(
+ span,
"inconsistent DepNode at `{:?}` for `{:?}`: \
current_dep_node_owner={} ({:?}), hir_id.owner={} ({:?})",
self.source_map.span_to_diagnostic_string(span),
-use super::{AstOwner, ImplTraitContext, ImplTraitPosition, ResolverAstLowering};
+use super::ResolverAstLoweringExt;
+use super::{AstOwner, ImplTraitContext, ImplTraitPosition};
use super::{LoweringContext, ParamMode};
use crate::{Arena, FnDeclKind};
use rustc_hir as hir;
use rustc_hir::def::{DefKind, Res};
use rustc_hir::def_id::{LocalDefId, CRATE_DEF_ID};
+use rustc_hir::definitions::Definitions;
use rustc_hir::PredicateOrigin;
use rustc_index::vec::{Idx, IndexVec};
+use rustc_middle::ty::{ResolverAstLowering, ResolverOutputs};
+use rustc_session::cstore::CrateStoreDyn;
use rustc_session::Session;
use rustc_span::source_map::DesugaringKind;
use rustc_span::symbol::{kw, sym, Ident};
pub(super) struct ItemLowerer<'a, 'hir> {
pub(super) sess: &'a Session,
- pub(super) resolver: &'a mut dyn ResolverAstLowering,
+ pub(super) definitions: &'a mut Definitions,
+ pub(super) cstore: &'a CrateStoreDyn,
+ pub(super) resolutions: &'a ResolverOutputs,
+ pub(super) resolver: &'a mut ResolverAstLowering,
pub(super) arena: &'hir Arena<'hir>,
pub(super) ast_index: &'a IndexVec<LocalDefId, AstOwner<'a>>,
pub(super) owners: &'a mut IndexVec<LocalDefId, hir::MaybeOwner<&'hir hir::OwnerInfo<'hir>>>,
let mut lctx = LoweringContext {
// Pseudo-globals.
sess: &self.sess,
+ definitions: self.definitions,
+ cstore: self.cstore,
+ resolutions: self.resolutions,
resolver: self.resolver,
arena: self.arena,
#[instrument(level = "debug", skip(self, c))]
fn lower_crate(&mut self, c: &Crate) {
- debug_assert_eq!(self.resolver.local_def_id(CRATE_NODE_ID), CRATE_DEF_ID);
-
+ debug_assert_eq!(self.resolver.node_id_to_def_id[&CRATE_NODE_ID], CRATE_DEF_ID);
self.with_lctx(CRATE_NODE_ID, |lctx| {
let module = lctx.lower_mod(&c.items, &c.spans);
lctx.lower_attrs(hir::CRATE_HIR_ID, &c.attrs);
}
fn lower_assoc_item(&mut self, item: &AssocItem, ctxt: AssocCtxt) {
- let def_id = self.resolver.local_def_id(item.id);
+ let def_id = self.resolver.node_id_to_def_id[&item.id];
let parent_id = {
- let parent = self.resolver.definitions().def_key(def_id).parent;
+ let parent = self.definitions.def_key(def_id).parent;
let local_def_index = parent.unwrap();
LocalDefId { local_def_index }
};
}
pub(super) fn lower_item_ref(&mut self, i: &Item) -> SmallVec<[hir::ItemId; 1]> {
- let mut node_ids = smallvec![hir::ItemId { def_id: self.resolver.local_def_id(i.id) }];
+ let mut node_ids = smallvec![hir::ItemId { def_id: self.local_def_id(i.id) }];
if let ItemKind::Use(ref use_tree) = &i.kind {
self.lower_item_id_use_tree(use_tree, i.id, &mut node_ids);
}
match tree.kind {
UseTreeKind::Nested(ref nested_vec) => {
for &(ref nested, id) in nested_vec {
- vec.push(hir::ItemId { def_id: self.resolver.local_def_id(id) });
+ vec.push(hir::ItemId { def_id: self.local_def_id(id) });
self.lower_item_id_use_tree(nested, id, vec);
}
}
for (_, &id) in
iter::zip(self.expect_full_res_from_use(base_id).skip(1), &[id1, id2])
{
- vec.push(hir::ItemId { def_id: self.resolver.local_def_id(id) });
+ vec.push(hir::ItemId { def_id: self.local_def_id(id) });
}
}
}
}
ItemKind::MacroDef(MacroDef { ref body, macro_rules }) => {
let body = P(self.lower_mac_args(body));
- let macro_kind = self.resolver.decl_macro_kind(self.resolver.local_def_id(id));
+ let macro_kind = self.resolver.decl_macro_kind(self.local_def_id(id));
hir::ItemKind::Macro(ast::MacroDef { body, macro_rules }, macro_kind)
}
ItemKind::MacCall(..) => {
// Essentially a single `use` which imports two names is desugared into
// two imports.
for new_node_id in [id1, id2] {
- let new_id = self.resolver.local_def_id(new_node_id);
+ let new_id = self.local_def_id(new_node_id);
let Some(res) = resolutions.next() else {
// Associate an HirId to both ids even if there is no resolution.
let _old = self.children.insert(
let ident = *ident;
let mut path = path.clone();
for seg in &mut path.segments {
- seg.id = self.resolver.next_node_id();
+ seg.id = self.next_node_id();
}
let span = path.span;
// Add all the nested `PathListItem`s to the HIR.
for &(ref use_tree, id) in trees {
- let new_hir_id = self.resolver.local_def_id(id);
+ let new_hir_id = self.local_def_id(id);
let mut prefix = prefix.clone();
// Give the segments new node-ids since they are being cloned.
for seg in &mut prefix.segments {
- seg.id = self.resolver.next_node_id();
+ seg.id = self.next_node_id();
}
// Each `use` import is an item and thus are owners of the
fn lower_foreign_item_ref(&mut self, i: &ForeignItem) -> hir::ForeignItemRef {
hir::ForeignItemRef {
- id: hir::ForeignItemId { def_id: self.resolver.local_def_id(i.id) },
+ id: hir::ForeignItemId { def_id: self.local_def_id(i.id) },
ident: self.lower_ident(i.ident),
span: self.lower_span(i.span),
}
}
AssocItemKind::MacCall(..) => unimplemented!(),
};
- let id = hir::TraitItemId { def_id: self.resolver.local_def_id(i.id) };
+ let id = hir::TraitItemId { def_id: self.local_def_id(i.id) };
let defaultness = hir::Defaultness::Default { has_value: has_default };
hir::TraitItemRef {
id,
let has_value = true;
let (defaultness, _) = self.lower_defaultness(i.kind.defaultness(), has_value);
hir::ImplItemRef {
- id: hir::ImplItemId { def_id: self.resolver.local_def_id(i.id) },
+ id: hir::ImplItemId { def_id: self.local_def_id(i.id) },
ident: self.lower_ident(i.ident),
span: self.lower_span(i.span),
defaultness,
generics
.params
.iter()
- .any(|p| def_id == self.resolver.local_def_id(p.id).to_def_id())
+ .any(|p| def_id == self.local_def_id(p.id).to_def_id())
}
// Either the `bounded_ty` is not a plain type parameter, or
// it's not found in the generic type parameters list.
match kind {
GenericParamKind::Const { .. } => None,
GenericParamKind::Type { .. } => {
- let def_id = self.resolver.local_def_id(id).to_def_id();
+ let def_id = self.local_def_id(id).to_def_id();
let ty_path = self.arena.alloc(hir::Path {
span: param_span,
res: Res::Def(DefKind::TyParam, def_id),
let res = self.resolver.get_lifetime_res(id).unwrap_or_else(|| {
panic!("Missing resolution for lifetime {:?} at {:?}", id, ident.span)
});
- let lt_id = self.resolver.next_node_id();
+ let lt_id = self.next_node_id();
let lifetime = self.new_named_lifetime_with_res(lt_id, ident_span, ident, res);
Some(hir::WherePredicate::RegionPredicate(hir::WhereRegionPredicate {
lifetime,
use rustc_data_structures::sync::Lrc;
use rustc_errors::{struct_span_err, Applicability};
use rustc_hir as hir;
-use rustc_hir::def::{DefKind, Namespace, PartialRes, PerNS, Res};
-use rustc_hir::def_id::{DefId, DefPathHash, LocalDefId, CRATE_DEF_ID};
-use rustc_hir::definitions::{DefKey, DefPathData, Definitions};
+use rustc_hir::def::{DefKind, LifetimeRes, Namespace, PartialRes, PerNS, Res};
+use rustc_hir::def_id::{LocalDefId, CRATE_DEF_ID};
+use rustc_hir::definitions::{DefPathData, Definitions};
use rustc_hir::{ConstArg, GenericArg, ItemLocalId, ParamName, TraitCandidate};
use rustc_index::vec::{Idx, IndexVec};
+use rustc_middle::ty::{ResolverAstLowering, ResolverOutputs};
use rustc_query_system::ich::StableHashingContext;
+use rustc_session::cstore::CrateStoreDyn;
use rustc_session::parse::feature_err;
use rustc_session::Session;
-use rustc_span::hygiene::{ExpnId, MacroKind};
+use rustc_span::hygiene::MacroKind;
use rustc_span::source_map::DesugaringKind;
use rustc_span::symbol::{kw, sym, Ident, Symbol};
use rustc_span::{Span, DUMMY_SP};
/// Used to assign IDs to HIR nodes that do not directly correspond to AST nodes.
sess: &'a Session,
- resolver: &'a mut dyn ResolverAstLowering,
+ definitions: &'a mut Definitions,
+ cstore: &'a CrateStoreDyn,
+ resolutions: &'a ResolverOutputs,
+ resolver: &'a mut ResolverAstLowering,
/// Used to allocate HIR nodes.
arena: &'hir Arena<'hir>,
allow_into_future: Option<Lrc<[Symbol]>>,
}
-/// Resolution for a lifetime appearing in a type.
-#[derive(Copy, Clone, Debug)]
-pub enum LifetimeRes {
- /// Successfully linked the lifetime to a generic parameter.
- Param {
- /// Id of the generic parameter that introduced it.
- param: LocalDefId,
- /// Id of the introducing place. That can be:
- /// - an item's id, for the item's generic parameters;
- /// - a TraitRef's ref_id, identifying the `for<...>` binder;
- /// - a BareFn type's id;
- /// - a Path's id when this path has parenthesized generic args.
- ///
- /// This information is used for impl-trait lifetime captures, to know when to or not to
- /// capture any given lifetime.
- binder: NodeId,
- },
- /// Created a generic parameter for an anonymous lifetime.
- Fresh {
- /// Id of the generic parameter that introduced it.
- param: LocalDefId,
- /// Id of the introducing place. See `Param`.
- binder: NodeId,
- },
- /// This variant is used for anonymous lifetimes that we did not resolve during
- /// late resolution. Shifting the work to the HIR lifetime resolver.
- Anonymous {
- /// Id of the introducing place. See `Param`.
- binder: NodeId,
- /// Whether this lifetime was spelled or elided.
- elided: bool,
- },
- /// Explicit `'static` lifetime.
- Static,
- /// Resolution failure.
- Error,
- /// HACK: This is used to recover the NodeId of an elided lifetime.
- ElidedAnchor { start: NodeId, end: NodeId },
-}
-
/// When we lower a lifetime, it is inserted in `captures`, and the resolution is modified so
/// to point to the lifetime parameter impl-trait will generate.
/// When traversing `for<...>` binders, they are inserted in `binders_to_ignore` so we know *not*
binders_to_ignore: FxHashSet<NodeId>,
}
-pub trait ResolverAstLowering {
- fn def_key(&self, id: DefId) -> DefKey;
-
- fn def_span(&self, id: LocalDefId) -> Span;
-
- fn item_generics_num_lifetimes(&self, def: DefId) -> usize;
-
- fn legacy_const_generic_args(&mut self, expr: &Expr) -> Option<Vec<usize>>;
-
- /// Obtains resolution for a `NodeId` with a single resolution.
+trait ResolverAstLoweringExt {
+ fn legacy_const_generic_args(&self, expr: &Expr) -> Option<Vec<usize>>;
fn get_partial_res(&self, id: NodeId) -> Option<PartialRes>;
-
- /// Obtains per-namespace resolutions for `use` statement with the given `NodeId`.
fn get_import_res(&self, id: NodeId) -> PerNS<Option<Res<NodeId>>>;
-
- /// Obtains resolution for a label with the given `NodeId`.
fn get_label_res(&self, id: NodeId) -> Option<NodeId>;
-
- /// Obtains resolution for a lifetime with the given `NodeId`.
fn get_lifetime_res(&self, id: NodeId) -> Option<LifetimeRes>;
-
- /// Obtain the list of lifetimes parameters to add to an item.
fn take_extra_lifetime_params(&mut self, id: NodeId) -> Vec<(Ident, NodeId, LifetimeRes)>;
+ fn decl_macro_kind(&self, def_id: LocalDefId) -> MacroKind;
+}
- fn create_stable_hashing_context(&self) -> StableHashingContext<'_>;
+impl ResolverAstLoweringExt for ResolverAstLowering {
+ fn legacy_const_generic_args(&self, expr: &Expr) -> Option<Vec<usize>> {
+ if let ExprKind::Path(None, path) = &expr.kind {
+ // Don't perform legacy const generics rewriting if the path already
+ // has generic arguments.
+ if path.segments.last().unwrap().args.is_some() {
+ return None;
+ }
- fn definitions(&self) -> &Definitions;
+ let partial_res = self.partial_res_map.get(&expr.id)?;
+ if partial_res.unresolved_segments() != 0 {
+ return None;
+ }
- fn next_node_id(&mut self) -> NodeId;
+ if let Res::Def(DefKind::Fn, def_id) = partial_res.base_res() {
+ // We only support cross-crate argument rewriting. Uses
+ // within the same crate should be updated to use the new
+ // const generics style.
+ if def_id.is_local() {
+ return None;
+ }
- fn take_trait_map(&mut self, node: NodeId) -> Option<Vec<hir::TraitCandidate>>;
+ if let Some(v) = self.legacy_const_generic_args.get(&def_id) {
+ return v.clone();
+ }
+ }
+ }
- fn opt_local_def_id(&self, node: NodeId) -> Option<LocalDefId>;
+ None
+ }
- fn local_def_id(&self, node: NodeId) -> LocalDefId;
+ /// Obtains resolution for a `NodeId` with a single resolution.
+ fn get_partial_res(&self, id: NodeId) -> Option<PartialRes> {
+ self.partial_res_map.get(&id).copied()
+ }
- fn def_path_hash(&self, def_id: DefId) -> DefPathHash;
+ /// Obtains per-namespace resolutions for `use` statement with the given `NodeId`.
+ fn get_import_res(&self, id: NodeId) -> PerNS<Option<Res<NodeId>>> {
+ self.import_res_map.get(&id).copied().unwrap_or_default()
+ }
- fn create_def(
- &mut self,
- parent: LocalDefId,
- node_id: ast::NodeId,
- data: DefPathData,
- expn_id: ExpnId,
- span: Span,
- ) -> LocalDefId;
+ /// Obtains resolution for a label with the given `NodeId`.
+ fn get_label_res(&self, id: NodeId) -> Option<NodeId> {
+ self.label_res_map.get(&id).copied()
+ }
- fn decl_macro_kind(&self, def_id: LocalDefId) -> MacroKind;
+ /// Obtains resolution for a lifetime with the given `NodeId`.
+ fn get_lifetime_res(&self, id: NodeId) -> Option<LifetimeRes> {
+ self.lifetimes_res_map.get(&id).copied()
+ }
+
+ /// Obtain the list of lifetimes parameters to add to an item.
+ fn take_extra_lifetime_params(&mut self, id: NodeId) -> Vec<(Ident, NodeId, LifetimeRes)> {
+ self.extra_lifetime_params_map.remove(&id).unwrap_or_default()
+ }
+
+ 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,
}
fn index_crate<'a>(
- resolver: &dyn ResolverAstLowering,
+ node_id_to_def_id: &FxHashMap<NodeId, LocalDefId>,
krate: &'a Crate,
) -> IndexVec<LocalDefId, AstOwner<'a>> {
- let mut indexer = Indexer { resolver, index: IndexVec::new() };
+ let mut indexer = Indexer { node_id_to_def_id, index: IndexVec::new() };
indexer.index.ensure_contains_elem(CRATE_DEF_ID, || AstOwner::NonOwner);
indexer.index[CRATE_DEF_ID] = AstOwner::Crate(krate);
visit::walk_crate(&mut indexer, krate);
return indexer.index;
struct Indexer<'s, 'a> {
- resolver: &'s dyn ResolverAstLowering,
+ node_id_to_def_id: &'s FxHashMap<NodeId, LocalDefId>,
index: IndexVec<LocalDefId, AstOwner<'a>>,
}
}
fn visit_item(&mut self, item: &'a ast::Item) {
- let def_id = self.resolver.local_def_id(item.id);
+ let def_id = self.node_id_to_def_id[&item.id];
self.index.ensure_contains_elem(def_id, || AstOwner::NonOwner);
self.index[def_id] = AstOwner::Item(item);
visit::walk_item(self, item)
}
fn visit_assoc_item(&mut self, item: &'a ast::AssocItem, ctxt: visit::AssocCtxt) {
- let def_id = self.resolver.local_def_id(item.id);
+ let def_id = self.node_id_to_def_id[&item.id];
self.index.ensure_contains_elem(def_id, || AstOwner::NonOwner);
self.index[def_id] = AstOwner::AssocItem(item, ctxt);
visit::walk_assoc_item(self, item, ctxt);
}
fn visit_foreign_item(&mut self, item: &'a ast::ForeignItem) {
- let def_id = self.resolver.local_def_id(item.id);
+ let def_id = self.node_id_to_def_id[&item.id];
self.index.ensure_contains_elem(def_id, || AstOwner::NonOwner);
self.index[def_id] = AstOwner::ForeignItem(item);
visit::walk_foreign_item(self, item);
/// Compute the hash for the HIR of the full crate.
/// This hash will then be part of the crate_hash which is stored in the metadata.
fn compute_hir_hash(
- resolver: &mut dyn ResolverAstLowering,
+ sess: &Session,
+ definitions: &Definitions,
+ cstore: &CrateStoreDyn,
+ resolver: &ResolverOutputs,
owners: &IndexVec<LocalDefId, hir::MaybeOwner<&hir::OwnerInfo<'_>>>,
) -> Fingerprint {
let mut hir_body_nodes: Vec<_> = owners
.iter_enumerated()
.filter_map(|(def_id, info)| {
let info = info.as_owner()?;
- let def_path_hash = resolver.definitions().def_path_hash(def_id);
+ let def_path_hash = definitions.def_path_hash(def_id);
Some((def_path_hash, info))
})
.collect();
hir_body_nodes.sort_unstable_by_key(|bn| bn.0);
let mut stable_hasher = StableHasher::new();
- let mut hcx = resolver.create_stable_hashing_context();
+ let mut hcx = StableHashingContext::new(sess, definitions, cstore, &resolver.source_span);
hir_body_nodes.hash_stable(&mut hcx, &mut stable_hasher);
stable_hasher.finish()
}
-pub fn lower_crate<'a, 'hir>(
- sess: &'a Session,
- krate: &'a Crate,
- resolver: &'a mut dyn ResolverAstLowering,
+pub fn lower_crate<'hir>(
+ sess: &Session,
+ krate: &Crate,
+ definitions: &mut Definitions,
+ cstore: &CrateStoreDyn,
+ resolutions: &ResolverOutputs,
+ mut resolver: ResolverAstLowering,
arena: &'hir Arena<'hir>,
) -> &'hir hir::Crate<'hir> {
let _prof_timer = sess.prof.verbose_generic_activity("hir_lowering");
- let ast_index = index_crate(resolver, krate);
+ let ast_index = index_crate(&resolver.node_id_to_def_id, krate);
let mut owners =
- IndexVec::from_fn_n(|_| hir::MaybeOwner::Phantom, resolver.definitions().def_index_count());
+ IndexVec::from_fn_n(|_| hir::MaybeOwner::Phantom, definitions.def_index_count());
for def_id in ast_index.indices() {
- item::ItemLowerer { sess, resolver, arena, ast_index: &ast_index, owners: &mut owners }
- .lower_node(def_id);
+ item::ItemLowerer {
+ sess,
+ definitions,
+ cstore,
+ resolutions,
+ resolver: &mut resolver,
+ arena,
+ ast_index: &ast_index,
+ owners: &mut owners,
+ }
+ .lower_node(def_id);
}
- let hir_hash = compute_hir_hash(resolver, &owners);
+ let hir_hash = compute_hir_hash(sess, definitions, cstore, resolutions, &owners);
let krate = hir::Crate { owners, hir_hash };
arena.alloc(krate)
}
}
impl<'a, 'hir> LoweringContext<'a, 'hir> {
+ fn create_stable_hashing_context(&self) -> StableHashingContext<'_> {
+ StableHashingContext::new(
+ self.sess,
+ self.definitions,
+ self.cstore,
+ &self.resolutions.source_span,
+ )
+ }
+
+ fn create_def(
+ &mut self,
+ parent: LocalDefId,
+ node_id: ast::NodeId,
+ data: DefPathData,
+ ) -> LocalDefId {
+ assert!(
+ self.opt_local_def_id(node_id).is_none(),
+ "adding a def'n for node-id {:?} and data {:?} but a previous def'n exists: {:?}",
+ node_id,
+ data,
+ self.definitions.def_key(self.local_def_id(node_id)),
+ );
+
+ let def_id = self.definitions.create_def(parent, data);
+
+ // Some things for which we allocate `LocalDefId`s don't correspond to
+ // anything in the AST, so they don't have a `NodeId`. For these cases
+ // we don't need a mapping from `NodeId` to `LocalDefId`.
+ if node_id != ast::DUMMY_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);
+ }
+
+ def_id
+ }
+
+ fn next_node_id(&mut self) -> NodeId {
+ let start = self.resolver.next_node_id;
+ let next = start.as_u32().checked_add(1).expect("input too large; ran out of NodeIds");
+ self.resolver.next_node_id = ast::NodeId::from_u32(next);
+ start
+ }
+
+ fn opt_local_def_id(&self, node: NodeId) -> Option<LocalDefId> {
+ self.resolver.node_id_to_def_id.get(&node).copied()
+ }
+
+ fn local_def_id(&self, node: NodeId) -> LocalDefId {
+ self.opt_local_def_id(node).unwrap_or_else(|| panic!("no entry for node id: `{:?}`", node))
+ }
+
#[instrument(level = "debug", skip(self, f))]
fn with_hir_id_owner(
&mut self,
owner: NodeId,
f: impl FnOnce(&mut Self) -> hir::OwnerNode<'hir>,
) {
- let def_id = self.resolver.local_def_id(owner);
+ let def_id = self.local_def_id(owner);
let current_attrs = std::mem::take(&mut self.attrs);
let current_bodies = std::mem::take(&mut self.bodies);
std::mem::replace(&mut self.item_local_id_counter, hir::ItemLocalId::new(1));
let current_impl_trait_defs = std::mem::take(&mut self.impl_trait_defs);
let current_impl_trait_bounds = std::mem::take(&mut self.impl_trait_bounds);
+ // Do not reset `next_node_id` and `node_id_to_def_id` as we want to refer to the
+ // subdefinitions' nodes.
// Always allocate the first `HirId` for the owner itself.
let _old = self.node_id_to_local_id.insert(owner, hir::ItemLocalId::new(0));
bodies.sort_by_key(|(k, _)| *k);
let bodies = SortedMap::from_presorted_elements(bodies);
let (hash_including_bodies, hash_without_bodies) = self.hash_owner(node, &bodies);
- let (nodes, parenting) =
- index::index_hir(self.sess, self.resolver.definitions(), node, &bodies);
+ let (nodes, parenting) = index::index_hir(self.sess, self.definitions, node, &bodies);
let nodes = hir::OwnerNodes {
hash_including_bodies,
hash_without_bodies,
local_id_to_def_id,
};
let attrs = {
- let mut hcx = self.resolver.create_stable_hashing_context();
+ let mut hcx = self.create_stable_hashing_context();
let mut stable_hasher = StableHasher::new();
attrs.hash_stable(&mut hcx, &mut stable_hasher);
let hash = stable_hasher.finish();
node: hir::OwnerNode<'hir>,
bodies: &SortedMap<hir::ItemLocalId, &'hir hir::Body<'hir>>,
) -> (Fingerprint, Fingerprint) {
- let mut hcx = self.resolver.create_stable_hashing_context();
+ let mut hcx = self.create_stable_hashing_context();
let mut stable_hasher = StableHasher::new();
hcx.with_hir_bodies(true, node.def_id(), bodies, |hcx| {
node.hash_stable(hcx, &mut stable_hasher)
self.item_local_id_counter.increment_by(1);
assert_ne!(local_id, hir::ItemLocalId::new(0));
- if let Some(def_id) = self.resolver.opt_local_def_id(ast_node_id) {
+ if let Some(def_id) = self.opt_local_def_id(ast_node_id) {
// Do not override a `MaybeOwner::Owner` that may already here.
self.children.entry(def_id).or_insert(hir::MaybeOwner::NonOwner(hir_id));
self.local_id_to_def_id.insert(local_id, def_id);
}
- if let Some(traits) = self.resolver.take_trait_map(ast_node_id) {
+ if let Some(traits) = self.resolver.trait_map.remove(&ast_node_id) {
self.trait_map.insert(hir_id.local_id, traits.into_boxed_slice());
}
}
fn next_id(&mut self) -> hir::HirId {
- let node_id = self.resolver.next_node_id();
+ let node_id = self.next_node_id();
self.lower_node_id(node_id)
}
allow_internal_unstable,
reason,
self.sess.edition(),
- self.resolver.create_stable_hashing_context(),
+ self.create_stable_hashing_context(),
)
}
// constructing the HIR for `impl bounds...` and then lowering that.
let parent_def_id = self.current_hir_id_owner;
- let impl_trait_node_id = self.resolver.next_node_id();
- self.resolver.create_def(
- parent_def_id,
- impl_trait_node_id,
- DefPathData::ImplTrait,
- ExpnId::root(),
- constraint.span,
- );
+ let impl_trait_node_id = self.next_node_id();
+ self.create_def(parent_def_id, impl_trait_node_id, DefPathData::ImplTrait);
self.with_dyn_type_scope(false, |this| {
- let node_id = this.resolver.next_node_id();
+ let node_id = this.next_node_id();
let ty = this.lower_ty(
&Ty {
id: node_id,
// Construct an AnonConst where the expr is the "ty"'s path.
let parent_def_id = self.current_hir_id_owner;
- let node_id = self.resolver.next_node_id();
+ let node_id = self.next_node_id();
// Add a definition for the in-band const def.
- self.resolver.create_def(
- parent_def_id,
- node_id,
- DefPathData::AnonConst,
- ExpnId::root(),
- ty.span,
- );
+ self.create_def(parent_def_id, node_id, DefPathData::AnonConst);
let span = self.lower_span(ty.span);
let path_expr = Expr {
debug_assert_eq!(start.plus(1), end);
start
} else {
- self.resolver.next_node_id()
+ self.next_node_id()
};
let span = self.sess.source_map().next_point(t.span.shrink_to_lo());
Lifetime { ident: Ident::new(kw::UnderscoreLifetime, span), id }
// frequently opened issues show.
let opaque_ty_span = self.mark_span_with_reason(DesugaringKind::OpaqueTy, span, None);
- let opaque_ty_def_id = self.resolver.local_def_id(opaque_ty_node_id);
+ let opaque_ty_def_id = self.local_def_id(opaque_ty_node_id);
let mut collected_lifetimes = FxHashMap::default();
self.with_hir_id_owner(opaque_ty_node_id, |lctx| {
let lifetime_defs = lctx.arena.alloc_from_iter(collected_lifetimes.iter().map(
|(_, &(span, p_id, p_name, _))| {
let hir_id = lctx.lower_node_id(p_id);
- debug_assert_ne!(lctx.resolver.opt_local_def_id(p_id), None);
+ debug_assert_ne!(lctx.opt_local_def_id(p_id), None);
let kind = if p_name.ident().name == kw::UnderscoreLifetime {
hir::LifetimeParamKind::Elided
let lifetimes = self.arena.alloc_from_iter(collected_lifetimes.into_iter().map(
|(_, (span, _, p_name, res))| {
- let id = self.resolver.next_node_id();
+ let id = self.next_node_id();
let ident = Ident::new(p_name.ident().name, span);
let l = self.new_named_lifetime_with_res(id, span, ident, res);
hir::GenericArg::Lifetime(l)
FnRetTy::Ty(ref ty) => {
let context = match fn_node_id {
Some(fn_node_id) if kind.impl_trait_return_allowed() => {
- let fn_def_id = self.resolver.local_def_id(fn_node_id);
+ let fn_def_id = self.local_def_id(fn_node_id);
ImplTraitContext::ReturnPositionOpaqueTy {
origin: hir::OpaqueTyOrigin::FnReturn(fn_def_id),
}
let opaque_ty_span = self.mark_span_with_reason(DesugaringKind::Async, span, None);
- let opaque_ty_def_id = self.resolver.local_def_id(opaque_ty_node_id);
- let fn_def_id = self.resolver.local_def_id(fn_node_id);
+ let opaque_ty_def_id = self.local_def_id(opaque_ty_node_id);
+ let fn_def_id = self.local_def_id(fn_node_id);
// When we create the opaque type for this async fn, it is going to have
// to capture all the lifetimes involved in the signature (including in the
debug!(?extra_lifetime_params);
for (ident, outer_node_id, outer_res) in extra_lifetime_params {
let Ident { name, span } = ident;
- let outer_def_id = self.resolver.local_def_id(outer_node_id);
- let inner_node_id = self.resolver.next_node_id();
+ let outer_def_id = self.local_def_id(outer_node_id);
+ let inner_node_id = self.next_node_id();
// Add a definition for the in scope lifetime def.
- self.resolver.create_def(
- opaque_ty_def_id,
- inner_node_id,
- DefPathData::LifetimeNs(name),
- ExpnId::root(),
- span.with_parent(None),
- );
+ self.create_def(opaque_ty_def_id, inner_node_id, DefPathData::LifetimeNs(name));
let (p_name, inner_res) = match outer_res {
// Input lifetime like `'a`:
let generic_params =
this.arena.alloc_from_iter(captures.iter().map(|(_, &(span, p_id, p_name, _))| {
let hir_id = this.lower_node_id(p_id);
- debug_assert_ne!(this.resolver.opt_local_def_id(p_id), None);
+ debug_assert_ne!(this.opt_local_def_id(p_id), None);
let kind = if p_name.ident().name == kw::UnderscoreLifetime {
hir::LifetimeParamKind::Elided
// generate `'_`.
let generic_args =
self.arena.alloc_from_iter(captures.into_iter().map(|(_, (span, _, p_name, res))| {
- let id = self.resolver.next_node_id();
+ let id = self.next_node_id();
let ident = Ident::new(p_name.ident().name, span);
let l = self.new_named_lifetime_with_res(id, span, ident, res);
hir::GenericArg::Lifetime(l)
LifetimeRes::Param { mut param, binder } => {
debug_assert_ne!(ident.name, kw::UnderscoreLifetime);
let p_name = ParamName::Plain(ident);
- if let Some(LifetimeCaptureContext { parent_def_id, captures, binders_to_ignore }) =
- &mut self.captured_lifetimes
- && !binders_to_ignore.contains(&binder)
- {
- match captures.entry(param) {
- Entry::Occupied(o) => param = self.resolver.local_def_id(o.get().1),
- Entry::Vacant(v) => {
- let p_id = self.resolver.next_node_id();
- let p_def_id = self.resolver.create_def(
- *parent_def_id,
- p_id,
- DefPathData::LifetimeNs(p_name.ident().name),
- ExpnId::root(),
- span.with_parent(None),
- );
-
- v.insert((span, p_id, p_name, res));
- param = p_def_id;
+ if let Some(mut captured_lifetimes) = self.captured_lifetimes.take() {
+ if !captured_lifetimes.binders_to_ignore.contains(&binder) {
+ match captured_lifetimes.captures.entry(param) {
+ Entry::Occupied(o) => param = self.local_def_id(o.get().1),
+ Entry::Vacant(v) => {
+ let p_id = self.next_node_id();
+ let p_def_id = self.create_def(
+ captured_lifetimes.parent_def_id,
+ p_id,
+ DefPathData::LifetimeNs(p_name.ident().name),
+ );
+
+ v.insert((span, p_id, p_name, res));
+ param = p_def_id;
+ }
}
}
+
+ self.captured_lifetimes = Some(captured_lifetimes);
}
hir::LifetimeName::Param(param, p_name)
}
LifetimeRes::Fresh { mut param, binder } => {
debug_assert_eq!(ident.name, kw::UnderscoreLifetime);
- if let Some(LifetimeCaptureContext { parent_def_id, captures, binders_to_ignore }) =
- &mut self.captured_lifetimes
- && !binders_to_ignore.contains(&binder)
- {
- match captures.entry(param) {
- Entry::Occupied(o) => param = self.resolver.local_def_id(o.get().1),
- Entry::Vacant(v) => {
- let p_id = self.resolver.next_node_id();
- let p_def_id = self.resolver.create_def(
- *parent_def_id,
- p_id,
- DefPathData::LifetimeNs(kw::UnderscoreLifetime),
- ExpnId::root(),
- span.with_parent(None),
- );
-
- v.insert((span, p_id, ParamName::Fresh, res));
- param = p_def_id;
+ if let Some(mut captured_lifetimes) = self.captured_lifetimes.take() {
+ if !captured_lifetimes.binders_to_ignore.contains(&binder) {
+ match captured_lifetimes.captures.entry(param) {
+ Entry::Occupied(o) => param = self.local_def_id(o.get().1),
+ Entry::Vacant(v) => {
+ let p_id = self.next_node_id();
+ let p_def_id = self.create_def(
+ captured_lifetimes.parent_def_id,
+ p_id,
+ DefPathData::LifetimeNs(kw::UnderscoreLifetime),
+ );
+
+ v.insert((span, p_id, ParamName::Fresh, res));
+ param = p_def_id;
+ }
}
}
+
+ self.captured_lifetimes = Some(captured_lifetimes);
}
hir::LifetimeName::Param(param, ParamName::Fresh)
}
LifetimeRes::Anonymous { binder, elided } => {
- if let Some(LifetimeCaptureContext { parent_def_id, captures, binders_to_ignore }) =
- &mut self.captured_lifetimes
- && !binders_to_ignore.contains(&binder)
- {
- let p_id = self.resolver.next_node_id();
- let p_def_id = self.resolver.create_def(
- *parent_def_id,
- p_id,
- DefPathData::LifetimeNs(kw::UnderscoreLifetime),
- ExpnId::root(),
- span.with_parent(None),
- );
- captures.insert(p_def_id, (span, p_id, ParamName::Fresh, res));
- hir::LifetimeName::Param(p_def_id, ParamName::Fresh)
- } else if elided {
+ let mut l_name = None;
+ if let Some(mut captured_lifetimes) = self.captured_lifetimes.take() {
+ if !captured_lifetimes.binders_to_ignore.contains(&binder) {
+ let p_id = self.next_node_id();
+ let p_def_id = self.create_def(
+ captured_lifetimes.parent_def_id,
+ p_id,
+ DefPathData::LifetimeNs(kw::UnderscoreLifetime),
+ );
+ captured_lifetimes
+ .captures
+ .insert(p_def_id, (span, p_id, ParamName::Fresh, res));
+ l_name = Some(hir::LifetimeName::Param(p_def_id, ParamName::Fresh));
+ }
+ self.captured_lifetimes = Some(captured_lifetimes);
+ };
+ l_name.unwrap_or(if elided {
hir::LifetimeName::Implicit
} else {
hir::LifetimeName::Underscore
- }
+ })
}
LifetimeRes::Static => hir::LifetimeName::Static,
LifetimeRes::Error => hir::LifetimeName::Error,
bounds: &[GenericBound],
) -> (hir::GenericParam<'hir>, Option<hir::WherePredicate<'hir>>, hir::TyKind<'hir>) {
// Add a definition for the in-band `Param`.
- let def_id = self.resolver.local_def_id(node_id);
+ let def_id = self.local_def_id(node_id);
let hir_bounds = self.lower_param_bounds(bounds, ImplTraitContext::Universal);
// Set the name to `impl Bound1 + Bound2`.
-use crate::ImplTraitPosition;
-
+use super::ResolverAstLoweringExt;
use super::{ImplTraitContext, LoweringContext, ParamMode};
+use crate::ImplTraitPosition;
use rustc_ast::ptr::P;
use rustc_ast::*;
use crate::ImplTraitPosition;
+use super::ResolverAstLoweringExt;
use super::{GenericArgsCtor, LifetimeRes, ParenthesizedGenericArgs};
use super::{ImplTraitContext, LoweringContext, ParamMode};
-use crate::def_id::DefId;
use crate::hir;
use rustc_ast as ast;
use rustc_ast::NodeId;
use rustc_macros::HashStable_Generic;
+use rustc_span::def_id::{DefId, LocalDefId};
use rustc_span::hygiene::MacroKind;
use rustc_span::Symbol;
matches!(self, Res::Def(DefKind::Ctor(_, CtorKind::Const), _) | Res::SelfCtor(..))
}
}
+
+/// Resolution for a lifetime appearing in a type.
+#[derive(Copy, Clone, Debug)]
+pub enum LifetimeRes {
+ /// Successfully linked the lifetime to a generic parameter.
+ Param {
+ /// Id of the generic parameter that introduced it.
+ param: LocalDefId,
+ /// Id of the introducing place. That can be:
+ /// - an item's id, for the item's generic parameters;
+ /// - a TraitRef's ref_id, identifying the `for<...>` binder;
+ /// - a BareFn type's id;
+ /// - a Path's id when this path has parenthesized generic args.
+ ///
+ /// This information is used for impl-trait lifetime captures, to know when to or not to
+ /// capture any given lifetime.
+ binder: NodeId,
+ },
+ /// Created a generic parameter for an anonymous lifetime.
+ Fresh {
+ /// Id of the generic parameter that introduced it.
+ param: LocalDefId,
+ /// Id of the introducing place. See `Param`.
+ binder: NodeId,
+ },
+ /// This variant is used for anonymous lifetimes that we did not resolve during
+ /// late resolution. Shifting the work to the HIR lifetime resolver.
+ Anonymous {
+ /// Id of the introducing place. See `Param`.
+ binder: NodeId,
+ /// Whether this lifetime was spelled or elided.
+ elided: bool,
+ },
+ /// Explicit `'static` lifetime.
+ Static,
+ /// Resolution failure.
+ Error,
+ /// HACK: This is used to recover the NodeId of an elided lifetime.
+ ElidedAnchor { start: NodeId, end: NodeId },
+}
use rustc_data_structures::fx::FxHashMap;
use rustc_data_structures::stable_hasher::StableHasher;
use rustc_index::vec::IndexVec;
-use rustc_span::hygiene::ExpnId;
use rustc_span::symbol::{kw, sym, Symbol};
-use rustc_span::Span;
use std::fmt::{self, Write};
use std::hash::Hash;
table: DefPathTable,
next_disambiguator: FxHashMap<(LocalDefId, DefPathData), u32>,
- /// Item with a given `LocalDefId` was defined during macro expansion with ID `ExpnId`.
- expansions_that_defined: FxHashMap<LocalDefId, ExpnId>,
-
- def_id_to_span: IndexVec<LocalDefId, Span>,
-
/// The [StableCrateId] of the local crate.
stable_crate_id: StableCrateId,
}
}
/// Adds a root definition (no parent) and a few other reserved definitions.
- pub fn new(stable_crate_id: StableCrateId, crate_span: Span) -> Definitions {
+ pub fn new(stable_crate_id: StableCrateId) -> Definitions {
let key = DefKey {
parent: None,
disambiguated_data: DisambiguatedDefPathData {
let root = LocalDefId { local_def_index: table.allocate(key, def_path_hash) };
assert_eq!(root.local_def_index, CRATE_DEF_INDEX);
- let mut def_id_to_span = IndexVec::new();
- // A relative span's parent must be an absolute span.
- debug_assert_eq!(crate_span.data_untracked().parent, None);
- let _root = def_id_to_span.push(crate_span);
- debug_assert_eq!(_root, root);
-
- Definitions {
- table,
- next_disambiguator: Default::default(),
- expansions_that_defined: Default::default(),
- def_id_to_span,
- stable_crate_id,
- }
+ Definitions { table, next_disambiguator: Default::default(), stable_crate_id }
}
/// Adds a definition with a parent definition.
- pub fn create_def(
- &mut self,
- parent: LocalDefId,
- data: DefPathData,
- expn_id: ExpnId,
- span: Span,
- ) -> LocalDefId {
- debug!("create_def(parent={:?}, data={:?}, expn_id={:?})", parent, data, expn_id);
+ pub fn create_def(&mut self, parent: LocalDefId, data: DefPathData) -> LocalDefId {
+ debug!("create_def(parent={:?}, data={:?})", parent, data);
// The root node must be created with `create_root_def()`.
assert!(data != DefPathData::CrateRoot);
debug!("create_def: after disambiguation, key = {:?}", key);
// Create the definition.
- let def_id = LocalDefId { local_def_index: self.table.allocate(key, def_path_hash) };
-
- if expn_id != ExpnId::root() {
- self.expansions_that_defined.insert(def_id, expn_id);
- }
-
- // A relative span's parent must be an absolute span.
- debug_assert_eq!(span.data_untracked().parent, None);
- let _id = self.def_id_to_span.push(span);
- debug_assert_eq!(_id, def_id);
-
- def_id
- }
-
- pub fn expansion_that_defined(&self, id: LocalDefId) -> ExpnId {
- self.expansions_that_defined.get(&id).copied().unwrap_or_else(ExpnId::root)
- }
-
- /// Retrieves the span of the given `DefId` if `DefId` is in the local crate.
- #[inline]
- pub fn def_span(&self, def_id: LocalDefId) -> Span {
- self.def_id_to_span[def_id]
+ LocalDefId { local_def_index: self.table.allocate(key, def_path_hash) }
}
pub fn iter_local_def_id(&self) -> impl Iterator<Item = LocalDefId> + '_ {
use rustc_errors::{Applicability, ErrorGuaranteed, MultiSpan, PResult};
use rustc_expand::base::{ExtCtxt, LintStoreExpand, ResolverExpand};
use rustc_hir::def_id::{StableCrateId, LOCAL_CRATE};
+use rustc_hir::definitions::Definitions;
use rustc_hir::Crate;
use rustc_lint::{EarlyCheckNode, LintStore};
use rustc_metadata::creader::CStore;
use rustc_middle::arena::Arena;
use rustc_middle::dep_graph::DepGraph;
use rustc_middle::ty::query::{ExternProviders, Providers};
-use rustc_middle::ty::{self, GlobalCtxt, RegisteredTools, ResolverOutputs, TyCtxt};
+use rustc_middle::ty::{self, GlobalCtxt, RegisteredTools, TyCtxt};
use rustc_mir_build as mir_build;
use rustc_parse::{parse_crate_from_file, parse_crate_from_source_str, validate_attr};
use rustc_passes::{self, hir_stats, layout_test};
use rustc_query_impl::{OnDiskCache, Queries as TcxQueries};
use rustc_resolve::{Resolver, ResolverArenas};
use rustc_session::config::{CrateType, Input, OutputFilenames, OutputType};
-use rustc_session::cstore::{MetadataLoader, MetadataLoaderDyn};
+use rustc_session::cstore::{CrateStoreDyn, MetadataLoader, MetadataLoaderDyn};
use rustc_session::output::{filename_for_input, filename_for_metadata};
use rustc_session::search_paths::PathKind;
use rustc_session::{Limit, Session};
f((&mut *resolver).as_mut().unwrap())
}
- pub fn to_resolver_outputs(resolver: Rc<RefCell<BoxedResolver>>) -> ResolverOutputs {
+ pub fn to_resolver_outputs(
+ resolver: Rc<RefCell<BoxedResolver>>,
+ ) -> (Definitions, Box<CrateStoreDyn>, ty::ResolverOutputs, ty::ResolverAstLowering)
+ {
match Rc::try_unwrap(resolver) {
Ok(resolver) => {
let mut resolver = resolver.into_inner();
Ok(krate)
}
-pub fn lower_to_hir<'res, 'tcx>(
- sess: &'tcx Session,
- resolver: &'res mut Resolver<'_>,
+fn lower_to_hir<'tcx>(
+ sess: &Session,
+ definitions: &mut Definitions,
+ cstore: &CrateStoreDyn,
+ resolutions: &ty::ResolverOutputs,
+ resolver: ty::ResolverAstLowering,
krate: Rc<ast::Crate>,
arena: &'tcx rustc_ast_lowering::Arena<'tcx>,
) -> &'tcx Crate<'tcx> {
// Lower AST to HIR.
- let hir_crate = rustc_ast_lowering::lower_crate(sess, &*krate, resolver, arena);
+ let hir_crate = rustc_ast_lowering::lower_crate(
+ sess,
+ &krate,
+ definitions,
+ cstore,
+ resolutions,
+ resolver,
+ arena,
+ );
// Drop AST to free memory
sess.time("drop_ast", || std::mem::drop(krate));
// incr. comp. yet.
dep_graph.assert_ignored();
+ let (mut definitions, cstore, resolver_outputs, resolver_for_lowering) =
+ BoxedResolver::to_resolver_outputs(resolver);
+
let sess = &compiler.session();
- let krate =
- resolver.borrow_mut().access(|resolver| lower_to_hir(sess, resolver, krate, hir_arena));
- let resolver_outputs = BoxedResolver::to_resolver_outputs(resolver);
+
+ // Lower AST to HIR.
+ let krate = lower_to_hir(
+ sess,
+ &mut definitions,
+ &*cstore,
+ &resolver_outputs,
+ resolver_for_lowering,
+ krate,
+ hir_arena,
+ );
let query_result_on_disk_cache = rustc_incremental::load_query_result_cache(sess);
sess,
lint_store,
arena,
+ definitions,
+ cstore,
resolver_outputs,
krate,
dep_graph,
}
fn encode_def_path_table(&mut self) {
- let table = self.tcx.resolutions(()).definitions.def_path_table();
+ let table = self.tcx.definitions_untracked().def_path_table();
if self.is_proc_macro {
for def_index in std::iter::once(CRATE_DEF_INDEX)
.chain(self.tcx.resolutions(()).proc_macros.iter().map(|p| p.local_def_index))
fn encode_def_path_hash_map(&mut self) -> LazyValue<DefPathHashMapRef<'static>> {
self.lazy(DefPathHashMapRef::BorrowedFromTcx(
- self.tcx.resolutions(()).definitions.def_path_hash_to_def_index_map(),
+ self.tcx.definitions_untracked().def_path_hash_to_def_index_map(),
))
}
pub fn def_key(self, def_id: LocalDefId) -> DefKey {
// Accessing the DefKey is ok, since it is part of DefPathHash.
- self.tcx.untracked_resolutions.definitions.def_key(def_id)
+ self.tcx.definitions_untracked().def_key(def_id)
}
pub fn def_path_from_hir_id(self, id: HirId) -> Option<DefPath> {
pub fn def_path(self, def_id: LocalDefId) -> DefPath {
// Accessing the DefPath is ok, since it is part of DefPathHash.
- self.tcx.untracked_resolutions.definitions.def_path(def_id)
+ self.tcx.definitions_untracked().def_path(def_id)
}
#[inline]
pub fn def_path_hash(self, def_id: LocalDefId) -> DefPathHash {
// Accessing the DefPathHash is ok, it is incr. comp. stable.
- self.tcx.untracked_resolutions.definitions.def_path_hash(def_id)
+ self.tcx.definitions_untracked().def_path_hash(def_id)
}
#[inline]
// Create a dependency to the crate to be sure we re-execute this when the amount of
// definitions change.
self.tcx.ensure().hir_crate(());
- self.tcx.untracked_resolutions.definitions.iter_local_def_id()
+ self.tcx.definitions_untracked().iter_local_def_id()
}
pub fn opt_def_kind(self, local_def_id: LocalDefId) -> Option<DefKind> {
let upstream_crates = upstream_crates(tcx);
+ let resolutions = tcx.resolutions(());
+
// We hash the final, remapped names of all local source files so we
// don't have to include the path prefix remapping commandline args.
// If we included the full mapping in the SVH, we could only have
upstream_crates.hash_stable(&mut hcx, &mut stable_hasher);
source_file_names.hash_stable(&mut hcx, &mut stable_hasher);
if tcx.sess.opts.debugging_opts.incremental_relative_spans {
- let definitions = &tcx.untracked_resolutions.definitions;
+ let definitions = &tcx.definitions_untracked();
let mut owner_spans: Vec<_> = krate
.owners
.iter_enumerated()
.filter_map(|(def_id, info)| {
let _ = info.as_owner()?;
let def_path_hash = definitions.def_path_hash(def_id);
- let span = definitions.def_span(def_id);
+ let span = resolutions.source_span[def_id];
debug_assert_eq!(span.parent(), None);
Some((def_path_hash, span))
})
tcx.sess.opts.dep_tracking_hash(true).hash_stable(&mut hcx, &mut stable_hasher);
tcx.sess.local_stable_crate_id().hash_stable(&mut hcx, &mut stable_hasher);
// Hash visibility information since it does not appear in HIR.
- let resolutions = tcx.resolutions(());
resolutions.visibilities.hash_stable(&mut hcx, &mut stable_hasher);
resolutions.has_pub_restricted.hash_stable(&mut hcx, &mut stable_hasher);
.crates(())
.iter()
.map(|&cnum| {
- let stable_crate_id = tcx.resolutions(()).cstore.stable_crate_id(cnum);
+ let stable_crate_id = tcx.stable_crate_id(cnum);
let hash = tcx.crate_hash(cnum);
(stable_crate_id, hash)
})
pub mod place;
use crate::ty::query::Providers;
-use crate::ty::{ImplSubject, TyCtxt};
+use crate::ty::{DefIdTree, ImplSubject, TyCtxt};
use rustc_data_structures::fingerprint::Fingerprint;
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
use rustc_hir::def_id::{DefId, LocalDefId};
use rustc_hir::*;
use rustc_query_system::ich::StableHashingContext;
-use rustc_span::DUMMY_SP;
+use rustc_span::{ExpnId, DUMMY_SP};
/// Top-level HIR node for current owner. This only contains the node for which
/// `HirId::local_id == 0`, and excludes bodies.
};
providers.hir_owner_nodes = |tcx, id| tcx.hir_crate(()).owners[id].map(|i| &i.nodes);
providers.hir_owner_parent = |tcx, id| {
- // Accessing the def_key is ok since its value is hashed as part of `id`'s DefPathHash.
- let parent = tcx.untracked_resolutions.definitions.def_key(id).parent;
- let parent = parent.map_or(CRATE_HIR_ID, |local_def_index| {
- let def_id = LocalDefId { local_def_index };
- let mut parent_hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
+ // Accessing the local_parent is ok since its value is hashed as part of `id`'s DefPathHash.
+ tcx.opt_local_parent(id).map_or(CRATE_HIR_ID, |parent| {
+ let mut parent_hir_id = tcx.hir().local_def_id_to_hir_id(parent);
if let Some(local_id) =
tcx.hir_crate(()).owners[parent_hir_id.owner].unwrap().parenting.get(&id)
{
parent_hir_id.local_id = *local_id;
}
parent_hir_id
- });
- parent
+ })
};
providers.hir_attrs =
|tcx, id| tcx.hir_crate(()).owners[id].as_owner().map_or(AttributeMap::EMPTY, |o| &o.attrs);
- providers.source_span = |tcx, def_id| tcx.resolutions(()).definitions.def_span(def_id);
+ providers.source_span =
+ |tcx, def_id| tcx.resolutions(()).source_span.get(def_id).copied().unwrap_or(DUMMY_SP);
providers.def_span = |tcx, def_id| {
let def_id = def_id.expect_local();
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
providers.all_local_trait_impls = |tcx, ()| &tcx.resolutions(()).trait_impls;
providers.expn_that_defined = |tcx, id| {
let id = id.expect_local();
- tcx.resolutions(()).definitions.expansion_that_defined(id)
+ tcx.resolutions(()).expn_that_defined.get(&id).copied().unwrap_or(ExpnId::root())
};
providers.in_scope_traits_map =
|tcx, id| tcx.hir_crate(()).owners[id].as_owner().map(|owner_info| &owner_info.trait_map);
}
query expn_that_defined(key: DefId) -> rustc_span::ExpnId {
- // This query reads from untracked data in definitions.
- eval_always
desc { |tcx| "expansion that defined `{}`", tcx.def_path_str(key) }
separate_provide_extern
}
use rustc_query_system::ich::StableHashingContext;
use rustc_serialize::opaque::{FileEncodeResult, FileEncoder};
use rustc_session::config::{CrateType, OutputFilenames};
+use rustc_session::cstore::CrateStoreDyn;
use rustc_session::lint::{Level, Lint};
use rustc_session::Limit;
use rustc_session::Session;
&self,
kind: TyKind<'tcx>,
sess: &Session,
- resolutions: &ty::ResolverOutputs,
+ definitions: &rustc_hir::definitions::Definitions,
+ cstore: &CrateStoreDyn,
+ source_span: &IndexVec<LocalDefId, Span>,
) -> Ty<'tcx> {
Ty(Interned::new_unchecked(
self.type_
let mut hasher = StableHasher::new();
let mut hcx = StableHashingContext::ignore_spans(
sess,
- &resolutions.definitions,
- &*resolutions.cstore,
+ definitions,
+ cstore,
+ source_span,
);
kind.hash_stable(&mut hcx, &mut hasher);
hasher.finish()
fn new(
interners: &CtxtInterners<'tcx>,
sess: &Session,
- resolutions: &ty::ResolverOutputs,
+ definitions: &rustc_hir::definitions::Definitions,
+ cstore: &CrateStoreDyn,
+ source_span: &IndexVec<LocalDefId, Span>,
) -> CommonTypes<'tcx> {
- let mk = |ty| interners.intern_ty(ty, sess, resolutions);
+ let mk = |ty| interners.intern_ty(ty, sess, definitions, cstore, source_span);
CommonTypes {
unit: mk(Tuple(List::empty())),
/// Common consts, pre-interned for your convenience.
pub consts: CommonConsts<'tcx>,
+ definitions: rustc_hir::definitions::Definitions,
+ cstore: Box<CrateStoreDyn>,
+
/// Output of the resolver.
pub(crate) untracked_resolutions: ty::ResolverOutputs,
s: &'tcx Session,
lint_store: Lrc<dyn Any + sync::Send + sync::Sync>,
arena: &'tcx WorkerLocal<Arena<'tcx>>,
- resolutions: ty::ResolverOutputs,
+ definitions: rustc_hir::definitions::Definitions,
+ cstore: Box<CrateStoreDyn>,
+ untracked_resolutions: ty::ResolverOutputs,
krate: &'tcx hir::Crate<'tcx>,
dep_graph: DepGraph,
on_disk_cache: Option<&'tcx dyn OnDiskCache<'tcx>>,
s.fatal(&err);
});
let interners = CtxtInterners::new(arena);
- let common_types = CommonTypes::new(&interners, s, &resolutions);
+ let common_types = CommonTypes::new(
+ &interners,
+ s,
+ &definitions,
+ &*cstore,
+ // This is only used to create a stable hashing context.
+ &untracked_resolutions.source_span,
+ );
let common_lifetimes = CommonLifetimes::new(&interners);
let common_consts = CommonConsts::new(&interners, &common_types);
arena,
interners,
dep_graph,
- untracked_resolutions: resolutions,
+ definitions,
+ cstore,
+ untracked_resolutions,
prof: s.prof.clone(),
types: common_types,
lifetimes: common_lifetimes,
pub fn def_key(self, id: DefId) -> rustc_hir::definitions::DefKey {
// Accessing the DefKey is ok, since it is part of DefPathHash.
if let Some(id) = id.as_local() {
- self.untracked_resolutions.definitions.def_key(id)
+ self.definitions.def_key(id)
} else {
- self.untracked_resolutions.cstore.def_key(id)
+ self.cstore.def_key(id)
}
}
pub fn def_path(self, id: DefId) -> rustc_hir::definitions::DefPath {
// Accessing the DefPath is ok, since it is part of DefPathHash.
if let Some(id) = id.as_local() {
- self.untracked_resolutions.definitions.def_path(id)
+ self.definitions.def_path(id)
} else {
- self.untracked_resolutions.cstore.def_path(id)
+ self.cstore.def_path(id)
}
}
pub fn def_path_hash(self, def_id: DefId) -> rustc_hir::definitions::DefPathHash {
// Accessing the DefPathHash is ok, it is incr. comp. stable.
if let Some(def_id) = def_id.as_local() {
- self.untracked_resolutions.definitions.def_path_hash(def_id)
+ self.definitions.def_path_hash(def_id)
} else {
- self.untracked_resolutions.cstore.def_path_hash(def_id)
+ self.cstore.def_path_hash(def_id)
}
}
if crate_num == LOCAL_CRATE {
self.sess.local_stable_crate_id()
} else {
- self.untracked_resolutions.cstore.stable_crate_id(crate_num)
+ self.cstore.stable_crate_id(crate_num)
}
}
if stable_crate_id == self.sess.local_stable_crate_id() {
LOCAL_CRATE
} else {
- self.untracked_resolutions.cstore.stable_crate_id_to_crate_num(stable_crate_id)
+ self.cstore.stable_crate_id_to_crate_num(stable_crate_id)
}
}
// If this is a DefPathHash from the local crate, we can look up the
// DefId in the tcx's `Definitions`.
if stable_crate_id == self.sess.local_stable_crate_id() {
- self.untracked_resolutions
- .definitions
- .local_def_path_hash_to_def_id(hash, err)
- .to_def_id()
+ self.definitions.local_def_path_hash_to_def_id(hash, err).to_def_id()
} else {
// If this is a DefPathHash from an upstream crate, let the CrateStore map
// it to a DefId.
- let cstore = &self.untracked_resolutions.cstore;
- let cnum = cstore.stable_crate_id_to_crate_num(stable_crate_id);
- cstore.def_path_hash_to_def_id(cnum, hash)
+ let cnum = self.cstore.stable_crate_id_to_crate_num(stable_crate_id);
+ self.cstore.def_path_hash_to_def_id(cnum, hash)
}
}
let (crate_name, stable_crate_id) = if def_id.is_local() {
(self.crate_name, self.sess.local_stable_crate_id())
} else {
- let cstore = &self.untracked_resolutions.cstore;
+ let cstore = &self.cstore;
(cstore.crate_name(def_id.krate), cstore.stable_crate_id(def_id.krate))
};
/// Note that this is *untracked* and should only be used within the query
/// system if the result is otherwise tracked through queries
- pub fn cstore_untracked(self) -> &'tcx ty::CrateStoreDyn {
- &*self.untracked_resolutions.cstore
+ pub fn cstore_untracked(self) -> &'tcx CrateStoreDyn {
+ &*self.cstore
}
/// Note that this is *untracked* and should only be used within the query
/// system if the result is otherwise tracked through queries
pub fn definitions_untracked(self) -> &'tcx hir::definitions::Definitions {
- &self.untracked_resolutions.definitions
+ &self.definitions
+ }
+
+ /// Note that this is *untracked* and should only be used within the query
+ /// system if the result is otherwise tracked through queries
+ #[inline]
+ pub fn source_span_untracked(self, def_id: LocalDefId) -> Span {
+ self.untracked_resolutions.source_span.get(def_id).copied().unwrap_or(DUMMY_SP)
}
#[inline(always)]
pub fn create_stable_hashing_context(self) -> StableHashingContext<'tcx> {
- let resolutions = &self.gcx.untracked_resolutions;
- StableHashingContext::new(self.sess, &resolutions.definitions, &*resolutions.cstore)
+ StableHashingContext::new(
+ self.sess,
+ &self.definitions,
+ &*self.cstore,
+ &self.untracked_resolutions.source_span,
+ )
}
#[inline(always)]
pub fn create_no_span_stable_hashing_context(self) -> StableHashingContext<'tcx> {
- let resolutions = &self.gcx.untracked_resolutions;
StableHashingContext::ignore_spans(
self.sess,
- &resolutions.definitions,
- &*resolutions.cstore,
+ &self.definitions,
+ &*self.cstore,
+ &self.untracked_resolutions.source_span,
)
}
#[allow(rustc::usage_of_ty_tykind)]
#[inline]
pub fn mk_ty(self, st: TyKind<'tcx>) -> Ty<'tcx> {
- self.interners.intern_ty(st, self.sess, &self.gcx.untracked_resolutions)
+ self.interners.intern_ty(
+ st,
+ self.sess,
+ &self.definitions,
+ &*self.cstore,
+ // This is only used to create a stable hashing context.
+ &self.untracked_resolutions.source_span,
+ )
}
#[inline]
pub use assoc::*;
pub use generics::*;
use rustc_ast as ast;
+use rustc_ast::node_id::NodeMap;
use rustc_attr as attr;
use rustc_data_structures::fingerprint::Fingerprint;
use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap, FxIndexSet};
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
use rustc_data_structures::tagged_ptr::CopyTaggedPtr;
use rustc_hir as hir;
-use rustc_hir::def::{CtorKind, CtorOf, DefKind, Res};
+use rustc_hir::def::{CtorKind, CtorOf, DefKind, LifetimeRes, Res};
use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, LocalDefIdMap};
use rustc_hir::Node;
+use rustc_index::vec::IndexVec;
use rustc_macros::HashStable;
use rustc_query_system::ich::StableHashingContext;
-use rustc_session::cstore::CrateStoreDyn;
+use rustc_span::hygiene::MacroKind;
use rustc_span::symbol::{kw, sym, Ident, Symbol};
-use rustc_span::Span;
+use rustc_span::{ExpnId, Span};
use rustc_target::abi::{Align, VariantIdx};
pub use subst::*;
pub use vtable::*;
#[derive(Debug)]
pub struct ResolverOutputs {
- pub definitions: rustc_hir::definitions::Definitions,
- pub cstore: Box<CrateStoreDyn>,
pub visibilities: FxHashMap<LocalDefId, Visibility>,
/// This field is used to decide whether we should make `PRIVATE_IN_PUBLIC` a hard error.
pub has_pub_restricted: bool,
+ /// Item with a given `LocalDefId` was defined during macro expansion with ID `ExpnId`.
+ pub expn_that_defined: FxHashMap<LocalDefId, ExpnId>,
+ /// Reference span for definitions.
+ pub source_span: IndexVec<LocalDefId, Span>,
pub access_levels: AccessLevels,
pub extern_crate_map: FxHashMap<LocalDefId, CrateNum>,
pub maybe_unused_trait_imports: FxIndexSet<LocalDefId>,
pub registered_tools: RegisteredTools,
}
+/// Resolutions that should only be used for lowering.
+/// This struct is meant to be consumed by lowering.
+#[derive(Debug)]
+pub struct ResolverAstLowering {
+ pub legacy_const_generic_args: FxHashMap<DefId, Option<Vec<usize>>>,
+
+ /// Resolutions for nodes that have a single resolution.
+ pub partial_res_map: NodeMap<hir::def::PartialRes>,
+ /// Resolutions for import nodes, which have multiple resolutions in different namespaces.
+ pub import_res_map: NodeMap<hir::def::PerNS<Option<Res<ast::NodeId>>>>,
+ /// Resolutions for labels (node IDs of their corresponding blocks or loops).
+ pub label_res_map: NodeMap<ast::NodeId>,
+ /// Resolutions for lifetimes.
+ pub lifetimes_res_map: NodeMap<LifetimeRes>,
+ /// Lifetime parameters that lowering will have to introduce.
+ pub extra_lifetime_params_map: NodeMap<Vec<(Ident, ast::NodeId, LifetimeRes)>>,
+
+ pub next_node_id: ast::NodeId,
+
+ pub node_id_to_def_id: FxHashMap<ast::NodeId, LocalDefId>,
+ pub def_id_to_node_id: IndexVec<LocalDefId, ast::NodeId>,
+
+ 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>,
+}
+
#[derive(Clone, Copy, Debug)]
pub struct MainDefinition {
pub res: Res<ast::NodeId>,
let dlo = u32::decode(decoder);
let dto = u32::decode(decoder);
- let enclosing =
- decoder.tcx.definitions_untracked().def_span(parent.unwrap()).data_untracked();
+ let enclosing = decoder.tcx.source_span_untracked(parent.unwrap()).data_untracked();
let span = Span::new(
enclosing.lo + BytePos::from_u32(dlo),
enclosing.lo + BytePos::from_u32(dto),
}
if let Some(parent) = span_data.parent {
- let enclosing = s.tcx.definitions_untracked().def_span(parent).data_untracked();
+ let enclosing = s.tcx.source_span(parent).data_untracked();
if enclosing.contains(span_data) {
TAG_RELATIVE_SPAN.encode(s);
(span_data.lo - enclosing.lo).to_u32().encode(s);
use rustc_hir as hir;
use rustc_hir::def_id::{DefId, LocalDefId};
use rustc_hir::definitions::{DefPathHash, Definitions};
+use rustc_index::vec::IndexVec;
use rustc_session::cstore::CrateStore;
use rustc_session::Session;
use rustc_span::source_map::SourceMap;
pub struct StableHashingContext<'a> {
definitions: &'a Definitions,
cstore: &'a dyn CrateStore,
+ source_span: &'a IndexVec<LocalDefId, Span>,
// The value of `-Z incremental-ignore-spans`.
// This field should only be used by `debug_opts_incremental_ignore_span`
incremental_ignore_spans: bool,
sess: &'a Session,
definitions: &'a Definitions,
cstore: &'a dyn CrateStore,
+ source_span: &'a IndexVec<LocalDefId, Span>,
always_ignore_spans: bool,
) -> Self {
let hash_spans_initial =
body_resolver: BodyResolver::Forbidden,
definitions,
cstore,
+ source_span,
incremental_ignore_spans: sess.opts.debugging_opts.incremental_ignore_spans,
caching_source_map: None,
raw_source_map: sess.source_map(),
sess: &'a Session,
definitions: &'a Definitions,
cstore: &'a dyn CrateStore,
+ source_span: &'a IndexVec<LocalDefId, Span>,
) -> Self {
Self::new_with_or_without_spans(
sess,
definitions,
cstore,
+ source_span,
/*always_ignore_spans=*/ false,
)
}
sess: &'a Session,
definitions: &'a Definitions,
cstore: &'a dyn CrateStore,
+ source_span: &'a IndexVec<LocalDefId, Span>,
) -> Self {
let always_ignore_spans = true;
- Self::new_with_or_without_spans(sess, definitions, cstore, always_ignore_spans)
+ Self::new_with_or_without_spans(sess, definitions, cstore, source_span, always_ignore_spans)
}
/// Allow hashing
#[inline]
fn def_span(&self, def_id: LocalDefId) -> Span {
- self.definitions.def_span(def_id)
+ self.source_span[def_id]
}
#[inline]
rustc_ast = { path = "../rustc_ast" }
rustc_arena = { path = "../rustc_arena" }
rustc_middle = { path = "../rustc_middle" }
-rustc_ast_lowering = { path = "../rustc_ast_lowering" }
rustc_ast_pretty = { path = "../rustc_ast_pretty" }
rustc_attr = { path = "../rustc_attr" }
rustc_data_structures = { path = "../rustc_data_structures" }
use rustc_ast::EnumDef;
use rustc_ast::ForeignMod;
use rustc_ast::NodeId;
-use rustc_ast_lowering::ResolverAstLowering;
use rustc_hir::def_id::LocalDefId;
use rustc_hir::def_id::CRATE_DEF_ID;
use rustc_middle::middle::privacy::AccessLevel;
use rustc_ast::visit::{self, AssocCtxt, Visitor};
use rustc_ast::{self as ast, AssocItem, AssocItemKind, MetaItemKind, StmtKind};
use rustc_ast::{Block, Fn, ForeignItem, ForeignItemKind, Impl, Item, ItemKind, NodeId};
-use rustc_ast_lowering::ResolverAstLowering;
use rustc_attr as attr;
use rustc_data_structures::sync::Lrc;
use rustc_errors::{struct_span_err, Applicability};
use rustc_metadata::creader::LoadedMacro;
use rustc_middle::bug;
use rustc_middle::metadata::ModChild;
-use rustc_middle::ty;
+use rustc_middle::ty::{self, DefIdTree};
use rustc_session::cstore::CrateStore;
use rustc_span::hygiene::{ExpnId, LocalExpnId, MacroKind};
use rustc_span::source_map::{respan, Spanned};
loop {
match self.get_module(def_id) {
Some(module) => return module,
- None => {
- def_id.index =
- self.def_key(def_id).parent.expect("non-root `DefId` without parent")
- }
+ None => def_id = self.parent(def_id),
}
}
}
use rustc_ast as ast;
use rustc_ast::node_id::NodeMap;
use rustc_ast::visit::{self, Visitor};
-use rustc_ast_lowering::ResolverAstLowering;
use rustc_data_structures::fx::FxHashSet;
use rustc_errors::{pluralize, MultiSpan};
use rustc_session::lint::builtin::{MACRO_USE_EXTERN_CRATE, UNUSED_IMPORTS};
use rustc_ast::visit::{self, FnKind};
use rustc_ast::walk_list;
use rustc_ast::*;
-use rustc_ast_lowering::ResolverAstLowering;
use rustc_expand::expand::AstFragment;
use rustc_hir::def_id::LocalDefId;
use rustc_hir::definitions::*;
use rustc_feature::BUILTIN_ATTRIBUTES;
use rustc_hir::def::Namespace::{self, *};
use rustc_hir::def::{self, CtorKind, CtorOf, DefKind, NonMacroAttrKind, PerNS};
-use rustc_hir::def_id::{DefId, CRATE_DEF_ID, LOCAL_CRATE};
+use rustc_hir::def_id::{DefId, LocalDefId, CRATE_DEF_ID, LOCAL_CRATE};
use rustc_hir::PrimTy;
+use rustc_index::vec::IndexVec;
use rustc_middle::bug;
use rustc_middle::ty::DefIdTree;
use rustc_session::lint::builtin::ABSOLUTE_PATHS_NOT_STARTING_WITH_CRATE;
};
if !candidates.is_empty() {
show_candidates(
- &self.definitions,
- self.session,
+ &self.session,
+ &self.source_span,
&mut err,
span,
&candidates,
err.span_help(span, &help_msg);
}
show_candidates(
- &self.definitions,
- self.session,
+ &self.session,
+ &self.source_span,
&mut err,
Some(span),
&import_suggestions,
let import_suggestions =
self.lookup_import_candidates(ident, Namespace::MacroNS, parent_scope, is_expected);
show_candidates(
- &self.definitions,
- self.session,
+ &self.session,
+ &self.source_span,
err,
None,
&import_suggestions,
/// entities with that name in all crates. This method allows outputting the
/// results of this search in a programmer-friendly way
fn show_candidates(
- definitions: &rustc_hir::definitions::Definitions,
session: &Session,
+ source_span: &IndexVec<LocalDefId, Span>,
err: &mut Diagnostic,
// This is `None` if all placement locations are inside expansions
use_placement_span: Option<Span>,
);
if let Some(local_def_id) = def_id.and_then(|did| did.as_local()) {
- let span = definitions.def_span(local_def_id);
+ let span = source_span[local_def_id];
let span = session.source_map().guess_head_span(span);
let mut multi_span = MultiSpan::from_span(span);
multi_span.push_span_label(span, "not accessible".to_string());
let mut spans = Vec::new();
for (name, _, def_id, _) in &inaccessible_path_strings {
if let Some(local_def_id) = def_id.and_then(|did| did.as_local()) {
- let span = definitions.def_span(local_def_id);
+ let span = source_span[local_def_id];
let span = session.source_map().guess_head_span(span);
spans.push((name, span));
} else {
use rustc_ast::ptr::P;
use rustc_ast::visit::{self, AssocCtxt, BoundKind, FnCtxt, FnKind, Visitor};
use rustc_ast::*;
-use rustc_ast_lowering::{LifetimeRes, ResolverAstLowering};
use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap};
use rustc_errors::DiagnosticId;
use rustc_hir::def::Namespace::{self, *};
-use rustc_hir::def::{self, CtorKind, DefKind, PartialRes, PerNS};
+use rustc_hir::def::{self, CtorKind, DefKind, LifetimeRes, PartialRes, PerNS};
use rustc_hir::def_id::{DefId, LocalDefId, CRATE_DEF_ID};
use rustc_hir::definitions::DefPathData;
use rustc_hir::{PrimTy, TraitCandidate};
self as ast, AssocItemKind, Expr, ExprKind, GenericParam, GenericParamKind, Item, ItemKind,
NodeId, Path, Ty, TyKind,
};
-use rustc_ast_lowering::ResolverAstLowering;
use rustc_ast_pretty::pprust::path_segment_to_string;
use rustc_data_structures::fx::{FxHashSet, FxIndexSet};
use rustc_errors::{
use rustc_ast::node_id::NodeMap;
use rustc_ast::{self as ast, NodeId, CRATE_NODE_ID};
use rustc_ast::{AngleBracketedArg, Crate, Expr, ExprKind, GenericArg, GenericArgs, LitKind, Path};
-use rustc_ast_lowering::{LifetimeRes, ResolverAstLowering};
use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap, FxIndexSet};
use rustc_data_structures::intern::Interned;
use rustc_data_structures::sync::Lrc;
use rustc_errors::{Applicability, DiagnosticBuilder, ErrorGuaranteed};
use rustc_expand::base::{DeriveResolutions, SyntaxExtension, SyntaxExtensionKind};
use rustc_hir::def::Namespace::*;
-use rustc_hir::def::{self, CtorOf, DefKind, PartialRes};
-use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, DefPathHash, LocalDefId};
+use rustc_hir::def::{self, CtorOf, DefKind, LifetimeRes, PartialRes};
+use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, LocalDefId};
use rustc_hir::def_id::{CRATE_DEF_ID, LOCAL_CRATE};
-use rustc_hir::definitions::{DefKey, DefPathData, Definitions};
+use rustc_hir::definitions::{DefPathData, Definitions};
use rustc_hir::TraitCandidate;
use rustc_index::vec::IndexVec;
use rustc_metadata::creader::{CStore, CrateLoader};
use rustc_middle::ty::query::Providers;
use rustc_middle::ty::{self, DefIdTree, MainDefinition, RegisteredTools, ResolverOutputs};
use rustc_query_system::ich::StableHashingContext;
-use rustc_session::cstore::{CrateStore, MetadataLoaderDyn};
+use rustc_session::cstore::{CrateStore, CrateStoreDyn, MetadataLoaderDyn};
use rustc_session::lint::LintBuffer;
use rustc_session::Session;
use rustc_span::hygiene::{ExpnId, LocalExpnId, MacroKind, SyntaxContext, Transparency};
session: &'a Session,
definitions: Definitions,
+ /// Item with a given `LocalDefId` was defined during macro expansion with ID `ExpnId`.
+ expn_that_defined: FxHashMap<LocalDefId, ExpnId>,
+ /// Reference span for definitions.
+ source_span: IndexVec<LocalDefId, Span>,
graph_root: Module<'a>,
}
}
-/// This interface is used through the AST→HIR step, to embed full paths into the HIR. After that
-/// the resolver is no longer needed as all the relevant information is inline.
-impl ResolverAstLowering for Resolver<'_> {
- fn def_key(&self, id: DefId) -> DefKey {
- if let Some(id) = id.as_local() {
- self.definitions.def_key(id)
- } else {
- self.cstore().def_key(id)
- }
- }
-
- #[inline]
- fn def_span(&self, id: LocalDefId) -> Span {
- self.definitions.def_span(id)
- }
-
- fn item_generics_num_lifetimes(&self, def_id: DefId) -> usize {
- if let Some(def_id) = def_id.as_local() {
- self.item_generics_num_lifetimes[&def_id]
- } else {
- self.cstore().item_generics_num_lifetimes(def_id, self.session)
- }
- }
-
- fn legacy_const_generic_args(&mut self, expr: &Expr) -> Option<Vec<usize>> {
- self.legacy_const_generic_args(expr)
- }
-
- fn get_partial_res(&self, id: NodeId) -> Option<PartialRes> {
- self.partial_res_map.get(&id).cloned()
- }
-
- fn get_import_res(&self, id: NodeId) -> PerNS<Option<Res>> {
- self.import_res_map.get(&id).cloned().unwrap_or_default()
- }
-
- fn get_label_res(&self, id: NodeId) -> Option<NodeId> {
- self.label_res_map.get(&id).cloned()
- }
-
- fn get_lifetime_res(&self, id: NodeId) -> Option<LifetimeRes> {
- self.lifetimes_res_map.get(&id).copied()
- }
-
- fn take_extra_lifetime_params(&mut self, id: NodeId) -> Vec<(Ident, NodeId, LifetimeRes)> {
- self.extra_lifetime_params_map.remove(&id).unwrap_or_default()
- }
-
- fn create_stable_hashing_context(&self) -> StableHashingContext<'_> {
- StableHashingContext::new(self.session, &self.definitions, self.crate_loader.cstore())
- }
-
- fn definitions(&self) -> &Definitions {
- &self.definitions
- }
-
- fn next_node_id(&mut self) -> NodeId {
- self.next_node_id()
- }
-
- fn take_trait_map(&mut self, node: NodeId) -> Option<Vec<TraitCandidate>> {
- self.trait_map.remove(&node)
- }
-
+impl Resolver<'_> {
fn opt_local_def_id(&self, node: NodeId) -> Option<LocalDefId> {
self.node_id_to_def_id.get(&node).copied()
}
- fn local_def_id(&self, node: NodeId) -> LocalDefId {
+ pub fn local_def_id(&self, node: NodeId) -> LocalDefId {
self.opt_local_def_id(node).unwrap_or_else(|| panic!("no entry for node id: `{:?}`", node))
}
- fn def_path_hash(&self, def_id: DefId) -> DefPathHash {
- match def_id.as_local() {
- Some(def_id) => self.definitions.def_path_hash(def_id),
- None => self.cstore().def_path_hash(def_id),
- }
- }
-
/// Adds a definition with a parent definition.
fn create_def(
&mut self,
self.definitions.def_key(self.node_id_to_def_id[&node_id]),
);
- let def_id = self.definitions.create_def(parent, data, expn_id, span);
+ let def_id = self.definitions.create_def(parent, data);
+
+ // Create the definition.
+ if expn_id != ExpnId::root() {
+ self.expn_that_defined.insert(def_id, expn_id);
+ }
+
+ // A relative span's parent must be an absolute span.
+ debug_assert_eq!(span.data_untracked().parent, None);
+ let _id = self.source_span.push(span);
+ debug_assert_eq!(_id, def_id);
// Some things for which we allocate `LocalDefId`s don't correspond to
// anything in the AST, so they don't have a `NodeId`. For these cases
def_id
}
- fn decl_macro_kind(&self, def_id: LocalDefId) -> MacroKind {
- self.builtin_macro_kinds.get(&def_id).copied().unwrap_or(MacroKind::Bang)
+ fn item_generics_num_lifetimes(&self, def_id: DefId) -> usize {
+ if let Some(def_id) = def_id.as_local() {
+ self.item_generics_num_lifetimes[&def_id]
+ } else {
+ self.cstore().item_generics_num_lifetimes(def_id, self.session)
+ }
}
}
&mut FxHashMap::default(),
);
- let definitions = Definitions::new(session.local_stable_crate_id(), krate.spans.inner_span);
+ let definitions = Definitions::new(session.local_stable_crate_id());
let mut visibilities = FxHashMap::default();
visibilities.insert(CRATE_DEF_ID, ty::Visibility::Public);
let mut invocation_parents = FxHashMap::default();
invocation_parents.insert(LocalExpnId::ROOT, (CRATE_DEF_ID, ImplTraitContext::Existential));
+ let mut source_span = IndexVec::default();
+ let _id = source_span.push(krate.spans.inner_span);
+ debug_assert_eq!(_id, CRATE_DEF_ID);
+
let mut extern_prelude: FxHashMap<Ident, ExternPreludeEntry<'_>> = session
.opts
.externs
session,
definitions,
+ expn_that_defined: Default::default(),
+ source_span,
// The outermost module has def ID 0; this is not reflected in the
// AST.
Default::default()
}
- pub fn into_outputs(self) -> ResolverOutputs {
+ pub fn into_outputs(
+ self,
+ ) -> (Definitions, Box<CrateStoreDyn>, ResolverOutputs, ty::ResolverAstLowering) {
let proc_macros = self.proc_macros.iter().map(|id| self.local_def_id(*id)).collect();
let definitions = self.definitions;
+ let cstore = Box::new(self.crate_loader.into_cstore());
+ let source_span = self.source_span;
+ let expn_that_defined = self.expn_that_defined;
let visibilities = self.visibilities;
let has_pub_restricted = self.has_pub_restricted;
let extern_crate_map = self.extern_crate_map;
let main_def = self.main_def;
let confused_type_with_std_module = self.confused_type_with_std_module;
let access_levels = self.access_levels;
- ResolverOutputs {
- definitions,
- cstore: Box::new(self.crate_loader.into_cstore()),
+ let resolutions = ResolverOutputs {
+ source_span,
+ expn_that_defined,
visibilities,
has_pub_restricted,
access_levels,
proc_macros,
confused_type_with_std_module,
registered_tools: self.registered_tools,
- }
+ };
+ let resolutions_lowering = ty::ResolverAstLowering {
+ legacy_const_generic_args: self.legacy_const_generic_args,
+ partial_res_map: self.partial_res_map,
+ import_res_map: self.import_res_map,
+ label_res_map: self.label_res_map,
+ lifetimes_res_map: self.lifetimes_res_map,
+ extra_lifetime_params_map: self.extra_lifetime_params_map,
+ next_node_id: self.next_node_id,
+ 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,
+ };
+ (definitions, cstore, resolutions, resolutions_lowering)
}
- pub fn clone_outputs(&self) -> ResolverOutputs {
+ pub fn clone_outputs(
+ &self,
+ ) -> (Definitions, Box<CrateStoreDyn>, ResolverOutputs, ty::ResolverAstLowering) {
let proc_macros = self.proc_macros.iter().map(|id| self.local_def_id(*id)).collect();
- ResolverOutputs {
- definitions: self.definitions.clone(),
- access_levels: self.access_levels.clone(),
- cstore: Box::new(self.cstore().clone()),
+ let definitions = self.definitions.clone();
+ let cstore = Box::new(self.cstore().clone());
+ let resolutions = ResolverOutputs {
+ source_span: self.source_span.clone(),
+ expn_that_defined: self.expn_that_defined.clone(),
visibilities: self.visibilities.clone(),
has_pub_restricted: self.has_pub_restricted,
extern_crate_map: self.extern_crate_map.clone(),
proc_macros,
confused_type_with_std_module: self.confused_type_with_std_module.clone(),
registered_tools: self.registered_tools.clone(),
- }
+ access_levels: self.access_levels.clone(),
+ };
+ let resolutions_lowering = ty::ResolverAstLowering {
+ legacy_const_generic_args: self.legacy_const_generic_args.clone(),
+ partial_res_map: self.partial_res_map.clone(),
+ import_res_map: self.import_res_map.clone(),
+ label_res_map: self.label_res_map.clone(),
+ lifetimes_res_map: self.lifetimes_res_map.clone(),
+ extra_lifetime_params_map: self.extra_lifetime_params_map.clone(),
+ next_node_id: self.next_node_id.clone(),
+ node_id_to_def_id: self.node_id_to_def_id.clone(),
+ def_id_to_node_id: self.def_id_to_node_id.clone(),
+ trait_map: self.trait_map.clone(),
+ builtin_macro_kinds: self.builtin_macro_kinds.clone(),
+ };
+ (definitions, cstore, resolutions, resolutions_lowering)
+ }
+
+ fn create_stable_hashing_context(&self) -> StableHashingContext<'_> {
+ StableHashingContext::new(
+ self.session,
+ &self.definitions,
+ self.crate_loader.cstore(),
+ &self.source_span,
+ )
}
pub fn cstore(&self) -> &CStore {
/// Retrieves the span of the given `DefId` if `DefId` is in the local crate.
#[inline]
pub fn opt_span(&self, def_id: DefId) -> Option<Span> {
- def_id.as_local().map(|def_id| self.definitions.def_span(def_id))
+ def_id.as_local().map(|def_id| self.source_span[def_id])
}
/// Checks if an expression refers to a function marked with
use crate::{DeriveData, Finalize, ParentScope, ResolutionError, Resolver, ScopeSet};
use crate::{ModuleKind, ModuleOrUniformRoot, NameBinding, PathResult, Segment};
use rustc_ast::{self as ast, Inline, ItemKind, ModKind, NodeId};
-use rustc_ast_lowering::ResolverAstLowering;
use rustc_ast_pretty::pprust;
use rustc_attr::StabilityLevel;
use rustc_data_structures::fx::FxHashSet;
// Dependencies listed in Cargo.toml do not need `extern crate`.
extern crate rustc_ast;
-extern crate rustc_ast_lowering;
extern crate rustc_ast_pretty;
extern crate rustc_attr;
extern crate rustc_const_eval;
use rustc_ast::visit::{self, AssocCtxt, Visitor};
use rustc_ast::{self as ast, ItemKind};
-use rustc_ast_lowering::ResolverAstLowering;
use rustc_data_structures::fx::FxHashMap;
use rustc_hir::def::Namespace::*;
use rustc_hir::def::{DefKind, Namespace, Res};