Skip to content

rustdoc: fix auto trait synthesis ICE with higher-ranked GAT bounds - #160734

Open
souvik1997 wants to merge 1 commit into
rust-lang:mainfrom
souvik1997:rustdoc-auto-trait-gat-region-dedup
Open

rustdoc: fix auto trait synthesis ICE with higher-ranked GAT bounds#160734
souvik1997 wants to merge 1 commit into
rust-lang:mainfrom
souvik1997:rustdoc-auto-trait-gat-region-dedup

Conversation

@souvik1997

@souvik1997 souvik1997 commented Aug 8, 2026

Copy link
Copy Markdown

rustdoc's auto trait impl synthesis deduplicates discovered clauses that differ only in their regions (AutoTraitFinder::add_user_clause), keeping the stricter (higher-ranked) one, because a ParamEnv containing both confuses SelectionContext: both candidates apply, selection reports ambiguity, and the sanity check in find_auto_trait_generics turns that into an ICE:

Unable to fulfill trait DefId(... ~ core[...]::marker::Send) for '...': [Ambiguity]

The dedup only compared regions appearing as direct generic arguments of the trait ref (T: Trait<'a> vs. for<'b> T: Trait<'b>). With generic associated types the differing region can instead sit inside a type argument: for<'w> <A as Alloc>::Wired<'w>: Send and <A as Alloc>::Wired<'static>: Send have unequal self types, so the dedup bailed out ("we can't compare lifetimes if the types are different"), both clauses landed in the ParamEnv, and the final fulfillment check panicked.

This PR compares the argument lists with all regions erased instead, and, when they are equal modulo regions, walks the regions of both argument lists in lockstep so the existing preference rules (keep the higher-ranked bound, drop region variables) also apply to regions nested inside type arguments.

Minimal reproduction (ICEs on stable and nightly before this change):

pub trait Alloc {
    type Wired<'w>;
}

pub struct Slot<A>(A);

unsafe impl<A: Alloc> Send for Slot<A> where for<'w> A::Wired<'w>: Send {}

pub struct Custody<A: Alloc>(Slot<A>, A::Wired<'static>);

Synthesizing Custody<A>: Send discovers for<'w> A::Wired<'w>: Send (from Slot's manual impl) and A::Wired<'static>: Send (from the second field); with this change the latter is subsumed by the former and the synthesized impl is rendered with the higher-ranked bound.

Fixes #144918.

#149019 rewrites the synthesis on the next solver and would also fix this; this PR is a targeted fix for the current implementation in the meantime.

The related #110741 hits the same panic through an ambiguous projection clause (FnOnce::Output), which add_user_clause does not deduplicate at all; that flavor is not addressed here.

`AutoTraitFinder::add_user_clause` deduplicates discovered clauses that
differ only in their regions, keeping the stricter (higher-ranked) one,
because a `ParamEnv` containing both confuses `SelectionContext`: both
candidates apply, selection reports ambiguity, and the sanity check in
`find_auto_trait_generics` turns that into an ICE ("Unable to fulfill
trait ...: [Ambiguity]").

The dedup only compared regions appearing as direct generic arguments of
the trait ref (`T: Trait<'a>` vs. `for<'b> T: Trait<'b>`). With generic
associated types the differing region can instead sit inside a type
argument: `for<'w> <A as Alloc>::Wired<'w>: Send` and
`<A as Alloc>::Wired<'static>: Send` have unequal self types, so the
dedup bailed out, both clauses landed in the `ParamEnv`, and the final
fulfillment check panicked.

Compare the argument lists with all regions erased instead, and, when
they are equal modulo regions, walk the regions of both argument lists
in lockstep so the existing preference rules also apply to regions
nested inside type arguments.

Fixes rust-lang#144918

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@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. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. T-rustdoc-frontend Relevant to the rustdoc-frontend team, which will review and decide on the web UI/UX output. labels Aug 8, 2026
@rustbot

rustbot commented Aug 8, 2026

Copy link
Copy Markdown
Collaborator

Thanks for the pull request, and welcome! The Rust Project is excited to review your changes, and you should hear from @JohnTitor (or someone else) some time within the next two weeks.

Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (S-waiting-on-review and S-waiting-on-author) stays updated, invoking these commands when appropriate:

  • @rustbot author: the review is finished, PR author should check the comments and take action accordingly
  • @rustbot review: the author is ready for a review, this PR will be queued again in the reviewer's queue
Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler, types
  • compiler, types expanded to 75 candidates
  • Random selection from 19 candidates

@rustbot

rustbot commented Aug 8, 2026

Copy link
Copy Markdown
Collaborator

⚠️ Warning ⚠️

  • There are issue links (such as #123) in the commit messages of the following commits.
    Please move them to the PR description, to avoid spamming the issues with references to the commit, and so this bot can automatically canonicalize them to avoid issues with subtree.

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

Labels

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. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. T-rustdoc-frontend Relevant to the rustdoc-frontend team, which will review and decide on the web UI/UX output.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

rustdoc: ICE: synthetic auto trait impls: unable to fulfill trait due to ambiguity

3 participants