diff options
| author | Pavel Verigo <paul.verigo@gmail.com> | 2025-07-23 20:52:26 +0200 |
|---|---|---|
| committer | Pavel Verigo <paul.verigo@gmail.com> | 2025-07-24 01:18:02 +0200 |
| commit | fcd9f521d2922f988786833db9f00a28757d418b (patch) | |
| tree | 7303e11999984e1759f1287c02c21c4426bc45ef /test/behavior/try.zig | |
| parent | bc8e1a74c514e487842c03ab32d7f6e49f42c529 (diff) | |
| download | zig-fcd9f521d2922f988786833db9f00a28757d418b.tar.gz zig-fcd9f521d2922f988786833db9f00a28757d418b.zip | |
stage2-wasm: implement try_ptr + is_(non_)err_ptr
Diffstat (limited to 'test/behavior/try.zig')
| -rw-r--r-- | test/behavior/try.zig | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/test/behavior/try.zig b/test/behavior/try.zig index dd9885868f..b3014ef669 100644 --- a/test/behavior/try.zig +++ b/test/behavior/try.zig @@ -121,3 +121,82 @@ test "'return try' through conditional" { comptime std.debug.assert(result == 123); } } + +test "try ptr propagation const" { + if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; + if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; + if (builtin.zig_backend == .stage2_spirv) return error.SkipZigTest; + if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; + if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest; + + const S = struct { + fn foo0() !u32 { + return 0; + } + + fn foo1() error{Bad}!u32 { + return 1; + } + + fn foo2() anyerror!u32 { + return 2; + } + + fn doTheTest() !void { + const res0: *const u32 = &(try foo0()); + const res1: *const u32 = &(try foo1()); + const res2: *const u32 = &(try foo2()); + try expect(res0.* == 0); + try expect(res1.* == 1); + try expect(res2.* == 2); + } + }; + try S.doTheTest(); + try comptime S.doTheTest(); +} + +test "try ptr propagation mutate" { + if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; + if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; + if (builtin.zig_backend == .stage2_spirv) return error.SkipZigTest; + if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; + if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest; + + const S = struct { + fn foo0() !u32 { + return 0; + } + + fn foo1() error{Bad}!u32 { + return 1; + } + + fn foo2() anyerror!u32 { + return 2; + } + + fn doTheTest() !void { + var f0 = foo0(); + var f1 = foo1(); + var f2 = foo2(); + + const res0: *u32 = &(try f0); + const res1: *u32 = &(try f1); + const res2: *u32 = &(try f2); + + res0.* += 1; + res1.* += 1; + res2.* += 1; + + try expect(f0 catch unreachable == 1); + try expect(f1 catch unreachable == 2); + try expect(f2 catch unreachable == 3); + + try expect(res0.* == 1); + try expect(res1.* == 2); + try expect(res2.* == 3); + } + }; + try S.doTheTest(); + try comptime S.doTheTest(); +} |
