Skip to content

Match guard can both move and static-promote a single constant #145237

@theemathas

Description

@theemathas

I'm not sure if this is a bug or not.

I expected the below code to produce a compile error, but instead it compiles fine.

struct Thing;

fn foo() -> &'static Thing {
    let y;
    match Thing {
        x if { y = &x; true } => {
            // This drop should invalidate y
            drop(x);
            y
        }
        _ => {
            panic!();
        }
    }
}

It seems that the single Thing constant is both moved and static-promoted. This seems very strange.

Related to #144939. Discovered in #141295 (comment).

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

Meta

Reproduces on the playground with version 1.91.0-nightly (2025-08-09 ca77504943887037504c)

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-borrow-checkerArea: The borrow checkerA-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)A-patternsRelating to patterns and pattern matchingC-discussionCategory: Discussion or questions that doesn't represent real issues.I-lang-nominatedNominated for discussion during a lang team meeting.I-lang-radarItems that are on lang's radar and will need eventual work or consideration.P-lang-drag-2Lang team prioritization drag level 2.https://rust-lang.zulipchat.com/#narrow/channel/410516-t-lang.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