| ... | @@ -3,9 +3,11 @@ const assert = std.debug.assert; | ... | @@ -3,9 +3,11 @@ const assert = std.debug.assert; |
| 3 | const expect = std.testing.expect; | 3 | const expect = std.testing.expect; |
| 4 | const expectError = std.testing.expectError; | 4 | const expectError = std.testing.expectError; |
| 5 | const expectEqual = std.testing.expectEqual; | 5 | const expectEqual = std.testing.expectEqual; |
| | 6 | const builtin = @import("builtin"); |
| 6 | | 7 | |
| 7 | test "switch on error union catch capture" { | 8 | test "switch on error union catch capture" { |
| 8 | if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; | 9 | if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; |
| | 10 | if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; |
| 9 | | 11 | |
| 10 | const S = struct { | 12 | const S = struct { |
| 11 | const Error = error{ A, B, C }; | 13 | const Error = error{ A, B, C }; |
| ... | @@ -16,6 +18,7 @@ test "switch on error union catch capture" { | ... | @@ -16,6 +18,7 @@ test "switch on error union catch capture" { |
| 16 | try testCapture(); | 18 | try testCapture(); |
| 17 | try testInline(); | 19 | try testInline(); |
| 18 | try testEmptyErrSet(); | 20 | try testEmptyErrSet(); |
| | 21 | try testAddressOf(); |
| 19 | } | 22 | } |
| 20 | | 23 | |
| 21 | fn testScalar() !void { | 24 | fn testScalar() !void { |
| ... | @@ -252,6 +255,44 @@ test "switch on error union catch capture" { | ... | @@ -252,6 +255,44 @@ test "switch on error union catch capture" { |
| 252 | try expectEqual(@as(u64, 0), b); | 255 | try expectEqual(@as(u64, 0), b); |
| 253 | } | 256 | } |
| 254 | } | 257 | } |
| | 258 | |
| | 259 | fn testAddressOf() !void { |
| | 260 | { |
| | 261 | const a: anyerror!usize = 0; |
| | 262 | const ptr = &(a catch |e| switch (e) { |
| | 263 | else => 3, |
| | 264 | }); |
| | 265 | comptime assert(@TypeOf(ptr) == *const usize); |
| | 266 | try expectEqual(ptr, &(a catch unreachable)); |
| | 267 | } |
| | 268 | { |
| | 269 | const a: anyerror!usize = error.A; |
| | 270 | const ptr = &(a catch |e| switch (e) { |
| | 271 | else => 3, |
| | 272 | }); |
| | 273 | comptime assert(@TypeOf(ptr) == *const comptime_int); |
| | 274 | try expectEqual(3, ptr.*); |
| | 275 | } |
| | 276 | { |
| | 277 | var a: anyerror!usize = 0; |
| | 278 | _ = &a; |
| | 279 | const ptr = &(a catch |e| switch (e) { |
| | 280 | else => return, |
| | 281 | }); |
| | 282 | comptime assert(@TypeOf(ptr) == *usize); |
| | 283 | ptr.* += 1; |
| | 284 | try expectEqual(@as(usize, 1), a catch unreachable); |
| | 285 | } |
| | 286 | { |
| | 287 | var a: anyerror!usize = error.A; |
| | 288 | _ = &a; |
| | 289 | const ptr = &(a catch |e| switch (e) { |
| | 290 | else => return, |
| | 291 | }); |
| | 292 | comptime assert(@TypeOf(ptr) == *usize); |
| | 293 | unreachable; |
| | 294 | } |
| | 295 | } |
| 255 | }; | 296 | }; |
| 256 | | 297 | |
| 257 | try comptime S.doTheTest(); | 298 | try comptime S.doTheTest(); |
| ... | @@ -260,6 +301,7 @@ test "switch on error union catch capture" { | ... | @@ -260,6 +301,7 @@ test "switch on error union catch capture" { |
| 260 | | 301 | |
| 261 | test "switch on error union if else capture" { | 302 | test "switch on error union if else capture" { |
| 262 | if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; | 303 | if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; |
| | 304 | if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; |
| 263 | | 305 | |
| 264 | const S = struct { | 306 | const S = struct { |
| 265 | const Error = error{ A, B, C }; | 307 | const Error = error{ A, B, C }; |
| ... | @@ -276,6 +318,7 @@ test "switch on error union if else capture" { | ... | @@ -276,6 +318,7 @@ test "switch on error union if else capture" { |
| 276 | try testInlinePtr(); | 318 | try testInlinePtr(); |
| 277 | try testEmptyErrSet(); | 319 | try testEmptyErrSet(); |
| 278 | try testEmptyErrSetPtr(); | 320 | try testEmptyErrSetPtr(); |
| | 321 | try testAddressOf(); |
| 279 | } | 322 | } |
| 280 | | 323 | |
| 281 | fn testScalar() !void { | 324 | fn testScalar() !void { |
| ... | @@ -747,6 +790,45 @@ test "switch on error union if else capture" { | ... | @@ -747,6 +790,45 @@ test "switch on error union if else capture" { |
| 747 | try expectEqual(@as(u64, 0), b); | 790 | try expectEqual(@as(u64, 0), b); |
| 748 | } | 791 | } |
| 749 | } | 792 | } |
| | 793 | |
| | 794 | fn testAddressOf() !void { |
| | 795 | if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; |
| | 796 | { |
| | 797 | const a: anyerror!usize = 0; |
| | 798 | const ptr = &(if (a) |*v| v.* else |e| switch (e) { |
| | 799 | else => 3, |
| | 800 | }); |
| | 801 | comptime assert(@TypeOf(ptr) == *const usize); |
| | 802 | try expectEqual(ptr, &(a catch unreachable)); |
| | 803 | } |
| | 804 | { |
| | 805 | const a: anyerror!usize = error.A; |
| | 806 | const ptr = &(if (a) |*v| v.* else |e| switch (e) { |
| | 807 | else => 3, |
| | 808 | }); |
| | 809 | comptime assert(@TypeOf(ptr) == *const comptime_int); |
| | 810 | try expectEqual(3, ptr.*); |
| | 811 | } |
| | 812 | { |
| | 813 | var a: anyerror!usize = 0; |
| | 814 | _ = &a; |
| | 815 | const ptr = &(if (a) |*v| v.* else |e| switch (e) { |
| | 816 | else => return, |
| | 817 | }); |
| | 818 | comptime assert(@TypeOf(ptr) == *usize); |
| | 819 | ptr.* += 1; |
| | 820 | try expectEqual(@as(usize, 1), a catch unreachable); |
| | 821 | } |
| | 822 | { |
| | 823 | var a: anyerror!usize = error.A; |
| | 824 | _ = &a; |
| | 825 | const ptr = &(if (a) |*v| v.* else |e| switch (e) { |
| | 826 | else => return, |
| | 827 | }); |
| | 828 | comptime assert(@TypeOf(ptr) == *usize); |
| | 829 | unreachable; |
| | 830 | } |
| | 831 | } |
| 750 | }; | 832 | }; |
| 751 | | 833 | |
| 752 | try comptime S.doTheTest(); | 834 | try comptime S.doTheTest(); |