-
Couldn't load subscription status.
- Fork 13.9k
Remove global next_disambiguator state and handle it with a DisambiguatorState type
#140453
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
2ac60bc
e561ec0
df1f8d1
ddff387
efc51ce
d16daf5
4869e20
6a48217
9f8c6d5
acb50d5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -68,7 +68,7 @@ impl DefPathTable { | |
| // | ||
| // See the documentation for DefPathHash for more information. | ||
| panic!( | ||
| "found DefPathHash collision between {def_path1:?} and {def_path2:?}. \ | ||
| "found DefPathHash collision between {def_path1:#?} and {def_path2:#?}. \ | ||
| Compilation cannot continue." | ||
| ); | ||
| } | ||
|
|
@@ -97,13 +97,31 @@ impl DefPathTable { | |
| } | ||
| } | ||
|
|
||
| #[derive(Debug)] | ||
| pub struct DisambiguatorState { | ||
| next: UnordMap<(LocalDefId, DefPathData), u32>, | ||
| } | ||
|
|
||
| impl DisambiguatorState { | ||
| pub fn new() -> Self { | ||
| Self { next: Default::default() } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I do prefer |
||
| } | ||
|
|
||
| /// Creates a `DisambiguatorState` where the next allocated `(LocalDefId, DefPathData)` pair | ||
| /// will have `index` as the disambiguator. | ||
| pub fn with(def_id: LocalDefId, data: DefPathData, index: u32) -> Self { | ||
| let mut this = Self::new(); | ||
| this.next.insert((def_id, data), index); | ||
| this | ||
| } | ||
| } | ||
|
|
||
| /// The definition table containing node definitions. | ||
| /// It holds the `DefPathTable` for `LocalDefId`s/`DefPath`s. | ||
| /// It also stores mappings to convert `LocalDefId`s to/from `HirId`s. | ||
| #[derive(Debug)] | ||
| pub struct Definitions { | ||
| table: DefPathTable, | ||
| next_disambiguator: UnordMap<(LocalDefId, DefPathData), u32>, | ||
| } | ||
|
|
||
| /// A unique identifier that we can use to lookup a definition | ||
|
|
@@ -127,7 +145,7 @@ impl DefKey { | |
| let DisambiguatedDefPathData { ref data, disambiguator } = self.disambiguated_data; | ||
|
|
||
| std::mem::discriminant(data).hash(&mut hasher); | ||
| if let Some(name) = data.get_opt_name() { | ||
| if let Some(name) = data.hashed_symbol() { | ||
| // Get a stable hash by considering the symbol chars rather than | ||
| // the symbol index. | ||
| name.as_str().hash(&mut hasher); | ||
|
|
@@ -173,7 +191,11 @@ impl DisambiguatedDefPathData { | |
| } | ||
| } | ||
| DefPathDataName::Anon { namespace } => { | ||
| write!(writer, "{{{}#{}}}", namespace, self.disambiguator) | ||
| if let DefPathData::AnonAssocTy(method) = self.data { | ||
| write!(writer, "{}::{{{}#{}}}", method, namespace, self.disambiguator) | ||
| } else { | ||
| write!(writer, "{{{}#{}}}", namespace, self.disambiguator) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
@@ -287,10 +309,13 @@ pub enum DefPathData { | |
| /// An existential `impl Trait` type node. | ||
| /// Argument position `impl Trait` have a `TypeNs` with their pretty-printed name. | ||
| OpaqueTy, | ||
| /// An anonymous associated type from an RPITIT. | ||
| AnonAssocTy, | ||
| /// An anonymous associated type from an RPITIT. The symbol refers to the name of the method | ||
| /// that defined the type. | ||
| AnonAssocTy(Symbol), | ||
Zoxc marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| /// A synthetic body for a coroutine's by-move body. | ||
| SyntheticCoroutineBody, | ||
| /// Additional static data referred to by a static. | ||
| NestedStatic, | ||
| } | ||
|
|
||
| impl Definitions { | ||
|
|
@@ -342,24 +367,33 @@ impl Definitions { | |
| let root = LocalDefId { local_def_index: table.allocate(key, def_path_hash) }; | ||
| assert_eq!(root.local_def_index, CRATE_DEF_INDEX); | ||
|
|
||
| Definitions { table, next_disambiguator: Default::default() } | ||
| Definitions { table } | ||
| } | ||
|
|
||
| /// Adds a definition with a parent definition. | ||
| pub fn create_def(&mut self, parent: LocalDefId, data: DefPathData) -> LocalDefId { | ||
| /// Creates a definition with a parent definition. | ||
| /// If there are multiple definitions with the same DefPathData and the same parent, use | ||
| /// `disambiguator` to differentiate them. Distinct `DisambiguatorState` instances are not | ||
| /// guaranteed to generate unique disambiguators and should instead ensure that the `parent` | ||
| /// and `data` pair is distinct from other instances. | ||
| pub fn create_def( | ||
| &mut self, | ||
| parent: LocalDefId, | ||
| data: DefPathData, | ||
| disambiguator: &mut DisambiguatorState, | ||
| ) -> LocalDefId { | ||
| // We can't use `Debug` implementation for `LocalDefId` here, since it tries to acquire a | ||
| // reference to `Definitions` and we're already holding a mutable reference. | ||
| debug!( | ||
| "create_def(parent={}, data={data:?})", | ||
| self.def_path(parent).to_string_no_crate_verbose(), | ||
| ); | ||
|
|
||
| // The root node must be created with `create_root_def()`. | ||
| // The root node must be created in `new()`. | ||
| assert!(data != DefPathData::CrateRoot); | ||
|
|
||
| // Find the next free disambiguator for this key. | ||
| let disambiguator = { | ||
| let next_disamb = self.next_disambiguator.entry((parent, data)).or_insert(0); | ||
| let next_disamb = disambiguator.next.entry((parent, data)).or_insert(0); | ||
| let disambiguator = *next_disamb; | ||
| *next_disamb = next_disamb.checked_add(1).expect("disambiguator overflow"); | ||
| disambiguator | ||
|
|
@@ -422,8 +456,30 @@ impl DefPathData { | |
| | Ctor | ||
| | AnonConst | ||
| | OpaqueTy | ||
| | AnonAssocTy | ||
| | SyntheticCoroutineBody => None, | ||
| | AnonAssocTy(..) | ||
| | SyntheticCoroutineBody | ||
| | NestedStatic => None, | ||
| } | ||
| } | ||
|
|
||
| fn hashed_symbol(&self) -> Option<Symbol> { | ||
| use self::DefPathData::*; | ||
| match *self { | ||
| TypeNs(name) | ValueNs(name) | MacroNs(name) | LifetimeNs(name) | AnonAssocTy(name) => { | ||
Zoxc marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Some(name) | ||
| } | ||
|
|
||
| Impl | ||
| | ForeignMod | ||
| | CrateRoot | ||
| | Use | ||
| | GlobalAsm | ||
| | Closure | ||
| | Ctor | ||
| | AnonConst | ||
| | OpaqueTy | ||
| | SyntheticCoroutineBody | ||
| | NestedStatic => None, | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -443,8 +499,9 @@ impl DefPathData { | |
| Ctor => DefPathDataName::Anon { namespace: sym::constructor }, | ||
| AnonConst => DefPathDataName::Anon { namespace: sym::constant }, | ||
| OpaqueTy => DefPathDataName::Anon { namespace: sym::opaque }, | ||
| AnonAssocTy => DefPathDataName::Anon { namespace: sym::anon_assoc }, | ||
| AnonAssocTy(..) => DefPathDataName::Anon { namespace: sym::anon_assoc }, | ||
| SyntheticCoroutineBody => DefPathDataName::Anon { namespace: sym::synthetic }, | ||
| NestedStatic => DefPathDataName::Anon { namespace: sym::nested }, | ||
| } | ||
| } | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.