behavior: add coverage for extern struct field overalignment

This commit is contained in:
Andrew Kelley 2026-01-28 14:10:27 -08:00
parent 37a9ca7163
commit 4d6f4e9cfd

View file

@ -2184,3 +2184,52 @@ test "pass a pointer to a comptime-only struct field to a function" {
const s: struct { x: type } = .{ .x = u42 };
try S.checkField(&s.x);
}
test "overaligned extern struct fields" {
const A = struct {
a: *anyopaque,
b: u64,
c: [1][]u8,
d: ?anyerror,
};
const B = union(enum) {
a: struct {
a: [2]usize,
b: C,
},
b: struct {
a: *anyopaque,
b: []const []u8,
c: C,
},
const C = union {
a: void,
b: *anyopaque,
c: anyerror!usize,
};
};
const D = extern struct {
a: u32,
};
const E = extern struct {
a: u32,
b: [2][@sizeOf(A)]u8 align(@alignOf(A)),
c: [2]u32,
d: [2][@sizeOf(B)]u8 align(@alignOf(B)),
fn cast(e: *@This()) *D {
e.a = 2;
return @ptrCast(e);
}
};
var e: E = undefined;
const d = e.cast();
try expect(d.a == 2);
try expect(std.mem.isAligned(@intFromPtr(&e.b), @alignOf(A)));
try expect(std.mem.isAligned(@intFromPtr(&e.c), @alignOf(u32)));
try expect(std.mem.isAligned(@intFromPtr(&e.d), @alignOf(B)));
}