Sema: add empty tuple to mutable slice coercion

This commit is contained in:
Andrew Kelley 2022-04-04 14:28:30 -07:00
parent b4bf3bdf7e
commit 51ef31a833
2 changed files with 18 additions and 0 deletions

View File

@ -18166,6 +18166,19 @@ fn coerce(
{
return sema.coerceTupleToSlicePtrs(block, dest_ty, dest_ty_src, inst, inst_src);
}
// empty tuple to zero-length slice
// note that this allows coercing to a mutable slice.
if (inst_ty.isSinglePointer() and
inst_ty.childType().tag() == .empty_struct_literal and
dest_info.size == .Slice)
{
const slice_val = try Value.Tag.slice.create(sema.arena, .{
.ptr = Value.undef,
.len = Value.zero,
});
return sema.addConstant(dest_ty, slice_val);
}
},
.Many => p: {
if (!inst_ty.isSlice()) break :p;

View File

@ -1386,3 +1386,8 @@ test "coerce undefined single-item pointer of array to error union of slice" {
const s = try b;
try expect(s.len == 0);
}
test "pointer to empty struct literal to mutable slice" {
var x: []i32 = &.{};
try expect(x.len == 0);
}