Proposal: don't allow unused bits in packed unions #19754
Labels
accepted
This proposal is planned.
breaking
Implementing this issue could cause existing code to no longer compile or have different behavior.
frontend
Tokenization, parsing, AstGen, Sema, and Liveness.
proposal
This issue suggests modifications. If it also has the "accepted" label then it is planned.
Milestone
Currently it is valid to do:
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: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 to0
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:
packed
semantics #19660The text was updated successfully, but these errors were encountered: