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(...@@ -5386,6 +5386,9 @@ fn unionDeclInner(
5386 return astgen.failNode(member_node, "union field missing type", .{});5386 return astgen.failNode(member_node, "union field missing type", .{});
5387 }5387 }
5388 if (member.ast.align_expr.unwrap()) |align_expr| {5388 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 }
5389 const align_inst = try expr(&block_scope, &block_scope.base, coerced_align_ri, align_expr);5392 const align_inst = try expr(&block_scope, &block_scope.base, coerced_align_ri, align_expr);
5390 wip_members.appendToField(@intFromEnum(align_inst));5393 wip_members.appendToField(@intFromEnum(align_inst));
5391 any_aligned_fields = true;5394 any_aligned_fields = true;
test/behavior/type.zig+2-2
...@@ -433,8 +433,8 @@ test "Type.Union" {...@@ -433,8 +433,8 @@ test "Type.Union" {
433 .layout = .@"packed",433 .layout = .@"packed",
434 .tag_type = null,434 .tag_type = null,
435 .fields = &.{435 .fields = &.{
436 .{ .name = "signed", .type = i32, .alignment = @alignOf(i32) },436 .{ .name = "signed", .type = i32, .alignment = 0 },
437 .{ .name = "unsigned", .type = u32, .alignment = @alignOf(u32) },437 .{ .name = "unsigned", .type = u32, .alignment = 0 },
438 },438 },
439 .decls = &.{},439 .decls = &.{},
440 },440 },
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 {...@@ -75,4 +75,5 @@ comptime {
75// :16:5: error: tuple field name '3' does not match field index 075// :16:5: error: tuple field name '3' does not match field index 0
76// :30:5: error: comptime field without default initialization value76// :30:5: error: comptime field without default initialization value
77// :44:5: error: extern struct fields cannot be marked comptime77// :44:5: error: extern struct fields cannot be marked comptime
78// :58:5: error: alignment in a packed struct field must be set to 078// :58:5: error: alignment of a packed struct field must be set to 0
79