Skip to content

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

@estebank

Description

@estebank

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:_]>`

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-impl-traitArea: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch.A-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`C-enhancementCategory: An issue proposing an enhancement or a PR with one.D-newcomer-roadblockDiagnostics: Confusing error or lint; hard to understand for new users.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions