Skip to content

Conversation

@andrewrk
Copy link
Member

@andrewrk andrewrk commented Sep 6, 2025

closes #19754

Upgrade Guide

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

⬇️

const U = packed union(u16) {
    x: packed struct(u16) {
        data: u8,
        padding: u8 = 0,
    },
    y: u16,
};

@andrewrk andrewrk added breaking Implementing this issue could cause existing code to no longer compile or have different behavior. release notes This PR should be mentioned in the release notes. labels Sep 6, 2025
@xdBronch
Copy link
Contributor

xdBronch commented Sep 6, 2025

dont the (planned?) semantics of packed structs mean any undefined bits cause the entire thing to be undefined? probably shouldnt recommend doing that in an upgrade guide

@andrewrk
Copy link
Member Author

andrewrk commented Sep 6, 2025

Good call, thank you.

@andrewrk
Copy link
Member Author

andrewrk commented Sep 6, 2025

/home/ci/actions-runner1/_work/zig/zig/test/behavior/field_parent_ptr.zig:1755:1: error: packed store

should I disable the test for stage2_aarch64 for now?

@andrewrk andrewrk merged commit 4c01275 into master Sep 6, 2025
24 of 26 checks passed
@andrewrk andrewrk deleted the packed-union-unused branch September 6, 2025 19:08
@Justus2308
Copy link
Contributor

Weren't explicit packed union backing ints part of #19754? That's still a compile error after this patch

@andrewrk
Copy link
Member Author

Sounds like #24715 to me

@Justus2308
Copy link
Contributor

Justus2308 commented Sep 24, 2025

I thought that's just about making the tag mandatory, I was referring to this comment
This snippet:

const U = packed union(u16) {
    x: packed struct(u16) {
        data: u8,
        padding: u8 = 0,
    },
    y: u16,
};

comptime {
    _ = U;
}

still doesn't compile with:

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

@andrewrk
Copy link
Member Author

Sorry, you're right. There is not a proposal open which is limited to supporting backing integer type for packed unions, although several proposals imply its existence. I'll add that right now.

@andrewrk
Copy link
Member Author

#25350

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

breaking Implementing this issue could cause existing code to no longer compile or have different behavior. release notes This PR should be mentioned in the release notes.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Proposal: don't allow unused bits in packed unions

3 participants