From 67bd0267db00cf71b5f3301559a39bac32b65476 Mon Sep 17 00:00:00 2001 From: LemonBoy Date: Tue, 10 Sep 2019 21:05:41 +0200 Subject: [PATCH] Correct calculation of padding length in struct Make sure the resulting type is in-sync with the one produced and used by LLVM. Fixes #3138 --- src/analyze.cpp | 2 +- test/stage1/behavior/struct.zig | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/analyze.cpp b/src/analyze.cpp index e0ba82f9b5..5d6a2d900c 100644 --- a/src/analyze.cpp +++ b/src/analyze.cpp @@ -7839,7 +7839,7 @@ static void resolve_llvm_types_struct(CodeGen *g, ZigType *struct_type, ResolveS assert(next_offset >= llvm_next_offset); if (next_offset > llvm_next_offset) { - size_t pad_bytes = next_offset - (field->offset + field_type->abi_size); + size_t pad_bytes = next_offset - llvm_next_offset; if (pad_bytes != 0) { LLVMTypeRef pad_llvm_type = LLVMArrayType(LLVMInt8Type(), pad_bytes); element_types[gen_field_index] = pad_llvm_type; diff --git a/test/stage1/behavior/struct.zig b/test/stage1/behavior/struct.zig index 610e7328ec..c0dc580c36 100644 --- a/test/stage1/behavior/struct.zig +++ b/test/stage1/behavior/struct.zig @@ -670,3 +670,22 @@ test "packed struct with non-ABI-aligned field" { expect(s.x == 1); expect(s.y == 42); } + +test "non-packed struct with u128 entry in union" { + const U = union(enum) { + Num: u128, + Void, + }; + + const S = struct { + f1: U, + f2: U, + }; + + var sx: S = undefined; + var s = &sx; + std.testing.expect(@ptrToInt(&s.f2) - @ptrToInt(&s.f1) == @byteOffsetOf(S, "f2")); + var v2 = U{ .Num = 123 }; + s.f2 = v2; + std.testing.expect(s.f2.Num == 123); +}