Skip to content

Commit b264cc4

Browse files
committed
test: add address of tests for error union switch
1 parent b43d21d commit b264cc4

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed

test/behavior/switch_on_captured_error.zig

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ test "switch on error union catch capture" {
1818
try testCapture();
1919
try testInline();
2020
try testEmptyErrSet();
21+
try testAddressOf();
2122
}
2223

2324
fn testScalar() !void {
@@ -254,6 +255,44 @@ test "switch on error union catch capture" {
254255
try expectEqual(@as(u64, 0), b);
255256
}
256257
}
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+
}
257296
};
258297

259298
try comptime S.doTheTest();
@@ -279,6 +318,7 @@ test "switch on error union if else capture" {
279318
try testInlinePtr();
280319
try testEmptyErrSet();
281320
try testEmptyErrSetPtr();
321+
try testAddressOf();
282322
}
283323

284324
fn testScalar() !void {
@@ -750,6 +790,45 @@ test "switch on error union if else capture" {
750790
try expectEqual(@as(u64, 0), b);
751791
}
752792
}
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+
}
753832
};
754833

755834
try comptime S.doTheTest();

0 commit comments

Comments
 (0)