diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2021-10-17 15:36:12 -0700 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2021-10-17 15:36:12 -0700 |
| commit | 07691db3ae061d8122f6c53c1f34c2fb0df2f7ef (patch) | |
| tree | 379046dbe83b30f007433360ad08d3bdee7e2234 /test/behavior/error.zig | |
| parent | 6534f2ef4f8161f4121326f19bc3cf89324f62c5 (diff) | |
| download | zig-07691db3ae061d8122f6c53c1f34c2fb0df2f7ef.tar.gz zig-07691db3ae061d8122f6c53c1f34c2fb0df2f7ef.zip | |
stage2: fix handling of error unions as return type
* LLVM backend: fix phi instruction not respecting `isByRef`
- Also fix `is_non_null` not respecting `isByRef`
* Type: implement abiSize for error unions
Diffstat (limited to 'test/behavior/error.zig')
| -rw-r--r-- | test/behavior/error.zig | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/test/behavior/error.zig b/test/behavior/error.zig index 6edb973e36..edbe866b95 100644 --- a/test/behavior/error.zig +++ b/test/behavior/error.zig @@ -49,3 +49,69 @@ pub fn baz() anyerror!i32 { test "error wrapping" { try expect((baz() catch unreachable) == 15); } + +test "unwrap simple value from error" { + const i = unwrapSimpleValueFromErrorDo() catch unreachable; + try expect(i == 13); +} +fn unwrapSimpleValueFromErrorDo() anyerror!isize { + return 13; +} + +test "error return in assignment" { + doErrReturnInAssignment() catch unreachable; +} + +fn doErrReturnInAssignment() anyerror!void { + var x: i32 = undefined; + x = try makeANonErr(); +} + +fn makeANonErr() anyerror!i32 { + return 1; +} + +test "syntax: optional operator in front of error union operator" { + comptime { + try expect(?(anyerror!i32) == ?(anyerror!i32)); + } +} + +test "widen cast integer payload of error union function call" { + const S = struct { + fn errorable() !u64 { + var x = @as(u64, try number()); + return x; + } + + fn number() anyerror!u32 { + return 1234; + } + }; + try expect((try S.errorable()) == 1234); +} + +test "debug info for optional error set" { + const SomeError = error{Hello}; + var a_local_variable: ?SomeError = null; + _ = a_local_variable; +} + +test "implicit cast to optional to error union to return result loc" { + const S = struct { + fn entry() !void { + var x: Foo = undefined; + if (func(&x)) |opt| { + try expect(opt != null); + } else |_| @panic("expected non error"); + } + fn func(f: *Foo) anyerror!?*Foo { + return f; + } + const Foo = struct { + field: i32, + }; + }; + try S.entry(); + //comptime S.entry(); TODO +} |
