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) {
52895289 case ZigTypeIdAnyFrame:
52905290 // TODO better hashing algorithm
52915291 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 }
52935296 case ZigTypeIdInvalid:
52945297 case ZigTypeIdUnreachable:
52955298 zig_unreachable();
test/stage1/behavior/call.zig+19
......@@ -1,5 +1,6 @@
11const std = @import("std");
22const expect = std.testing.expect;
3const expectEqual = std.testing.expectEqual;
34
45test "basic invocations" {
56 const foo = struct {
......@@ -53,3 +54,21 @@ test "tuple parameters" {
5354 expect(@call(.{ .modifier = .always_inline }, add, separate_args3) == 46);
5455 }
5556}
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}