Skip to content

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

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

Activity

  1. added
    A-diagnosticsArea: Messages for errors, warnings, and lints
    A-lifetimesArea: Lifetimes / regions
    C-bugCategory: This is a bug.
    NLL-diagnosticsWorking towards the "diagnostic parity" goal
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    on Jun 9, 2020
  2. added 2 commits that reference this issue on Oct 5, 2021
    2c1c140
    bf62c6d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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