rust-lang / rust-lang/rust

Trying to move a `&mut` out of a `static` gives confusing error

Open
#142,772 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Code
static DANGLING: &mut () = unsafe { &mut *(1 as *mut ()) };
const WUT: &mut () = DANGLING;
Current output
error[E0596]: cannot borrow `*DANGLING` as mutable, as `DANGLING` is an immutable static item
 --> src/lib.rs:2:22
  |
2 | const WUT: &mut () = DANGLING;
  |                      ^^^^^^^^ cannot borrow as mutable

For more information about this error, try `rustc --explain E0596`.
error: could not compile `playground` (lib) due to 1 previous error
Desired output
Mention the fact that &mut T doesn't implement Copy.
Rationale and extra context

Found while investigating #140123

Other cases

Rust Version
Reproducible on the playground with version 1.89.0-nightly (2025-06-11 e703dff8fe220b78195c)
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.

Research direction

Reproduce the provided Rust code on the reported nightly and search the compiler diagnostic implementation and tests for E0596. Trace how moving a mutable reference from an immutable static is diagnosed; done means a regression test covers this example and the diagnostic explains that &mut T does not implement Copy.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.