diff --git a/src/Sema.zig b/src/Sema.zig index 65550bec00..1430e80ac0 100644 --- a/src/Sema.zig +++ b/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; diff --git a/test/behavior/cast.zig b/test/behavior/cast.zig index 4e952828c5..0473a36033 100644 --- a/test/behavior/cast.zig +++ b/test/behavior/cast.zig @@ -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); +}