aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVeikka Tuominen <git@vexu.eu>2022-06-15 18:00:29 +0300
committerVeikka Tuominen <git@vexu.eu>2022-06-17 18:57:02 +0300
commitb9dcbe6b4c0bee37b31bf46424f284af331d4696 (patch)
tree3b412150f58deab886ff4002053447a77d539310
parentffa6f895fff52846bc4b82cc9b449d0f7224d7d9 (diff)
downloadzig-b9dcbe6b4c0bee37b31bf46424f284af331d4696.tar.gz
zig-b9dcbe6b4c0bee37b31bf46424f284af331d4696.zip
Sema: handle sentinels in tupleToArray
-rw-r--r--src/Sema.zig12
-rw-r--r--test/behavior/array.zig11
2 files changed, 20 insertions, 3 deletions
diff --git a/src/Sema.zig b/src/Sema.zig
index af3e824855..1d5342b841 100644
--- a/src/Sema.zig
+++ b/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);
diff --git a/test/behavior/array.zig b/test/behavior/array.zig
index bccff1edf0..6482bfa1fe 100644
--- a/test/behavior/array.zig
+++ b/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);
+}