authorgravatar for pentuppup@noreply.codeberg.orgpentuppup <pentuppup@noreply.codeberg.org> 2026-09-25 09:39:18-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2026-10-09 15:53:36-07:00
log20057b47d6e20059e8bb44e51f6cf5fd8e9534b6
tree60f10000a33fa5c623e6defe003445634047c992
parent27a58ea8832aece227bde9d333970046fb5cfee9

Sema: fix array sentinel being discarded when coerced to slice


3 files changed, 9 insertions(+), 3 deletions(-)

src/Sema.zig+1-1
......@@ -4253,7 +4253,7 @@ fn zirCoercePtrElemTy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileE
42534253 const want_ty = try pt.arrayType(.{
42544254 .len = val_ty.arrayLen(zcu),
42554255 .child = elem_ty.toIntern(),
4256 .sentinel = if (ptr_ty.sentinel(zcu)) |s| s.toIntern() else .none,
4256 .sentinel = if (val_ty.sentinel(zcu)) |s| s.toIntern() else .none,
42574257 });
42584258 return sema.coerce(block, want_ty, uncoerced_val, src);
42594259 },
test/behavior/slice.zig+6
......@@ -823,6 +823,12 @@ test "slice sentinel access at comptime" {
823823 try expect(slice0.len == 3);
824824 try expect(slice0[slice0.len] == 0);
825825 }
826 {
827 const slice0: []const u8 = &[_:0]u8{ '1', '2', '3' };
828
829 try expect(slice0.len == 3);
830 try expect(slice0.ptr[slice0.len] == 0);
831 }
826832}
827833
828834test "slicing array with sentinel as end index" {
test/cases/compile_errors/slice_sentinel_mismatch-1.zig+2-2
......@@ -10,7 +10,7 @@ export fn entry2() void {
1010
1111// error
1212//
13// :2:37: error: expected type '[2:1]u8', found '[2:2]u8'
14// :2:37: note: array sentinel '2' cannot cast into array sentinel '1'
13// :2:29: error: expected type '[:1]const u8', found '*const [2:2]u8'
14// :2:29: note: pointer sentinel '2' cannot cast into pointer sentinel '1'
1515// :7:29: error: expected type '[:1]const u8', found '[:2]const u8'
1616// :7:29: note: pointer sentinel '2' cannot cast into pointer sentinel '1'