From 2ca752ea1ad4afb9d510687ae097c709668316b9 Mon Sep 17 00:00:00 2001 From: Veikka Tuominen Date: Wed, 6 Jul 2022 11:36:39 +0300 Subject: Module: add `.node_offset_un_op` --- src/Module.zig | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'src/Module.zig') diff --git a/src/Module.zig b/src/Module.zig index 1f70a44df5..d4e981f834 100644 --- a/src/Module.zig +++ b/src/Module.zig @@ -2501,6 +2501,16 @@ pub const SrcLoc = struct { const token_starts = tree.tokens.items(.start); return token_starts[tok_index]; }, + .node_offset_un_op => |node_off| { + const tree = try src_loc.file_scope.getTree(gpa); + const node_datas = tree.nodes.items(.data); + const node = src_loc.declRelativeToNodeIndex(node_off); + + const main_tokens = tree.nodes.items(.main_token); + const tok_index = main_tokens[node_datas[node].lhs]; + const token_starts = tree.tokens.items(.start); + return token_starts[tok_index]; + }, } } @@ -2728,6 +2738,9 @@ pub const LazySrcLoc = union(enum) { /// to the elem expression. /// The Decl is determined contextually. node_offset_array_type_elem: i32, + /// The source location points to the operand of an unary expression. + /// The Decl is determined contextually. + node_offset_un_op: i32, pub const nodeOffset = if (TracedOffset.want_tracing) nodeOffsetDebug else nodeOffsetRelease; @@ -2788,6 +2801,7 @@ pub const LazySrcLoc = union(enum) { .node_offset_array_type_len, .node_offset_array_type_sentinel, .node_offset_array_type_elem, + .node_offset_un_op, => .{ .file_scope = decl.getFileScope(), .parent_decl_node = decl.src_node, -- cgit v1.2.3 From 27ee4141592c7a9d77a2d73f5fa6a3c6262ac7fc Mon Sep 17 00:00:00 2001 From: Veikka Tuominen Date: Wed, 6 Jul 2022 12:17:04 +0300 Subject: Sema: improve slice source locations --- src/Module.zig | 37 ++++++++++++++++++++-- src/Sema.zig | 14 ++++---- .../compile_errors/stage2/out_of_bounds_index.zig | 8 ++--- 3 files changed, 46 insertions(+), 13 deletions(-) (limited to 'src/Module.zig') diff --git a/src/Module.zig b/src/Module.zig index d4e981f834..3c4962c587 100644 --- a/src/Module.zig +++ b/src/Module.zig @@ -2171,7 +2171,11 @@ pub const SrcLoc = struct { const token_starts = tree.tokens.items(.start); return token_starts[tok_index]; }, - .node_offset_slice_sentinel => |node_off| { + .node_offset_slice_ptr, + .node_offset_slice_start, + .node_offset_slice_end, + .node_offset_slice_sentinel, + => |node_off| { const tree = try src_loc.file_scope.getTree(gpa); const node_tags = tree.nodes.items(.tag); const node = src_loc.declRelativeToNodeIndex(node_off); @@ -2182,7 +2186,15 @@ pub const SrcLoc = struct { else => unreachable, }; const main_tokens = tree.nodes.items(.main_token); - const tok_index = main_tokens[full.ast.sentinel]; + const tok_index = main_tokens[ + switch (src_loc.lazy) { + .node_offset_slice_ptr => full.ast.sliced, + .node_offset_slice_start => full.ast.start, + .node_offset_slice_end => full.ast.end, + .node_offset_slice_sentinel => full.ast.sentinel, + else => unreachable, + } + ]; const token_starts = tree.tokens.items(.start); return token_starts[tok_index]; }, @@ -2624,6 +2636,24 @@ pub const LazySrcLoc = union(enum) { /// to the index expression. /// The Decl is determined contextually. node_offset_array_access_index: i32, + /// The source location points to the LHS of a slice expression + /// expression, found by taking this AST node index offset from the containing + /// Decl AST node, which points to a slice AST node. Next, navigate + /// to the sentinel expression. + /// The Decl is determined contextually. + node_offset_slice_ptr: i32, + /// The source location points to start expression of a slice expression + /// expression, found by taking this AST node index offset from the containing + /// Decl AST node, which points to a slice AST node. Next, navigate + /// to the sentinel expression. + /// The Decl is determined contextually. + node_offset_slice_start: i32, + /// The source location points to the end expression of a slice + /// expression, found by taking this AST node index offset from the containing + /// Decl AST node, which points to a slice AST node. Next, navigate + /// to the sentinel expression. + /// The Decl is determined contextually. + node_offset_slice_end: i32, /// The source location points to the sentinel expression of a slice /// expression, found by taking this AST node index offset from the containing /// Decl AST node, which points to a slice AST node. Next, navigate @@ -2781,6 +2811,9 @@ pub const LazySrcLoc = union(enum) { .node_offset_builtin_call_arg4, .node_offset_builtin_call_arg5, .node_offset_array_access_index, + .node_offset_slice_ptr, + .node_offset_slice_start, + .node_offset_slice_end, .node_offset_slice_sentinel, .node_offset_call_func, .node_offset_field_name, diff --git a/src/Sema.zig b/src/Sema.zig index 31ba644ea8..a41adc461c 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -22615,9 +22615,9 @@ fn analyzeSlice( sentinel_opt: Air.Inst.Ref, sentinel_src: LazySrcLoc, ) CompileError!Air.Inst.Ref { - const ptr_src = src; // TODO better source location - const start_src = src; // TODO better source location - const end_src = src; // TODO better source location + const ptr_src: LazySrcLoc = .{ .node_offset_slice_ptr = src.node_offset.x }; + const start_src: LazySrcLoc = .{ .node_offset_slice_start = src.node_offset.x }; + const end_src: LazySrcLoc = .{ .node_offset_slice_end = src.node_offset.x }; // Slice expressions can operate on a variable whose type is an array. This requires // the slice operand to be a pointer. In the case of a non-array, it will be a double pointer. const ptr_ptr_ty = sema.typeOf(ptr_ptr); @@ -22647,7 +22647,7 @@ fn analyzeSlice( array_ty = double_child_ty; elem_ty = double_child_ty.childType(); } else { - return sema.fail(block, ptr_src, "slice of single-item pointer", .{}); + return sema.fail(block, src, "slice of single-item pointer", .{}); } }, .Many, .C => { @@ -22660,7 +22660,7 @@ fn analyzeSlice( if (ptr_ptr_child_ty.ptrSize() == .C) { if (try sema.resolveDefinedValue(block, ptr_src, ptr_or_slice)) |ptr_val| { if (ptr_val.isNull()) { - return sema.fail(block, ptr_src, "slice of null pointer", .{}); + return sema.fail(block, src, "slice of null pointer", .{}); } } } @@ -22673,7 +22673,7 @@ fn analyzeSlice( elem_ty = ptr_ptr_child_ty.childType(); }, }, - else => return sema.fail(block, ptr_src, "slice of non-array type '{}'", .{ptr_ptr_child_ty.fmt(mod)}), + else => return sema.fail(block, src, "slice of non-array type '{}'", .{ptr_ptr_child_ty.fmt(mod)}), } const ptr = if (slice_ty.isSlice()) @@ -22846,7 +22846,7 @@ fn analyzeSlice( return sema.addConstUndef(return_ty); } - return sema.fail(block, ptr_src, "non-zero length slice of undefined pointer", .{}); + return sema.fail(block, src, "non-zero length slice of undefined pointer", .{}); } const return_ty = try Type.ptr(sema.arena, mod, .{ diff --git a/test/cases/compile_errors/stage2/out_of_bounds_index.zig b/test/cases/compile_errors/stage2/out_of_bounds_index.zig index ea9bc6521d..3f5b71d530 100644 --- a/test/cases/compile_errors/stage2/out_of_bounds_index.zig +++ b/test/cases/compile_errors/stage2/out_of_bounds_index.zig @@ -23,7 +23,7 @@ comptime { // error // target=native // -// :4:26: error: end index 6 out of bounds for slice of length 4 +1 (sentinel) -// :9:22: error: end index 6 out of bounds for array of length 4 +1 (sentinel) -// :14:22: error: end index 5 out of bounds for array of length 4 -// :19:22: error: start index 3 is larger than end index 2 +// :4:30: error: end index 6 out of bounds for slice of length 4 +1 (sentinel) +// :9:26: error: end index 6 out of bounds for array of length 4 +1 (sentinel) +// :14:26: error: end index 5 out of bounds for array of length 4 +// :19:23: error: start index 3 is larger than end index 2 -- cgit v1.2.3