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

[confusing error message] iterate over str #46216

Closed
gnzlbg opened this issue Nov 23, 2017 · 5 comments · Fixed by #47613
Closed

[confusing error message] iterate over str #46216

gnzlbg opened this issue Nov 23, 2017 · 5 comments · Fixed by #47613
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-resolve Area: Path resolution C-enhancement Category: An issue proposing an enhancement or a PR with one. WG-diagnostics Working group: Diagnostics

Comments

@gnzlbg
Copy link
Contributor

gnzlbg commented Nov 23, 2017

Given:

fn main() {
    for c in "foobarbaz" {
        println!("{}", c);
    }
}

Rust produces (playground):

error[E0277]: the trait bound `&str: std::iter::Iterator` is not satisfied
 --> src/main.rs:2:5
  |
2 | /     for c in "foobarbaz" {
3 | |         println!("{}", c);
4 | |     }
  | |_____^ `&str` is not an iterator; maybe try calling `.iter()` or a similar method
  |
  = help: the trait `std::iter::Iterator` is not implemented for `&str`
  = note: required by `std::iter::IntoIterator::into_iter`

The suggestion says iter or a similar method, but &str doesn't have an .iter() method (that's why it says "or a similar method").

I don't think this is good enough.

It should scan the methods of str that return something that implements Iterator (or can be auto-deref to something that implements it) and suggest those. In this case, chars(), bytes(),... The error message should look more like this:

error[E0277]: the trait bound `&str: std::iter::Iterator` is not satisfied
 --> src/main.rs:2:5
  |
2 | /     for c in "foobarbaz" {
3 | |         println!("{}", c);
4 | |     }
  | |_____^ `&str` is not an iterator
  |
  = help: the trait `std::iter::Iterator` is not implemented for `&str`
  = note: required by `std::iter::IntoIterator::into_iter`

The following methods of `&str` return an `Iterator`:
  - `chars`
  - `bytes`
  - ... 
@kennytm kennytm added A-diagnostics Area: Messages for errors, warnings, and lints A-resolve Area: Path resolution C-enhancement Category: An issue proposing an enhancement or a PR with one. labels Nov 23, 2017
@GuillaumeGomez
Copy link
Member

Sounds like an extension of the very controversial methods suggestion. But why not? If other people agree on it, I can add it.

@zackmdavis
Copy link
Member

(Given that for loops automatically invoke .into_iter(), it's not clear that we should ever recommend .iter() here. (That is, most of the time this "maybe try calling .iter() or a similar method" message appears, .iter() won't exist, because if it did, the type likely already implements IntoIterator too and we wouldn't be in this situation.))

@zackmdavis
Copy link
Member

The message comes from rustc_on_unimplemented, so this would seem to be dependent on #44755.

@est31
Copy link
Member

est31 commented Nov 24, 2017

I would like such a message

@estebank estebank added E-needs-mentor WG-diagnostics Working group: Diagnostics labels Dec 7, 2017
Manishearth added a commit to Manishearth/rust that referenced this issue Feb 7, 2018
…nikomatsakis

Add filtering options to `rustc_on_unimplemented`

- Add filtering options to `rustc_on_unimplemented` for local traits, filtering on `Self` and type arguments.
- Add a way to provide custom notes.
- Tweak binops text.
- Add filter to detect wether `Self` is local or belongs to another crate.
- Add filter to `Iterator` diagnostic for `&str`.

Partly addresses rust-lang#44755 with a different syntax, as a first approach. Fixes rust-lang#46216, fixes rust-lang#37522, CC rust-lang#34297, rust-lang#46806.
@estebank
Copy link
Contributor

error[E0277]: the trait bound `&str: std::iter::Iterator` is not satisfied
 --> src/main.rs:2:14
  |
2 |     for c in "foobarbaz" {
  |              ^^^^^^^^^^^ `&str` is not an iterator; try calling `.chars()` or `.bytes()`
  |
  = help: the trait `std::iter::Iterator` is not implemented for `&str`
  = note: required by `std::iter::IntoIterator::into_iter`

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-resolve Area: Path resolution C-enhancement Category: An issue proposing an enhancement or a PR with one. WG-diagnostics Working group: Diagnostics
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants