authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-08-10 10:59:26+03:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-08-10 15:18:09-04:00
log40447b25e87ece9eee2d83d1c78c365ad7ab40f1
treea5546ed8efe05ed9de5de88924238333b38c3edc
parent49a270b2038709a6a0c1f4de604696278769257b

Sema: fix expansion of repeated value

Closes #12386

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

src/Sema.zig+4-1
......@@ -23990,7 +23990,10 @@ fn beginComptimePtrMutation(
2399023990 const array_len_including_sentinel =
2399123991 try sema.usizeCast(block, src, parent.ty.arrayLenIncludingSentinel());
2399223992 const elems = try arena.alloc(Value, array_len_including_sentinel);
23993 mem.set(Value, elems, repeated_val);
23993 if (elems.len > 0) elems[0] = repeated_val;
23994 for (elems[1..]) |*elem| {
23995 elem.* = try repeated_val.copy(arena);
23996 }
2399423997
2399523998 val_ptr.* = try Value.Tag.aggregate.create(arena, elems);
2399623999
test/behavior/eval.zig+17
......@@ -1293,3 +1293,20 @@ test "mutate through pointer-like optional at comptime" {
12931293 try expect(payload_ptr.*.* == 16);
12941294 }
12951295}
1296
1297test "repeated value is correctly expanded" {
1298 const S = struct { x: [4]i8 = std.mem.zeroes([4]i8) };
1299 const M = struct { x: [4]S = std.mem.zeroes([4]S) };
1300
1301 comptime {
1302 var res = M{};
1303 for (.{ 1, 2, 3 }) |i| res.x[i].x[i] = i;
1304
1305 try expectEqual(M{ .x = .{
1306 .{ .x = .{ 0, 0, 0, 0 } },
1307 .{ .x = .{ 0, 1, 0, 0 } },
1308 .{ .x = .{ 0, 0, 2, 0 } },
1309 .{ .x = .{ 0, 0, 0, 3 } },
1310 } }, res);
1311 }
1312}