2017-01-05 08:57:48 +00:00
|
|
|
const assert = @import("std").debug.assert;
|
|
|
|
|
2016-12-19 21:45:15 +00:00
|
|
|
var result: [3]u8 = undefined;
|
|
|
|
var index: usize = undefined;
|
|
|
|
|
|
|
|
error FalseNotAllowed;
|
|
|
|
|
2016-12-19 22:25:09 +00:00
|
|
|
fn runSomeErrorDefers(x: bool) -> %bool {
|
2016-12-19 21:45:15 +00:00
|
|
|
index = 0;
|
|
|
|
defer {result[index] = 'a'; index += 1;};
|
|
|
|
%defer {result[index] = 'b'; index += 1;};
|
|
|
|
defer {result[index] = 'c'; index += 1;};
|
|
|
|
return if (x) x else error.FalseNotAllowed;
|
|
|
|
}
|
|
|
|
|
2016-12-19 22:25:09 +00:00
|
|
|
fn runSomeMaybeDefers(x: bool) -> ?bool {
|
|
|
|
index = 0;
|
|
|
|
defer {result[index] = 'a'; index += 1;};
|
|
|
|
?defer {result[index] = 'b'; index += 1;};
|
|
|
|
defer {result[index] = 'c'; index += 1;};
|
|
|
|
return if (x) x else null;
|
|
|
|
}
|
|
|
|
|
2016-12-19 21:45:15 +00:00
|
|
|
fn mixingNormalAndErrorDefers() {
|
|
|
|
@setFnTest(this);
|
|
|
|
|
2016-12-19 22:25:09 +00:00
|
|
|
assert(%%runSomeErrorDefers(true));
|
2016-12-19 21:45:15 +00:00
|
|
|
assert(result[0] == 'c');
|
|
|
|
assert(result[1] == 'a');
|
|
|
|
|
2016-12-19 22:25:09 +00:00
|
|
|
const ok = runSomeErrorDefers(false) %% |err| {
|
2016-12-19 21:45:15 +00:00
|
|
|
assert(err == error.FalseNotAllowed);
|
|
|
|
true
|
|
|
|
};
|
|
|
|
assert(ok);
|
|
|
|
assert(result[0] == 'c');
|
|
|
|
assert(result[1] == 'b');
|
|
|
|
assert(result[2] == 'a');
|
|
|
|
}
|
|
|
|
|
2016-12-19 22:25:09 +00:00
|
|
|
fn mixingNormalAndMaybeDefers() {
|
|
|
|
@setFnTest(this);
|
|
|
|
|
|
|
|
assert(??runSomeMaybeDefers(true));
|
|
|
|
assert(result[0] == 'c');
|
|
|
|
assert(result[1] == 'a');
|
|
|
|
|
|
|
|
const ok = runSomeMaybeDefers(false) ?? true;
|
|
|
|
assert(ok);
|
|
|
|
assert(result[0] == 'c');
|
|
|
|
assert(result[1] == 'b');
|
|
|
|
assert(result[2] == 'a');
|
|
|
|
}
|