Skip to content

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

Description

@Sunshine40

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsE-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.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