Skip to content

&raw const gets lifetime-extended / static promoted in match guard #144939

@theemathas

Description

@theemathas

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 #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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)A-patternsRelating to patterns and pattern matchingC-bugCategory: This is a bug.needs-triageThis issue may need triage. Remove it if it has been sufficiently triaged.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions