From 32d755beb8d83a666100ca9455df51b93caff1fc Mon Sep 17 00:00:00 2001 From: Julian <58199799+mov-rax@users.noreply.github.com> Date: Mon, 3 Oct 2022 05:05:12 -0500 Subject: [PATCH] Sema: require reified packed struct fields to have zero alignment --- src/Sema.zig | 4 ++++ ...uct_field_alignment_unavailable_for_reify_type.zig | 11 +++++++++++ 2 files changed, 15 insertions(+) create mode 100644 test/cases/compile_errors/packed_struct_field_alignment_unavailable_for_reify_type.zig diff --git a/src/Sema.zig b/src/Sema.zig index aed09d6201..ed62fe02b2 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -17682,6 +17682,10 @@ fn reifyStruct( } const abi_align = @intCast(u29, (try alignment_val.getUnsignedIntAdvanced(target, sema.kit(block, src))).?); + if (layout == .Packed and abi_align != 0) { + return sema.fail(block, src, "alignment in a packed struct field must be set to 0", .{}); + } + const field_name = try name_val.toAllocatedBytes( Type.initTag(.const_slice_u8), new_decl_arena_allocator, 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 new file mode 100644 index 0000000000..8c17e9100b --- /dev/null +++ b/test/cases/compile_errors/packed_struct_field_alignment_unavailable_for_reify_type.zig @@ -0,0 +1,11 @@ +export fn entry() void { + _ = @Type(.{ .Struct = .{ .layout = .Packed, .fields = &.{ + .{ .name = "one", .field_type = u4, .default_value = null, .is_comptime = false, .alignment = 2 }, + }, .decls = &.{}, .is_tuple = false } }); +} + +// error +// backend=stage2 +// target=native +// +// :2:9: error: alignment in a packed struct field must be set to 0