Skip to content

Commit ce3bff8

Browse files
committed
Sema: improve slice source locations
1 parent e5a3482 commit ce3bff8

File tree

3 files changed

+46
-13
lines changed

3 files changed

+46
-13
lines changed

src/Module.zig

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2171,7 +2171,11 @@ pub const SrcLoc = struct {
21712171
const token_starts = tree.tokens.items(.start);
21722172
return token_starts[tok_index];
21732173
},
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| {
21752179
const tree = try src_loc.file_scope.getTree(gpa);
21762180
const node_tags = tree.nodes.items(.tag);
21772181
const node = src_loc.declRelativeToNodeIndex(node_off);
@@ -2182,7 +2186,15 @@ pub const SrcLoc = struct {
21822186
else => unreachable,
21832187
};
21842188
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+
];
21862198
const token_starts = tree.tokens.items(.start);
21872199
return token_starts[tok_index];
21882200
},
@@ -2624,6 +2636,24 @@ pub const LazySrcLoc = union(enum) {
26242636
/// to the index expression.
26252637
/// The Decl is determined contextually.
26262638
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,
26272657
/// The source location points to the sentinel expression of a slice
26282658
/// expression, found by taking this AST node index offset from the containing
26292659
/// Decl AST node, which points to a slice AST node. Next, navigate
@@ -2781,6 +2811,9 @@ pub const LazySrcLoc = union(enum) {
27812811
.node_offset_builtin_call_arg4,
27822812
.node_offset_builtin_call_arg5,
27832813
.node_offset_array_access_index,
2814+
.node_offset_slice_ptr,
2815+
.node_offset_slice_start,
2816+
.node_offset_slice_end,
27842817
.node_offset_slice_sentinel,
27852818
.node_offset_call_func,
27862819
.node_offset_field_name,

src/Sema.zig

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22616,9 +22616,9 @@ fn analyzeSlice(
2261622616
sentinel_opt: Air.Inst.Ref,
2261722617
sentinel_src: LazySrcLoc,
2261822618
) CompileError!Air.Inst.Ref {
22619-
const ptr_src = src; // TODO better source location
22620-
const start_src = src; // TODO better source location
22621-
const end_src = src; // TODO better source location
22619+
const ptr_src: LazySrcLoc = .{ .node_offset_slice_ptr = src.node_offset.x };
22620+
const start_src: LazySrcLoc = .{ .node_offset_slice_start = src.node_offset.x };
22621+
const end_src: LazySrcLoc = .{ .node_offset_slice_end = src.node_offset.x };
2262222622
// Slice expressions can operate on a variable whose type is an array. This requires
2262322623
// the slice operand to be a pointer. In the case of a non-array, it will be a double pointer.
2262422624
const ptr_ptr_ty = sema.typeOf(ptr_ptr);
@@ -22648,7 +22648,7 @@ fn analyzeSlice(
2264822648
array_ty = double_child_ty;
2264922649
elem_ty = double_child_ty.childType();
2265022650
} else {
22651-
return sema.fail(block, ptr_src, "slice of single-item pointer", .{});
22651+
return sema.fail(block, src, "slice of single-item pointer", .{});
2265222652
}
2265322653
},
2265422654
.Many, .C => {
@@ -22661,7 +22661,7 @@ fn analyzeSlice(
2266122661
if (ptr_ptr_child_ty.ptrSize() == .C) {
2266222662
if (try sema.resolveDefinedValue(block, ptr_src, ptr_or_slice)) |ptr_val| {
2266322663
if (ptr_val.isNull()) {
22664-
return sema.fail(block, ptr_src, "slice of null pointer", .{});
22664+
return sema.fail(block, src, "slice of null pointer", .{});
2266522665
}
2266622666
}
2266722667
}
@@ -22674,7 +22674,7 @@ fn analyzeSlice(
2267422674
elem_ty = ptr_ptr_child_ty.childType();
2267522675
},
2267622676
},
22677-
else => return sema.fail(block, ptr_src, "slice of non-array type '{}'", .{ptr_ptr_child_ty.fmt(mod)}),
22677+
else => return sema.fail(block, src, "slice of non-array type '{}'", .{ptr_ptr_child_ty.fmt(mod)}),
2267822678
}
2267922679

2268022680
const ptr = if (slice_ty.isSlice())
@@ -22847,7 +22847,7 @@ fn analyzeSlice(
2284722847
return sema.addConstUndef(return_ty);
2284822848
}
2284922849

22850-
return sema.fail(block, ptr_src, "non-zero length slice of undefined pointer", .{});
22850+
return sema.fail(block, src, "non-zero length slice of undefined pointer", .{});
2285122851
}
2285222852

2285322853
const return_ty = try Type.ptr(sema.arena, mod, .{

test/cases/compile_errors/stage2/out_of_bounds_index.zig

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ comptime {
2323
// error
2424
// target=native
2525
//
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

0 commit comments

Comments
 (0)