Commit 5b9c5191ab

Jacob Young <jacobly0@users.noreply.github.com>
2022-09-09 08:28:56
type: print comptime on fn type params
This avoids the following confusing error message: error: expected type 'fn(i32, i32) void', found 'fn(i32, i32) void'
1 parent 8e631ee
Changed files (3)
src
test
behavior
cases
src/type.zig
@@ -2042,6 +2042,9 @@ pub const Type = extern union {
                 try writer.writeAll("fn(");
                 for (fn_info.param_types) |param_ty, i| {
                     if (i != 0) try writer.writeAll(", ");
+                    if (fn_info.paramIsComptime(i)) {
+                        try writer.writeAll("comptime ");
+                    }
                     if (std.math.cast(u5, i)) |index| if (@truncate(u1, fn_info.noalias_bits >> index) != 0) {
                         try writer.writeAll("noalias ");
                     };
test/behavior/typename.zig
@@ -122,7 +122,7 @@ test "top level decl" {
     );
     // generic fn
     try expectEqualStrings(
-        "fn(type) type",
+        "fn(comptime type) type",
         @typeName(@TypeOf(TypeFromFn)),
     );
 }
@@ -244,5 +244,5 @@ test "comptime parameters not converted to anytype in function type" {
     if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
 
     const T = fn (fn (type) void, void) void;
-    try expectEqualStrings("fn(fn(type) void, void) void", @typeName(T));
+    try expectEqualStrings("fn(comptime fn(comptime type) void, void) void", @typeName(T));
 }
test/cases/compile_errors/comptime_param_coersion.zig
@@ -0,0 +1,20 @@
+pub export fn entry() void {
+    comptime var x: fn (comptime i32, comptime i32) void = undefined;
+    x = bar;
+}
+pub export fn entry1() void {
+    comptime var x: fn (i32, i32) void = undefined;
+    x = foo;
+}
+
+fn foo(comptime _: i32, comptime _: i32) void {}
+fn bar(comptime _: i32, _: i32) void {}
+
+// error
+// backend=stage2
+// target=native
+//
+// :3:9: error: expected type 'fn(comptime i32, comptime i32) void', found 'fn(comptime i32, i32) void'
+// :3:9: note: non-comptime parameter 1 cannot cast into a comptime parameter
+// :7:9: error: expected type 'fn(i32, i32) void', found 'fn(comptime i32, comptime i32) void'
+// :7:9: note: generic function cannot cast into a non-generic function