2017-01-05 00:57:48 -08:00
|
|
|
const assert = @import("std").debug.assert;
|
|
|
|
|
2016-12-19 13:45:15 -08:00
|
|
|
var result: [3]u8 = undefined;
|
|
|
|
var index: usize = undefined;
|
|
|
|
|
2018-01-31 19:48:40 -08:00
|
|
|
fn runSomeErrorDefers(x: bool) !bool {
|
2016-12-19 13:45:15 -08:00
|
|
|
index = 0;
|
2017-12-21 21:50:30 -08:00
|
|
|
defer {result[index] = 'a'; index += 1;}
|
2018-01-23 20:08:09 -08:00
|
|
|
errdefer {result[index] = 'b'; index += 1;}
|
2017-12-21 21:50:30 -08:00
|
|
|
defer {result[index] = 'c'; index += 1;}
|
2016-12-19 13:45:15 -08:00
|
|
|
return if (x) x else error.FalseNotAllowed;
|
|
|
|
}
|
|
|
|
|
2017-05-21 07:41:57 -07:00
|
|
|
test "mixing normal and error defers" {
|
2018-01-08 21:07:01 -08:00
|
|
|
assert(runSomeErrorDefers(true) catch unreachable);
|
2016-12-19 13:45:15 -08:00
|
|
|
assert(result[0] == 'c');
|
|
|
|
assert(result[1] == 'a');
|
|
|
|
|
2018-01-07 14:28:20 -08:00
|
|
|
const ok = runSomeErrorDefers(false) catch |err| x: {
|
2016-12-19 13:45:15 -08:00
|
|
|
assert(err == error.FalseNotAllowed);
|
2017-12-21 21:50:30 -08:00
|
|
|
break :x true;
|
2016-12-19 13:45:15 -08:00
|
|
|
};
|
|
|
|
assert(ok);
|
|
|
|
assert(result[0] == 'c');
|
|
|
|
assert(result[1] == 'b');
|
|
|
|
assert(result[2] == 'a');
|
|
|
|
}
|
2017-05-21 07:41:57 -07:00
|
|
|
|
|
|
|
test "break and continue inside loop inside defer expression" {
|
|
|
|
testBreakContInDefer(10);
|
|
|
|
comptime testBreakContInDefer(10);
|
|
|
|
}
|
|
|
|
|
2018-01-25 01:10:11 -08:00
|
|
|
fn testBreakContInDefer(x: usize) void {
|
2017-05-21 07:41:57 -07:00
|
|
|
defer {
|
|
|
|
var i: usize = 0;
|
|
|
|
while (i < x) : (i += 1) {
|
|
|
|
if (i < 5) continue;
|
|
|
|
if (i == 5) break;
|
|
|
|
}
|
|
|
|
assert(i == 5);
|
2017-12-21 21:50:30 -08:00
|
|
|
}
|
2017-05-21 07:41:57 -07:00
|
|
|
}
|
2018-04-23 15:06:33 -07:00
|
|
|
|
|
|
|
test "defer and labeled break" {
|
|
|
|
var i = usize(0);
|
|
|
|
|
|
|
|
blk: {
|
|
|
|
defer i += 1;
|
|
|
|
break :blk;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(i == 1);
|
|
|
|
}
|