Skip to content

repr(transparent) accepts too many types as ZST (repr_transparent_non_zst_fields, formerly repr_transparent_external_private_fields) #78586

@scottmcm

Description

@scottmcm

This tracks eventually removing support for repr(transparent) types ignoring ZST that might not actually remain ZST in the future or on other targets.

Types that can gain more fields

In crate A:

#[non_exhaustive]
pub struct UnitType {}

In crate B:

#[repr(transparent)]
pub struct TransparentThing(i32, UnitType);

I expected that to fail in crate B, because the reason crate A put #[non_exhaustive] on the type was to allow it to get additional fields in the future, which would no longer allow it to work in repr(transparent).

But this actually compiles today, creating what I would consider an accidental semver hazard.

https://rust-lang.zulipchat.com/#narrow/stream/213817-t-lang/topic/Stability.20of.201-ZSTness/near/215120408


repr(C) types

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

#[repr(transparent)]
pub struct TransparentThing(i32, CZst);

This should fail since CZst actually is not a ZST for all C compilers, and since the type can be relevant for the ABI.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-reprArea: the `#[repr(stuff)]` attributeC-bugCategory: This is a bug.T-langRelevant to the language team

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions