Skip to content

Fix ICE when collapsing associated type with mismatched bound vars - #161649

Open
im-lunex wants to merge 1 commit into
rust-lang:mainfrom
im-lunex:fix_#161547
Open

im-lunex wants to merge 1 commit into
rust-lang:mainfrom
im-lunex:fix_#161547

Conversation

@im-lunex

@im-lunex im-lunex commented Aug 24, 2026

Copy link
Copy Markdown
Contributor

the ICE caused by collapse_candidates_to_subtrait_pick because it decided whether one trait was a supertrait just basing on the def_id and ignoring the generic args.

the fix approach adds an check using anonymize_bound_vars before merging... if the check fails we return NONE to sign ambiguity and the existing ambiguity diagnostic (E0221) handles the rest...

added 2 test and 1 .stderr

tested with -> tests/ui/supertrait-shadowing/alpha-equivalent-duplicate-supertrait-binders.rs and tests/ui/supertrait-shadowing/ambiguous-duplicate-supertrait-binders.rs

Fixes #161547

@rustbot

rustbot commented Aug 24, 2026

Copy link
Copy Markdown
Collaborator

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 Aug 24, 2026
@rustbot

This comment was marked as outdated.

@fmease fmease assigned fmease and unassigned dingxiangfei2009 Aug 24, 2026
Comment on lines +1301 to +1306
if child_trait.def_id() == remaining_trait.def_id()
&& self.tcx().anonymize_bound_vars(child_trait)
!= self.tcx().anonymize_bound_vars(remaining_trait)
{
return None;
}

@fmease fmease Sep 19, 2026

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.

This certainly isn't correct since it makes us start rejecting code like this:

//@ compile-flags: -Znext-solver
#![feature(supertrait_item_shadowing)]

trait A<'a> { type Ty; }
trait B: for<'a> A<'a> + A<'static> { type Ty; }

fn f<T: B>() {
    let _: T::Ty; // PASS -> AMBIGUITY
}

However, there should be no ambiguity under supertrait_item_shadowing as clearly the candidate <T as B>::Ty shadows both <T as A<'static>>::Ty and (pseudo) for<'a> <T as A<'a>>::Ty. It doesn't matter if the bound vars or the arguments mismatch. Identification by DefId is sufficient.

Similarly, this would make us reject code like below despite there being no ambiguity:

#![feature(supertrait_item_shadowing)]

trait A<D> { type Ty; }
trait B: A<i32> + A<()> { type Ty; }

fn f<T: B>() {
    let _: T::Ty; // PASS -> AMBIGUITY
}

View changes since the review

@fmease fmease Sep 19, 2026

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.

For reference, the fn analogue (which uses the collapse_candidates_to_subtrait_pick from method/probe.rs instead) continues to PASS under your PR meaning they now diverge in behavior:

//@ compile-flags: -Znext-solver
#![feature(supertrait_item_shadowing)]

trait A<'a> { fn f(); }
trait B: for<'a> A<'a> + A<'static> { fn f(); }

fn f<T: B>() {
    let _ = T::f(); // PASS
}

@fmease fmease Sep 19, 2026

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 the root cause is somewhere else.

@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 Sep 19, 2026
@rustbot

rustbot commented Sep 19, 2026

Copy link
Copy Markdown
Collaborator

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

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

Labels

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.

[ICE]: expected a region, but found another kind

4 participants