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(
1816618166 {
1816718167 return sema.coerceTupleToSlicePtrs(block, dest_ty, dest_ty_src, inst, inst_src);
1816818168 }
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 }
1816918182 },
1817018183 .Many => p: {
1817118184 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" {
13861386 const s = try b;
13871387 try expect(s.len == 0);
13881388}
1389
1390test "pointer to empty struct literal to mutable slice" {
1391 var x: []i32 = &.{};
1392 try expect(x.len == 0);
1393}