From 865b53f2860405a718262abf9a794d2bf9529dbc Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Thu, 15 Jun 2017 23:47:05 -0400 Subject: [PATCH] fix alignOf builtin * fix assertion error when type is not yet complete * fix alignment value closes #391 --- src/ir.cpp | 3 ++- test/behavior.zig | 1 + test/cases/alignof.zig | 11 +++++++++++ 3 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 test/cases/alignof.zig diff --git a/src/ir.cpp b/src/ir.cpp index 2f5ee84e2c..c327722612 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -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; diff --git a/test/behavior.zig b/test/behavior.zig index 5f8958be2d..09236b8096 100644 --- a/test/behavior.zig +++ b/test/behavior.zig @@ -1,4 +1,5 @@ comptime { + _ = @import("cases/alignof.zig"); _ = @import("cases/array.zig"); _ = @import("cases/asm.zig"); _ = @import("cases/atomics.zig"); diff --git a/test/cases/alignof.zig b/test/cases/alignof.zig new file mode 100644 index 0000000000..27b95c7fdc --- /dev/null +++ b/test/cases/alignof.zig @@ -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); + } +}