Auto merge of #98106 - cjgillot:split-definitions, r=michaelwoerister
Split up `Definitions` and `ResolverAstLowering`. Split off https://github.com/rust-lang/rust/pull/95573 r? `@michaelwoerister`
This commit is contained in:
commit
3a8b0144c8
32 changed files with 598 additions and 485 deletions
|
@ -1,9 +1,9 @@
|
|||
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;
|
||||
|
||||
|
@ -711,3 +711,43 @@ impl<Id> Res<Id> {
|
|||
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 },
|
||||
}
|
||||
|
|
|
@ -11,9 +11,7 @@ use crate::def_path_hash_map::DefPathHashMap;
|
|||
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;
|
||||
|
@ -101,11 +99,6 @@ pub struct Definitions {
|
|||
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,
|
||||
}
|
||||
|
@ -323,7 +316,7 @@ impl Definitions {
|
|||
}
|
||||
|
||||
/// 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 {
|
||||
|
@ -340,30 +333,12 @@ impl Definitions {
|
|||
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);
|
||||
|
@ -386,28 +361,7 @@ impl Definitions {
|
|||
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> + '_ {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue