Skip to content

delegation: fix def path hash collision, add per parent disambiguators#153955

Open
aerooneqq wants to merge 1 commit intorust-lang:mainfrom
aerooneqq:def-path-hash-collision
Open

delegation: fix def path hash collision, add per parent disambiguators#153955
aerooneqq wants to merge 1 commit intorust-lang:mainfrom
aerooneqq:def-path-hash-collision

Conversation

@aerooneqq
Copy link
Copy Markdown
Contributor

@aerooneqq aerooneqq commented Mar 16, 2026

View all comments

This PR addresses the following delegation issues:

Fixes #153410. Part of #118212.

r? @petrochenkov

@rustbot rustbot added 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 Mar 16, 2026
@petrochenkov
Copy link
Copy Markdown
Contributor

petrochenkov commented Mar 16, 2026

This is all pretty awful.
A number of other def ids / def paths are also created during AST lowering, but they don't require keeping per-node disambiguator tables around. Why is delegation so special in this regard?

What exactly definitions create collisions like this? Generic parameters?

As I understand the small per-node disambiguator tables partially duplicate content from the big table.
What prevents us from looking up the current disambiguators in the big table instead?

@petrochenkov petrochenkov 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 Mar 16, 2026
@aerooneqq
Copy link
Copy Markdown
Contributor Author

aerooneqq commented Mar 17, 2026

A number of other def ids / def paths are also created during AST lowering, but they don't require keeping per-node disambiguator tables around. Why is delegation so special in this regard?

We generate new DefIds for each delegation generic param, as creation of DefId requires a name, we want to use names that are defined in a function being reused, for example for proper error messages. It is possible to create DefIds with, for example, sym::dummy name, but in this case error messages will be terrible. If we prefer to use names of generic params of the delegee, then we use names that are created by user, so they can clash with already used names in delegation.

The difference between delegation and other DefIds creation is that latter don't use user-defined names, as other cases of DefIds creation are DefPathData::DesugaredAnonymousLifetime which uses Some(kw::UnderscoreLifetime) as name and DefPathData::LateAnonConst which uses None as name.

Maybe we can create our own DefPathData variant and use it during creation of DefIds, but it still will need to hold the name of the param, as it is required for example in hir_ty_param_name. I am not sure that this is a correct option.

What exactly definitions create collisions like this? Generic parameters?

From our side yes, when we create generic params, their def path data can be either DefPathData::TypeNs(symbol) for DefKind::TyParam, DefPathData::ValueNs(symbol) for DefKind::ConstParam or DefPathData::LifetimeNs(symbol) for DefKind::LifetimeParam, so this def path data can have a collision with anything that have same def path created in scope of delegation LocalDefId during resolve stage.

What prevents us from looking up the current disambiguators in the big table instead?

Big table which is Resolver::disambiguator is not passed anywhere, as DefIds which are created during AST -> HIR lowering before this PR do not need it. Passing the whole disambiguator to lowering stage seems to be not the best option as it contains many unneeded information and it is not passed from Resolver to ResolverAstLowering now.

Internally DisambiguatorState uses UnordMap<(LocalDefId, DefPathData), u32> which prevents from fast lookups by LocalDefId only and changing it (for example to UnordMap<LocalDefId, UnordMap<DefPathData, u32>>) seemed to be out of scope of this PR.

Given this underlying structure I also didn't want to iterate over all map to find delegation-related key-values, and I also didn't want to expose the possibility of traversal of whole DisambiguatorState.

This left me with an option that I implemented in this PR: for each delegation we create additional disambiguator which follows the main one and then stored in delegation_infos map, for this purpose I exposed next operation on DisambiguatorState, which is also not that good, beacuase it was inlined in create_def function, thus only create_def could update it, but in the given conditions I think it is more-or-less OK solution.

@aerooneqq
Copy link
Copy Markdown
Contributor Author

@rustbot ready

To get feedback on the comments.

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Mar 17, 2026
@rust-bors

This comment has been minimized.

@aerooneqq aerooneqq force-pushed the def-path-hash-collision branch from fbb4d60 to b9ef605 Compare March 19, 2026 09:51
@rustbot

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@petrochenkov
Copy link
Copy Markdown
Contributor

petrochenkov commented Mar 19, 2026

This PR may be relevant:

It solved the collision problem by adding new DefPathData variants.

@petrochenkov
Copy link
Copy Markdown
Contributor

Blocked on #154049.
Can try the approach with new DefPathData variants in the meantime.
@rustbot blocked

@rustbot rustbot added S-blocked Status: Blocked on something else such as an RFC or other implementation work. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Mar 19, 2026
@aerooneqq aerooneqq force-pushed the def-path-hash-collision branch from 5c0e8e9 to ef583eb Compare March 20, 2026 13:17
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Mar 20, 2026

Some changes occurred in compiler/rustc_sanitizers

cc @rcvalle

@rustbot rustbot added the PG-exploit-mitigations Project group: Exploit mitigations label Mar 20, 2026
@aerooneqq aerooneqq changed the title Fix delegation def path hash collision, fix ICE connected to dummy spans Fix delegation def path hash collision Mar 20, 2026
@aerooneqq aerooneqq marked this pull request as draft March 20, 2026 14:31
@rustbot rustbot added the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Mar 20, 2026
@rust-bors

This comment has been minimized.

@aerooneqq aerooneqq force-pushed the def-path-hash-collision branch from ef583eb to 5b48d28 Compare March 25, 2026 13:57
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Apr 9, 2026
@petrochenkov petrochenkov 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 Apr 9, 2026
@rust-bors

This comment has been minimized.

@aerooneqq aerooneqq force-pushed the def-path-hash-collision branch from cd53e9f to 01d6692 Compare April 10, 2026 07:47
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 10, 2026

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@aerooneqq aerooneqq changed the title delegation: fix def path hash collision, add per owner disambiguators delegation: fix def path hash collision, add per parent disambiguators Apr 10, 2026
@aerooneqq
Copy link
Copy Markdown
Contributor Author

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Apr 10, 2026
@petrochenkov
Copy link
Copy Markdown
Contributor

r=me after squashing commits.
@rustbot author

@rustbot rustbot 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 Apr 10, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 10, 2026

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@petrochenkov
Copy link
Copy Markdown
Contributor

@cjgillot @oli-obk
This PR effectively reverts #143361 and passes disambiguators from name resolution to AST lowering again (but now in a per-owner fashion).

I think the approach from #143361 is not very scalable, because it leaks internal details like desugaring something during AST lowering into the stable mangling scheme.

If we followed it in this PR, we'd need separate DefPathData and mangling symbols for delegated functions (as opposed to regular functions), but nobody, except perhaps diagnostics, should care whether the function was delegated or not after AST lowering. Other future desugarings may require even more new DefPathDatas and mangling symbols, I'd rather stop it now.
Unfortunately, definitions created even later, after AST lowering, like DefPathData::NestedStatic, still require changing the mangling scheme.

(I'll keep this PR open for a few days before approving.)

@aerooneqq aerooneqq force-pushed the def-path-hash-collision branch from 571b91e to 23a9f3b Compare April 10, 2026 11:36
@aerooneqq
Copy link
Copy Markdown
Contributor Author

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Apr 10, 2026
@petrochenkov petrochenkov added S-waiting-on-t-compiler Status: Awaiting decision from T-compiler and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. perf-regression Performance regression. PG-exploit-mitigations Project group: Exploit mitigations T-rustdoc-frontend Relevant to the rustdoc-frontend team, which will review and decide on the web UI/UX output. A-query-system Area: The rustc query system (https://rustc-dev-guide.rust-lang.org/query.html) labels Apr 10, 2026
@aerooneqq aerooneqq force-pushed the def-path-hash-collision branch from 23a9f3b to 5d9cfeb Compare April 10, 2026 19:49
@rustbot rustbot added the PG-exploit-mitigations Project group: Exploit mitigations label Apr 10, 2026
@aerooneqq
Copy link
Copy Markdown
Contributor Author

Removed duplicated test for #143498.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

F-fn_delegation `#![feature(fn_delegation)]` PG-exploit-mitigations Project group: Exploit mitigations S-waiting-on-t-compiler Status: Awaiting decision from T-compiler 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.

[ICE]: found DefPathHash collision between DefPath

6 participants