authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-09-20 18:46:27-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-09-20 18:51:26-07:00
log500afbf0760d5de7598a4988d1a4b08c21b2dc29
treef940b22f0010140b59f73c87f65ea91f6fbfd978
parented7ff0b693037078f451a7c6c1124611060f4892

add behavior test: resist alias of explicit copy...

...of array passed as arg closes #22906

1 files changed, 21 insertions(+), 0 deletions(-)

test/behavior/array.zig+21
......@@ -1125,3 +1125,24 @@ test "splat with an error union or optional result type" {
11251125 _ = try S.doTest(@Vector(4, u32));
11261126 _ = try S.doTest([4]u32);
11271127}
1128
1129test "resist alias of explicit copy of array passed as arg" {
1130 const S = struct {
1131 const Thing = [1]u32;
1132
1133 fn destroy_and_replace(box_b: *Thing, a: Thing, box_a: *Thing) void {
1134 box_a.* = undefined;
1135 box_b.* = a;
1136 }
1137 };
1138
1139 var buf_a: S.Thing = .{1234};
1140 var buf_b: S.Thing = .{5678};
1141 const box_a = &buf_a;
1142 const box_b = &buf_b;
1143
1144 const a = box_a.*; // explicit copy
1145 S.destroy_and_replace(box_b, a, box_a);
1146
1147 try expect(buf_b[0] == 1234);
1148}