Files
zig/test/cases/compile_errors/condition_comptime_reason_explained.zig
r00ster91 ee4ced9683 write function types consistently with a space before fn keyword
Currently, the compiler (like @typeName) writes it `fn(...) Type` but
zig fmt writes it `fn (...) Type` (notice the space after `fn`).
This inconsistency is now resolved and function types are consistently
written the zig fmt way. Before this there were more `fn (...) Type`
occurrences than `fn(...) Type` already.
2023-09-19 15:15:05 +03:00

51 lines
1.4 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
// backend=stage2
// target=native
//
// :8:9: error: unable to resolve comptime value
// :8:9: note: condition in comptime branch must be comptime-known
// :7:13: note: expression is evaluated at comptime because the function returns a comptime-only type 'tmp.S'
// :2:12: note: struct requires comptime because of this field
// :2:12: note: use '*const fn () void' for a function pointer type
// :19:15: note: called from here
// :22:13: error: unable to resolve comptime value
// :22:13: note: condition in comptime switch must be comptime-known
// :21:17: note: expression is evaluated at comptime because the function returns a comptime-only type 'tmp.S'
// :2:12: note: struct requires comptime because of this field
// :2:12: note: use '*const fn () void' for a function pointer type
// :32:19: note: called from here