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...@@ -5220,13 +5220,13 @@ static bool is_llvm_value_unnamed_type(TypeTableEntry *type_entry, LLVMValueRef
5220}5220}
52215221
5222static LLVMValueRef gen_const_val_ptr(CodeGen *g, ConstExprValue *const_val, const char *name) {5222static LLVMValueRef gen_const_val_ptr(CodeGen *g, ConstExprValue *const_val, const char *name) {
5223 render_const_val_global(g, const_val, name);
5224 switch (const_val->data.x_ptr.special) {5223 switch (const_val->data.x_ptr.special) {
5225 case ConstPtrSpecialInvalid:5224 case ConstPtrSpecialInvalid:
5226 case ConstPtrSpecialDiscard:5225 case ConstPtrSpecialDiscard:
5227 zig_unreachable();5226 zig_unreachable();
5228 case ConstPtrSpecialRef:5227 case ConstPtrSpecialRef:
5229 {5228 {
5229 render_const_val_global(g, const_val, name);
5230 ConstExprValue *pointee = const_val->data.x_ptr.data.ref.pointee;5230 ConstExprValue *pointee = const_val->data.x_ptr.data.ref.pointee;
5231 render_const_val(g, pointee, "");5231 render_const_val(g, pointee, "");
5232 render_const_val_global(g, pointee, "");5232 render_const_val_global(g, pointee, "");
...@@ -5237,6 +5237,7 @@ static LLVMValueRef gen_const_val_ptr(CodeGen *g, ConstExprValue *const_val, con...@@ -5237,6 +5237,7 @@ static LLVMValueRef gen_const_val_ptr(CodeGen *g, ConstExprValue *const_val, con
5237 }5237 }
5238 case ConstPtrSpecialBaseArray:5238 case ConstPtrSpecialBaseArray:
5239 {5239 {
5240 render_const_val_global(g, const_val, name);
5240 ConstExprValue *array_const_val = const_val->data.x_ptr.data.base_array.array_val;5241 ConstExprValue *array_const_val = const_val->data.x_ptr.data.base_array.array_val;
5241 size_t elem_index = const_val->data.x_ptr.data.base_array.elem_index;5242 size_t elem_index = const_val->data.x_ptr.data.base_array.elem_index;
5242 assert(array_const_val->type->id == TypeTableEntryIdArray);5243 assert(array_const_val->type->id == TypeTableEntryIdArray);
...@@ -5257,6 +5258,7 @@ static LLVMValueRef gen_const_val_ptr(CodeGen *g, ConstExprValue *const_val, con...@@ -5257,6 +5258,7 @@ static LLVMValueRef gen_const_val_ptr(CodeGen *g, ConstExprValue *const_val, con
5257 }5258 }
5258 case ConstPtrSpecialBaseStruct:5259 case ConstPtrSpecialBaseStruct:
5259 {5260 {
5261 render_const_val_global(g, const_val, name);
5260 ConstExprValue *struct_const_val = const_val->data.x_ptr.data.base_struct.struct_val;5262 ConstExprValue *struct_const_val = const_val->data.x_ptr.data.base_struct.struct_val;
5261 assert(struct_const_val->type->id == TypeTableEntryIdStruct);5263 assert(struct_const_val->type->id == TypeTableEntryIdStruct);
5262 if (struct_const_val->type->zero_bits) {5264 if (struct_const_val->type->zero_bits) {
...@@ -5279,6 +5281,7 @@ static LLVMValueRef gen_const_val_ptr(CodeGen *g, ConstExprValue *const_val, con...@@ -5279,6 +5281,7 @@ static LLVMValueRef gen_const_val_ptr(CodeGen *g, ConstExprValue *const_val, con
5279 }5281 }
5280 case ConstPtrSpecialHardCodedAddr:5282 case ConstPtrSpecialHardCodedAddr:
5281 {5283 {
5284 render_const_val_global(g, const_val, name);
5282 uint64_t addr_value = const_val->data.x_ptr.data.hard_coded_addr.addr;5285 uint64_t addr_value = const_val->data.x_ptr.data.hard_coded_addr.addr;
5283 TypeTableEntry *usize = g->builtin_types.entry_usize;5286 TypeTableEntry *usize = g->builtin_types.entry_usize;
5284 const_val->global_refs->llvm_value = LLVMConstIntToPtr(LLVMConstInt(usize->type_ref, addr_value, false),5287 const_val->global_refs->llvm_value = LLVMConstIntToPtr(LLVMConstInt(usize->type_ref, addr_value, false),
test/behavior.zig+1
...@@ -10,6 +10,7 @@ comptime {...@@ -10,6 +10,7 @@ comptime {
10 _ = @import("cases/bool.zig");10 _ = @import("cases/bool.zig");
11 _ = @import("cases/bugs/1111.zig");11 _ = @import("cases/bugs/1111.zig");
12 _ = @import("cases/bugs/1230.zig");12 _ = @import("cases/bugs/1230.zig");
13 _ = @import("cases/bugs/1277.zig");
13 _ = @import("cases/bugs/394.zig");14 _ = @import("cases/bugs/394.zig");
14 _ = @import("cases/bugs/655.zig");15 _ = @import("cases/bugs/655.zig");
15 _ = @import("cases/bugs/656.zig");16 _ = @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}