From a4695d4db91763b5aabe15c8cc4c906325815d50 Mon Sep 17 00:00:00 2001 From: Matthew Lugg Date: Wed, 21 Oct 2026 16:07:51 +0100 Subject: [PATCH] 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 --- src/Sema.zig | 1 + test/behavior/cast.zig | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/src/Sema.zig b/src/Sema.zig index fbf99b86c364b2a2aaa0d6e7524614d3f1ecffdd..78649a79aac1945799b1a899cc87148e7e954e8d 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -28452,6 +28452,7 @@ fn coerceExtra( .child = dest_info.child, .sentinel = dest_info.sentinel, }); + try sema.ensureLayoutResolved(empty_array_ty, inst_src, .init); const empty_array_val = try pt.aggregateValue(empty_array_ty, &.{}); const empty_array_ptr = try sema.uavRef(empty_array_val); return sema.coerceArrayPtrToSlice(block, dest_ty, empty_array_ptr, inst_src); diff --git a/test/behavior/cast.zig b/test/behavior/cast.zig index 46d7b1758e0229a10bd4f1163becd58826cd6d9c..eb252a3d5cea2a895643ddda3643e2b7d598a592 100644 --- a/test/behavior/cast.zig +++ b/test/behavior/cast.zig @@ -3164,3 +3164,23 @@ test "coercing a coerced function" { }; try S.doTheTest(); } + +test "pointer to empty tuple coerces to any slice" { + const tuple_ptr = &.{}; + + const const_slice_0: []const u8 = tuple_ptr; + const const_slice_1: []const struct {} = tuple_ptr; + const const_slice_2: []align(64) const struct { x: u8 } = tuple_ptr; + + const mut_slice_0: []const u8 = tuple_ptr; + const mut_slice_1: []const struct {} = tuple_ptr; + const mut_slice_2: []align(64) const struct { x: u8 } = tuple_ptr; + + comptime assert(const_slice_0.len == 0); + comptime assert(const_slice_1.len == 0); + comptime assert(const_slice_2.len == 0); + + comptime assert(mut_slice_0.len == 0); + comptime assert(mut_slice_1.len == 0); + comptime assert(mut_slice_2.len == 0); +} -- 2.54.0