Commit 40447b25e8

Veikka Tuominen <git@vexu.eu>
2022-08-10 09:59:26
Sema: fix expansion of repeated value
Closes #12386
1 parent 49a270b
Changed files (2)
src
test
behavior
src/Sema.zig
@@ -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);
 
test/behavior/eval.zig
@@ -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);
+    }
+}