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 8 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
@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.

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
@oli-obk oli-obk removed the PG-exploit-mitigations Project group: Exploit mitigations label May 2, 2025
@Zoxc Zoxc force-pushed the next-disambiguator branch from d5a3b9c to 6a48217 Compare May 2, 2025 13:51
@rustbot rustbot added the PG-exploit-mitigations Project group: Exploit mitigations label May 2, 2025
@oli-obk oli-obk added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels May 2, 2025
@@ -154,6 +168,8 @@ pub fn intern_const_alloc_recursive<'tcx, M: CompileTimeMachine<'tcx, const_eval
intern_kind: InternKind,
ret: &MPlaceTy<'tcx>,
) -> Result<(), InternResult> {
let mut disambiguator = DisambiguatorState::new();
Copy link
Member

Choose a reason for hiding this comment

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

This is just a number, right? So it's cheap to create even when we don't need it (like for non-static consts)?

Copy link
Contributor

Choose a reason for hiding this comment

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

it's a hash map, but that's also cheap if it never gets pushed to

Copy link
Member

@RalfJung RalfJung May 2, 2025

Choose a reason for hiding this comment

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

Makes sense. A perf run would probably still be a good idea (once the PR is ready for that) given how this changes a bunch of stuff all over the compiler and some of that could easily be hot code.

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-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants