rust-lang / rust-lang/rust

Confusing error when trying to promote a const to a static item

Open
#154,382 1 comment 0 reactions 1 assignee View on GitHub

@Hiryxx is already working on this.

Since Apr 4, 2026.

A-constant-promotion A-diagnostics T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Code
use std::cell::Cell;

struct Mutable(Cell<u32>);
impl Mutable {
    const fn new(a: u32) -> Self { Self(Cell::new(a)) }
}
fn main() {
    let _: &'static _ = &const { 0u32 };
    let _: &'static _ = &const { Mutable::new(0u32) };
    let _: &'static _ =  const { &Mutable::new(0u32) };
}
Current output
error[E0716]: temporary value dropped while borrowed
  --> src/main.rs:8:26
   |
 8 |     let _: &'static _ = &const { Mutable::new(0u32) };
   |            ----------    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ creates a temporary value which is freed while still in use
   |            |
   |            type annotation requires that borrow lasts for `'static`
 9 |     let _: &'static _ =  const { &Mutable::new(0u32) };
10 | }
   | - temporary value is freed at the end of this statement
Desired output
error[E0492]: interior mutable shared borrows of temporaries that have their lifetime extended until the end of the program are not allowed
 --> src/main.rs:9:34
  |
9 |     let _: &'static _ =  &const { Mutable::new(0u32) };
  |                                  ^^^^^^^^^^^^^^^^^^^ this borrow of an interior mutable value refers to such a temporary
  |
  = note: Temporaries in constants and statics can have their lifetime extended until the end of the program
  = note: To avoid accidentally creating global mutable state, such temporaries must be immutable
  = help: If you really want global mutable state, try replacing the temporary by an interior mutable `static` or a `static mut`
Rationale and extra context

The compiler error says that a temporary value is being dropped. This is only the case because promotion for the value has failed. Yet, the error makes no reference to this and does not hint that this promotion is not possible for the type in question (which is not obvious when the internal mutability is hidden, or must be conservatively assumed for generic parameters).

Other cases

Rust Version
rustc 1.93.1 (01f6ddf75 2026-02-11)
binary: rustc
commit-hash: 01f6ddf7588f42ae2d7eb0a2f21d44e8e96674cf
commit-date: 2026-02-11
host: x86_64-pc-windows-msvc
release: 1.93.1
LLVM version: 21.1.8

can be reproduced on the playground with stable and nightly version
Anything else?

No response

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.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.