-
Notifications
You must be signed in to change notification settings - Fork 61
Layout of single-variant enums #79
Copy link
Copy link
Open
Labels
A-layoutTopic: Related to data structure layout (`#[repr]`)Topic: Related to data structure layout (`#[repr]`)C-open-questionCategory: An open question that we should revisitCategory: An open question that we should revisitS-not-opsemDespite being in this repo, this is not primarily a T-opsem questionDespite being in this repo, this is not primarily a T-opsem questionT-lang
Metadata
Metadata
Assignees
Labels
A-layoutTopic: Related to data structure layout (`#[repr]`)Topic: Related to data structure layout (`#[repr]`)C-open-questionCategory: An open question that we should revisitCategory: An open question that we should revisitS-not-opsemDespite being in this repo, this is not primarily a T-opsem questionDespite being in this repo, this is not primarily a T-opsem questionT-lang
Type
Fields
Give feedbackNo fields configured for issues without a type.
Enums that contain a single variant and which do not have an
explicit
#[repr]annotation are an important special case. Sincethere is only a single variant, the enum must be instantiated with
that variant, which means that the enum is in fact equivalent to a
struct. The question then is to what extent we should guarantee
that the two share an equivalent layout, and also how to define the
interaction with uninhabited types.
As presently implemented, the compiler will use the same layout for
structs and for single variant enums (as long as they do not have a
#[repr]annotation that overrides that choice). So, for example, thestruct
SomeStructand the enumSomeEnumwould have an equivalentlayout (playground)::
Similarly, the struct
SomeStructand the enumSomeVariantin thisexample would also be equivalent in their layout
(playground):
In fact, the compiler will use this optimized layout even for enums
that define multiple variants, as long as all but one of the variants
is uninhabited
(playground):