aboutsummaryrefslogtreecommitdiff
path: root/test/behavior/error.zig
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2022-05-24 15:10:18 -0700
committerAndrew Kelley <andrew@ziglang.org>2022-05-24 15:34:52 -0700
commitc711c788f0a840f45d0d7423efe2f946b47caafb (patch)
tree48f57fc696e485937a7b96477657bdd8cdaff534 /test/behavior/error.zig
parentc847a462ae11e0d483ad877b3ecc9ec291c29bb3 (diff)
downloadzig-c711c788f0a840f45d0d7423efe2f946b47caafb.tar.gz
zig-c711c788f0a840f45d0d7423efe2f946b47caafb.zip
stage2: fixes for error unions, optionals, errors
* `?E` where E is an error set with only one field now lowers the same as `bool`. * Fix implementation of errUnionErrOffset and errUnionPayloadOffset to properly compute the offset of each field. Also name them the same as the corresponding LLVM functions and have the same function signature, to avoid confusion. This fixes a bug where wasm was passing the error union type instead of the payload type. * Fix C backend handling of optionals with zero-bit payload types. * C backend: separate out airOptionalPayload and airOptionalPayloadPtr which reduces branching and cleans up control flow. * Make Type.isNoReturn return true for error sets with no fields. * Make `?error{}` have only one possible value (null).
Diffstat (limited to 'test/behavior/error.zig')
-rw-r--r--test/behavior/error.zig34
1 files changed, 33 insertions, 1 deletions
diff --git a/test/behavior/error.zig b/test/behavior/error.zig
index 83a9384d71..4f316aeab2 100644
--- a/test/behavior/error.zig
+++ b/test/behavior/error.zig
@@ -121,7 +121,7 @@ test "debug info for optional error set" {
if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
- const SomeError = error{Hello};
+ const SomeError = error{ Hello, Hello2 };
var a_local_variable: ?SomeError = null;
_ = a_local_variable;
}
@@ -454,6 +454,38 @@ test "optional error set is the same size as error set" {
comptime try expect(S.returnsOptErrSet() == null);
}
+test "optional error set with only one error is the same size as bool" {
+ if (builtin.zig_backend == .stage1) return error.SkipZigTest;
+ if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
+ if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
+
+ const E = error{only};
+ comptime try expect(@sizeOf(?E) == @sizeOf(bool));
+ comptime try expect(@alignOf(?E) == @alignOf(bool));
+ const S = struct {
+ fn gimmeNull() ?E {
+ return null;
+ }
+ fn gimmeErr() ?E {
+ return error.only;
+ }
+ };
+ try expect(S.gimmeNull() == null);
+ try expect(error.only == S.gimmeErr().?);
+ comptime try expect(S.gimmeNull() == null);
+ comptime try expect(error.only == S.gimmeErr().?);
+}
+
+test "optional empty error set" {
+ if (builtin.zig_backend == .stage1) return error.SkipZigTest;
+
+ const T = ?error{};
+ var t: T = undefined;
+ if (t != null) {
+ @compileError("test failed");
+ }
+}
+
test "nested catch" {
if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO