Skip to content

Borrowed value does not live long enough beta regression #101822

@marmeladema

Description

@marmeladema

Code

I tried this code:

use std::collections::BTreeMap; struct Context<'e> { _data: &'e [u8], } pub struct Rule<'s> { _rule: Box<dyn for<'e> Fn(&'e Context<'e>) -> bool + Sync + Send + 's>, } pub struct Scheme {} pub struct Parser<'s> { _scheme: &'s Scheme, } impl<'s> Parser<'s> { pub fn new(_scheme: &'s Scheme) -> Self { Self { _scheme } } } #[derive(Default)] pub struct Config<'s> { _data: BTreeMap<String, Rule<'s>>, } impl<'s> Config<'s> { pub fn update(&mut self, _: Parser<'s>) {} } pub fn main() { let mut configuration = Config::default(); let scheme = Scheme {}; let parser = Parser::new(&scheme); configuration.update(parser); }

I expected to see this happen: it compiles successfully

Instead, this happened:

error[[E0597]](https://doc.rust-lang.org/beta/error-index.html#E0597): `scheme` does not live long enough --> src/main.rs:35:30 | 35 | let parser = Parser::new(&scheme); | ^^^^^^^ borrowed value does not live long enough ... 38 | } | - | | | `scheme` dropped here while still borrowed | borrow might be used here, when `configuration` is dropped and runs the destructor for type `Config<'_>` | = note: values in a scope are dropped in the opposite order they are defined For more information about this error, try `rustc --explain E0597`. 

Beta: https://play.rust-lang.org/?version=beta&mode=debug&edition=2021&gist=3f293aff3e5dc16ad303940d4613514a
Stable: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=3f293aff3e5dc16ad303940d4613514a

Version it worked on

It most recently worked on: 1.63.0 (but probably before too)

$ rustc --version --verbose rustc 1.63.0 (4b91a6ea7 2022-08-08) binary: rustc commit-hash: 4b91a6ea7258a947e59c6522cd5898e7c0a6a88f commit-date: 2022-08-08 host: x86_64-unknown-linux-gnu release: 1.63.0 LLVM version: 14.0.5 

Version with regression

rustc --version --verbose:

$ rustc +beta --version --verbose rustc 1.64.0-beta.6 (25912c097 2022-09-09) binary: rustc commit-hash: 25912c09710a2de2a97b4904e9b528b73439927d commit-date: 2022-09-09 host: x86_64-unknown-linux-gnu release: 1.64.0-beta.6 LLVM version: 14.0.6 

This might be an acceptable breakage but we faced this regression in production code so I figured it might be worthwhile creating a bug report for this in case it was an unnoticed regression.

@rustbot modify labels: +regression-from-stable-to-beta -regression-untriaged

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.regression-from-stable-to-betaPerformance or correctness regression from stable to beta.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions