What is this lint about
Consider the following code:
S here is a static variable accessible to Rust but defined and initialized elsewhere.
External static variables cannot be accessed safely from Rust code for several reasons:
S can contain an arbitrary bit pattern not necessarily being a valid value of type T.
- even if all bit patterns are valid values of
T, S is still not controlled by Rust ownership system and can be accessed from other threads potentially creating data races.
See #35112 for more details as well as examples of how this can lead to crashes.
Previously safe accesses to extern statics were erroneously permitted outside of unsafe blocks. #36173 fixed this oversight.
How to fix this warning/error
Surround accesses to extern statics with unsafe blocks and make sure these accesses are actually valid.
Current status
What is this lint about
Consider the following code:
Shere is a static variable accessible to Rust but defined and initialized elsewhere.External static variables cannot be accessed safely from Rust code for several reasons:
Scan contain an arbitrary bit pattern not necessarily being a valid value of typeT.T,Sis still not controlled by Rust ownership system and can be accessed from other threads potentially creating data races.See #35112 for more details as well as examples of how this can lead to crashes.
Previously safe accesses to extern statics were erroneously permitted outside of unsafe blocks. #36173 fixed this oversight.
How to fix this warning/error
Surround accesses to extern statics with
unsafeblocks and make sure these accesses are actually valid.Current status
safe_extern_staticslint as warn-by-defaultsafe_extern_staticslint deny-by-defaultsafe_extern_staticslint a hard error