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-10 00:22:23+02:00
log85c863e348d5ba2a1aca3455fefa073047e77e4b
tree2d1cc5f18a898bf8379e98eaa823f8fa73cafcc8
parent4a7f1339ac08bd61ddb6a25127a0ae34dc55c26d

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...@@ -4253,7 +4253,7 @@ fn zirCoercePtrElemTy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileE
4253 const want_ty = try pt.arrayType(.{4253 const want_ty = try pt.arrayType(.{
4254 .len = val_ty.arrayLen(zcu),4254 .len = val_ty.arrayLen(zcu),
4255 .child = elem_ty.toIntern(),4255 .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,
4257 });4257 });
4258 return sema.coerce(block, want_ty, uncoerced_val, src);4258 return sema.coerce(block, want_ty, uncoerced_val, src);
4259 },4259 },
test/behavior/slice.zig+6
...@@ -823,6 +823,12 @@ test "slice sentinel access at comptime" {...@@ -823,6 +823,12 @@ test "slice sentinel access at comptime" {
823 try expect(slice0.len == 3);823 try expect(slice0.len == 3);
824 try expect(slice0[slice0.len] == 0);824 try expect(slice0[slice0.len] == 0);
825 }825 }
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 }
826}832}
827833
828test "slicing array with sentinel as end index" {834test "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 {...@@ -10,7 +10,7 @@ export fn entry2() void {
1010
11// error11// error
12//12//
13// :2:37: error: expected type '[2:1]u8', found '[2:2]u8'13// :2:29: error: expected type '[:1]const u8', found '*const [2:2]u8'
14// :2:37: note: array sentinel '2' cannot cast into array sentinel '1'14// :2:29: note: pointer sentinel '2' cannot cast into pointer sentinel '1'
15// :7:29: error: expected type '[:1]const u8', found '[:2]const u8'15// :7:29: error: expected type '[:1]const u8', found '[:2]const u8'
16// :7:29: note: pointer sentinel '2' cannot cast into pointer sentinel '1'16// :7:29: note: pointer sentinel '2' cannot cast into pointer sentinel '1'