Fix codegen error for some union initializers

Closes #3377
master
LemonBoy 2020-01-14 19:01:04 +01:00 committed by Andrew Kelley
parent af2ede4d96
commit 505b9db909
2 changed files with 11 additions and 2 deletions

View File

@ -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));

View File

@ -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);
}