diff --git a/src/codegen.cpp b/src/codegen.cpp index d64c191c93..9bab5fd878 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -7195,7 +7195,7 @@ check: switch (const_val->special) { union_value_ref = LLVMGetUndef(union_type_ref); make_unnamed_struct = false; } else { - uint64_t field_type_bytes = LLVMStoreSizeOfType(g->target_data_ref, + uint64_t field_type_bytes = LLVMABISizeOfType(g->target_data_ref, get_llvm_type(g, payload_value->type)); uint64_t pad_bytes = type_entry->data.unionation.union_abi_size - field_type_bytes; LLVMValueRef correctly_typed_value = gen_const_val(g, payload_value, ""); @@ -7235,7 +7235,7 @@ check: switch (const_val->special) { uint64_t last_field_offset = LLVMOffsetOfElement(g->target_data_ref, LLVMTypeOf(result), 1); uint64_t end_offset = last_field_offset + LLVMStoreSizeOfType(g->target_data_ref, LLVMTypeOf(fields[1])); - uint64_t expected_sz = LLVMStoreSizeOfType(g->target_data_ref, get_llvm_type(g, type_entry)); + uint64_t expected_sz = LLVMABISizeOfType(g->target_data_ref, get_llvm_type(g, type_entry)); unsigned pad_sz = expected_sz - end_offset; if (pad_sz != 0) { fields[2] = LLVMGetUndef(LLVMArrayType(LLVMInt8Type(), pad_sz)); diff --git a/test/stage1/behavior/union.zig b/test/stage1/behavior/union.zig index a977e31853..a24bee03e6 100644 --- a/test/stage1/behavior/union.zig +++ b/test/stage1/behavior/union.zig @@ -620,3 +620,12 @@ test "0-sized extern union definition" { expect(U.f == 1); } + +test "union initializer generates padding only if needed" { + const U = union(enum) { + A: u24, + }; + + var v = U{ .A = 532 }; + expect(v.A == 532); +}