rust-lang / rust-lang/rust

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

Open
#145,237 28 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

View all comments

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 https://github.com/rust-lang/rust/pull/141295#issuecomment-3173106871.

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

Meta

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

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 by reproducing the example on the Rust nightly version noted in the issue, then read related issue #144939 and the discussion in PR #141295. Trace how match guards, moves, and static promotion interact, and establish whether this should be rejected. Done means the behavior is resolved and covered by a compiler regression test.

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
Quiet
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.