Skip to content

impl trait error message around 'static is probably confusing #48467

Closed
@nikomatsakis

Description

@nikomatsakis

The error message for this example seems potentially confusing:

#![feature(dyn_trait)]
#![feature(conservative_impl_trait)]
#![feature(underscore_lifetimes)]

fn a<T>(items: &[T]) -> Box<impl Iterator> {
    Box::new(items.iter()) //~ ERROR cannot infer an appropriate lifetime
}

fn main() { }

I get:

error[E0621]: explicit lifetime required in the type of `items`
 --> src/main.rs:5:29
  |
5 | fn a<T>(items: &[T]) -> Box<impl Iterator> {
  |         -----               ^^^^^^^^^^^^^ lifetime `'static` required
  |         |
  |         consider changing the type of `items` to `&'static [T]`

What's going wrong here is that impl Iterator can only capture lifetimes that it names, and this one doesn't name any lifetimes. It can be fixed by changing to impl Iterator + '_ -- this is probably what we want to suggest? I'm not sure how best to phrase this, suggestions welcome.

cc @cramertj @estebank

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.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions