Skip to content

repr(transparent) on generic type skips "exactly one non-zero-sized field" check #77841

Closed
@mahkoh

Description

@mahkoh
#[repr(transparent)]
struct X(()); // ERROR: needs exactly one non-zero-sized field, but has 0

But

#[repr(transparent)]
struct X<T>(T);

println!("{}", mem::align_of_val(&X(())));  // 1
println!("{}", mem::size_of_val(&X(())));   // 0

The reference says

The transparent representation can only be used on a struct or an enum with a single variant that has:

  • a single field with non-zero size, and
  • any number of fields with size 0 and alignment 1 (e.g. PhantomData).

I do not see a reason for this restriction. Maybe simply replace "a single" by "at most one" and remove the check.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-layoutArea: Memory layout of typesA-zstArea: Zero-sized types (ZSTs).C-bugCategory: This is a bug.T-langRelevant to the language team, which will review and decide on the PR/issue.disposition-mergeThis issue / PR is in PFCP or FCP with a disposition to merge it.finished-final-comment-periodThe final comment period is finished for this PR / Issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions