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

@@ -57,8 +57,9 @@ pub fn addCases(ctx: *Cases, b: *std.Build) !void {
\\}
, &[_][]const u8{
":3:12: error: unable to resolve comptime value",
":2:55: note: generic function instantiated with comptime-only return type '?fn () void' is evaluated at comptime",
":2:55: note: use '*const fn () void' for a function pointer type",
":3:19: note: call to generic function instantiated with comptime-only return type '?fn () void' is evaluated at comptime",
":2:55: note: return type declared here",
":3:19: note: use '*const fn () void' for a function pointer type",
});
case.addSourceFile("b.zig",
\\pub const ElfDynLib = struct {
@@ -193,10 +194,12 @@ pub fn addCases(ctx: *Cases, b: *std.Build) !void {
\\ import.anytypeFunction(S{ .x = x, .y = u32 });
\\}
, &[_][]const u8{
":4:33: error: runtime-known argument passed to comptime parameter",
":1:38: note: declared comptime here",
":8:36: error: runtime-known argument passed to comptime parameter",
":2:41: note: declared comptime here",
":4:33: error: unable to resolve comptime value",
":4:33: note: argument to comptime parameter must be comptime-known",
":1:29: note: parameter declared comptime here",
":8:36: error: unable to resolve comptime value",
":8:36: note: argument to comptime parameter must be comptime-known",
":2:32: note: parameter declared comptime here",
":13:32: error: unable to resolve comptime value",
":13:32: note: initializer of comptime-only struct 'tmp.callAnytypeFunctionWithRuntimeComptimeOnlyType.S' must be comptime-known",
":12:35: note: struct requires comptime because of this field",