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
Code
Current output
Desired output
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
Anything else?
No response