Open
Description
I tried this code:
const OKAY_SOMEHOW: &mut [f32] = std::convert::identity(&mut []);
// error[E0764]: mutable references are not allowed in the final value of constants
const BAD: &mut [f32] = &mut [];
I expected to see this happen: Both constants compile or both fail to compile.
Instead, this happened: BAD
failed to compile.
I'm not sure which is the intended behavior or if it is UB. I don't know the rules around constants that hold mutable references.
This fails to compile in constants, statics and const {}
blocks. This succeeds when using const functions to wrap the expressions, even in constants, statics, and const {}
blocks.
Additional examples: https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=7b7a327957429e5706e520755d9cd467
See also an ICE related to this "ICE error performing operation: fully_perform
" #140123
Meta
rustc --version --verbose
:
rustc 1.88.0-nightly (b8c54d635 2025-04-20)
binary: rustc
commit-hash: b8c54d6358926028ac2fab1ec2b8665c70edb1c0
commit-date: 2025-04-20
host: x86_64-unknown-linux-gnu
release: 1.88.0-nightly
LLVM version: 20.1.2
Backtrace
RUST_BACKTRACE=1 cargo +nightly build
Compiling rust_playground3 v0.1.0 (/tmp/rust_playground3)
error[E0764]: mutable references are not allowed in the final value of constants
--> src/lib.rs:4:25
|
4 | const BAD: &mut [f32] = &mut [];
| ^^^^^^^
For more information about this error, try `rustc --explain E0764`.
error: could not compile `rust_playground3` (lib) due to 1 previous error