authorgravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2026-10-21 16:07:51+01:00
committergravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2026-10-23 01:35:27+02:00
log3a7eee7d0bc55891d3b83339a1679f1bf88759b7
tree0df11dcf391e6914c6d5c0902ac0c79b7994506c
parentc36f2bad31bdf7cae2a976fe07e2952753d553c4

Sema: fix coercion from untyped `&.{}` to empty slice

We weren't resolving the type before creating the value, which was causing a crash in the LLVM backend. Resolves: https://codeberg.org/ziglang/zig/issues/37155

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

src/Sema.zig+1
...@@ -28480,6 +28480,7 @@ fn coerceExtra(...@@ -28480,6 +28480,7 @@ fn coerceExtra(
28480 .child = dest_info.child,28480 .child = dest_info.child,
28481 .sentinel = dest_info.sentinel,28481 .sentinel = dest_info.sentinel,
28482 });28482 });
28483 try sema.ensureLayoutResolved(empty_array_ty, inst_src, .init);
28483 const empty_array_val = try pt.aggregateValue(empty_array_ty, &.{});28484 const empty_array_val = try pt.aggregateValue(empty_array_ty, &.{});
28484 const empty_array_ptr = try sema.uavRef(empty_array_val);28485 const empty_array_ptr = try sema.uavRef(empty_array_val);
28485 return sema.coerceArrayPtrToSlice(block, dest_ty, empty_array_ptr, inst_src);28486 return sema.coerceArrayPtrToSlice(block, dest_ty, empty_array_ptr, inst_src);
test/behavior/cast.zig+20
...@@ -3164,3 +3164,23 @@ test "coercing a coerced function" {...@@ -3164,3 +3164,23 @@ test "coercing a coerced function" {
3164 };3164 };
3165 try S.doTheTest();3165 try S.doTheTest();
3166}3166}
3167
3168test "pointer to empty tuple coerces to any slice" {
3169 const tuple_ptr = &.{};
3170
3171 const const_slice_0: []const u8 = tuple_ptr;
3172 const const_slice_1: []const struct {} = tuple_ptr;
3173 const const_slice_2: []align(64) const struct { x: u8 } = tuple_ptr;
3174
3175 const mut_slice_0: []const u8 = tuple_ptr;
3176 const mut_slice_1: []const struct {} = tuple_ptr;
3177 const mut_slice_2: []align(64) const struct { x: u8 } = tuple_ptr;
3178
3179 comptime assert(const_slice_0.len == 0);
3180 comptime assert(const_slice_1.len == 0);
3181 comptime assert(const_slice_2.len == 0);
3182
3183 comptime assert(mut_slice_0.len == 0);
3184 comptime assert(mut_slice_1.len == 0);
3185 comptime assert(mut_slice_2.len == 0);
3186}