mirror of
https://github.com/ziglang/zig.git
synced 2026-02-18 07:18:38 +00:00
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'
This commit is contained in:
parent
8e631ee3e7
commit
5b9c5191ab
@ -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 ");
|
||||
};
|
||||
|
||||
@ -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));
|
||||
}
|
||||
|
||||
20
test/cases/compile_errors/comptime_param_coersion.zig
Normal file
20
test/cases/compile_errors/comptime_param_coersion.zig
Normal file
@ -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
|
||||
Loading…
x
Reference in New Issue
Block a user