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

Clarify error on ambiguous associated type ("Try using Iterator" instead of "X as Trait") #46585

Closed
binary132 opened this issue Dec 8, 2017 · 6 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. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. E-mentor Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. WG-diagnostics Working group: Diagnostics

Comments

@binary132
Copy link

Hi, I think this error message is unclear.

https://play.rust-lang.org/?gist=5b08c55cf63a625ced0cbaf83b4db0de&version=stable

The solution is here:

https://play.rust-lang.org/?gist=3ce26cc2d8cbd16533b884a1f8f76900&version=stable

@oli-obk suggested mentoring a PR to add a suggestion for possible trait types in the impl. This Issue serves as a starting point.

@binary132 binary132 changed the title Clarify error on associated type ("Try using Iterator" instead of "X as Trait") Clarify error on associated type ("Try using Iterator" instead of "X as Trait") Dec 8, 2017
@binary132 binary132 changed the title Clarify error on associated type ("Try using Iterator" instead of "X as Trait") Clarify error on ambiguous associated type ("Try using Iterator" instead of "X as Trait") Dec 8, 2017
@binary132
Copy link
Author

binary132 commented Dec 8, 2017

Thought: if Self implements Iterator, and Iterator fulfills the requirement, then the ambiguity can be resolved automatically, right?

@oli-obk
Copy link
Contributor

oli-obk commented Dec 8, 2017

The idea is that both

error[E0576]: cannot find associated type `Item` in trait `Trait`
  --> src/main.rs:23:54
   |
23 |     fn get_next(&mut self) -> Option<<Foo as Trait>::Item> {
   |                                                      ^^^^ not found in `Trait`

and

error[E0433]: failed to resolve. Use of undeclared type or module `Trait`
  --> src/main.rs:21:46
   |
21 |     fn get_next(&mut self) -> Option<<Foo as Trait>::Item> {
   |                                              ^^^^^ Use of undeclared type or module `Trait`

crawl the list of traits that Foo implements, find any with the appropriate item, and suggest them.

The error is reported in

(format!("cannot find {} `{}` in {}{}", expected, item_str, mod_prefix, mod_str),

Since this error is reported during name resolution, I don't think it is possible to figure out all the traits that the type implements. Instead we can crawl the AST to find all impl SomeTrait for Foo, check their item names and then suggest them.

@estebank estebank added A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. E-mentor Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion. WG-diagnostics Working group: Diagnostics labels Dec 8, 2017
@Pulkit07
Copy link
Contributor

Hey, I am going to try fix this.

@estebank
Copy link
Contributor

Current output:

  • When using <Self as Trait>::Item, only one error:
error[E0433]: failed to resolve: use of undeclared type or module `Trait`
  --> src/main.rs:23:46
   |
23 |     fn get_next(&mut self) -> Option<<Foo as Trait>::Item> {
   |                                              ^^^^^ use of undeclared type or module `Trait`
  • When using Self or Foo:
error[E0223]: ambiguous associated type
  --> src/main.rs:23:38
   |
23 |     fn get_next(&mut self) -> Option<Self::Item> {
   |                                      ^^^^^^^^^^ help: use fully-qualified syntax: `<Foo as Trait>::Item`

@oli-obk's idea to crawl the traits implemented for Foo and suggest the one(s) that have Item defined is still applicable.

@estebank estebank added D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` labels Oct 15, 2019
@estebank estebank added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Jan 22, 2020
@andronat
Copy link

Any updates on this? I recently stumbled upon this issue when I was trying to implement some trait for mockall.

@estebank
Copy link
Contributor

estebank commented Jan 8, 2023

@andronat #106585

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Jan 13, 2023
…rrors

When suggesting writing a fully qualified path probe for appropriate types

Address the more common part of rust-lang#46585.
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Jan 13, 2023
…rrors

When suggesting writing a fully qualified path probe for appropriate types

Address the more common part of rust-lang#46585.
@bors bors closed this as completed in 12ddf77 Jan 14, 2023
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. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. E-mentor Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. WG-diagnostics Working group: Diagnostics
Projects
None yet
Development

No branches or pull requests

5 participants