mirror of
https://github.com/ziglang/zig.git
synced 2025-12-06 06:13:07 +00:00
Before, the system to replace a result location pointer with a traditional break instruction did not notice the case when one of the bodies was unreachable. Now, the emitted ZIR code is improved and simplified in this case, resulting in a new passing behavior test.
76 lines
1.5 KiB
Zig
76 lines
1.5 KiB
Zig
const std = @import("std");
|
|
const expect = std.testing.expect;
|
|
const expectEqual = std.testing.expectEqual;
|
|
|
|
test "if statements" {
|
|
shouldBeEqual(1, 1);
|
|
firstEqlThird(2, 1, 2);
|
|
}
|
|
fn shouldBeEqual(a: i32, b: i32) void {
|
|
if (a != b) {
|
|
unreachable;
|
|
} else {
|
|
return;
|
|
}
|
|
}
|
|
fn firstEqlThird(a: i32, b: i32, c: i32) void {
|
|
if (a == b) {
|
|
unreachable;
|
|
} else if (b == c) {
|
|
unreachable;
|
|
} else if (a == c) {
|
|
return;
|
|
} else {
|
|
unreachable;
|
|
}
|
|
}
|
|
|
|
test "else if expression" {
|
|
try expect(elseIfExpressionF(1) == 1);
|
|
}
|
|
fn elseIfExpressionF(c: u8) u8 {
|
|
if (c == 0) {
|
|
return 0;
|
|
} else if (c == 1) {
|
|
return 1;
|
|
} else {
|
|
return @as(u8, 2);
|
|
}
|
|
}
|
|
|
|
// #2297
|
|
var global_with_val: anyerror!u32 = 0;
|
|
var global_with_err: anyerror!u32 = error.SomeError;
|
|
|
|
test "unwrap mutable global var" {
|
|
if (global_with_val) |v| {
|
|
try expect(v == 0);
|
|
} else |_| {
|
|
unreachable;
|
|
}
|
|
if (global_with_err) |_| {
|
|
unreachable;
|
|
} else |e| {
|
|
try expect(e == error.SomeError);
|
|
}
|
|
}
|
|
|
|
test "labeled break inside comptime if inside runtime if" {
|
|
var answer: i32 = 0;
|
|
var c = true;
|
|
if (c) {
|
|
answer = if (true) blk: {
|
|
break :blk @as(i32, 42);
|
|
};
|
|
}
|
|
try expect(answer == 42);
|
|
}
|
|
|
|
test "const result loc, runtime if cond, else unreachable" {
|
|
const Num = enum { One, Two };
|
|
|
|
var t = true;
|
|
const x = if (t) Num.Two else unreachable;
|
|
try expect(x == .Two);
|
|
}
|