Skip to content
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

Improve the help message for the error on using fully qualified syntax for nested associated types #59225

Open
salmans opened this issue Mar 16, 2019 · 3 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`. C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@salmans
Copy link

salmans commented Mar 16, 2019

Here is a small example to reproduce the error and its help message:

trait A {
    type Item;
}

trait B {
    type Item: A;

    fn trait_fn(x: Self::Item::Item);
}

Then I get the following error:

error[E0223]: ambiguous associated type
 --> src/main.rs:8:20
  |
8 |     fn trait_fn(x: Self::Item::Item);
  |                    ^^^^^^^^^^^^^^^^ help: use fully-qualified syntax: `<<Self as B>::Item as Trait>::Item`

It looks like the word "Trait" in <<Self as B>::Item as Trait>::Item should be replaced with the trait name A so that the suggested expression compiles:
help: use fully-qualified syntax: <<Self as B>::Item as A>::Item.

@Centril Centril added A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Mar 16, 2019
@JohnTitor
Copy link
Member

I think this error is occured here:

self.report_ambiguous_associated_type(span,
&qself_ty.to_string(),
"Trait",
&assoc_ident.as_str());

We can replace this "Trait", but I don't know what to use.(If someone tells me that, I'll improve wording and open a PR.)

zackmdavis added a commit to zackmdavis/rust that referenced this issue Jul 6, 2019
We have the trait at this point, so we can name it in the error
message, rather than using "Trait" as a (potentially confusing)
placeholder.

Thanks to Yuki "@JohnTitor" Okushi for pointing out where to look (in
the same file) for a closely related issue for ambiguous associated
types (as opposed to items; that was rust-lang#59225, except that one won't be
quite as easy to resolve, because we actually don't have the trait
`DefId` at that point).
Centril added a commit to Centril/rust that referenced this issue Jul 7, 2019
…_assoc_item, r=petrochenkov

name the trait in ambiguous-associated-items fully qualified suggestion

We have the trait at this point, so we can name it in the error message, rather than using "Trait" as a (potentially confusing) placeholder.

Thanks to Yuki "@JohnTitor" Okushi for pointing out where to look (in the same file) for a closely related issue for ambiguous associated types (as opposed to items; that was rust-lang#59225, except that one won't be
quite as easy to resolve, because we actually don't have the trait `DefId` at that point).

r? @petrochenkov
@crlf0710 crlf0710 added the C-enhancement Category: An issue proposing an enhancement or a PR with one. label Jun 11, 2020
@pickfire
Copy link
Contributor

pickfire commented Feb 1, 2021

I got something similar while playing with diesel. At the same time the error also seemed somewhat incorrect to me? Is this similar here or considered a different issue?

error[E0223]: ambiguous associated type
  --> src/main.rs:19:45
   |
19 |     let query = diesel::update(posts.filter(posts::columns::id.eq_any(subquery)))
   |                                             ^^^^^^^^^^^^^^^^^^ help: use fully-qualified syntax: `<hello::schema::posts::table as Trait>::columns`

I was a bit confused what can I do here, it would be good to have some suggestions on what kind of traits could be use for as Trait, or maybe the error message is incorrect. posts::columns::id is a unit struct. I don't quite recall the step to reproduce since I got it working already.

@jmstudyacc
Copy link

jmstudyacc commented Mar 20, 2021

On enums3 of the Rustlings exercises you can replicate this error with the following:

enum Message {
    // TODO: implement the message variant types based on their usage below
    ChangeColor(State::color),
    Echo(String),
    Move(Point),
    Quit,

}

struct Point {
    x: u8,
    y: u8,
}

struct State {
    color: (u8, u8, u8),
    position: Point,
    quit: bool,
}

Will produce:

error[E0223]: ambiguous associated type
  --> exercises/enums/enums3.rs:21:13
   |
21 |     color: (State::color),
   |             ^^^^^^^^^^^^ help: use fully-qualified syntax: `<State as Trait>::color`

It would be great to get a bit more guidance on the reasoning.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`. C-enhancement Category: An issue proposing an enhancement or a PR with one. 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

6 participants