Skip to content

non byte-aligned nested packed structs #16748

@xxxbxxx

Description

@xxxbxxx

Zig Version

0.12.0-dev.29+d1788ecdd

Steps to Reproduce and Observed Behavior

    const S = packed struct {
        bit: u1,
        p: packed struct {
            a: u7,
            b: u8,
        },

        var s: @This() = .{ .bit = 0, .p = .{ .a = 5, .b = 6 } };
    };

    const ptr_p_a = &S.s.p.a;
    const ptr_p_b = &S.s.p.b;
    if (@as(u7,5) != ptr_p_a.*) @panic("boom");
    if (@as(u8,6) != ptr_p_b.*) @panic("boom");     // <<< fails

    if (@intFromPtr(&S.s.p.b) - @intFromPtr(&S.s) != 1) @panic("boom");     // <<<< fails

Expected Behavior

To my understanding of https://ziglang.org/documentation/0.11.0/#packed-struct, the test should pass and ptr_p_b should be a simple *u8.

My guess is that maybe the issue is somewhere around the internal representation of the pointer in Sema.structFieldPtrByIndex():

const val = try mod.intern(.{ .ptr = .{
   .ty = ptr_field_ty.toIntern(),
   .addr = .{ .field = .{
       .base = try struct_ptr_val.intern(struct_ptr_ty, mod),      // <<<< not byte aligned
       .index = field_index,
   } },

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugObserved behavior contradicts documented or intended behaviormiscompilationThe compiler reports success but produces semantically incorrect code.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions