| author | |
| committer | |
| log | 5d51d4474a0c61f08c264c03ecbdf651d91afe82 |
| tree | 96b99d998f9ed55da1b74dc13834c99da5921b35 |
| parent | 2e40a1d22e6837fdfbe8d56065a7ca03a5270601 |
| signature |
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 |
| 885 | 885 | const lhs_is_slice_sentinel = lhs_tag == .slice_sentinel; |
| 886 | 886 | const lhs_is_open_slice = lhs_tag == .slice_open or |
| 887 | 887 | (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 | |
| 889 | 889 | lhs_is_open_slice and |
| 890 | 890 | nodeIsTriviallyZero(tree, full.ast.start)) |
| 891 | 891 | { |
| ... | ... | @@ -897,7 +897,7 @@ fn expr(gz: *GenZir, scope: *Scope, ri: ResultInfo, node: Ast.Node.Index) InnerE |
| 897 | 897 | } else try expr(gz, scope, .{ .rl = .{ .coerced_ty = .usize_type } }, node_datas[full.ast.sliced].rhs); |
| 898 | 898 | |
| 899 | 899 | 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); | |
| 901 | 901 | const sentinel = if (full.ast.sentinel != 0) try expr(gz, scope, .{ .rl = .none }, full.ast.sentinel) else .none; |
| 902 | 902 | try emitDbgStmt(gz, cursor); |
| 903 | 903 | 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" { |
| 98 | 98 | } |
| 99 | 99 | } |
| 100 | 100 | |
| 101 | test "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 | ||
| 101 | 118 | test "slice of type" { |
| 102 | 119 | comptime { |
| 103 | 120 | var types_array = [_]type{ i32, f64, type }; |