REPR_TRANSPARENT_NON_ZST_FIELDS in rustc_lint::builtin - Rust
Description: The `repr_transparent_non_zst_fields` lint detects types marked `#[repr(transparent)]` that (transitively) contain a type that is not guaranteed to remain a ZST type under all configurations.

Skip to main content

## REPR_TRANSPARENT_NON_ZST_FIELDS

# Static REPR_TRANSPARENT_NON_ZST_FIELDS

Source

    pub static REPR_TRANSPARENT_NON_ZST_FIELDS: &'static Lint

Expand description

The `repr_transparent_non_zst_fields` lint detects types marked `#[repr(transparent)]` that (transitively) contain a type that is not guaranteed to remain a ZST type under all configurations.

#### §Example

ⓘ

    #![deny(repr_transparent_external_private_fields)]
    use foo::NonExhaustiveZst;

    #[repr(C)]
    struct CZst([u8; 0]);

    #[repr(transparent)]
    struct Bar(u32, ([u32; 0], NonExhaustiveZst));
    #[repr(transparent)]
    struct Baz(u32, CZst);

This will produce:

    error: zero-sized fields in repr(transparent) cannot contain external non-exhaustive types
     --> src/main.rs:5:28
      |
    5 | struct Bar(u32, ([u32; 0], NonExhaustiveZst));
      |                            ^^^^^^^^^^^^^^^^
      |
    note: the lint level is defined here
     --> src/main.rs:1:9
      |
    1 | #![deny(repr_transparent_external_private_fields)]
      |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
      = note: for more information, see issue #78586 <https://github.com/rust-lang/rust/issues/78586>
      = note: this field contains `NonExhaustiveZst`, which is marked with `#[non_exhaustive]`, so it could become non-zero-sized in the future.

    error: zero-sized fields in repr(transparent) cannot contain `#[repr(C)]` types
     --> src/main.rs:5:28
      |
    5 | struct Baz(u32, CZst);
      |                 ^^^^
      = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
      = note: for more information, see issue #78586 <https://github.com/rust-lang/rust/issues/78586>
      = note: this field contains `CZst`, which is a `#[repr(C)]` type, so it is not guaranteed to be zero-sized on all targets.

#### §Explanation

Previous, Rust accepted fields that contain external private zero-sized types, even though those types could gain a non-zero-sized field in a future, semver-compatible update.

Rust also accepted fields that contain `repr(C)` zero-sized types, even though those types are not guaranteed to be zero-sized on all targets, and even though those types can make a difference for the ABI (and therefore cannot be ignored by `repr(transparent)`).

This is a future-incompatible lint to transition this to a hard error in the future. See issue #78586 for more details.