authorgravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2022-09-09 02:28:56-04:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-09-10 01:55:52+03:00
log5b9c5191ab919f4166a9e0c4486bd57bb2533791
treefd45835c1c92cb296354109968c9c0045b0f018c
parent8e631ee3e7b4e7b4466c0efafaffb4151447785f

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'

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

src/type.zig+3
...@@ -2042,6 +2042,9 @@ pub const Type = extern union {...@@ -2042,6 +2042,9 @@ pub const Type = extern union {
2042 try writer.writeAll("fn(");2042 try writer.writeAll("fn(");
2043 for (fn_info.param_types) |param_ty, i| {2043 for (fn_info.param_types) |param_ty, i| {
2044 if (i != 0) try writer.writeAll(", ");2044 if (i != 0) try writer.writeAll(", ");
2045 if (fn_info.paramIsComptime(i)) {
2046 try writer.writeAll("comptime ");
2047 }
2045 if (std.math.cast(u5, i)) |index| if (@truncate(u1, fn_info.noalias_bits >> index) != 0) {2048 if (std.math.cast(u5, i)) |index| if (@truncate(u1, fn_info.noalias_bits >> index) != 0) {
2046 try writer.writeAll("noalias ");2049 try writer.writeAll("noalias ");
2047 };2050 };
test/behavior/typename.zig+2-2
...@@ -122,7 +122,7 @@ test "top level decl" {...@@ -122,7 +122,7 @@ test "top level decl" {
122 );122 );
123 // generic fn123 // generic fn
124 try expectEqualStrings(124 try expectEqualStrings(
125 "fn(type) type",125 "fn(comptime type) type",
126 @typeName(@TypeOf(TypeFromFn)),126 @typeName(@TypeOf(TypeFromFn)),
127 );127 );
128}128}
...@@ -244,5 +244,5 @@ test "comptime parameters not converted to anytype in function type" {...@@ -244,5 +244,5 @@ test "comptime parameters not converted to anytype in function type" {
244 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO244 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
245245
246 const T = fn (fn (type) void, void) void;246 const T = fn (fn (type) void, void) void;
247 try expectEqualStrings("fn(fn(type) void, void) void", @typeName(T));247 try expectEqualStrings("fn(comptime fn(comptime type) void, void) void", @typeName(T));
248}248}
test/cases/compile_errors/comptime_param_coersion.zig created+20
...@@ -0,0 +1,20 @@
1pub export fn entry() void {
2 comptime var x: fn (comptime i32, comptime i32) void = undefined;
3 x = bar;
4}
5pub export fn entry1() void {
6 comptime var x: fn (i32, i32) void = undefined;
7 x = foo;
8}
9
10fn foo(comptime _: i32, comptime _: i32) void {}
11fn bar(comptime _: i32, _: i32) void {}
12
13// error
14// backend=stage2
15// target=native
16//
17// :3:9: error: expected type 'fn(comptime i32, comptime i32) void', found 'fn(comptime i32, i32) void'
18// :3:9: note: non-comptime parameter 1 cannot cast into a comptime parameter
19// :7:9: error: expected type 'fn(i32, i32) void', found 'fn(comptime i32, comptime i32) void'
20// :7:9: note: generic function cannot cast into a non-generic function