rust-lang / rust-lang/rust

`ref` patterns can const-promote a single constant more than once, and can both const-promote and move from the same value.

Open
#145,555 18 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-const-eval A-constant-promotion A-patterns C-bug I-lang-radar T-lang
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 tried this code:

#![allow(dead_code)]

struct NotCopy { data: i32 }

fn main() {
    let ref x @ ref y = NotCopy { data: 1 };
    assert!(std::ptr::eq(x, y));
}

I ran the above code in Miri. The assertion failed.

It seems to me that the NotCopy constant is const-promoted twice, resulting in two implicit NotCopy statics. (I'm guessing that, outside of miri, the consts happen to be deduplicated.)

As a result, it is observable whether const promotion happened or not. In my understanding, promotion is supposed to happen only when it's not observable whether it has happened. Therefore, I think this behavior is incorrect.

See also #145237, where a constant is both moved and promoted.

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

Meta

Reproducible on the playground in version 1.91.0-nightly (2025-08-17 425a9c0a0e365c0b8c6c)

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 with the provided Rust reproducer in Miri and compare its behavior with the related case in #145237. Trace how the ref pattern handles the shared NotCopy value and const promotion. Done means the observable behavior is consistent with the intended promotion rules and the reproducer no longer exposes distinct references unexpectedly.

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
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.