zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

commit e82d67233b615e04fb5e4f31cd93216a2c1c2899 (tree)
parent 5678a600ffb2ec27f88ab6a308b4eaf56c60daed
Author: David Rubin <david@vortan.dev>
Date:   Fri,  1 Aug 2025 14:57:06 -0700

disallow alignment on packed union fields

Diffstat:
Mlib/std/zig/AstGen.zig | 3+++
Mtest/behavior/type.zig | 4++--
Dtest/cases/compile_errors/packed_struct_field_alignment_unavailable_for_reify_type.zig | 9---------
Atest/cases/compile_errors/packed_union_alignment_override.zig | 9+++++++++
Mtest/cases/compile_errors/reify_struct.zig | 3++-
5 files changed, 16 insertions(+), 12 deletions(-)

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