Commit 44fb5275c1

Andrew Kelley <andrew@ziglang.org>
2019-08-19 20:46:12
fix array multiplication not setting parent value info
closes #3095
1 parent 3f7f520
Changed files (2)
src
test
stage1
behavior
src/ir.cpp
@@ -13963,8 +13963,11 @@ static IrInstruction *ir_analyze_array_mult(IrAnalyze *ira, IrInstructionBinOp *
     uint64_t i = 0;
     for (uint64_t x = 0; x < mult_amt; x += 1) {
         for (uint64_t y = 0; y < old_array_len; y += 1) {
-            copy_const_val(&out_val->data.x_array.data.s_none.elements[i],
-                &array_val->data.x_array.data.s_none.elements[y], false);
+            ConstExprValue *elem_dest_val = &out_val->data.x_array.data.s_none.elements[i];
+            copy_const_val(elem_dest_val, &array_val->data.x_array.data.s_none.elements[y], false);
+            elem_dest_val->parent.id = ConstParentIdArray;
+            elem_dest_val->parent.data.p_array.array_val = out_val;
+            elem_dest_val->parent.data.p_array.elem_index = i;
             i += 1;
         }
     }
test/stage1/behavior/array.zig
@@ -1,5 +1,6 @@
-const expect = @import("std").testing.expect;
-const mem = @import("std").mem;
+const std = @import("std");
+const expect = std.testing.expect;
+const mem = std.mem;
 
 test "arrays" {
     var array: [5]u32 = undefined;
@@ -274,3 +275,20 @@ test "double nested array to const slice cast in array literal" {
     S.entry(2);
     comptime S.entry(2);
 }
+
+test "read/write through global variable array of struct fields initialized via array mult" {
+    const S = struct {
+        fn doTheTest() void {
+            expect(storage[0].term == 1);
+            storage[0] = MyStruct{ .term = 123 };
+            expect(storage[0].term == 123);
+        }
+
+        pub const MyStruct = struct {
+            term: usize,
+        };
+
+        var storage: [1]MyStruct = [_]MyStruct{MyStruct{ .term = 1 }} ** 1;
+    };
+    S.doTheTest();
+}