Visitar URL original
Tweak diagnostic when encountering ambiguous associated items at the type level by estebank · Pull Request #165207 · rust-lang/rust · GitHub
Skip to content

Tweak diagnostic when encountering ambiguous associated items at the type level - #165207

Open
estebank wants to merge 16 commits into
rust-lang:mainfrom
estebank:ambiguous-context
Open

estebank wants to merge 16 commits into
rust-lang:mainfrom
estebank:ambiguous-context

Conversation

@estebank

@estebank estebank commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

View all comments

The lines showing traits A and B are new:

error[E0221]: ambiguous associated function `f` in bounds of `T`
  --> $DIR/path-ambiguous-late-bound-vars.rs:27:5
   |
LL | trait A {
LL |     fn f() -> impl Sized;
   |     --------------------- ambiguous `f` from `A`
...
LL | trait B {
LL |     fn f<'b>() -> impl Sized;
   |     ------------------------- ambiguous `f` from `B`
...
LL |     T::f(..):,
   |     ^^^^^^^^ ambiguous associated function `f`
   |
help: use fully-qualified syntax to disambiguate
   |
LL -     T::f(..):,
LL +     <T as B>::f(..):,
   |
help: use fully-qualified syntax to disambiguate
   |
LL -     T::f(..):,
LL +     <T as A>::f(..):,
   |

Show the place of the assoc items even when non-local.

On ambiguous assoc type, suggest alternative with a structured suggestion:

help: consider being explicit about the bounds
   |
LL - fn foo<T: Trait<Assoc = u32>>() {}
LL + fn foo<T: Trait<> + Super<i32, Assoc = u32> + Super<u32, Assoc = u32>>() {}
   |

When encountering ambiguous associated type in bounds, provide structured suggestion for where bounds with correct syntax:

help: consider introducing a new type parameter `U` and adding `where` constraints
   |
LL ~ fn foo<T>(_: U, _: T) where
LL +         U: Trait,
LL +         U: Super<i32, Assoc = u32>,
LL +         U: Super<u32, Assoc = u32>
   |

Do not suggest introducing a where bound on local binding.

Fix #97583.

@rustbot

rustbot commented Oct 30, 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 Oct 30, 2026
@rustbot

rustbot commented Oct 30, 2026

Copy link
Copy Markdown
Collaborator

r? @jieyouxu

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

  • Owners of files modified in this PR: compiler
  • compiler expanded to 77 candidates
  • Random selection from 21 candidates

@estebank estebank changed the title [tiny] Provide context when pointing at ambiguous assoc item Tweak diagnostic when encountering ambiguous associated function in trait type Nov 1, 2026
where_bounds.join(",\n"),
));
if !suggestions.is_empty() {
suggestions.push((span, String::new()));

@estebank estebank Nov 1, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This doesn't account for impl Trait<A=(), B=()>, which would leave impl Trait<, B=()> in the suggested code.

View changes since the review

@fmease

fmease commented Nov 1, 2026 •

Copy link
Copy Markdown
Member

You seem to be in the process of fixing #97583. Note that I've never completed the work because there are so many edge cases to consider.

My WIP branch is / was: main...fmease:rust:fix-diag-sugg-on-ambig-assoc-item-constraint.

See the (messy) test file in that patch for some edge cases (but feel free to avert your eyes, it wasn't meant for others to see). IIRC I gave up several times trying to compute the correct higher-ranked binders (for<...>) for the structured suggestion and maybe due to some other things, too.

@fmease fmease assigned fmease and unassigned jieyouxu Nov 1, 2026
@estebank

estebank commented Nov 1, 2026

Copy link
Copy Markdown
Contributor Author

@fmease I'll try to incorporate as much as possible both on this PR and follow ups. When it comes to the trickiest structured suggestions, I think a plain message as a fallback is reasonable instead of burn out from san loss.

@estebank

estebank commented Nov 1, 2026

Copy link
Copy Markdown
Contributor Author

To reviewer: Review per commit, some of the changes are independent of each other, but were noticed while seeing the diagnostics change, and separate PRs will conflict. I can split things out as needed.

@fmease fmease changed the title Tweak diagnostic when encountering ambiguous associated function in trait type Tweak diagnostic when encountering ambiguous associated items at the type level Nov 2, 2026
@estebank

estebank commented Nov 2, 2026

Copy link
Copy Markdown
Contributor Author

@fmease would you mind taking a look? I haven't fully handled the suggestions in the face of malformed input (could just hide the suggestion in that case, or even the whole error). I'm pretty happy with the results, but you'll have a fresh perspective given you've been looking at the problem already.

@estebank estebank Nov 2, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

We could point at type Bar here. Also, E0191 also needs context Span. Edit: I'd done that already ^_^'

View changes since the review

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

The lines showing traits A and B are new:

```
error[E0221]: ambiguous associated function `f` in bounds of `T`
  --> $DIR/path-ambiguous-late-bound-vars.rs:27:5
   |
LL | trait A {
LL |     fn f() -> impl Sized;
   |     --------------------- ambiguous `f` from `A`
...
LL | trait B {
LL |     fn f<'b>() -> impl Sized;
   |     ------------------------- ambiguous `f` from `B`
...
LL |     T::f(..):,
   |     ^^^^^^^^ ambiguous associated function `f`
   |
help: use fully-qualified syntax to disambiguate
   |
LL -     T::f(..):,
LL +     <T as B>::f(..):,
   |
help: use fully-qualified syntax to disambiguate
   |
LL -     T::f(..):,
LL +     <T as A>::f(..):,
   |
```
Provide structured suggestion:

```
help: consider being explicit about the bounds
   |
LL - fn foo<T: Trait<Assoc = u32>>() {}
LL + fn foo<T: Trait<> + Super<i32, Assoc = u32> + Super<u32, Assoc = u32>>() {}
   |
```

When suggesting to add a `where` clause, use proper syntax:

```
   = help: consider introducing a new type parameter `T` and adding `where` constraints:
               where
                   T: BoxCar,
                   T: Vehicle<Color = COLOR>,
                   T: Box<Color = COLOR>
```
When encountering ambiguous associated type in bounds, provide structured suggestion:

```
help: consider introducing a new type parameter `U` and adding `where` constraints
   |
LL ~ fn foo<T>(_: U, _: T) where
LL +         U: Trait,
LL +         U: Super<i32, Assoc = u32>,
LL +         U: Super<u32, Assoc = u32>
   |
```
Make it so the suggestion message still has all information even without the suggested code being visible.
Do not leave stray commas behind even if there are type parameters after assoc type.

```
help: consider being explicit about the bounds
   |
LL -     fn f(_: impl C<T = (), bool>) {}
LL +     fn f(_: impl C<bool> + with_args_after::B<(bool,), T = ()> + with_args_after::module::A<((bool,),), T = ()>) {}
   |
```
@rustbot

rustbot commented Nov 2, 2026

Copy link
Copy Markdown
Collaborator

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.

@rust-log-analyzer

This comment has been minimized.

```
error[E0222]: ambiguous associated type `T` in bounds of `gat::C`
  --> $DIR/dyn-trait-in-param-or-body.rs:85:20
   |
LL |     trait A { type T<Q, K>; }
   |               ------------ ambiguous `T` from `gat::A`
LL |     trait B: A { type T<Q>; }
   |                  --------- ambiguous `T` from `gat::B`
...
LL |     fn f(_: impl C<T<i32> = ()>) {}
   |                    ^^^^^^^^^^^ ambiguous associated type `T`
   |
help: consider being explicit about the bounds
   |
LL -     fn f(_: impl C<T<i32> = ()>) {}
LL +     fn f(_: impl C + gat::B<T<i32> = ()> + gat::A<T<i32> = ()>) {}
   |
```
@rustbot

rustbot commented Nov 2, 2026

Copy link
Copy Markdown
Collaborator

These commits modify the Cargo.lock file. Unintentional changes to Cargo.lock can be introduced when switching branches and rebasing PRs.

If this was unintentional then you should revert the changes before this PR is merged.
Otherwise, you can ignore this comment.

Comment on lines +556 to +559
let args = rustc_hir_pretty::generic_args_to_string(
&(&tcx as &dyn HirTyCtxt<'_>) as &dyn PpAnn,
constraint.gen_args,
);

@estebank estebank Nov 3, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

If we want to avoid the new crate dep, we can instead use the textual snippet, which I would still produce applicable suggestions.

View changes since the review

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Doing this gets us some nice benefits for free: bounds and equality terms are both handled the same way, we don't need to evaluate the term, we don't need the new dep to handle GATs, and we get around the incorrect rendering of consts using the gca! macro.

This branch has not been deployed

No deployments
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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ambiguous assoc types result in incorrect suggestion

5 participants