Skip to content

Static lifetimes checking regression in rustc 1.41.0 #69114

Closed
@ilyavennik

Description

@ilyavennik

I tried this code:

static FOO: u8 = 42;
static mut BAR: &'static u8 = &FOO;

fn main() {
    unsafe {
        println!("{}", BAR);
        foo();
        println!("{}", BAR);
    }
}

fn foo() {
    let n = 42;
    unsafe {
        BAR = &n;
    }
}

I expected to see this:

error[E0597]: `n` does not live long enough
  --> src/main.rs:15:15
   |
15 |         BAR = &n;
   |         ------^^
   |         |     |
   |         |     borrowed value does not live long enough
   |         assignment requires that `n` is borrowed for `'static`
16 |     }
17 | }
   | - `n` dropped here while still borrowed

error: aborting due to previous error

For more information about this error, try `rustc --explain E0597`.

Instead, happens undefined behavior. Program successfully compiles in the Rust stable 1.41.0 but fails to compile in stable 1.40.0 (thats what I want).

Metadata

Metadata

Assignees

Labels

A-borrow-checkerArea: The borrow checkerA-lifetimesArea: Lifetimes / regionsC-bugCategory: This is a bug.I-unsoundIssue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/SoundnessP-highHigh 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