fix alignOf builtin

* fix assertion error when type is not yet complete
 * fix alignment value

closes #391
This commit is contained in:
Andrew Kelley 2017-06-15 23:47:05 -04:00
parent ae61e26680
commit 865b53f286
3 changed files with 14 additions and 1 deletions

View File

@ -12993,6 +12993,7 @@ static TypeTableEntry *ir_analyze_instruction_alignof(IrAnalyze *ira, IrInstruct
return ira->codegen->builtin_types.entry_invalid;
TypeTableEntry *type_entry = ir_resolve_type(ira, type_value);
ensure_complete_type(ira->codegen, type_entry);
if (type_is_invalid(type_entry)) {
return ira->codegen->builtin_types.entry_invalid;
} else if (type_entry->id == TypeTableEntryIdUnreachable) {
@ -13000,7 +13001,7 @@ static TypeTableEntry *ir_analyze_instruction_alignof(IrAnalyze *ira, IrInstruct
buf_sprintf("no align available for type '%s'", buf_ptr(&type_entry->name)));
return ira->codegen->builtin_types.entry_invalid;
} else {
uint64_t align_in_bytes = LLVMABISizeOfType(ira->codegen->target_data_ref, type_entry->type_ref);
uint64_t align_in_bytes = LLVMABIAlignmentOfType(ira->codegen->target_data_ref, type_entry->type_ref);
ConstExprValue *out_val = ir_build_const_from(ira, &instruction->base);
bignum_init_unsigned(&out_val->data.x_bignum, align_in_bytes);
return ira->codegen->builtin_types.entry_num_lit_int;

View File

@ -1,4 +1,5 @@
comptime {
_ = @import("cases/alignof.zig");
_ = @import("cases/array.zig");
_ = @import("cases/asm.zig");
_ = @import("cases/atomics.zig");

11
test/cases/alignof.zig Normal file
View File

@ -0,0 +1,11 @@
const assert = @import("std").debug.assert;
const builtin = @import("builtin");
const Foo = struct { x: u32, y: u32, z: u32, };
test "@alignOf(T) before referencing T" {
comptime assert(@alignOf(Foo) != @maxValue(usize));
if (builtin.arch == builtin.Arch.x86_64) {
comptime assert(@alignOf(Foo) == 4);
}
}