Skip to content

"only named lifetimes allowed in impl Trait" could have a better error message #43719

Closed
@Manishearth

Description

@Manishearth
struct A {
    x: [(u32, u32); 10]
}

impl A {
    
    fn iter_values(&self) -> impl Iterator<Item=u32>{
        self.x.iter().map(|a| a.0)
    }
}

gives the error:

error[E0564]: only named lifetimes are allowed in `impl Trait`, but `` was found in the type `std::iter::Map<std::slice::Iter<'_, (u32, u32)>, [closure@src/main.rs:12:27: 12:34]>`
  --> src/main.rs:11:30
   |
11 |     fn iter_values(&self) -> impl Iterator<Item=u32>{
   |                              ^^^^^^^^^^^^^^^^^^^^^^^

The correct fix is fn iter_values<'a>(&'a self) -> impl Iterator<Item=u32> + 'a (or use into_iter, but that's not always an option), however this error message has no indication that this is the way to go in, in fact not many people are aware of the Trait + 'a syntax. We probably can do better.

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.C-enhancementCategory: An issue proposing an enhancement or a PR with one.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.WG-diagnosticsWorking group: Diagnostics

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions