authorgravatar for david@vortan.devDavid Rubin <david@vortan.dev> 2025-08-01 14:57:06-07:00
committergravatar for david@vortan.devDavid Rubin <david@vortan.dev> 2025-08-02 09:51:26-07:00
loge82d67233b615e04fb5e4f31cd93216a2c1c2899
tree875708455162f38785153010da521930321ef4b9
parent5678a600ffb2ec27f88ab6a308b4eaf56c60daed
signaturelock-open Commit is signed but in an unrecognized format.

disallow alignment on packed union fields


5 files changed, 16 insertions(+), 12 deletions(-)

lib/std/zig/AstGen.zig+3
......@@ -5386,6 +5386,9 @@ fn unionDeclInner(
53865386 return astgen.failNode(member_node, "union field missing type", .{});
53875387 }
53885388 if (member.ast.align_expr.unwrap()) |align_expr| {
5389 if (layout == .@"packed") {
5390 return astgen.failNode(align_expr, "unable to override alignment of packed union fields", .{});
5391 }
53895392 const align_inst = try expr(&block_scope, &block_scope.base, coerced_align_ri, align_expr);
53905393 wip_members.appendToField(@intFromEnum(align_inst));
53915394 any_aligned_fields = true;
test/behavior/type.zig+2-2
......@@ -433,8 +433,8 @@ test "Type.Union" {
433433 .layout = .@"packed",
434434 .tag_type = null,
435435 .fields = &.{
436 .{ .name = "signed", .type = i32, .alignment = @alignOf(i32) },
437 .{ .name = "unsigned", .type = u32, .alignment = @alignOf(u32) },
436 .{ .name = "signed", .type = i32, .alignment = 0 },
437 .{ .name = "unsigned", .type = u32, .alignment = 0 },
438438 },
439439 .decls = &.{},
440440 },
test/cases/compile_errors/packed_struct_field_alignment_unavailable_for_reify_type.zig deleted-9
......@@ -1,9 +0,0 @@
1export fn entry() void {
2 _ = @Type(.{ .@"struct" = .{ .layout = .@"packed", .fields = &.{
3 .{ .name = "one", .type = u4, .default_value_ptr = null, .is_comptime = false, .alignment = 2 },
4 }, .decls = &.{}, .is_tuple = false } });
5}
6
7// error
8//
9// :2:9: error: alignment in a packed struct field must be set to 0
test/cases/compile_errors/packed_union_alignment_override.zig created+9
......@@ -0,0 +1,9 @@
1const U = packed union {
2 x: f32,
3 y: u8 align(10),
4 z: u32,
5};
6
7// error
8//
9// :3:17: error: unable to override alignment of packed union fields
test/cases/compile_errors/reify_struct.zig+2-1
......@@ -75,4 +75,5 @@ comptime {
7575// :16:5: error: tuple field name '3' does not match field index 0
7676// :30:5: error: comptime field without default initialization value
7777// :44:5: error: extern struct fields cannot be marked comptime
78// :58:5: error: alignment in a packed struct field must be set to 0
78// :58:5: error: alignment of a packed struct field must be set to 0
79