Skip to content

Proposal: don't allow unused bits in packed unions #19754

Open
@jacobly0

Description

@jacobly0

Currently it is valid to do:

const U = packed union {
    x: u8,
    y: u16,
};

However, there is not plainly one possible way of mapping this representation to bits, a feature of other packed types that seems desirable. For example, enum (u5) { ... } plainly represents 5 bits in an obvious manner and is allowed in packed contexts, but ?u8 has two reasonable ways of mapping to 9 bits and is not allowed in packed contexts.

My proposal for resolving this ambiguity is to require all fields of a packed union to have the same @bitSizeOf as a required backing integer type. The above type is still representable as:

const U = packed union(u16) {
    x: packed struct(u16) {
        used: u8,
        unused: u8 = undefined,
    },
    y: u16,
};

But this type now has the benefit of being explicit about the order of the unused bits and therefore only has one reasonable bitwise representation. Also, depending on how #19634 is resolved, this explicitness could be very important as having an undefined field in a packed container may become much less useful in the future, and it may be necessary to initialize to 0 instead for many use cases. To that end, this proposal encourages that explicit thought is put into how the padding bits should be defined.

Related:

Metadata

Metadata

Assignees

No one assigned

    Labels

    acceptedThis proposal is planned.breakingImplementing this issue could cause existing code to no longer compile or have different behavior.frontendTokenization, parsing, AstGen, Sema, and Liveness.proposalThis issue suggests modifications. If it also has the "accepted" label then it is planned.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions