Skip to content

Wrong lifetime error on nightly 1.67.0-nightly (ff8c8dfbe 2022-11-22) #104815

Closed
rust-skia/rust-skia
#740
@la10736

Description

@la10736

Some rstest users report an error on nightly-2022-11-22 (see la10736/rstest#169 ).
I narrowed down the regression to the following code:

Code

I tried this code:

struct It;

struct Data {
    items: Vec<It>,
}

impl Data {
    fn new() -> Self {
        Self {
            items: vec![It, It],
        }
    }

    fn content(&self) -> impl Iterator<Item = &It> {
        self.items.iter()
    }
}

struct Container<'a> {
    name: String,
    resolver: Box<dyn Resolver + 'a>,
}

impl<'a> Container<'a> {
    fn new<R: Resolver + 'a>(name: &str, resolver: R) -> Self {
        Self {
            name: name.to_owned(),
            resolver: Box::new(resolver),
        }
    }
}

trait Resolver {}

impl<R: Resolver> Resolver for &R {}

impl Resolver for It {}

fn get<'a>(mut items: impl Iterator<Item = &'a It>) -> impl Resolver + 'a {
    items.next().unwrap()
}

fn main() {
    let data = Data::new();
    let resolver = get(data.content());

    let _ = ["a", "b"]
        .iter()
        .map(|&n| Container::new(n, &resolver))
        .map(|c| c.name)
        .collect::<Vec<_>>();
}

I expected to see this happen: It should compile

Instead, this happened: not compile

mdamico@miklap:~/dev_random/lifetime$ cargo build
   Compiling lifetime v0.1.0 (/home/mdamico/dev_random/lifetime)
error: `get<'_, impl Iterator<Item = &It>>::{opaque#1}<'_>` does not live long enough
  --> src/main.rs:49:19
   |
49 |         .map(|&n| Container::new(n, &resolver))
   |                   ^^^^^^^^^^^^^^

error: `get<'_, impl Iterator<Item = &It>>::{opaque#1}<'_>` does not live long enough
  --> src/main.rs:49:19
   |
49 |         .map(|&n| Container::new(n, &resolver))
   |                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: could not compile `lifetime` due to 2 previous errors

Version it worked on

It most recently worked on: 1.65.0 stable, 1.66.0-beta.2 (0040709 2022-11-20), nightly-2022-11-21

Version with regression

rustc --version --verbose:

mdamico@miklap:~/dev_random/lifetime$ rustc --version --verbose
rustc 1.67.0-nightly (b7bc90fea 2022-11-21)
binary: rustc
commit-hash: b7bc90fea3b441234a84b49fdafeb75815eebbab
commit-date: 2022-11-21
host: x86_64-unknown-linux-gnu
release: 1.67.0-nightly
LLVM version: 15.0.4

Backtrace

Backtrace

<backtrace>

Metadata

Metadata

Assignees

Labels

C-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.regression-from-stable-to-nightlyPerformance or correctness regression from stable to nightly.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions