Skip to content

Does the repr(transparent) guarantee work for fields that are empty types? #485

Open
@Manishearth

Description

@Manishearth

Context: hyperium/hyper#3500

The repr(transparent) RFC states:

his attribute can be placed on newtypes, i.e. structures (and structure tuples) with a single field, and on structures that are logically equivalent to a newtype, i.e. structures with multiple fields where only a single one of them has a non-zero size.

The reference states:

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<T>).

Structs and enums with this representation have the same layout and ABI as the single non-zero sized field.

It's unclear if this applies to situations where a contained type is an empty type (enum Empty {})

For example, what are the implications of this?

#[repr(transparent)]
struct Foo(String, Empty);

enum Empty {}

From the RFC, this type is not "logically equvialent to a newtype", and repr(transparent) should not apply. From the reference, the other field is size 0 alignment 1, so repr(transparent) should make the repr the same as String (I believe this is the current behavior in rustc), but it does feel like an edge case.

It would be nice to have this explicitly documented, rather than inferred.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions