-
Notifications
You must be signed in to change notification settings - Fork 13.3k
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
base: master
Are you sure you want to change the base?
Conversation
This comment has been minimized.
This comment has been minimized.
@@ -63,7 +64,7 @@ pub struct CompileTimeMachine<'tcx> { | |||
/// If `Some`, we are evaluating the initializer of the static with the given `LocalDefId`, | |||
/// storing the result in the given `AllocId`. | |||
/// Used to prevent reads from a static's base allocation, as that may allow for self-initialization loops. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doc comment should be updated to explain the role of this DisambiguatorState
component.
Looking at the const_eval diff in this PR, I am completely clueless about what is happening.^^
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Const eval is generating statics (not quite sure why, that's should probably be part of the comment) and DisambiguatorState
is used to ensure these have unique names (def paths).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Const eval is generating statics (not quite sure why, that's should probably be part of the comment)
That part I can explain. :D Though it's way outside the scope for this comment I think -- this field is not directly related to generating those statics.
A static
can require more than one allocation for its result, e.g. if you do static S = &Vec::new();
. We generate anonymous statics for those inner allocations. (Note that this example does not involve any const promotion.)
It seems like what you are doing is funneling some state through to the allocation interner. That is the place where we should have comments explaining why and how we generate anonymous statics.
DisambiguatorState is used to ensure these have unique names (def paths).
Seems like ideally this would be managed by the interner. It's not relevant during const-eval, only after the end of const-eval, so ideally it should not be present during const-eval.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yea that one is my fault. It was just the minimal diff allowing this, doing it solely in the interner... I guess would work unconditionally, not even basing it off on whether we are interning a static item, but just generally. Most code won't use it, but initializing an empty map is not more expensive than wrapping it in an option
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Which interner are you talking about? It could also just be a counter, if it's per-source static.
I also noticed it's using a nested
symbol. That's should probably be a DefPathData
variant to avoid collisions with source items.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Which interner are you talking about?
compiler/rustc_const_eval/src/interpret/intern.rs
|
||
impl DisambiguatorState { | ||
pub fn new() -> Self { | ||
Self { next: Default::default() } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#[derive(Default)]
can be used instead of this.
@@ -288,7 +310,7 @@ pub enum DefPathData { | |||
/// Argument position `impl Trait` have a `TypeNs` with their pretty-printed name. | |||
OpaqueTy, | |||
/// An anonymous associated type from an RPITIT. | |||
AnonAssocTy, | |||
AnonAssocTy(Symbol), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Need a comment telling what the symbol is.
@@ -411,7 +442,9 @@ impl DefPathData { | |||
pub fn get_opt_name(&self) -> Option<Symbol> { | |||
use self::DefPathData::*; | |||
match *self { | |||
TypeNs(name) | ValueNs(name) | MacroNs(name) | LifetimeNs(name) => Some(name), | |||
TypeNs(name) | ValueNs(name) | MacroNs(name) | LifetimeNs(name) | AnonAssocTy(name) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The AnonAssocTy
case here doesn't seem right.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It does seem a little bit odd. I could duplicate get_opt_name
to have a variant that's used for stable hashing.
@@ -1972,8 +1974,10 @@ impl<'tcx> TyCtxt<'tcx> { | |||
parent: LocalDefId, | |||
name: Option<Symbol>, | |||
def_kind: DefKind, | |||
def_path_data: Option<DefPathData>, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
def_path_data: Option<DefPathData>, | |
override_def_path_data: Option<DefPathData>, |
or something like this.
None, | ||
DefKind::SyntheticCoroutineBody, | ||
None, | ||
&mut DisambiguatorState::new(), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This needs a comment explaining why the disambiguate is correct, the removed comment sort of did that.
tcx, | ||
rbv: &mut rbv, | ||
scope: &Scope::Root { opt_parent_item: None }, | ||
disambiguator: &mut DisambiguatorState::new(), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably also needs a comment telling what is the def parenting here and why the disambiguator is correct.
This removes
Definitions.next_disambiguator
as it doesn't guarantee deterministic def paths whencreate_def
is called in parallel. Instead a newDisambiguatorState
type is passed as a mutable reference tocreate_def
to help create unique def paths.create_def
calls with distinctDisambiguatorState
instances must ensure that that the def paths are unique without its help.Anon associated types did rely on this global state for uniqueness and are changed to use (method they're defined in + their position in the method return type) as the
DefPathData
to ensure uniqueness. This also means that the method they're defined in appears in error messages, which is nicer.cc @oli-obk