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

Handle type error caused by returning specific type that would be solved by using impl Trait #61271

Closed
estebank opened this issue May 28, 2019 · 1 comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-impl-trait Area: impl Trait. Universally / existentially quantified anonymous types with static dispatch. 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. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@estebank
Copy link
Contributor

estebank commented May 28, 2019

https://mobile.twitter.com/mountain_ghosts/status/1133339596958392320

When encountering a type error on the return expression not matching the return type, which is a type argument for a trait, try to evaluate the return expression as if the return type had been impl of the expected trait. If that would resolve the problem, suggest it.

error[E0308]: mismatched types
 --> src/lib.rs:5:5
  |
1 |   fn permute<'a, T, I>(sides: &'a [T]) -> I
  |                     - this type parameter - expected `I` because of return type
...
5 | /     sides.iter().enumerate().map(|(i, _)| {
6 | |         let window = sides.iter().cycle().skip(i);
7 | |         window.take(sides.len()).collect()
8 | |     })
  | |______^ expected type parameter `I`, found struct `std::iter::Map`
  |
  = note: expected type parameter `I`
                     found struct `std::iter::Map<std::iter::Enumerate<std::slice::Iter<'_, T>>, [closure@src/lib.rs:5:34: 8:6 sides:_]>`
@estebank estebank added C-enhancement Category: An issue proposing an enhancement or a PR with one. A-diagnostics Area: Messages for errors, warnings, and lints A-impl-trait Area: impl Trait. Universally / existentially quantified anonymous types with static dispatch. A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`. labels May 28, 2019
@estebank estebank added the D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. label Oct 18, 2019
@estebank estebank added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Jan 21, 2020
@estebank
Copy link
Contributor Author

error[[E0308]](https://doc.rust-lang.org/nightly/error-index.html#E0308): mismatched types
 --> src/lib.rs:5:5
  |
1 |   fn permute<'a, T, I>(sides: &'a [T]) -> I
  |                     -                     -
  |                     |                     |
  |                     |                     expected `I` because of return type
  |                     this type parameter   help: consider using an impl return type: `impl Iterator<Item = Vec<&'a T>>`
...
5 | /     sides.iter().enumerate().map(|(i, _)| {
6 | |         let window = sides.iter().cycle().skip(i);
7 | |         window.take(sides.len()).collect()
8 | |     })
  | |______^ expected type parameter `I`, found struct `Map`
  |
  = note: expected type parameter `I`
                     found struct `Map<Enumerate<std::slice::Iter<'_, T>>, [closure@src/lib.rs:5:34: 5:42]>`

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-impl-trait Area: impl Trait. Universally / existentially quantified anonymous types with static dispatch. 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. 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

1 participant