authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-08-22 14:31:30-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-08-22 14:31:30-04:00
log4b68ef45af54abd7ba56878f93132ca608891cf1
treec55317df4451d47d77c168c231fdd8d1fe140566
parent5aeb3217ee42753f7bc837fdfe7cc04fb132d150

fix incorrectly generating an unused const fn global

closes #1277

3 files changed, 20 insertions(+), 1 deletions(-)

src/codegen.cpp+4-1
......@@ -5220,13 +5220,13 @@ static bool is_llvm_value_unnamed_type(TypeTableEntry *type_entry, LLVMValueRef
52205220}
52215221
52225222static LLVMValueRef gen_const_val_ptr(CodeGen *g, ConstExprValue *const_val, const char *name) {
5223 render_const_val_global(g, const_val, name);
52245223 switch (const_val->data.x_ptr.special) {
52255224 case ConstPtrSpecialInvalid:
52265225 case ConstPtrSpecialDiscard:
52275226 zig_unreachable();
52285227 case ConstPtrSpecialRef:
52295228 {
5229 render_const_val_global(g, const_val, name);
52305230 ConstExprValue *pointee = const_val->data.x_ptr.data.ref.pointee;
52315231 render_const_val(g, pointee, "");
52325232 render_const_val_global(g, pointee, "");
......@@ -5237,6 +5237,7 @@ static LLVMValueRef gen_const_val_ptr(CodeGen *g, ConstExprValue *const_val, con
52375237 }
52385238 case ConstPtrSpecialBaseArray:
52395239 {
5240 render_const_val_global(g, const_val, name);
52405241 ConstExprValue *array_const_val = const_val->data.x_ptr.data.base_array.array_val;
52415242 size_t elem_index = const_val->data.x_ptr.data.base_array.elem_index;
52425243 assert(array_const_val->type->id == TypeTableEntryIdArray);
......@@ -5257,6 +5258,7 @@ static LLVMValueRef gen_const_val_ptr(CodeGen *g, ConstExprValue *const_val, con
52575258 }
52585259 case ConstPtrSpecialBaseStruct:
52595260 {
5261 render_const_val_global(g, const_val, name);
52605262 ConstExprValue *struct_const_val = const_val->data.x_ptr.data.base_struct.struct_val;
52615263 assert(struct_const_val->type->id == TypeTableEntryIdStruct);
52625264 if (struct_const_val->type->zero_bits) {
......@@ -5279,6 +5281,7 @@ static LLVMValueRef gen_const_val_ptr(CodeGen *g, ConstExprValue *const_val, con
52795281 }
52805282 case ConstPtrSpecialHardCodedAddr:
52815283 {
5284 render_const_val_global(g, const_val, name);
52825285 uint64_t addr_value = const_val->data.x_ptr.data.hard_coded_addr.addr;
52835286 TypeTableEntry *usize = g->builtin_types.entry_usize;
52845287 const_val->global_refs->llvm_value = LLVMConstIntToPtr(LLVMConstInt(usize->type_ref, addr_value, false),
test/behavior.zig+1
......@@ -10,6 +10,7 @@ comptime {
1010 _ = @import("cases/bool.zig");
1111 _ = @import("cases/bugs/1111.zig");
1212 _ = @import("cases/bugs/1230.zig");
13 _ = @import("cases/bugs/1277.zig");
1314 _ = @import("cases/bugs/394.zig");
1415 _ = @import("cases/bugs/655.zig");
1516 _ = @import("cases/bugs/656.zig");
test/cases/bugs/1277.zig created+15
......@@ -0,0 +1,15 @@
1const std = @import("std");
2
3const S = struct {
4 f: ?fn () i32,
5};
6
7const s = S{ .f = f };
8
9fn f() i32 {
10 return 1234;
11}
12
13test "don't emit an LLVM global for a const function when it's in an optional in a struct" {
14 std.debug.assertOrPanic(s.f.?() == 1234);
15}