Skip to content

Compile error when directly using &mut in explicity const context #140126

Open
@RedRam567

Description

@RedRam567

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)C-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions