authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-09-13 19:12:25-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-09-13 19:12:25-04:00
log1e03cf1739c9c7407c4b3a56ee5f2705805c6a83
tree2a0dabd42babeb6c6920b5543605a7c4a5369590
parentc06a61e9bf93810174255474598cfeae785cfbd6
signaturelock-open Commit is signed but in an unrecognized format.

fix assertion failure on compile-time `@intToPtr` of function


2 files changed, 21 insertions(+), 3 deletions(-)

src/codegen.cpp+10-3
...@@ -5886,9 +5886,16 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const c...@@ -5886,9 +5886,16 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const c
5886 case ZigTypeIdEnum:5886 case ZigTypeIdEnum:
5887 return bigint_to_llvm_const(type_entry->type_ref, &const_val->data.x_enum_tag);5887 return bigint_to_llvm_const(type_entry->type_ref, &const_val->data.x_enum_tag);
5888 case ZigTypeIdFn:5888 case ZigTypeIdFn:
5889 assert(const_val->data.x_ptr.special == ConstPtrSpecialFunction);5889 if (const_val->data.x_ptr.special == ConstPtrSpecialFunction) {
5890 assert(const_val->data.x_ptr.mut == ConstPtrMutComptimeConst);5890 assert(const_val->data.x_ptr.mut == ConstPtrMutComptimeConst);
5891 return fn_llvm_value(g, const_val->data.x_ptr.data.fn.fn_entry);5891 return fn_llvm_value(g, const_val->data.x_ptr.data.fn.fn_entry);
5892 } else if (const_val->data.x_ptr.special == ConstPtrSpecialHardCodedAddr) {
5893 LLVMTypeRef usize_type_ref = g->builtin_types.entry_usize->type_ref;
5894 uint64_t addr = const_val->data.x_ptr.data.hard_coded_addr.addr;
5895 return LLVMConstIntToPtr(LLVMConstInt(usize_type_ref, addr, false), type_entry->type_ref);
5896 } else {
5897 zig_unreachable();
5898 }
5892 case ZigTypeIdPointer:5899 case ZigTypeIdPointer:
5893 return gen_const_val_ptr(g, const_val, name);5900 return gen_const_val_ptr(g, const_val, name);
5894 case ZigTypeIdErrorUnion:5901 case ZigTypeIdErrorUnion:
test/cases/cast.zig+11
...@@ -526,3 +526,14 @@ test "*usize to *void" {...@@ -526,3 +526,14 @@ test "*usize to *void" {
526 var v = @ptrCast(*void, &i);526 var v = @ptrCast(*void, &i);
527 v.* = {};527 v.* = {};
528}528}
529
530test "compile time int to ptr of function" {
531 foobar(FUNCTION_CONSTANT);
532}
533
534pub const FUNCTION_CONSTANT = @intToPtr(PFN_void, @maxValue(usize));
535pub const PFN_void = extern fn (*c_void) void;
536
537fn foobar(func: PFN_void) void {
538 std.debug.assert(@ptrToInt(func) == @maxValue(usize));
539}