mirror of
https://github.com/ziglang/zig.git
synced 2025-12-24 15:13:08 +00:00
Sema: add empty tuple to mutable slice coercion
This commit is contained in:
parent
b4bf3bdf7e
commit
51ef31a833
13
src/Sema.zig
13
src/Sema.zig
@ -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;
|
||||
|
||||
@ -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);
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user