Skip to content

Tracking issue for safe_extern_statics compatibility lint #36247

Closed
@petrochenkov

Description

@petrochenkov

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 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.B-unstableBlocker: Implemented in the nightly compiler and unstable.C-future-incompatibilityCategory: Future-incompatibility lintsT-langRelevant to the language 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