authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-07-06 12:17:04+03:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-07-07 10:50:06+03:00
log27ee4141592c7a9d77a2d73f5fa6a3c6262ac7fc
tree9077c3617875ad8511d3da525d5a0554ecbb2c98
parent2ca752ea1ad4afb9d510687ae097c709668316b9

Sema: improve slice source locations


3 files changed, 46 insertions(+), 13 deletions(-)

src/Module.zig+35-2
...@@ -2171,7 +2171,11 @@ pub const SrcLoc = struct {...@@ -2171,7 +2171,11 @@ pub const SrcLoc = struct {
2171 const token_starts = tree.tokens.items(.start);2171 const token_starts = tree.tokens.items(.start);
2172 return token_starts[tok_index];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 const tree = try src_loc.file_scope.getTree(gpa);2179 const tree = try src_loc.file_scope.getTree(gpa);
2176 const node_tags = tree.nodes.items(.tag);2180 const node_tags = tree.nodes.items(.tag);
2177 const node = src_loc.declRelativeToNodeIndex(node_off);2181 const node = src_loc.declRelativeToNodeIndex(node_off);
...@@ -2182,7 +2186,15 @@ pub const SrcLoc = struct {...@@ -2182,7 +2186,15 @@ pub const SrcLoc = struct {
2182 else => unreachable,2186 else => unreachable,
2183 };2187 };
2184 const main_tokens = tree.nodes.items(.main_token);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 const token_starts = tree.tokens.items(.start);2198 const token_starts = tree.tokens.items(.start);
2187 return token_starts[tok_index];2199 return token_starts[tok_index];
2188 },2200 },
...@@ -2624,6 +2636,24 @@ pub const LazySrcLoc = union(enum) {...@@ -2624,6 +2636,24 @@ pub const LazySrcLoc = union(enum) {
2624 /// to the index expression.2636 /// to the index expression.
2625 /// The Decl is determined contextually.2637 /// The Decl is determined contextually.
2626 node_offset_array_access_index: i32,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 /// The source location points to the sentinel expression of a slice2657 /// The source location points to the sentinel expression of a slice
2628 /// expression, found by taking this AST node index offset from the containing2658 /// expression, found by taking this AST node index offset from the containing
2629 /// Decl AST node, which points to a slice AST node. Next, navigate2659 /// Decl AST node, which points to a slice AST node. Next, navigate
...@@ -2781,6 +2811,9 @@ pub const LazySrcLoc = union(enum) {...@@ -2781,6 +2811,9 @@ pub const LazySrcLoc = union(enum) {
2781 .node_offset_builtin_call_arg4,2811 .node_offset_builtin_call_arg4,
2782 .node_offset_builtin_call_arg5,2812 .node_offset_builtin_call_arg5,
2783 .node_offset_array_access_index,2813 .node_offset_array_access_index,
2814 .node_offset_slice_ptr,
2815 .node_offset_slice_start,
2816 .node_offset_slice_end,
2784 .node_offset_slice_sentinel,2817 .node_offset_slice_sentinel,
2785 .node_offset_call_func,2818 .node_offset_call_func,
2786 .node_offset_field_name,2819 .node_offset_field_name,
src/Sema.zig+7-7
...@@ -22615,9 +22615,9 @@ fn analyzeSlice(...@@ -22615,9 +22615,9 @@ fn analyzeSlice(
22615 sentinel_opt: Air.Inst.Ref,22615 sentinel_opt: Air.Inst.Ref,
22616 sentinel_src: LazySrcLoc,22616 sentinel_src: LazySrcLoc,
22617) CompileError!Air.Inst.Ref {22617) CompileError!Air.Inst.Ref {
22618 const ptr_src = src; // TODO better source location22618 const ptr_src: LazySrcLoc = .{ .node_offset_slice_ptr = src.node_offset.x };
22619 const start_src = src; // TODO better source location22619 const start_src: LazySrcLoc = .{ .node_offset_slice_start = src.node_offset.x };
22620 const end_src = src; // TODO better source location22620 const end_src: LazySrcLoc = .{ .node_offset_slice_end = src.node_offset.x };
22621 // Slice expressions can operate on a variable whose type is an array. This requires22621 // Slice expressions can operate on a variable whose type is an array. This requires
22622 // the slice operand to be a pointer. In the case of a non-array, it will be a double pointer.22622 // the slice operand to be a pointer. In the case of a non-array, it will be a double pointer.
22623 const ptr_ptr_ty = sema.typeOf(ptr_ptr);22623 const ptr_ptr_ty = sema.typeOf(ptr_ptr);
...@@ -22647,7 +22647,7 @@ fn analyzeSlice(...@@ -22647,7 +22647,7 @@ fn analyzeSlice(
22647 array_ty = double_child_ty;22647 array_ty = double_child_ty;
22648 elem_ty = double_child_ty.childType();22648 elem_ty = double_child_ty.childType();
22649 } else {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 .Many, .C => {22653 .Many, .C => {
...@@ -22660,7 +22660,7 @@ fn analyzeSlice(...@@ -22660,7 +22660,7 @@ fn analyzeSlice(
22660 if (ptr_ptr_child_ty.ptrSize() == .C) {22660 if (ptr_ptr_child_ty.ptrSize() == .C) {
22661 if (try sema.resolveDefinedValue(block, ptr_src, ptr_or_slice)) |ptr_val| {22661 if (try sema.resolveDefinedValue(block, ptr_src, ptr_or_slice)) |ptr_val| {
22662 if (ptr_val.isNull()) {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,7 +22673,7 @@ fn analyzeSlice(
22673 elem_ty = ptr_ptr_child_ty.childType();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 }
2267822678
22679 const ptr = if (slice_ty.isSlice())22679 const ptr = if (slice_ty.isSlice())
...@@ -22846,7 +22846,7 @@ fn analyzeSlice(...@@ -22846,7 +22846,7 @@ fn analyzeSlice(
22846 return sema.addConstUndef(return_ty);22846 return sema.addConstUndef(return_ty);
22847 }22847 }
2284822848
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 }
2285122851
22852 const return_ty = try Type.ptr(sema.arena, mod, .{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,7 +23,7 @@ comptime {
23// error23// error
24// target=native24// target=native
25//25//
26// :4:26: error: end index 6 out of bounds for slice of length 4 +1 (sentinel)26// :4:30: 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)27// :9:26: 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 428// :14:26: error: end index 5 out of bounds for array of length 4
29// :19:22: error: start index 3 is larger than end index 229// :19:23: error: start index 3 is larger than end index 2