From 8b1780d9396aa7bd919f2ec5e003f981bbce07d5 Mon Sep 17 00:00:00 2001 From: Veikka Tuominen Date: Tue, 10 Jan 2023 14:04:38 +0200 Subject: [PATCH] Sema: fix condition for omitting comptime arg from function type Closes #14164 --- src/Sema.zig | 2 +- ...d_generic_function_param_type_mismatch.zig | 24 +++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 test/cases/compile_errors/nested_generic_function_param_type_mismatch.zig diff --git a/src/Sema.zig b/src/Sema.zig index ec698f7a3b..8263468fbf 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -9005,7 +9005,7 @@ fn zirParam( else => |e| return e, } or comptime_syntax; if (sema.inst_map.get(inst)) |arg| { - if (is_comptime) { + if (is_comptime and sema.preallocated_new_func != null) { // We have a comptime value for this parameter so it should be elided from the // function type of the function instruction in this block. const coerced_arg = try sema.coerce(block, param_ty, arg, src); diff --git a/test/cases/compile_errors/nested_generic_function_param_type_mismatch.zig b/test/cases/compile_errors/nested_generic_function_param_type_mismatch.zig new file mode 100644 index 0000000000..2c05a1dd4a --- /dev/null +++ b/test/cases/compile_errors/nested_generic_function_param_type_mismatch.zig @@ -0,0 +1,24 @@ +pub fn sort( + comptime T: type, + items: []T, + context: anytype, + lessThan: *const fn (context: @TypeOf(context), lhs: T, rhs: T) u32, +) void { + _ = items; + _ = lessThan; +} +fn foo(_: void, _: u8, _: u8) u32 { + return 0; +} +pub export fn entry() void { + var items = [_]u8{ 3, 5, 7, 2, 6, 9, 4 }; + sort(u8, &items, void, foo); +} + +// error +// backend=llvm +// target=native +// +// :15:28: error: expected type '*const fn(comptime type, u8, u8) u32', found '*const fn(void, u8, u8) u32' +// :15:28: note: pointer type child 'fn(void, u8, u8) u32' cannot cast into pointer type child 'fn(comptime type, u8, u8) u32' +// :15:28: note: non-generic function cannot cast into a generic function