authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-04-04 14:28:30-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-04-04 14:29:08-07:00
log51ef31a8331e92103253a37ddfdacbd263cee81d
tree4c4d493f1daa49a8fe04cb22d8f0485a90af7504
parentb4bf3bdf7eac05c5e4ff887294385946f4dd5f3f

Sema: add empty tuple to mutable slice coercion


2 files changed, 18 insertions(+), 0 deletions(-)

src/Sema.zig+13
...@@ -18166,6 +18166,19 @@ fn coerce(...@@ -18166,6 +18166,19 @@ fn coerce(
18166 {18166 {
18167 return sema.coerceTupleToSlicePtrs(block, dest_ty, dest_ty_src, inst, inst_src);18167 return sema.coerceTupleToSlicePtrs(block, dest_ty, dest_ty_src, inst, inst_src);
18168 }18168 }
18169
18170 // empty tuple to zero-length slice
18171 // note that this allows coercing to a mutable slice.
18172 if (inst_ty.isSinglePointer() and
18173 inst_ty.childType().tag() == .empty_struct_literal and
18174 dest_info.size == .Slice)
18175 {
18176 const slice_val = try Value.Tag.slice.create(sema.arena, .{
18177 .ptr = Value.undef,
18178 .len = Value.zero,
18179 });
18180 return sema.addConstant(dest_ty, slice_val);
18181 }
18169 },18182 },
18170 .Many => p: {18183 .Many => p: {
18171 if (!inst_ty.isSlice()) break :p;18184 if (!inst_ty.isSlice()) break :p;
test/behavior/cast.zig+5
...@@ -1386,3 +1386,8 @@ test "coerce undefined single-item pointer of array to error union of slice" {...@@ -1386,3 +1386,8 @@ test "coerce undefined single-item pointer of array to error union of slice" {
1386 const s = try b;1386 const s = try b;
1387 try expect(s.len == 0);1387 try expect(s.len == 0);
1388}1388}
1389
1390test "pointer to empty struct literal to mutable slice" {
1391 var x: []i32 = &.{};
1392 try expect(x.len == 0);
1393}