Skip to content

ambiguous assoc types result in incorrect suggestion #97583

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
lcnr opened this issue May 31, 2022 · 1 comment
Open

ambiguous assoc types result in incorrect suggestion #97583

lcnr opened this issue May 31, 2022 · 1 comment
Assignees
Labels
A-associated-items Area: Associated items (types, constants & functions) A-diagnostics Area: Messages for errors, warnings, and lints D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@lcnr
Copy link
Contributor

lcnr commented May 31, 2022

trait Super<T> {
    type Assoc;
}
trait Trait: Super<u32> + Super<i32> {}

fn foo<T: Trait<Assoc = u32>>() {}
error[[E0222]](https://doc.rust-lang.org/stable/error-index.html#E0222): ambiguous associated type `Assoc` in bounds of `Trait`
 --> src/lib.rs:6:17
  |
2 |     type Assoc;
  |     -----------
  |     |
  |     ambiguous `Assoc` from `Super<i32>`
  |     ambiguous `Assoc` from `Super<u32>`
...
6 | fn foo<T: Trait<Assoc = u32>>() {}
  |                 ^^^^^^^^^^^ ambiguous associated type `Assoc`
  |
  = help: consider introducing a new type parameter `T` and adding `where` constraints:
              where
                  T: Trait,
                  T: Super<i32>::Assoc = u32,
                  T: Super<u32>::Assoc = u32

and

trait Super<T> {
    type Assoc;
}
trait Trait: Super<u32> + Super<i32> {}

fn main() {
    let _: dyn Trait<Assoc = u32>;
}
error[[E0222]](https://doc.rust-lang.org/stable/error-index.html#E0222): ambiguous associated type `Assoc` in bounds of `Trait`
 --> src/main.rs:7:22
  |
2 |     type Assoc;
  |     -----------
  |     |
  |     ambiguous `Assoc` from `Super<i32>`
  |     ambiguous `Assoc` from `Super<u32>`
...
7 |     let _: dyn Trait<Assoc = u32>;
  |                      ^^^^^^^^^^^ ambiguous associated type `Assoc`
  |
  = help: consider introducing a new type parameter `T` and adding `where` constraints:
              where
                  T: Trait,
                  T: Super<i32>::Assoc = u32,
                  T: Super<u32>::Assoc = u32

error[[E0191]](https://doc.rust-lang.org/stable/error-index.html#E0191): the value of the associated type `Assoc` (from trait `Super`) must be specified
 --> src/main.rs:7:16
  |
2 |     type Assoc;
  |     ----------- `Assoc` defined here
...
7 |     let _: dyn Trait<Assoc = u32>;
  |                ^^^^^^^^^^^^^^^^^^ help: specify the associated type: `Trait<Assoc = u32, Assoc = Type>`

Trying to write T: Super<i32>::Assoc = u32 results in a syntax error.

@lcnr lcnr added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels May 31, 2022
@lcnr lcnr changed the title ambiguous assoc types result in bogus suggestion ambiguous assoc types result in incorrect suggestion May 31, 2022
@estebank estebank added A-associated-items Area: Associated items (types, constants & functions) D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. labels Jun 2, 2022
@fmease
Copy link
Member

fmease commented Nov 19, 2023

For the first case (where-clauses, impl-Trait) it could suggest (duplicating the equality bound):

- Trait<Assoc = u32>
+ Trait + Super<u32, Assoc = u32> + Super<i32, Assoc = u32>

For dyn-Trait, it shouldn't suggest anything and maybe explain that what the user intends to write can't be expressed (we can't disentangle the bounds like above as dyn-Trait can only have one non-auto / principal trait).

@fmease fmease self-assigned this Nov 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-associated-items Area: Associated items (types, constants & functions) A-diagnostics Area: Messages for errors, warnings, and lints D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

3 participants