authorgravatar for techatrix@mailbox.orgTechatrix <techatrix@mailbox.org> 2024-12-29 05:53:47+01:00
committergravatar for techatrix@mailbox.orgTechatrix <techatrix@mailbox.org> 2024-12-29 07:00:35+01:00
log5d51d4474a0c61f08c264c03ecbdf651d91afe82
tree96b99d998f9ed55da1b74dc13834c99da5921b35
parent2e40a1d22e6837fdfbe8d56065a7ca03a5270601
signaturebadge-check Signed by SSH key SHA256:HYC3SjXQcAt6uwv9pu/6OoVQ2rUH8rb5zKiUHSe9uxk

fix slice of slice with sentinel but no end index

example: ```zig test { var foo = ""; _ = foo[0..][0.. :0]; } ``` A `.slice_sentinel` ast node may not have an end index.

2 files changed, 19 insertions(+), 2 deletions(-)

lib/std/zig/AstGen.zig+2-2
......@@ -885,7 +885,7 @@ fn expr(gz: *GenZir, scope: *Scope, ri: ResultInfo, node: Ast.Node.Index) InnerE
885885 const lhs_is_slice_sentinel = lhs_tag == .slice_sentinel;
886886 const lhs_is_open_slice = lhs_tag == .slice_open or
887887 (lhs_is_slice_sentinel and tree.fullSlice(full.ast.sliced).?.ast.end == 0);
888 if (node_tags[node] != .slice_open and
888 if (full.ast.end != 0 and
889889 lhs_is_open_slice and
890890 nodeIsTriviallyZero(tree, full.ast.start))
891891 {
......@@ -897,7 +897,7 @@ fn expr(gz: *GenZir, scope: *Scope, ri: ResultInfo, node: Ast.Node.Index) InnerE
897897 } else try expr(gz, scope, .{ .rl = .{ .coerced_ty = .usize_type } }, node_datas[full.ast.sliced].rhs);
898898
899899 const cursor = maybeAdvanceSourceCursorToMainToken(gz, node);
900 const len = if (full.ast.end != 0) try expr(gz, scope, .{ .rl = .{ .coerced_ty = .usize_type } }, full.ast.end) else .none;
900 const len = try expr(gz, scope, .{ .rl = .{ .coerced_ty = .usize_type } }, full.ast.end);
901901 const sentinel = if (full.ast.sentinel != 0) try expr(gz, scope, .{ .rl = .none }, full.ast.sentinel) else .none;
902902 try emitDbgStmt(gz, cursor);
903903 const result = try gz.addPlNode(.slice_length, node, Zir.Inst.SliceLength{
test/behavior/slice.zig+17
......@@ -98,6 +98,23 @@ test "comptime slice of slice preserves comptime var" {
9898 }
9999}
100100
101test "open slice of open slice with sentinel" {
102 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
103
104 var slice: [:0]const u8 = "hello";
105 _ = &slice;
106
107 comptime assert(@TypeOf(slice[0..][0.. :0]) == [:0]const u8);
108 try expect(slice[0..][0.. :0].len == 5);
109 try expect(slice[0..][0.. :0][0] == 'h');
110 try expect(slice[0..][0.. :0][5] == 0);
111
112 comptime assert(@TypeOf(slice[1..][0.. :0]) == [:0]const u8);
113 try expect(slice[1..][0.. :0].len == 4);
114 try expect(slice[1..][0.. :0][0] == 'e');
115 try expect(slice[1..][0.. :0][4] == 0);
116}
117
101118test "slice of type" {
102119 comptime {
103120 var types_array = [_]type{ i32, f64, type };