Skip to content

Bit-packed struct with undefined default has inconsistent access #19981

Description

@Arnau478

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

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugObserved behavior contradicts documented or intended behavior

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions