Skip to content

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

Open
@theemathas

Description

@theemathas

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsT-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