rust-lang / rust-lang/rust

Improve error[E0492]: statics cannot refer to interior mutable data

Open
#124,232 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Code
static STATIC_REF: &AtomicIsize = &AtomicIsize::new(0);
Current output
error[E0492]: statics cannot refer to interior mutable data
 --> src/lib.rs:3:35
  |
3 | static STATIC_REF: &AtomicIsize = &AtomicIsize::new(0);
  |                                   ^^^^^^^^^^^^^^^^^^^^ this borrow of an interior mutable value may end up in the final value
  |
  = help: to fix this, the value can be extracted to a separate `static` item and then referenced
Desired output
error[E0492]: statics cannot refer to anonymous interior mutable data
 --> src/lib.rs:3:35
  |
3 | static STATIC_REF: &AtomicIsize = &AtomicIsize::new(0);
  |                                   ^^^^^^^^^^^^^^^^^^^^ this borrow of an interior mutable value may end up in the final value
  |
  = help: to fix this, the value can be extracted to a separate `static` item and then referenced
Rationale and extra context

more discussion here: https://users.rust-lang.org/t/why-compiler-error-e0492-statics-cannot-refer-to-interior-mutable-data/110266

Other cases

No response

Rust Version
rustc 1.77.2 (25ef9e3d8 2024-04-09)
binary: rustc
commit-hash: 25ef9e3d85d934b27d9dada2f9dd52b1dc63bb04
commit-date: 2024-04-09
host: x86_64-pc-windows-msvc
release: 1.77.2
LLVM version: 17.0.6
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

Start by reproducing the provided static snippet and inspecting the existing E0492 diagnostic path. Update the wording so anonymous interior mutable data is distinguished, then verify the resulting error text against the desired output and add or adjust the relevant diagnostic test.

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
Active
Clarity
Clearly specified
Newbie friendliness
68/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.