Skip to content

Commit ad3a846

Browse files
committed
test: add address of tests for error union switch
1 parent 368b94f commit ad3a846

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed

test/behavior/switch_on_captured_error.zig

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,45 @@ test "switch on error union catch capture" {
259259
try S.doTheTest();
260260
}
261261

262+
test "address of catch error union switch" {
263+
if (builtin.target.isWasm()) return error.SkipZigTest;
264+
{
265+
const a: anyerror!usize = 0;
266+
const ptr = &(a catch |e| switch (e) {
267+
else => 3,
268+
});
269+
comptime assert(@TypeOf(ptr) == *const usize);
270+
try expectEqual(ptr, &(a catch unreachable));
271+
}
272+
{
273+
const a: anyerror!usize = error.A;
274+
const ptr = &(a catch |e| switch (e) {
275+
else => 3,
276+
});
277+
comptime assert(@TypeOf(ptr) == *const comptime_int);
278+
try expectEqual(3, ptr.*);
279+
}
280+
{
281+
var a: anyerror!usize = 0;
282+
_ = &a;
283+
const ptr = &(a catch |e| switch (e) {
284+
else => return,
285+
});
286+
comptime assert(@TypeOf(ptr) == *usize);
287+
ptr.* += 1;
288+
try expectEqual(@as(usize, 1), a catch unreachable);
289+
}
290+
{
291+
var a: anyerror!usize = error.A;
292+
_ = &a;
293+
const ptr = &(a catch |e| switch (e) {
294+
else => return,
295+
});
296+
comptime assert(@TypeOf(ptr) == *usize);
297+
unreachable;
298+
}
299+
}
300+
262301
test "switch on error union if else capture" {
263302
if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
264303

@@ -753,3 +792,42 @@ test "switch on error union if else capture" {
753792
try comptime S.doTheTest();
754793
try S.doTheTest();
755794
}
795+
796+
test "address of if error union switch" {
797+
if (builtin.target.isWasm()) return error.SkipZigTest;
798+
{
799+
const a: anyerror!usize = 0;
800+
const ptr = &(if (a) |*v| v.* else |e| switch (e) {
801+
else => 3,
802+
});
803+
comptime assert(@TypeOf(ptr) == *const usize);
804+
try expectEqual(ptr, &(a catch unreachable));
805+
}
806+
{
807+
const a: anyerror!usize = error.A;
808+
const ptr = &(if (a) |*v| v.* else |e| switch (e) {
809+
else => 3,
810+
});
811+
comptime assert(@TypeOf(ptr) == *const comptime_int);
812+
try expectEqual(3, ptr.*);
813+
}
814+
{
815+
var a: anyerror!usize = 0;
816+
_ = &a;
817+
const ptr = &(if (a) |*v| v.* else |e| switch (e) {
818+
else => return,
819+
});
820+
comptime assert(@TypeOf(ptr) == *usize);
821+
ptr.* += 1;
822+
try expectEqual(@as(usize, 1), a catch unreachable);
823+
}
824+
{
825+
var a: anyerror!usize = error.A;
826+
_ = &a;
827+
const ptr = &(if (a) |*v| v.* else |e| switch (e) {
828+
else => return,
829+
});
830+
comptime assert(@TypeOf(ptr) == *usize);
831+
unreachable;
832+
}
833+
}

0 commit comments

Comments
 (0)