Skip to content
New issue

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

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

Already on GitHub? # to your account

Wrong suggestion for struct's type-param that is not iter::Iterator but should #46806

Closed
lukaslueg opened this issue Dec 18, 2017 · 2 comments · Fixed by #54946
Closed

Wrong suggestion for struct's type-param that is not iter::Iterator but should #46806

lukaslueg opened this issue Dec 18, 2017 · 2 comments · Fixed by #54946
Labels
A-diagnostics Area: Messages for errors, warnings, and lints 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

@lukaslueg
Copy link
Contributor

In the following example, Rust 1.22.1 and nightly complain about T of Foobar not being iter::Iterator, which IterWrapper demands.

use std::iter;

struct IterWrapper<T:iter::Iterator> {
    iter: T
}

struct Foobar<T> {
    w: IterWrapper<T>
}

The suggestion consider adding a "where T: std::iter::Iterator" bound is correct, the main suggestion maybe try calling ".iter()" or a similar method is not applicable to this situation, though.

error[E0277]: the trait bound `T: std::iter::Iterator` is not satisfied
 --> src/main.rs:8:5
  |
8 |     w: IterWrapper<T>
  |     ^^^^^^^^^^^^^^^^^ `T` is not an iterator; maybe try calling `.iter()` or a similar method
  |
  = help: the trait `std::iter::Iterator` is not implemented for `T`
  = help: consider adding a `where T: std::iter::Iterator` bound
  = note: required by `IterWrapper`

error: aborting due to previous error

Same problem for an impl<T> Foobar<T>: Rustc suggest to call .iter() on the type-parameter, which is nonsense.

@zackmdavis
Copy link
Member

#44755 could help with this. (Currently, the "maybe try calling .iter()" language is hardcoded rather than depending on what we know about T.)

@pietroalbini pietroalbini added C-enhancement Category: An issue proposing an enhancement or a PR with one. 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 Jan 30, 2018
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

estebank commented Feb 9, 2018

To accomplish this correctly, we need to modify on_unimplemented_note to be able to query wether Self is a type argument, so that we can suggest a trait bound, instead of using iter. We would still be unable to tell the compiler to point Selfs span (that might be another worthwhile improvement to rustc_on_unimplemented).

estebank added a commit to estebank/rust that referenced this issue Oct 10, 2018
 - Detect one element array of `Range` type, which is potentially a typo:
   `for _ in [0..10] {}` where iterating between `0` and `10` was intended.
   (rust-lang#23141)
 - Suggest `.bytes()` and `.chars()` for `String`.
 - Suggest borrowing or `.iter()` on arrays (rust-lang#36391)
 - Suggest using range literal when iterating on integers (rust-lang#34353)
 - Do not suggest `.iter()` by default (rust-lang#50773, rust-lang#46806)
bors added a commit that referenced this issue Oct 15, 2018
Add filtering option to `rustc_on_unimplemented` and reword `Iterator` E0277 errors

 - Add more targetting filters for arrays to `rustc_on_unimplemented` (Fix #53766)
 - Detect one element array of `Range` type, which is potentially a typo:
   `for _ in [0..10] {}` where iterating between `0` and `10` was intended.
   (Fix #23141)
 - Suggest `.bytes()` and `.chars()` for `String`.
 - Suggest borrowing or `.iter()` on arrays (Fix #36391)
 - Suggest using range literal when iterating on integers (Fix #34353)
 - Do not suggest `.iter()` by default (Fix #50773, fix #46806)
 - Add regression test (Fix #22872)
bors added a commit that referenced this issue Oct 17, 2018
Add filtering option to `rustc_on_unimplemented` and reword `Iterator` E0277 errors

 - Add more targetting filters for arrays to `rustc_on_unimplemented` (Fix #53766)
 - Detect one element array of `Range` type, which is potentially a typo:
   `for _ in [0..10] {}` where iterating between `0` and `10` was intended.
   (Fix #23141)
 - Suggest `.bytes()` and `.chars()` for `String`.
 - Suggest borrowing or `.iter()` on arrays (Fix #36391)
 - Suggest using range literal when iterating on integers (Fix #34353)
 - Do not suggest `.iter()` by default (Fix #50773, fix #46806)
 - Add regression test (Fix #22872)
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints 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

Successfully merging a pull request may close this issue.

4 participants