Match guard can both move and static-promote a single constant
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 119k
- Forks
- 16.1k
- PR merge metrics
- PR metrics pending
Description
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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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