You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Improve error[E0492]: statics cannot refer to interior mutable data #124232
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
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.
error[E0492]: interior mutable shared borrows of temporaries that have their lifetime extended until the end of the program are not allowed --> $DIR/static-temporary-borrow-diagnostic-issue-124232.rs:10:38 |LL | static TEMPORARY_REF: &AtomicIsize = &AtomicIsize::new(0); | ^^^^^^^^^^^^^^^^^^^^ this borrow of an interior mutable value refers to such a temporary | = note: temporaries in constants and statics can have their lifetime extended until the end of the program = note: to avoid accidentally creating global mutable state, such temporaries must be immutable = help: if you really want global mutable state, try replacing the temporary by an interior mutable `static` or a `static mut`error: aborting due to 1 previous errorFor more information about this error, try `rustc --explain E0492`.
Activity
steffahn commentedon 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
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
staticis then also very unsurprising IMO.chenyukang commentedon Sep 15, 2026
now the output adds more information:
link #140942
chenyukang commentedon Sep 20, 2026
there is already test in main:
https://github.com/chenyukang/rust/blob/73601a241ab6d6265af828be2ff88f40ce0f5582/tests/ui/error-codes/E0492.rs#L1-L11
so i think we may close this issue.