rust icon indicating copy to clipboard operation
rust copied to clipboard

Lifetime may not live enough regression

Open iamsauravsharma opened this issue 1 year ago • 7 comments

Code

I tried this code:

struct Test {
    field: Vec<&'static str>
}

impl Test {
    fn field<'field>(&self) -> impl Iterator<Item = &&'field str> {
        self.field.iter()
    }
}

I expected to see this happen: no compilation error Instead, this happened:

error: lifetime may not live long enough
  --> src/lib.rs:21:9
   |
20 |     fn field<'field>(&self) -> impl Iterator<Item = &&'field str> {
   |              ------  - let's call the lifetime of this reference `'1`
   |              |
   |              lifetime `'field` defined here
21 |         self.field.iter()
   |         ^^^^^^^^^^^^^^^^^ method was supposed to return data with lifetime `'1` but it is returning data with lifetime `'field`

error: could not compile `playground` (lib) due to 1 previous error

It most recently works on nightly-2024-03-06 but returns error on nightly-2024-03-07

iamsauravsharma avatar Mar 09 '24 10:03 iamsauravsharma

I believe this should be due to the soundness fix in #121679.

lqd avatar Mar 09 '24 14:03 lqd

Yes, that PR was a follow up to https://github.com/rust-lang/rust/pull/115008#issuecomment-1757775356

Your code example can be used unsoundly, even if a smarter analysis could infer the right lifetimes.

We should have cratered the change and opened a fix PR against your repo, apologies about the unannounced breakage. I'll start a crater run now

oli-obk avatar Mar 10 '24 12:03 oli-obk

@craterbot run name=opaque-soundness-fix start=80399064afa4a2cd153f30d02c25f7ea0383ed65 end=09bc67b9158392361780e779d32997f14cc75c39 mode=check

oli-obk avatar Mar 10 '24 12:03 oli-obk

:rotating_light: Error: failed to parse the command

:sos: If you have any trouble with Crater please ping @rust-lang/infra! :information_source: Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more

craterbot avatar Mar 10 '24 12:03 craterbot

@craterbot run name=opaque-soundness-fix start=80399064afa4a2cd153f30d02c25f7ea0383ed65 end=09bc67b9158392361780e779d32997f14cc75c39 mode=check-only

oli-obk avatar Mar 10 '24 12:03 oli-obk

:ok_hand: Experiment opaque-soundness-fix created and queued. :mag: You can check out the queue and this experiment's details.

:information_source: Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more

craterbot avatar Mar 10 '24 12:03 craterbot

No problem, I noticed this issue only in one of the recently released updates of third-party crates that I've been using. I've already created PR for fix, cedar-policy/cedar#712, for it.

iamsauravsharma avatar Mar 10 '24 13:03 iamsauravsharma

:construction: Experiment opaque-soundness-fix is now running

:information_source: Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more

craterbot avatar Mar 18 '24 05:03 craterbot

@craterbot abort

Mark-Simulacrum avatar Mar 18 '24 15:03 Mark-Simulacrum

:wastebasket: Experiment opaque-soundness-fix deleted!

:information_source: Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more

craterbot avatar Mar 18 '24 15:03 craterbot

@craterbot run name=opaque-soundness-fix start=master#80399064afa4a2cd153f30d02c25f7ea0383ed65 end=master#09bc67b9158392361780e779d32997f14cc75c39 mode=check-only

Mark-Simulacrum avatar Mar 18 '24 15:03 Mark-Simulacrum

:ok_hand: Experiment opaque-soundness-fix created and queued. :mag: You can check out the queue and this experiment's details.

:information_source: Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more

craterbot avatar Mar 18 '24 15:03 craterbot

:construction: Experiment opaque-soundness-fix is now running

:information_source: Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more

craterbot avatar Mar 23 '24 05:03 craterbot

:tada: Experiment opaque-soundness-fix is completed! :bar_chart: 9 regressed and 1 fixed (428380 total) :newspaper: Open the full report.

:warning: If you notice any spurious failure please add them to the blacklist! :information_source: Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more

craterbot avatar Mar 24 '24 23:03 craterbot

I opened PRs against all affected crates. The original reporter's crate did not show up since it was already fixed before crater started running

oli-obk avatar Mar 25 '24 15:03 oli-obk