Skip to content

Lifetime error with #![feature(nll)] and RPIT gives bad suggestion #73159

Closed
@Aaron1011

Description

@Aaron1011

The following code:

#![feature(nll)]

struct Foo<'a>(&'a [u8]);

impl<'a> Foo<'a> {
    fn make_it(&self) -> impl Iterator<Item = u8> {
        self.0.iter().copied()
    }
}

gives the following error:

error: lifetime may not live long enough
 --> src/lib.rs:6:26
  |
5 | impl<'a> Foo<'a> {
  |      -- lifetime `'a` defined here
6 |     fn make_it(&self) -> impl Iterator<Item = u8> {
  |                          ^^^^^^^^^^^^^^^^^^^^^^^^ opaque type requires that `'a` must outlive `'static`
  |
  = help: consider replacing `'a` with `'static`

Suggsting that the user change the impl to impl Foo<'static> is usually the wrong thing to do - it's more likely that the user wants to write impl Iterator<Item = u8> + 'a

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-lifetimesArea: Lifetimes / regionsC-bugCategory: This is a bug.NLL-diagnosticsWorking towards the "diagnostic parity" goalT-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