Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Sunshine40 opened this issue Apr 21, 2024 · 1 comment
Open

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

Sunshine40 opened this issue Apr 21, 2024 · 1 comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@Sunshine40
Copy link
Contributor

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

@Sunshine40 Sunshine40 added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Apr 21, 2024
@steffahn
Copy link
Member

steffahn commented Apr 21, 2024

I don't think "anonymous" adds much clarity at all. The main problem I see is that the general message that "statics cannot refer to interior mutable data" is just plain wrong, and "anonymous" doesn't fix it.

Thinking of other suboptimal alternatives, "constant initializer expressions for statics cannot refer to interior mutable data" would be wrong, too; other statics containing interior mutable data can be referenced in those.

Constant static promotion is not relevant here, either, because with interior mutability it doesn't happen in the first place.

I'd guess, this really is about lifetime extended temporaries in static initializers (the ordinary lifetime extension, not the similar term in the context of 'const promotion').

Maybe a short message could thus be something like

statics initializers cannot refer to interior mutable data outside of static items

and the help texts or notes could call out the value that is subject to lifetime extension; with this error message and note/help text as context, the suggestion to move that into a separate static is then also very unsurprising IMO.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

2 participants