zig icon indicating copy to clipboard operation
zig copied to clipboard

Bit-packed struct with undefined default has inconsistent access

Open Arnau478 opened this issue 9 months ago • 7 comments

Zig Version

0.13.0-dev.211+6a65561e3

Steps to Reproduce and Observed Behavior

Run the following code:

const std = @import("std");

pub const Foo = packed struct(u2) {
    a: bool = undefined,
    b: bool,
};

const foo = Foo{
    .b = true,
};

pub fn main() void {
    std.debug.print("{}\n", .{foo});
    std.debug.print("{}\n", .{foo.b});
}

which prints

example.Foo{ .a = false, .b = false }
true

Notice how when we print foo, we get .b = false, but when we print foo.b we get true.

Expected Behavior

Both of them should print b as being true, but the first one doesn't.

A few things I've noticed:

  • If you change the undefined default value with false, then everything works as expected
  • Pretty recent regression, works in zig 0.12.0

Arnau478 avatar May 16 '24 11:05 Arnau478