Sema: rewrite semantic analysis of function calls

This rewrite improves some error messages, hugely simplifies the logic,
and fixes several bugs. One of these bugs is technically a new rule
which Andrew and I agreed on: if a parameter has a comptime-only type
but is not declared `comptime`, then the corresponding call argument
should not be *evaluated* at comptime; only resolved. Implementing this
required changing how function types work a little, which in turn
required allowing a new kind of function coercion for some generic use
cases: function coercions are now allowed to implicitly *remove*
`comptime` annotations from parameters with comptime-only types. This is
okay because removing the annotation affects only the call site.

Resolves: #22262
This commit is contained in:
mlugg
2025-01-05 05:27:48 +00:00
parent 3f95003d4c
commit e9bd2d45d4
36 changed files with 842 additions and 1221 deletions

View File

@@ -363,7 +363,7 @@ test "comptime modification of const struct field" {
}
test "refer to the type of a generic function" {
const Func = fn (type) void;
const Func = fn (comptime type) void;
const f: Func = doNothingWithType;
f(i32);
}

View File

@@ -427,7 +427,7 @@ test "generic function passed as comptime argument" {
if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
const S = struct {
fn doMath(comptime f: fn (type, i32, i32) error{Overflow}!i32, a: i32, b: i32) !void {
fn doMath(comptime f: fn (comptime type, i32, i32) error{Overflow}!i32, a: i32, b: i32) !void {
const result = try f(i32, a, b);
try expect(result == 11);
}

View File

@@ -1511,7 +1511,7 @@ test "if inside struct init inside if" {
test "optional generic function label struct field" {
const Options = struct {
isFoo: ?fn (type) u8 = defaultIsFoo,
isFoo: ?fn (comptime type) u8 = defaultIsFoo,
fn defaultIsFoo(comptime _: type) u8 {
return 123;
}

View File

@@ -238,7 +238,7 @@ test "comptime parameters not converted to anytype in function type" {
if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
const T = fn (fn (type) void, void) void;
const T = fn (comptime fn (comptime type) void, void) void;
try expectEqualStrings("fn (comptime fn (comptime type) void, void) void", @typeName(T));
}