Closed
Description
What is this lint about
Consider the following code:
extern {
static S: T;
}
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 typeT
.- 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
- Issue deprecation warnings for safe accesses to extern statics #36173 introduces the
safe_extern_statics
lint as warn-by-default - Make sufficiently old or low-impact compatibility lints deny-by-default #42894 makes the
safe_extern_statics
lint deny-by-default - PR ? makes the
safe_extern_statics
lint a hard error