Commit f33af8f071

drew <reserveblue@protonmail.com>
2021-11-15 09:08:57
fix array airStoreUndefined for arrays
1 parent cf99afc
Changed files (2)
src
codegen
test
behavior
src/codegen/c.zig
@@ -1583,7 +1583,7 @@ fn airStoreUndefined(f: *Function, dest_ptr: CValue, dest_type: Type) !CValue {
             try writer.writeAll("));\n");
         },
         else => {
-            const indirection = if (dest_type.zigTypeTag() == .Array) "" else "*";
+            const indirection = if (dest_type.childType().zigTypeTag() == .Array) "" else "*";
 
             try writer.writeAll("memset(");
             try f.writeCValue(writer, dest_ptr);
@@ -1608,7 +1608,7 @@ fn airStore(f: *Function, inst: Air.Inst.Index) !CValue {
         return try airStoreUndefined(f, dest_ptr, lhs_type);
 
     // Don't check this for airStoreUndefined as that will work for arrays already
-    if (lhs_type.zigTypeTag() == .Array)
+    if (lhs_type.childType().zigTypeTag() == .Array)
         return f.fail("TODO: C backend: implement airStore for arrays", .{});
 
     const writer = f.object.writer();
test/behavior/cast_c.zig
@@ -247,3 +247,14 @@ test "*const ?[*]const T to [*c]const [*c]const T" {
     try expect(b.*[0] == 'o');
     try expect(b[0][1] == 'k');
 }
+
+test "array coersion to undefined at runtime" {
+    @setRuntimeSafety(true);
+
+    var array = [4]u8{ 3, 4, 5, 6 };
+    var undefined_val = [4]u8{ 0xAA, 0xAA, 0xAA, 0xAA };
+
+    try expect(std.mem.eql(u8, &array, &array));
+    array = undefined;
+    try expect(std.mem.eql(u8, &array, &undefined_val));
+}