Commit b9dcbe6b4c

Veikka Tuominen <git@vexu.eu>
2022-06-15 17:00:29
Sema: handle sentinels in tupleToArray
1 parent ffa6f89
Changed files (2)
src
test
behavior
src/Sema.zig
@@ -21793,7 +21793,7 @@ fn coerceTupleToArray(
 ) !Air.Inst.Ref {
     const inst_ty = sema.typeOf(inst);
     const inst_len = inst_ty.arrayLen();
-    const dest_len = try sema.usizeCast(block, dest_ty_src, dest_ty.arrayLen());
+    const dest_len = dest_ty.arrayLen();
 
     if (dest_len != inst_len) {
         const msg = msg: {
@@ -21808,13 +21808,19 @@ fn coerceTupleToArray(
         return sema.failWithOwnedErrorMsg(block, msg);
     }
 
-    const element_vals = try sema.arena.alloc(Value, dest_len);
-    const element_refs = try sema.arena.alloc(Air.Inst.Ref, dest_len);
+    const dest_elems = try sema.usizeCast(block, dest_ty_src, dest_ty.arrayLenIncludingSentinel());
+    const element_vals = try sema.arena.alloc(Value, dest_elems);
+    const element_refs = try sema.arena.alloc(Air.Inst.Ref, dest_elems);
     const dest_elem_ty = dest_ty.childType();
 
     var runtime_src: ?LazySrcLoc = null;
     for (element_vals) |*elem, i_usize| {
         const i = @intCast(u32, i_usize);
+        if (i_usize == inst_len) {
+            elem.* = dest_ty.sentinel().?;
+            element_refs[i] = try sema.addConstant(dest_elem_ty, elem.*);
+            break;
+        }
         const elem_src = inst_src; // TODO better source location
         const elem_ref = try tupleField(sema, block, inst_src, inst, elem_src, i);
         const coerced = try sema.coerce(block, dest_elem_ty, elem_ref, elem_src);
test/behavior/array.zig
@@ -582,3 +582,14 @@ test "array with comptime only element type" {
     try testing.expect(a[0] == u32);
     try testing.expect(a[1] == i32);
 }
+
+test "tuple to array handles sentinel" {
+    if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
+    if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
+
+    const S = struct {
+        const a = .{ 1, 2, 3 };
+        var b: [3:0]u8 = a;
+    };
+    try expect(S.b[0] == 1);
+}