You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This was found by @devzero. I'm not sure if this is a bug report or a proposal, but it seems pretty clear to me that this should compile:
constEnum=enum { a, b };
pubfnmain() !void {
varx: usize=4;
vary=false;
vare: Enum=switch(x) {
else=>blk: {
if (y) {
break :blk.a;
}
break :blk.b;
}
};
}
Instead it reports this error:
.\src\day08.zig:7:22: error: values of type '(enum literal)' must be comptime known
else => blk: {
^
C:\Program Files\Zig\Current\lib\zig\std\start.zig:334:40: note: referenced here
const result = root.main() catch |err| {
^
It seems like the problem is that the named block in the switch is not inferring the result type from the switch expression. So when the compiler tries to compile the break statements, it does peer type resolution instead of result type inference and comes up with EnumLiteral. Since EnumLiterals can't be runtime values, compilation fails.
The specific behavior I think should work is:
result types should be inferred through if, else, and switch expressions into their sub-expressions.
result types should be inferred through while, for, and block expressions into their associated break statements.
The text was updated successfully, but these errors were encountered:
SpexGuy
changed the title
Compile error when nesting multiple expressions
Result type lost when nesting multiple expressions
Dec 7, 2020
This was found by @devzero. I'm not sure if this is a bug report or a proposal, but it seems pretty clear to me that this should compile:
Instead it reports this error:
It seems like the problem is that the named block in the switch is not inferring the result type from the switch expression. So when the compiler tries to compile the break statements, it does peer type resolution instead of result type inference and comes up with EnumLiteral. Since EnumLiterals can't be runtime values, compilation fails.
The specific behavior I think should work is:
if
,else
, andswitch
expressions into their sub-expressions.while
,for
, and block expressions into their associatedbreak
statements.The text was updated successfully, but these errors were encountered: