Skip to content

&raw const gets 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-constant-promotionArea: constant promotionA-patternsRelating to patterns and pattern matchingC-discussionCategory: Discussion or questions that doesn't represent real issues.T-langRelevant to the language team

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions