Sema: fix expansion of repeated value

Closes #12386
This commit is contained in:
Veikka Tuominen 2022-08-10 10:59:26 +03:00 committed by Andrew Kelley
parent 49a270b203
commit 40447b25e8
2 changed files with 21 additions and 1 deletions

View File

@ -23990,7 +23990,10 @@ fn beginComptimePtrMutation(
const array_len_including_sentinel =
try sema.usizeCast(block, src, parent.ty.arrayLenIncludingSentinel());
const elems = try arena.alloc(Value, array_len_including_sentinel);
mem.set(Value, elems, repeated_val);
if (elems.len > 0) elems[0] = repeated_val;
for (elems[1..]) |*elem| {
elem.* = try repeated_val.copy(arena);
}
val_ptr.* = try Value.Tag.aggregate.create(arena, elems);

View File

@ -1293,3 +1293,20 @@ test "mutate through pointer-like optional at comptime" {
try expect(payload_ptr.*.* == 16);
}
}
test "repeated value is correctly expanded" {
const S = struct { x: [4]i8 = std.mem.zeroes([4]i8) };
const M = struct { x: [4]S = std.mem.zeroes([4]S) };
comptime {
var res = M{};
for (.{ 1, 2, 3 }) |i| res.x[i].x[i] = i;
try expectEqual(M{ .x = .{
.{ .x = .{ 0, 0, 0, 0 } },
.{ .x = .{ 0, 1, 0, 0 } },
.{ .x = .{ 0, 0, 2, 0 } },
.{ .x = .{ 0, 0, 0, 3 } },
} }, res);
}
}