commit 4d6f4e9cfd00669efc1c688d9509010ddc4e840f (tree)
parent 37a9ca71634ea256ddc9466b7773feb394e02a8d
Author: Andrew Kelley <andrew@ziglang.org>
Date: Wed, 28 Jan 2026 14:10:27 -0800
behavior: add coverage for extern struct field overalignment
Diffstat:
1 file changed, 49 insertions(+), 0 deletions(-)
diff --git a/test/behavior/struct.zig b/test/behavior/struct.zig
@@ -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)));
+}