Skip to content

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

Zoxc
Copy link
Contributor

@Zoxc Zoxc commented Apr 29, 2025

This removes Definitions.next_disambiguator as it doesn't guarantee deterministic def paths when create_def is called in parallel. Instead a new DisambiguatorState type is passed as a mutable reference to create_def to help create unique def paths. create_def calls with distinct DisambiguatorState 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

@rustbot
Copy link
Collaborator

rustbot commented Apr 29, 2025

r? @estebank

rustbot has assigned @estebank.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added PG-exploit-mitigations Project group: Exploit mitigations S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Apr 29, 2025
@rustbot
Copy link
Collaborator

rustbot commented Apr 29, 2025

Some changes occurred to the CTFE / Miri interpreter

cc @rust-lang/miri

Some changes occurred to the CTFE machinery

cc @RalfJung, @oli-obk, @lcnr

Some changes occurred to MIR optimizations

cc @rust-lang/wg-mir-opt

Some changes occurred in compiler/rustc_sanitizers

cc @rcvalle

@rust-log-analyzer

This comment has been minimized.

@petrochenkov petrochenkov self-assigned this Apr 29, 2025
@@ -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.
Copy link
Member

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.^^

Copy link
Contributor Author

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).

Copy link
Member

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.

Copy link
Contributor

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

Copy link
Contributor Author

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.

Copy link
Member

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

@oli-obk oli-obk assigned oli-obk and unassigned estebank Apr 29, 2025

impl DisambiguatorState {
pub fn new() -> Self {
Self { next: Default::default() }
Copy link
Contributor

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),
Copy link
Contributor

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) => {
Copy link
Contributor

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.

Copy link
Contributor Author

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>,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def_path_data: Option<DefPathData>,
override_def_path_data: Option<DefPathData>,

or something like this.

None,
DefKind::SyntheticCoroutineBody,
None,
&mut DisambiguatorState::new(),
Copy link
Contributor

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(),
Copy link
Contributor

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.

@petrochenkov petrochenkov removed their assignment Apr 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
PG-exploit-mitigations Project group: Exploit mitigations S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
7 participants