| author | |
| committer | |
| log | 27ee4141592c7a9d77a2d73f5fa6a3c6262ac7fc |
| tree | 9077c3617875ad8511d3da525d5a0554ecbb2c98 |
| parent | 2ca752ea1ad4afb9d510687ae097c709668316b9 |
3 files changed, 46 insertions(+), 13 deletions(-)
src/Module.zig+35-2| ... | ... | @@ -2171,7 +2171,11 @@ pub const SrcLoc = struct { |
| 2171 | 2171 | const token_starts = tree.tokens.items(.start); |
| 2172 | 2172 | return token_starts[tok_index]; |
| 2173 | 2173 | }, |
| 2174 | .node_offset_slice_sentinel => |node_off| { | |
| 2174 | .node_offset_slice_ptr, | |
| 2175 | .node_offset_slice_start, | |
| 2176 | .node_offset_slice_end, | |
| 2177 | .node_offset_slice_sentinel, | |
| 2178 | => |node_off| { | |
| 2175 | 2179 | const tree = try src_loc.file_scope.getTree(gpa); |
| 2176 | 2180 | const node_tags = tree.nodes.items(.tag); |
| 2177 | 2181 | const node = src_loc.declRelativeToNodeIndex(node_off); |
| ... | ... | @@ -2182,7 +2186,15 @@ pub const SrcLoc = struct { |
| 2182 | 2186 | else => unreachable, |
| 2183 | 2187 | }; |
| 2184 | 2188 | const main_tokens = tree.nodes.items(.main_token); |
| 2185 | const tok_index = main_tokens[full.ast.sentinel]; | |
| 2189 | const tok_index = main_tokens[ | |
| 2190 | switch (src_loc.lazy) { | |
| 2191 | .node_offset_slice_ptr => full.ast.sliced, | |
| 2192 | .node_offset_slice_start => full.ast.start, | |
| 2193 | .node_offset_slice_end => full.ast.end, | |
| 2194 | .node_offset_slice_sentinel => full.ast.sentinel, | |
| 2195 | else => unreachable, | |
| 2196 | } | |
| 2197 | ]; | |
| 2186 | 2198 | const token_starts = tree.tokens.items(.start); |
| 2187 | 2199 | return token_starts[tok_index]; |
| 2188 | 2200 | }, |
| ... | ... | @@ -2624,6 +2636,24 @@ pub const LazySrcLoc = union(enum) { |
| 2624 | 2636 | /// to the index expression. |
| 2625 | 2637 | /// The Decl is determined contextually. |
| 2626 | 2638 | node_offset_array_access_index: i32, |
| 2639 | /// The source location points to the LHS of a slice expression | |
| 2640 | /// expression, found by taking this AST node index offset from the containing | |
| 2641 | /// Decl AST node, which points to a slice AST node. Next, navigate | |
| 2642 | /// to the sentinel expression. | |
| 2643 | /// The Decl is determined contextually. | |
| 2644 | node_offset_slice_ptr: i32, | |
| 2645 | /// The source location points to start expression of a slice expression | |
| 2646 | /// expression, found by taking this AST node index offset from the containing | |
| 2647 | /// Decl AST node, which points to a slice AST node. Next, navigate | |
| 2648 | /// to the sentinel expression. | |
| 2649 | /// The Decl is determined contextually. | |
| 2650 | node_offset_slice_start: i32, | |
| 2651 | /// The source location points to the end expression of a slice | |
| 2652 | /// expression, found by taking this AST node index offset from the containing | |
| 2653 | /// Decl AST node, which points to a slice AST node. Next, navigate | |
| 2654 | /// to the sentinel expression. | |
| 2655 | /// The Decl is determined contextually. | |
| 2656 | node_offset_slice_end: i32, | |
| 2627 | 2657 | /// The source location points to the sentinel expression of a slice |
| 2628 | 2658 | /// expression, found by taking this AST node index offset from the containing |
| 2629 | 2659 | /// Decl AST node, which points to a slice AST node. Next, navigate |
| ... | ... | @@ -2781,6 +2811,9 @@ pub const LazySrcLoc = union(enum) { |
| 2781 | 2811 | .node_offset_builtin_call_arg4, |
| 2782 | 2812 | .node_offset_builtin_call_arg5, |
| 2783 | 2813 | .node_offset_array_access_index, |
| 2814 | .node_offset_slice_ptr, | |
| 2815 | .node_offset_slice_start, | |
| 2816 | .node_offset_slice_end, | |
| 2784 | 2817 | .node_offset_slice_sentinel, |
| 2785 | 2818 | .node_offset_call_func, |
| 2786 | 2819 | .node_offset_field_name, |
src/Sema.zig+7-7| ... | ... | @@ -22615,9 +22615,9 @@ fn analyzeSlice( |
| 22615 | 22615 | sentinel_opt: Air.Inst.Ref, |
| 22616 | 22616 | sentinel_src: LazySrcLoc, |
| 22617 | 22617 | ) CompileError!Air.Inst.Ref { |
| 22618 | const ptr_src = src; // TODO better source location | |
| 22619 | const start_src = src; // TODO better source location | |
| 22620 | const end_src = src; // TODO better source location | |
| 22618 | const ptr_src: LazySrcLoc = .{ .node_offset_slice_ptr = src.node_offset.x }; | |
| 22619 | const start_src: LazySrcLoc = .{ .node_offset_slice_start = src.node_offset.x }; | |
| 22620 | const end_src: LazySrcLoc = .{ .node_offset_slice_end = src.node_offset.x }; | |
| 22621 | 22621 | // Slice expressions can operate on a variable whose type is an array. This requires |
| 22622 | 22622 | // the slice operand to be a pointer. In the case of a non-array, it will be a double pointer. |
| 22623 | 22623 | const ptr_ptr_ty = sema.typeOf(ptr_ptr); |
| ... | ... | @@ -22647,7 +22647,7 @@ fn analyzeSlice( |
| 22647 | 22647 | array_ty = double_child_ty; |
| 22648 | 22648 | elem_ty = double_child_ty.childType(); |
| 22649 | 22649 | } else { |
| 22650 | return sema.fail(block, ptr_src, "slice of single-item pointer", .{}); | |
| 22650 | return sema.fail(block, src, "slice of single-item pointer", .{}); | |
| 22651 | 22651 | } |
| 22652 | 22652 | }, |
| 22653 | 22653 | .Many, .C => { |
| ... | ... | @@ -22660,7 +22660,7 @@ fn analyzeSlice( |
| 22660 | 22660 | if (ptr_ptr_child_ty.ptrSize() == .C) { |
| 22661 | 22661 | if (try sema.resolveDefinedValue(block, ptr_src, ptr_or_slice)) |ptr_val| { |
| 22662 | 22662 | if (ptr_val.isNull()) { |
| 22663 | return sema.fail(block, ptr_src, "slice of null pointer", .{}); | |
| 22663 | return sema.fail(block, src, "slice of null pointer", .{}); | |
| 22664 | 22664 | } |
| 22665 | 22665 | } |
| 22666 | 22666 | } |
| ... | ... | @@ -22673,7 +22673,7 @@ fn analyzeSlice( |
| 22673 | 22673 | elem_ty = ptr_ptr_child_ty.childType(); |
| 22674 | 22674 | }, |
| 22675 | 22675 | }, |
| 22676 | else => return sema.fail(block, ptr_src, "slice of non-array type '{}'", .{ptr_ptr_child_ty.fmt(mod)}), | |
| 22676 | else => return sema.fail(block, src, "slice of non-array type '{}'", .{ptr_ptr_child_ty.fmt(mod)}), | |
| 22677 | 22677 | } |
| 22678 | 22678 | |
| 22679 | 22679 | const ptr = if (slice_ty.isSlice()) |
| ... | ... | @@ -22846,7 +22846,7 @@ fn analyzeSlice( |
| 22846 | 22846 | return sema.addConstUndef(return_ty); |
| 22847 | 22847 | } |
| 22848 | 22848 | |
| 22849 | return sema.fail(block, ptr_src, "non-zero length slice of undefined pointer", .{}); | |
| 22849 | return sema.fail(block, src, "non-zero length slice of undefined pointer", .{}); | |
| 22850 | 22850 | } |
| 22851 | 22851 | |
| 22852 | 22852 | const return_ty = try Type.ptr(sema.arena, mod, .{ |
test/cases/compile_errors/stage2/out_of_bounds_index.zig+4-4| ... | ... | @@ -23,7 +23,7 @@ comptime { |
| 23 | 23 | // error |
| 24 | 24 | // target=native |
| 25 | 25 | // |
| 26 | // :4:26: error: end index 6 out of bounds for slice of length 4 +1 (sentinel) | |
| 27 | // :9:22: error: end index 6 out of bounds for array of length 4 +1 (sentinel) | |
| 28 | // :14:22: error: end index 5 out of bounds for array of length 4 | |
| 29 | // :19:22: error: start index 3 is larger than end index 2 | |
| 26 | // :4:30: error: end index 6 out of bounds for slice of length 4 +1 (sentinel) | |
| 27 | // :9:26: error: end index 6 out of bounds for array of length 4 +1 (sentinel) | |
| 28 | // :14:26: error: end index 5 out of bounds for array of length 4 | |
| 29 | // :19:23: error: start index 3 is larger than end index 2 |