Template compilation errors on bad switch

This commit is contained in:
jaina heartles 2022-12-10 01:26:44 -08:00
parent ad513d70e2
commit 882ff042e2

View file

@ -130,6 +130,11 @@ fn executeStatement(
.@"switch" => |switch_stmt| {
const expr = evaluateExpression(switch_stmt.expression, args, captures, context);
const exhaustive = switch_stmt.cases.len == std.meta.fields(@TypeOf(expr)).len;
if (exhaustive and switch_stmt.else_branch != null) @compileError("Unused else branch in switch");
if (!exhaustive and switch_stmt.else_branch == null) @compileError("Not all switch cases covered");
var found = false;
inline for (switch_stmt.cases) |case| {
if (std.meta.isTag(expr, case.header.tag)) {