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
49 lines
1.3 KiB
Zig
49 lines
1.3 KiB
Zig
const S = struct {
|
|
fnPtr: fn () void,
|
|
};
|
|
fn bar() void {}
|
|
fn baz() void {}
|
|
var runtime: bool = true;
|
|
fn ifExpr() S {
|
|
if (runtime) {
|
|
return .{
|
|
.fnPtr = bar,
|
|
};
|
|
} else {
|
|
return .{
|
|
.fnPtr = baz,
|
|
};
|
|
}
|
|
}
|
|
pub export fn entry1() void {
|
|
_ = ifExpr();
|
|
}
|
|
fn switchExpr() S {
|
|
switch (runtime) {
|
|
true => return .{
|
|
.fnPtr = bar,
|
|
},
|
|
false => return .{
|
|
.fnPtr = baz,
|
|
},
|
|
}
|
|
}
|
|
pub export fn entry2() void {
|
|
_ = switchExpr();
|
|
}
|
|
|
|
// error
|
|
//
|
|
// :8:9: error: unable to resolve comptime value
|
|
// :19:15: note: called at comptime from here
|
|
// :19:15: note: call to function with comptime-only return type 'tmp.S' is evaluated at comptime
|
|
// :7:13: note: return type declared here
|
|
// :2:12: note: struct requires comptime because of this field
|
|
// :2:12: note: use '*const fn () void' for a function pointer type
|
|
// :22:13: error: unable to resolve comptime value
|
|
// :32:19: note: called at comptime from here
|
|
// :32:19: note: call to function with comptime-only return type 'tmp.S' is evaluated at comptime
|
|
// :21:17: note: return type declared here
|
|
// :2:12: note: struct requires comptime because of this field
|
|
// :2:12: note: use '*const fn () void' for a function pointer type
|