From 85c863e348d5ba2a1aca3455fefa073047e77e4b Mon Sep 17 00:00:00 2001 From: pentuppup Date: Fri, 25 Sep 2026 09:39:18 -0400 Subject: [PATCH] Sema: fix array sentinel being discarded when coerced to slice --- src/Sema.zig | 2 +- test/behavior/slice.zig | 6 ++++++ test/cases/compile_errors/slice_sentinel_mismatch-1.zig | 4 ++-- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/Sema.zig b/src/Sema.zig index d9c196bb22a14fa1852a81bbf0808998768eaab8..82f96bd869ccba22898a948b9b4b51b695b19d94 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -4253,7 +4253,7 @@ fn zirCoercePtrElemTy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileE const want_ty = try pt.arrayType(.{ .len = val_ty.arrayLen(zcu), .child = elem_ty.toIntern(), - .sentinel = if (ptr_ty.sentinel(zcu)) |s| s.toIntern() else .none, + .sentinel = if (val_ty.sentinel(zcu)) |s| s.toIntern() else .none, }); return sema.coerce(block, want_ty, uncoerced_val, src); }, diff --git a/test/behavior/slice.zig b/test/behavior/slice.zig index 9ba9014c66200f546c90a9e9fd1c3d6c72416559..e4d7e9d1a75b4e0026b68f403ae649a7bd8a0d86 100644 --- a/test/behavior/slice.zig +++ b/test/behavior/slice.zig @@ -823,6 +823,12 @@ test "slice sentinel access at comptime" { try expect(slice0.len == 3); try expect(slice0[slice0.len] == 0); } + { + const slice0: []const u8 = &[_:0]u8{ '1', '2', '3' }; + + try expect(slice0.len == 3); + try expect(slice0.ptr[slice0.len] == 0); + } } test "slicing array with sentinel as end index" { diff --git a/test/cases/compile_errors/slice_sentinel_mismatch-1.zig b/test/cases/compile_errors/slice_sentinel_mismatch-1.zig index de776eeb048061573970643d51ce0522ac1d4047..897717571c95af9f9b0413d58edc5ba2ccdf58af 100644 --- a/test/cases/compile_errors/slice_sentinel_mismatch-1.zig +++ b/test/cases/compile_errors/slice_sentinel_mismatch-1.zig @@ -10,7 +10,7 @@ export fn entry2() void { // error // -// :2:37: error: expected type '[2:1]u8', found '[2:2]u8' -// :2:37: note: array sentinel '2' cannot cast into array sentinel '1' +// :2:29: error: expected type '[:1]const u8', found '*const [2:2]u8' +// :2:29: note: pointer sentinel '2' cannot cast into pointer sentinel '1' // :7:29: error: expected type '[:1]const u8', found '[:2]const u8' // :7:29: note: pointer sentinel '2' cannot cast into pointer sentinel '1' -- 2.54.0