Skip to content

Add supertrait item shadowing for type-level path resolution#152225

Open
Amanieu wants to merge 1 commit intorust-lang:mainfrom
Amanieu:type-supertrait-shadowing
Open

Add supertrait item shadowing for type-level path resolution#152225
Amanieu wants to merge 1 commit intorust-lang:mainfrom
Amanieu:type-supertrait-shadowing

Conversation

@Amanieu
Copy link
Copy Markdown
Member

@Amanieu Amanieu commented Feb 6, 2026

This makes type-level name resolution (used primarily for associated types) also prefer subtrait items to supertrait items in case of ambiguity.

This addresses the concern from #148605 about the inconsistency with name resolution in expressions and method calls.

@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Feb 6, 2026

HIR ty lowering was modified

cc @fmease

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

rustbot commented Feb 6, 2026

r? @madsmtm

rustbot has assigned @madsmtm.
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

Why was this reviewer chosen?

The reviewer was selected based on:

  • People who recently interacted with files modified in this PR: compiler
  • compiler expanded to 20 candidates
  • Random selection from 12 candidates

println!("{}", size_of::<T::T>());
}

fn generic2<T: C<T = i16>>() {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

What about T: B<T = i16> and T: A<T = i8> - do those resolve to B::T and A::T respectively?

Also, please use a different name for the generic and the associated type, it's a bit confusing as-is.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I've cleaned up the example and added tests showing how B<T = i16> and A<T = i8> are resolved.

@jackh726
Copy link
Copy Markdown
Member

jackh726 commented Feb 9, 2026

r? types

@rustbot rustbot added the T-types Relevant to the types team, which will review and decide on the PR/issue. label Feb 9, 2026
@traviscross traviscross added the I-lang-radar Items that are on lang's radar and will need eventual work or consideration. label Feb 10, 2026
@madsmtm
Copy link
Copy Markdown
Contributor

madsmtm commented Feb 26, 2026

r? types

Wait, I'm not sure if that should've unassigned me or not? In any case, if you also need a compiler reviewer, I'm the wrong one for the job, so I'm gonna unassign myself.

@madsmtm madsmtm removed their assignment Feb 26, 2026
@Amanieu Amanieu force-pushed the type-supertrait-shadowing branch from 1cde4c7 to 5a9ef14 Compare March 28, 2026 12:33
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Mar 28, 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.

Copy link
Copy Markdown
Member

@jackh726 jackh726 left a comment

Choose a reason for hiding this comment

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

In general, I think this needs some more tests. I've already commented about adding the testing attr, but ideally we should go through the existing tests/ui/methods/supertrait-shadowing tests (which in general are pretty thorough) and write the equivalent test for associated types and consts. These cover cases like multiple ancestors, unsatisfied trait bounds, scoping, and (in a PR I'm about to make) stability.

I also would like to see test that show that <U as A>::T works just fine, even if you have U: B.

View changes since this review

use std::mem::size_of;

trait A {
type T;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please use a different name for the associated type to name lexically clash with the param.

println!("{}", size_of::<U::T>());
}

fn generic3<U: B<T = i16>>() {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

What is missing here is a test of U: B<T = i8> and U: B, just to show the former is not callable (because of the blanket impl), and the latter still uses <U as B>::T. (Similar for C below)

trait C: B {}
impl<T> C for T {}

fn main() {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We have rustc_dump_predicates, which we could use to verify the predicates are getting lowered as we expect. It would be good to use that in this (or another) test.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

So, this test probably doesn't make sense here, right? This is not about method resolution.

/// supertrait of another candidate trait.
///
/// This implements RFC #3624.
fn collapse_candidates_to_subtrait_pick(
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think docs here and for collapse_candidates_to_subtrait_pick in method::probe should link to each other to help ensure they are kept up to date.

It's kind of unfortunate we can't have some shared code here.

continue;
}

// This pick is not a supertrait of the `child_pick`.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I have a commit with some cleanup that I'm about to push - but child_pick doesn't exist. Should be child_trait. Here and below.

@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 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

I-lang-radar Items that are on lang's radar and will need eventual work or consideration. 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. T-types Relevant to the types team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants