From 882ff042e24eeaf5f6a50ebf0ad270158b3ecb25 Mon Sep 17 00:00:00 2001 From: jaina heartles Date: Sat, 10 Dec 2022 01:26:44 -0800 Subject: [PATCH] Template compilation errors on bad switch --- src/template/lib.zig | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/template/lib.zig b/src/template/lib.zig index 7f02a20..33b55d3 100644 --- a/src/template/lib.zig +++ b/src/template/lib.zig @@ -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)) {