Skip to content

nested packed struct incorrect behavior #14632

@archaistvolts

Description

@archaistvolts

Zig Version

0.11.0-dev.1577+11cc1c16f

Steps to Reproduce and Observed Behavior

the failing tests are marked with // this fails. the following struct field configuration manifests the issue.

const std = @import("std");

pub const Options = packed struct {
    foo: bool = false,
    bar: bool = false,
    pretty_print: packed struct {
        enabled: bool = false,
        num_spaces: u4 = 4,
        space_char: enum { space, tab } = .space,
        indent: u8 = 0,
    } = .{},
    baz: bool = false,
};
test {
    var opts = Options{};
    opts.pretty_print.indent += 1;
    // prints 00000000000100001
    std.debug.print("{b:0>17}\n", .{@bitCast(u17, opts)});
    // this fails
    try std.testing.expect(!opts.foo);
    try std.testing.expect(!opts.bar);
    try std.testing.expect(!opts.pretty_print.enabled);
    try std.testing.expectEqual(@as(u4, 4), opts.pretty_print.num_spaces);
    // this fails. is actually 0
    try std.testing.expectEqual(@as(u8, 1), opts.pretty_print.indent);
    try std.testing.expect(!opts.baz);
}

other configurations don't seem to manifest this issue. ie moving the nested struct to first position fixes the issue - atleast for the nested fields. i haven't checked any of the other outer fields after moving the nested struct to the top.

Expected Behavior

test pass

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugObserved behavior contradicts documented or intended behavior

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions