Commit 50754ba336

LemonBoy <thatlemon@gmail.com>
2020-01-14 12:19:10
Fix ICE when BoundFn are passed as parameters
Closes #4022 Closes #3699
1 parent 4c87281
Changed files (2)
src
test
stage1
behavior
src/analyze.cpp
@@ -5289,7 +5289,10 @@ static uint32_t hash_const_val(ZigValue *const_val) {
         case ZigTypeIdAnyFrame:
             // TODO better hashing algorithm
             return 3747294894;
-        case ZigTypeIdBoundFn:
+        case ZigTypeIdBoundFn: {
+            assert(const_val->data.x_bound_fn.fn != nullptr);
+            return 3677364617 ^ hash_ptr(const_val->data.x_bound_fn.fn);
+        }
         case ZigTypeIdInvalid:
         case ZigTypeIdUnreachable:
             zig_unreachable();
test/stage1/behavior/call.zig
@@ -1,5 +1,6 @@
 const std = @import("std");
 const expect = std.testing.expect;
+const expectEqual = std.testing.expectEqual;
 
 test "basic invocations" {
     const foo = struct {
@@ -53,3 +54,21 @@ test "tuple parameters" {
         expect(@call(.{ .modifier = .always_inline }, add, separate_args3) == 46);
     }
 }
+
+test "comptime call with bound function as parameter" {
+    const S = struct {
+        fn ReturnType(func: var) type {
+            return switch (@typeInfo(@TypeOf(func))) {
+                .BoundFn => |info| info,
+                else => unreachable,
+            }.return_type orelse void;
+        }
+
+        fn call_me_maybe() ?i32 {
+            return 123;
+        }
+    };
+
+    var inst: S = undefined;
+    expectEqual(?i32, S.ReturnType(inst.call_me_maybe));
+}