rust-lang / rust-lang/rust

`&raw const` gets static-promoted in match guard

Open
#144,939 5 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

A-const-eval A-constant-promotion A-patterns C-discussion T-lang
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

I tried this code:

static X: &i32 = unsafe {
    let wut = &*'a: {
        match one() {
            temp if { break 'a &raw const temp } => {}
            _ => {}
        }
        loop {}
    };
    wut
};

const fn one() -> i32 {
    1
}

fn main() {
    println!("{X}");
}

I expected some kind of error, possibly UB. Instead, the code runs fine and prints 1.

It seems that the &raw const temp is somehow causing temp to be lifetime-extended. This seems wrong. Only non-raw referencing should cause lifetime extension or static promotion.

See also https://github.com/rust-lang/rust/issues/142229, where a similar weirdness causes an ICE. cc @RalfJung

This issue was discovered jointly with @danielhenrymantilla

@rustbot labels +A-const-eval +A-patterns

Meta

Reproducible on the rust playground with version 1.91.0-nightly (2025-08-03 f34ba774c78ea32b7c40)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the standalone Rust reproducer and the compiler areas covered by the A-const-eval and A-patterns labels. Determine why the raw const reference in the match guard is lifetime-extended or statically promoted, then add a regression test showing the expected behavior and run the relevant compiler tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.