authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-06-15 18:00:29+03:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-06-17 18:57:02+03:00
logb9dcbe6b4c0bee37b31bf46424f284af331d4696
tree3b412150f58deab886ff4002053447a77d539310
parentffa6f895fff52846bc4b82cc9b449d0f7224d7d9

Sema: handle sentinels in tupleToArray


2 files changed, 20 insertions(+), 3 deletions(-)

src/Sema.zig+9-3
...@@ -21793,7 +21793,7 @@ fn coerceTupleToArray(...@@ -21793,7 +21793,7 @@ fn coerceTupleToArray(
21793) !Air.Inst.Ref {21793) !Air.Inst.Ref {
21794 const inst_ty = sema.typeOf(inst);21794 const inst_ty = sema.typeOf(inst);
21795 const inst_len = inst_ty.arrayLen();21795 const inst_len = inst_ty.arrayLen();
21796 const dest_len = try sema.usizeCast(block, dest_ty_src, dest_ty.arrayLen());21796 const dest_len = dest_ty.arrayLen();
2179721797
21798 if (dest_len != inst_len) {21798 if (dest_len != inst_len) {
21799 const msg = msg: {21799 const msg = msg: {
...@@ -21808,13 +21808,19 @@ fn coerceTupleToArray(...@@ -21808,13 +21808,19 @@ fn coerceTupleToArray(
21808 return sema.failWithOwnedErrorMsg(block, msg);21808 return sema.failWithOwnedErrorMsg(block, msg);
21809 }21809 }
2181021810
21811 const element_vals = try sema.arena.alloc(Value, dest_len);21811 const dest_elems = try sema.usizeCast(block, dest_ty_src, dest_ty.arrayLenIncludingSentinel());
21812 const element_refs = try sema.arena.alloc(Air.Inst.Ref, dest_len);21812 const element_vals = try sema.arena.alloc(Value, dest_elems);
21813 const element_refs = try sema.arena.alloc(Air.Inst.Ref, dest_elems);
21813 const dest_elem_ty = dest_ty.childType();21814 const dest_elem_ty = dest_ty.childType();
2181421815
21815 var runtime_src: ?LazySrcLoc = null;21816 var runtime_src: ?LazySrcLoc = null;
21816 for (element_vals) |*elem, i_usize| {21817 for (element_vals) |*elem, i_usize| {
21817 const i = @intCast(u32, i_usize);21818 const i = @intCast(u32, i_usize);
21819 if (i_usize == inst_len) {
21820 elem.* = dest_ty.sentinel().?;
21821 element_refs[i] = try sema.addConstant(dest_elem_ty, elem.*);
21822 break;
21823 }
21818 const elem_src = inst_src; // TODO better source location21824 const elem_src = inst_src; // TODO better source location
21819 const elem_ref = try tupleField(sema, block, inst_src, inst, elem_src, i);21825 const elem_ref = try tupleField(sema, block, inst_src, inst, elem_src, i);
21820 const coerced = try sema.coerce(block, dest_elem_ty, elem_ref, elem_src);21826 const coerced = try sema.coerce(block, dest_elem_ty, elem_ref, elem_src);
test/behavior/array.zig+11
...@@ -582,3 +582,14 @@ test "array with comptime only element type" {...@@ -582,3 +582,14 @@ test "array with comptime only element type" {
582 try testing.expect(a[0] == u32);582 try testing.expect(a[0] == u32);
583 try testing.expect(a[1] == i32);583 try testing.expect(a[1] == i32);
584}584}
585
586test "tuple to array handles sentinel" {
587 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
588 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
589
590 const S = struct {
591 const a = .{ 1, 2, 3 };
592 var b: [3:0]u8 = a;
593 };
594 try expect(S.b[0] == 1);
595}