authorgravatar for pentuppup@noreply.codeberg.orgpentuppup <pentuppup@noreply.codeberg.org> 2026-07-17 19:52:36-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2026-10-10 00:27:07+02:00
logf46b6501a2626dae6b6419e904972ab702fab27e
tree437bcbdddeb1833bd81edf7a7a14553ada92e6eb
parent1f011a9bac506ae62fff5e5e2c7843be7365419c

AstGen: give result type to slice by length sentinel


2 files changed, 12 insertions(+), 1 deletions(-)

lib/std/zig/AstGen.zig+4-1
...@@ -880,7 +880,10 @@ fn expr(gz: *GenZir, scope: *Scope, ri: ResultInfo, node: Ast.Node.Index) InnerE...@@ -880,7 +880,10 @@ fn expr(gz: *GenZir, scope: *Scope, ri: ResultInfo, node: Ast.Node.Index) InnerE
880 const start = try expr(gz, scope, .{ .rl = .{ .coerced_ty = .usize_type } }, lhs_extra.start);880 const start = try expr(gz, scope, .{ .rl = .{ .coerced_ty = .usize_type } }, lhs_extra.start);
881 const cursor = maybeAdvanceSourceCursorToMainToken(gz, node);881 const cursor = maybeAdvanceSourceCursorToMainToken(gz, node);
882 const len = try expr(gz, scope, .{ .rl = .{ .coerced_ty = .usize_type } }, full.ast.end.unwrap().?);882 const len = try expr(gz, scope, .{ .rl = .{ .coerced_ty = .usize_type } }, full.ast.end.unwrap().?);
883 const sentinel = if (full.ast.sentinel.unwrap()) |sentinel| try expr(gz, scope, .{ .rl = .none }, sentinel) else .none;883 const sentinel = if (full.ast.sentinel.unwrap()) |sentinel| s: {
884 const sentinel_ty = try gz.addUnNode(.slice_sentinel_ty, lhs, node);
885 break :s try expr(gz, scope, .{ .rl = .{ .coerced_ty = sentinel_ty } }, sentinel);
886 } else .none;
884 try emitDbgStmt(gz, cursor);887 try emitDbgStmt(gz, cursor);
885 const result = try gz.addPlNode(.slice_length, node, Zir.Inst.SliceLength{888 const result = try gz.addPlNode(.slice_length, node, Zir.Inst.SliceLength{
886 .lhs = lhs,889 .lhs = lhs,
test/behavior/slice.zig+8
...@@ -1090,6 +1090,14 @@ test "sentinel expression in slice operation has result type" {...@@ -1090,6 +1090,14 @@ test "sentinel expression in slice operation has result type" {
1090 comptime assert(slice.len == 2);1090 comptime assert(slice.len == 2);
1091 comptime assert(slice[0] == 1);1091 comptime assert(slice[0] == 1);
1092 comptime assert(slice[1] == 2);1092 comptime assert(slice[1] == 2);
1093
1094 const by_len = arr[0..][0..2 :@intCast(sentinel)];
1095
1096 comptime assert(@TypeOf(by_len) == *const [2:sentinel]u16);
1097 comptime assert(by_len[2] == sentinel);
1098 comptime assert(by_len.len == 2);
1099 comptime assert(by_len[0] == 1);
1100 comptime assert(by_len[1] == 2);
1093}1101}
10941102
1095test "conditionally return second argument slice" {1103test "conditionally return second argument slice" {