Skip to content

Commit 505b9db

Browse files
LemonBoyandrewrk
authored andcommitted
Fix codegen error for some union initializers
Closes #3377
1 parent af2ede4 commit 505b9db

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

src/codegen.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7195,7 +7195,7 @@ check: switch (const_val->special) {
71957195
union_value_ref = LLVMGetUndef(union_type_ref);
71967196
make_unnamed_struct = false;
71977197
} else {
7198-
uint64_t field_type_bytes = LLVMStoreSizeOfType(g->target_data_ref,
7198+
uint64_t field_type_bytes = LLVMABISizeOfType(g->target_data_ref,
71997199
get_llvm_type(g, payload_value->type));
72007200
uint64_t pad_bytes = type_entry->data.unionation.union_abi_size - field_type_bytes;
72017201
LLVMValueRef correctly_typed_value = gen_const_val(g, payload_value, "");
@@ -7235,7 +7235,7 @@ check: switch (const_val->special) {
72357235
uint64_t last_field_offset = LLVMOffsetOfElement(g->target_data_ref, LLVMTypeOf(result), 1);
72367236
uint64_t end_offset = last_field_offset +
72377237
LLVMStoreSizeOfType(g->target_data_ref, LLVMTypeOf(fields[1]));
7238-
uint64_t expected_sz = LLVMStoreSizeOfType(g->target_data_ref, get_llvm_type(g, type_entry));
7238+
uint64_t expected_sz = LLVMABISizeOfType(g->target_data_ref, get_llvm_type(g, type_entry));
72397239
unsigned pad_sz = expected_sz - end_offset;
72407240
if (pad_sz != 0) {
72417241
fields[2] = LLVMGetUndef(LLVMArrayType(LLVMInt8Type(), pad_sz));

test/stage1/behavior/union.zig

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -620,3 +620,12 @@ test "0-sized extern union definition" {
620620

621621
expect(U.f == 1);
622622
}
623+
624+
test "union initializer generates padding only if needed" {
625+
const U = union(enum) {
626+
A: u24,
627+
};
628+
629+
var v = U{ .A = 532 };
630+
expect(v.A == 532);
631+
}

0 commit comments

Comments
 (0)