aboutsummaryrefslogtreecommitdiff
path: root/test/behavior/switch.zig
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2022-01-17 15:21:58 -0700
committerAndrew Kelley <andrew@ziglang.org>2022-01-17 15:23:50 -0700
commitf4e051e35d8019c9a8d99ccae8f2e9d8f032629a (patch)
treecbeebf38b85f5aaaaff50ea7f9f875c02ac63667 /test/behavior/switch.zig
parent79628d48a4429818bddef2e86e2d7073d1955302 (diff)
downloadzig-f4e051e35d8019c9a8d99ccae8f2e9d8f032629a.tar.gz
zig-f4e051e35d8019c9a8d99ccae8f2e9d8f032629a.zip
Sema: fix comptime break semantics
Previously, breaking from an outer block at comptime would result in incorrect control flow. Now there is a mechanism, `error.ComptimeBreak`, similar to `error.ComptimeReturn`, to send comptime control flow further up the stack, to its matching block. This commit also introduces a new log scope. To use it, pass `--debug-log sema_zir` and you will see 1 line per ZIR instruction semantically analyzed. This is useful when you want to understand what comptime control flow is doing while debugging the compiler. One more `switch` test case is passing.
Diffstat (limited to 'test/behavior/switch.zig')
-rw-r--r--test/behavior/switch.zig21
1 files changed, 21 insertions, 0 deletions
diff --git a/test/behavior/switch.zig b/test/behavior/switch.zig
index caec804ec2..c435d70b9f 100644
--- a/test/behavior/switch.zig
+++ b/test/behavior/switch.zig
@@ -326,3 +326,24 @@ test "anon enum literal used in switch on union enum" {
},
}
}
+
+test "switch all prongs unreachable" {
+ try testAllProngsUnreachable();
+ comptime try testAllProngsUnreachable();
+}
+
+fn testAllProngsUnreachable() !void {
+ try expect(switchWithUnreachable(1) == 2);
+ try expect(switchWithUnreachable(2) == 10);
+}
+
+fn switchWithUnreachable(x: i32) i32 {
+ while (true) {
+ switch (x) {
+ 1 => return 2,
+ 2 => break,
+ else => continue,
+ }
+ }
+ return 10;
+}