Skip to content

1.79 regression in const temporary lifetime extension #126562

Closed as not planned
Closed as not planned
@CAD97

Description

@CAD97

Code

I tried this code: [playground] [godbolt]

pub struct Dir<'a>(&'a [File<'a>]);
impl<'a> Dir<'a> {
    pub const fn new(x: &'a [File<'a>]) -> Self {
        Self(x)
    }
}

pub struct File<'a>(&'a str);
impl<'a> File<'a> {
    pub const fn new(x: &'a str) -> Self {
        Self(x)
    }
}

pub static OK: Dir<'static> = Dir::new(&[File::new("")]);
pub static ERR: Dir<'static> = if true {
    Dir::new(&[File::new("")]) //~ ERROR: temporary value dropped while borrowed
} else {
    unreachable!()
};

I expected to see this happen: code compiles: temporary is promoted to &'static due to the const evaluation context.

Instead, this happened: code errors: temporary value dropped while borrowed.

Version it worked on

It most recently worked on: 1.78

Version with regression

rustc --version --verbose:

rustc 1.79.0 (129f3b996 2024-06-10)
binary: rustc
commit-hash: 129f3b9964af4d4a709d1383930ade12dfe7c081
commit-date: 2024-06-10
host: x86_64-pc-windows-msvc
release: 1.79.0
LLVM version: 18.1.7

Meta

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

Bisects to #121557

Initially reported on urlo: https://users.rust-lang.org/t/code-compiles-in-1-78-0-but-not-in-1-79-0/113038?u=cad97

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)A-lifetimesArea: Lifetimes / regionsC-bugCategory: This is a bug.P-mediumMedium priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.regression-from-stable-to-stablePerformance or correctness regression from one stable version to another.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions