Skip to content

allow explicit backing integer of packed union #25350

@andrewrk

Description

@andrewrk

Related:

const std = @import("std");
const expect = std.testing.expect;

const U = packed union(u16) {
    a: u16,
    b: packed struct(u16) {
        a: u7,
        b: u9,
    },
};

test "packed union with explicit integer type" {
    const x: u16 = 1234;
    const u: U = @bitCast(x);
    try expect(u.a == 1234);
    try expect(u.b.a == 82);
    try expect(u.b.b == 9);
}

Currently gives:

test.zig:4:24: error: packed union does not support enum tag type
const U = packed union(u16) {
                       ^~~

Instead, this test should pass.

Semantics Clarifications

When combined with #24714:

  • just like enums, the type has well-defined memory layout if and only if explicit backing integer provided
  • just like enums, the type is allowed in extern contexts if and only if explicit backing integer provided
  • just like enums, the type is allowed in switch expressions independent of explicit backing integer existing
  • just like enums, the type supports equality independent of explicit backing integer existing

Metadata

Metadata

Assignees

No one assigned

    Labels

    acceptedThis proposal is planned.enhancementSolving this issue will likely involve adding new logic or components to the codebase.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