authorgravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2020-01-14 12:19:10+01:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-01-14 13:09:23-05:00
log50754ba336e99ac3db972a8b4e7bc583d77f2892
treeed8b11fcf5bd5aa73587635dcaa269f5f448f578
parent4c87281b5cf0e9268107f6456aeb16d097d1eb76

Fix ICE when BoundFn are passed as parameters

Closes #4022 Closes #3699

2 files changed, 23 insertions(+), 1 deletions(-)

src/analyze.cpp+4-1
...@@ -5289,7 +5289,10 @@ static uint32_t hash_const_val(ZigValue *const_val) {...@@ -5289,7 +5289,10 @@ static uint32_t hash_const_val(ZigValue *const_val) {
5289 case ZigTypeIdAnyFrame:5289 case ZigTypeIdAnyFrame:
5290 // TODO better hashing algorithm5290 // TODO better hashing algorithm
5291 return 3747294894;5291 return 3747294894;
5292 case ZigTypeIdBoundFn:5292 case ZigTypeIdBoundFn: {
5293 assert(const_val->data.x_bound_fn.fn != nullptr);
5294 return 3677364617 ^ hash_ptr(const_val->data.x_bound_fn.fn);
5295 }
5293 case ZigTypeIdInvalid:5296 case ZigTypeIdInvalid:
5294 case ZigTypeIdUnreachable:5297 case ZigTypeIdUnreachable:
5295 zig_unreachable();5298 zig_unreachable();
test/stage1/behavior/call.zig+19
...@@ -1,5 +1,6 @@...@@ -1,5 +1,6 @@
1const std = @import("std");1const std = @import("std");
2const expect = std.testing.expect;2const expect = std.testing.expect;
3const expectEqual = std.testing.expectEqual;
34
4test "basic invocations" {5test "basic invocations" {
5 const foo = struct {6 const foo = struct {
...@@ -53,3 +54,21 @@ test "tuple parameters" {...@@ -53,3 +54,21 @@ test "tuple parameters" {
53 expect(@call(.{ .modifier = .always_inline }, add, separate_args3) == 46);54 expect(@call(.{ .modifier = .always_inline }, add, separate_args3) == 46);
54 }55 }
55}56}
57
58test "comptime call with bound function as parameter" {
59 const S = struct {
60 fn ReturnType(func: var) type {
61 return switch (@typeInfo(@TypeOf(func))) {
62 .BoundFn => |info| info,
63 else => unreachable,
64 }.return_type orelse void;
65 }
66
67 fn call_me_maybe() ?i32 {
68 return 123;
69 }
70 };
71
72 var inst: S = undefined;
73 expectEqual(?i32, S.ReturnType(inst.call_me_maybe));
74}