authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-07-16 12:22:53+03:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2022-07-16 12:22:53+03:00
logb2486fbc5e83cca2ce31a6a16b2ad4ff8df170df
treebd96fb988a0bed8307cdafcbe6eecf1ac8bcfc93
parentda94227f783ec3c92859c4713b80a668f1183f96
parentcf207df5926c2f303ad92069e44bec51bfa44148
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #12121 from Vexu/span

Stage2 point to error location using spans

6 files changed, 179 insertions(+), 180 deletions(-)

src/Compilation.zig+36-29
......@@ -338,7 +338,7 @@ pub const AllErrors = struct {
338338 src_path: []const u8,
339339 line: u32,
340340 column: u32,
341 byte_offset: u32,
341 span: Module.SrcLoc.Span,
342342 /// Usually one, but incremented for redundant messages.
343343 count: u32 = 1,
344344 /// Does not include the trailing newline.
......@@ -427,9 +427,16 @@ pub const AllErrors = struct {
427427 else => try stderr.writeByte(b),
428428 };
429429 try stderr.writeByte('\n');
430 try stderr.writeByteNTimes(' ', src.column);
430 // TODO basic unicode code point monospace width
431 const before_caret = src.span.main - src.span.start;
432 // -1 since span.main includes the caret
433 const after_caret = src.span.end - src.span.main -| 1;
434 try stderr.writeByteNTimes(' ', src.column - before_caret);
431435 ttyconf.setColor(stderr, .Green);
432 try stderr.writeAll("^\n");
436 try stderr.writeByteNTimes('~', before_caret);
437 try stderr.writeByte('^');
438 try stderr.writeByteNTimes('~', after_caret);
439 try stderr.writeByte('\n');
433440 ttyconf.setColor(stderr, .Reset);
434441 }
435442 }
......@@ -469,7 +476,7 @@ pub const AllErrors = struct {
469476 hasher.update(src.src_path);
470477 std.hash.autoHash(&hasher, src.line);
471478 std.hash.autoHash(&hasher, src.column);
472 std.hash.autoHash(&hasher, src.byte_offset);
479 std.hash.autoHash(&hasher, src.span.main);
473480 },
474481 .plain => |plain| {
475482 hasher.update(plain.msg);
......@@ -488,7 +495,7 @@ pub const AllErrors = struct {
488495 mem.eql(u8, a_src.src_path, b_src.src_path) and
489496 a_src.line == b_src.line and
490497 a_src.column == b_src.column and
491 a_src.byte_offset == b_src.byte_offset;
498 a_src.span.main == b_src.span.main;
492499 },
493500 .plain => return false,
494501 },
......@@ -527,20 +534,20 @@ pub const AllErrors = struct {
527534 std.hash_map.default_max_load_percentage,
528535 ).init(allocator);
529536 const err_source = try module_err_msg.src_loc.file_scope.getSource(module.gpa);
530 const err_byte_offset = try module_err_msg.src_loc.byteOffset(module.gpa);
531 const err_loc = std.zig.findLineColumn(err_source.bytes, err_byte_offset);
537 const err_span = try module_err_msg.src_loc.span(module.gpa);
538 const err_loc = std.zig.findLineColumn(err_source.bytes, err_span.main);
532539
533540 for (module_err_msg.notes) |module_note| {
534541 const source = try module_note.src_loc.file_scope.getSource(module.gpa);
535 const byte_offset = try module_note.src_loc.byteOffset(module.gpa);
536 const loc = std.zig.findLineColumn(source.bytes, byte_offset);
542 const span = try module_note.src_loc.span(module.gpa);
543 const loc = std.zig.findLineColumn(source.bytes, span.main);
537544 const file_path = try module_note.src_loc.file_scope.fullPath(allocator);
538545 const note = &notes_buf[note_i];
539546 note.* = .{
540547 .src = .{
541548 .src_path = file_path,
542549 .msg = try allocator.dupe(u8, module_note.msg),
543 .byte_offset = byte_offset,
550 .span = span,
544551 .line = @intCast(u32, loc.line),
545552 .column = @intCast(u32, loc.column),
546553 .source_line = if (err_loc.eql(loc)) null else try allocator.dupe(u8, loc.source_line),
......@@ -566,7 +573,7 @@ pub const AllErrors = struct {
566573 .src = .{
567574 .src_path = file_path,
568575 .msg = try allocator.dupe(u8, module_err_msg.msg),
569 .byte_offset = err_byte_offset,
576 .span = err_span,
570577 .line = @intCast(u32, err_loc.line),
571578 .column = @intCast(u32, err_loc.column),
572579 .notes = notes_buf[0..note_i],
......@@ -593,16 +600,16 @@ pub const AllErrors = struct {
593600 while (item_i < items_len) : (item_i += 1) {
594601 const item = file.zir.extraData(Zir.Inst.CompileErrors.Item, extra_index);
595602 extra_index = item.end;
596 const err_byte_offset = blk: {
597 const token_starts = file.tree.tokens.items(.start);
603 const err_span = blk: {
598604 if (item.data.node != 0) {
599 const main_tokens = file.tree.nodes.items(.main_token);
600 const main_token = main_tokens[item.data.node];
601 break :blk token_starts[main_token];
605 break :blk Module.SrcLoc.nodeToSpan(&file.tree, item.data.node);
602606 }
603 break :blk token_starts[item.data.token] + item.data.byte_offset;
607 const token_starts = file.tree.tokens.items(.start);
608 const start = token_starts[item.data.token] + item.data.byte_offset;
609 const end = start + @intCast(u32, file.tree.tokenSlice(item.data.token).len);
610 break :blk Module.SrcLoc.Span{ .start = start, .end = end, .main = start };
604611 };
605 const err_loc = std.zig.findLineColumn(file.source, err_byte_offset);
612 const err_loc = std.zig.findLineColumn(file.source, err_span.main);
606613
607614 var notes: []Message = &[0]Message{};
608615 if (item.data.notes != 0) {
......@@ -612,22 +619,22 @@ pub const AllErrors = struct {
612619 for (notes) |*note, i| {
613620 const note_item = file.zir.extraData(Zir.Inst.CompileErrors.Item, body[i]);
614621 const msg = file.zir.nullTerminatedString(note_item.data.msg);
615 const byte_offset = blk: {
616 const token_starts = file.tree.tokens.items(.start);
622 const span = blk: {
617623 if (note_item.data.node != 0) {
618 const main_tokens = file.tree.nodes.items(.main_token);
619 const main_token = main_tokens[note_item.data.node];
620 break :blk token_starts[main_token];
624 break :blk Module.SrcLoc.nodeToSpan(&file.tree, note_item.data.node);
621625 }
622 break :blk token_starts[note_item.data.token] + note_item.data.byte_offset;
626 const token_starts = file.tree.tokens.items(.start);
627 const start = token_starts[note_item.data.token] + note_item.data.byte_offset;
628 const end = start + @intCast(u32, file.tree.tokenSlice(note_item.data.token).len);
629 break :blk Module.SrcLoc.Span{ .start = start, .end = end, .main = start };
623630 };
624 const loc = std.zig.findLineColumn(file.source, byte_offset);
631 const loc = std.zig.findLineColumn(file.source, span.main);
625632
626633 note.* = .{
627634 .src = .{
628635 .src_path = try file.fullPath(arena),
629636 .msg = try arena.dupe(u8, msg),
630 .byte_offset = byte_offset,
637 .span = span,
631638 .line = @intCast(u32, loc.line),
632639 .column = @intCast(u32, loc.column),
633640 .notes = &.{}, // TODO rework this function to be recursive
......@@ -642,7 +649,7 @@ pub const AllErrors = struct {
642649 .src = .{
643650 .src_path = try file.fullPath(arena),
644651 .msg = try arena.dupe(u8, msg),
645 .byte_offset = err_byte_offset,
652 .span = err_span,
646653 .line = @intCast(u32, err_loc.line),
647654 .column = @intCast(u32, err_loc.column),
648655 .notes = notes,
......@@ -688,7 +695,7 @@ pub const AllErrors = struct {
688695 .src_path = try arena.dupe(u8, src.src_path),
689696 .line = src.line,
690697 .column = src.column,
691 .byte_offset = src.byte_offset,
698 .span = src.span,
692699 .source_line = if (src.source_line) |s| try arena.dupe(u8, s) else null,
693700 .notes = try dupeList(src.notes, arena),
694701 } },
......@@ -2662,7 +2669,7 @@ pub fn getAllErrorsAlloc(self: *Compilation) !AllErrors {
26622669 .msg = try std.fmt.allocPrint(arena_allocator, "unable to build C object: {s}", .{
26632670 err_msg.msg,
26642671 }),
2665 .byte_offset = 0,
2672 .span = .{ .start = 0, .end = 1, .main = 0 },
26662673 .line = err_msg.line,
26672674 .column = err_msg.column,
26682675 .source_line = null, // TODO
src/Module.zig+107-136
......@@ -2082,60 +2082,65 @@ pub const SrcLoc = struct {
20822082 return @bitCast(Ast.Node.Index, offset + @bitCast(i32, src_loc.parent_decl_node));
20832083 }
20842084
2085 pub fn byteOffset(src_loc: SrcLoc, gpa: Allocator) !u32 {
2085 pub const Span = struct {
2086 start: u32,
2087 end: u32,
2088 main: u32,
2089 };
2090
2091 pub fn span(src_loc: SrcLoc, gpa: Allocator) !Span {
20862092 switch (src_loc.lazy) {
20872093 .unneeded => unreachable,
2088 .entire_file => return 0,
2094 .entire_file => return Span{ .start = 0, .end = 1, .main = 0 },
20892095
2090 .byte_abs => |byte_index| return byte_index,
2096 .byte_abs => |byte_index| return Span{ .start = byte_index, .end = byte_index + 1, .main = byte_index },
20912097
20922098 .token_abs => |tok_index| {
20932099 const tree = try src_loc.file_scope.getTree(gpa);
2094 const token_starts = tree.tokens.items(.start);
2095 return token_starts[tok_index];
2100 const start = tree.tokens.items(.start)[tok_index];
2101 const end = start + @intCast(u32, tree.tokenSlice(tok_index).len);
2102 return Span{ .start = start, .end = end, .main = start };
20962103 },
20972104 .node_abs => |node| {
20982105 const tree = try src_loc.file_scope.getTree(gpa);
2099 const token_starts = tree.tokens.items(.start);
2100 const tok_index = tree.firstToken(node);
2101 return token_starts[tok_index];
2106 return nodeToSpan(tree, node);
21022107 },
21032108 .byte_offset => |byte_off| {
21042109 const tree = try src_loc.file_scope.getTree(gpa);
2105 const token_starts = tree.tokens.items(.start);
2106 return token_starts[src_loc.declSrcToken()] + byte_off;
2110 const tok_index = src_loc.declSrcToken();
2111 const start = tree.tokens.items(.start)[tok_index] + byte_off;
2112 const end = start + @intCast(u32, tree.tokenSlice(tok_index).len);
2113 return Span{ .start = start, .end = end, .main = start };
21072114 },
21082115 .token_offset => |tok_off| {
21092116 const tree = try src_loc.file_scope.getTree(gpa);
21102117 const tok_index = src_loc.declSrcToken() + tok_off;
2111 const token_starts = tree.tokens.items(.start);
2112 return token_starts[tok_index];
2118 const start = tree.tokens.items(.start)[tok_index];
2119 const end = start + @intCast(u32, tree.tokenSlice(tok_index).len);
2120 return Span{ .start = start, .end = end, .main = start };
21132121 },
21142122 .node_offset => |traced_off| {
21152123 const node_off = traced_off.x;
21162124 const tree = try src_loc.file_scope.getTree(gpa);
21172125 const node = src_loc.declRelativeToNodeIndex(node_off);
21182126 assert(src_loc.file_scope.tree_loaded);
2119 const main_tokens = tree.nodes.items(.main_token);
2120 const tok_index = main_tokens[node];
2121 const token_starts = tree.tokens.items(.start);
2122 return token_starts[tok_index];
2127 return nodeToSpan(tree, node);
21232128 },
21242129 .node_offset_bin_op => |node_off| {
21252130 const tree = try src_loc.file_scope.getTree(gpa);
21262131 const node = src_loc.declRelativeToNodeIndex(node_off);
21272132 assert(src_loc.file_scope.tree_loaded);
2128 const main_tokens = tree.nodes.items(.main_token);
2129 const tok_index = main_tokens[node];
2130 const token_starts = tree.tokens.items(.start);
2131 return token_starts[tok_index];
2133 return nodeToSpan(tree, node);
21322134 },
2133 .node_offset_back2tok => |node_off| {
2135 .node_offset_initializer => |node_off| {
21342136 const tree = try src_loc.file_scope.getTree(gpa);
21352137 const node = src_loc.declRelativeToNodeIndex(node_off);
2136 const tok_index = tree.firstToken(node) - 2;
2137 const token_starts = tree.tokens.items(.start);
2138 return token_starts[tok_index];
2138 return tokensToSpan(
2139 tree,
2140 tree.firstToken(node) - 3,
2141 tree.lastToken(node),
2142 tree.nodes.items(.main_token)[node] - 2,
2143 );
21392144 },
21402145 .node_offset_var_decl_ty => |node_off| {
21412146 const tree = try src_loc.file_scope.getTree(gpa);
......@@ -2148,14 +2153,13 @@ pub const SrcLoc = struct {
21482153 .aligned_var_decl => tree.alignedVarDecl(node),
21492154 else => unreachable,
21502155 };
2151 const tok_index = if (full.ast.type_node != 0) blk: {
2152 const main_tokens = tree.nodes.items(.main_token);
2153 break :blk main_tokens[full.ast.type_node];
2154 } else blk: {
2155 break :blk full.ast.mut_token + 1; // the name token
2156 };
2157 const token_starts = tree.tokens.items(.start);
2158 return token_starts[tok_index];
2156 if (full.ast.type_node != 0) {
2157 return nodeToSpan(tree, full.ast.type_node);
2158 }
2159 const tok_index = full.ast.mut_token + 1; // the name token
2160 const start = tree.tokens.items(.start)[tok_index];
2161 const end = start + @intCast(u32, tree.tokenSlice(tok_index).len);
2162 return Span{ .start = start, .end = end, .main = start };
21592163 },
21602164 .node_offset_builtin_call_arg0 => |n| return src_loc.byteOffsetBuiltinCallArg(gpa, n, 0),
21612165 .node_offset_builtin_call_arg1 => |n| return src_loc.byteOffsetBuiltinCallArg(gpa, n, 1),
......@@ -2167,10 +2171,7 @@ pub const SrcLoc = struct {
21672171 const tree = try src_loc.file_scope.getTree(gpa);
21682172 const node_datas = tree.nodes.items(.data);
21692173 const node = src_loc.declRelativeToNodeIndex(node_off);
2170 const main_tokens = tree.nodes.items(.main_token);
2171 const tok_index = main_tokens[node_datas[node].rhs];
2172 const token_starts = tree.tokens.items(.start);
2173 return token_starts[tok_index];
2174 return nodeToSpan(tree, node_datas[node].rhs);
21742175 },
21752176 .node_offset_slice_ptr,
21762177 .node_offset_slice_start,
......@@ -2186,18 +2187,14 @@ pub const SrcLoc = struct {
21862187 .slice_sentinel => tree.sliceSentinel(node),
21872188 else => unreachable,
21882189 };
2189 const main_tokens = tree.nodes.items(.main_token);
2190 const tok_index = main_tokens[
2191 switch (src_loc.lazy) {
2192 .node_offset_slice_ptr => full.ast.sliced,
2193 .node_offset_slice_start => full.ast.start,
2194 .node_offset_slice_end => full.ast.end,
2195 .node_offset_slice_sentinel => full.ast.sentinel,
2196 else => unreachable,
2197 }
2198 ];
2199 const token_starts = tree.tokens.items(.start);
2200 return token_starts[tok_index];
2190 const part_node = 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 return nodeToSpan(tree, part_node);
22012198 },
22022199 .node_offset_call_func => |node_off| {
22032200 const tree = try src_loc.file_scope.getTree(gpa);
......@@ -2219,10 +2216,7 @@ pub const SrcLoc = struct {
22192216
22202217 else => unreachable,
22212218 };
2222 const main_tokens = tree.nodes.items(.main_token);
2223 const tok_index = main_tokens[full.ast.fn_expr];
2224 const token_starts = tree.tokens.items(.start);
2225 return token_starts[tok_index];
2219 return nodeToSpan(tree, full.ast.fn_expr);
22262220 },
22272221 .node_offset_field_name => |node_off| {
22282222 const tree = try src_loc.file_scope.getTree(gpa);
......@@ -2233,16 +2227,14 @@ pub const SrcLoc = struct {
22332227 .field_access => node_datas[node].rhs,
22342228 else => tree.firstToken(node) - 2,
22352229 };
2236 const token_starts = tree.tokens.items(.start);
2237 return token_starts[tok_index];
2230 const start = tree.tokens.items(.start)[tok_index];
2231 const end = start + @intCast(u32, tree.tokenSlice(tok_index).len);
2232 return Span{ .start = start, .end = end, .main = start };
22382233 },
22392234 .node_offset_deref_ptr => |node_off| {
22402235 const tree = try src_loc.file_scope.getTree(gpa);
2241 const node_datas = tree.nodes.items(.data);
22422236 const node = src_loc.declRelativeToNodeIndex(node_off);
2243 const tok_index = node_datas[node].lhs;
2244 const token_starts = tree.tokens.items(.start);
2245 return token_starts[tok_index];
2237 return nodeToSpan(tree, node);
22462238 },
22472239 .node_offset_asm_source => |node_off| {
22482240 const tree = try src_loc.file_scope.getTree(gpa);
......@@ -2253,10 +2245,7 @@ pub const SrcLoc = struct {
22532245 .@"asm" => tree.asmFull(node),
22542246 else => unreachable,
22552247 };
2256 const main_tokens = tree.nodes.items(.main_token);
2257 const tok_index = main_tokens[full.ast.template];
2258 const token_starts = tree.tokens.items(.start);
2259 return token_starts[tok_index];
2248 return nodeToSpan(tree, full.ast.template);
22602249 },
22612250 .node_offset_asm_ret_ty => |node_off| {
22622251 const tree = try src_loc.file_scope.getTree(gpa);
......@@ -2269,11 +2258,7 @@ pub const SrcLoc = struct {
22692258 };
22702259 const asm_output = full.outputs[0];
22712260 const node_datas = tree.nodes.items(.data);
2272 const ret_ty_node = node_datas[asm_output].lhs;
2273 const main_tokens = tree.nodes.items(.main_token);
2274 const tok_index = main_tokens[ret_ty_node];
2275 const token_starts = tree.tokens.items(.start);
2276 return token_starts[tok_index];
2261 return nodeToSpan(tree, node_datas[asm_output].lhs);
22772262 },
22782263
22792264 .node_offset_for_cond, .node_offset_if_cond => |node_off| {
......@@ -2290,41 +2275,26 @@ pub const SrcLoc = struct {
22902275 .@"for" => tree.forFull(node).ast.cond_expr,
22912276 else => unreachable,
22922277 };
2293 const main_tokens = tree.nodes.items(.main_token);
2294 const tok_index = main_tokens[src_node];
2295 const token_starts = tree.tokens.items(.start);
2296 return token_starts[tok_index];
2278 return nodeToSpan(tree, src_node);
22972279 },
22982280 .node_offset_bin_lhs => |node_off| {
22992281 const tree = try src_loc.file_scope.getTree(gpa);
23002282 const node = src_loc.declRelativeToNodeIndex(node_off);
23012283 const node_datas = tree.nodes.items(.data);
2302 const src_node = node_datas[node].lhs;
2303 const main_tokens = tree.nodes.items(.main_token);
2304 const tok_index = main_tokens[src_node];
2305 const token_starts = tree.tokens.items(.start);
2306 return token_starts[tok_index];
2284 return nodeToSpan(tree, node_datas[node].lhs);
23072285 },
23082286 .node_offset_bin_rhs => |node_off| {
23092287 const tree = try src_loc.file_scope.getTree(gpa);
23102288 const node = src_loc.declRelativeToNodeIndex(node_off);
23112289 const node_datas = tree.nodes.items(.data);
2312 const src_node = node_datas[node].rhs;
2313 const main_tokens = tree.nodes.items(.main_token);
2314 const tok_index = main_tokens[src_node];
2315 const token_starts = tree.tokens.items(.start);
2316 return token_starts[tok_index];
2290 return nodeToSpan(tree, node_datas[node].rhs);
23172291 },
23182292
23192293 .node_offset_switch_operand => |node_off| {
23202294 const tree = try src_loc.file_scope.getTree(gpa);
23212295 const node = src_loc.declRelativeToNodeIndex(node_off);
23222296 const node_datas = tree.nodes.items(.data);
2323 const src_node = node_datas[node].lhs;
2324 const main_tokens = tree.nodes.items(.main_token);
2325 const tok_index = main_tokens[src_node];
2326 const token_starts = tree.tokens.items(.start);
2327 return token_starts[tok_index];
2297 return nodeToSpan(tree, node_datas[node].lhs);
23282298 },
23292299
23302300 .node_offset_switch_special_prong => |node_off| {
......@@ -2347,9 +2317,7 @@ pub const SrcLoc = struct {
23472317 mem.eql(u8, tree.tokenSlice(main_tokens[case.ast.values[0]]), "_"));
23482318 if (!is_special) continue;
23492319
2350 const tok_index = main_tokens[case_node];
2351 const token_starts = tree.tokens.items(.start);
2352 return token_starts[tok_index];
2320 return nodeToSpan(tree, case_node);
23532321 } else unreachable;
23542322 },
23552323
......@@ -2375,9 +2343,7 @@ pub const SrcLoc = struct {
23752343
23762344 for (case.ast.values) |item_node| {
23772345 if (node_tags[item_node] == .switch_range) {
2378 const tok_index = main_tokens[item_node];
2379 const token_starts = tree.tokens.items(.start);
2380 return token_starts[tok_index];
2346 return nodeToSpan(tree, item_node);
23812347 }
23822348 }
23832349 } else unreachable;
......@@ -2403,10 +2369,7 @@ pub const SrcLoc = struct {
24032369 },
24042370 else => unreachable,
24052371 };
2406 const main_tokens = tree.nodes.items(.main_token);
2407 const tok_index = main_tokens[full.ast.callconv_expr];
2408 const token_starts = tree.tokens.items(.start);
2409 return token_starts[tok_index];
2372 return nodeToSpan(tree, full.ast.callconv_expr);
24102373 },
24112374
24122375 .node_offset_fn_type_ret_ty => |node_off| {
......@@ -2421,21 +2384,14 @@ pub const SrcLoc = struct {
24212384 .fn_proto => tree.fnProto(node),
24222385 else => unreachable,
24232386 };
2424 const main_tokens = tree.nodes.items(.main_token);
2425 const tok_index = main_tokens[full.ast.return_type];
2426 const token_starts = tree.tokens.items(.start);
2427 return token_starts[tok_index];
2387 return nodeToSpan(tree, full.ast.return_type);
24282388 },
24292389
24302390 .node_offset_anyframe_type => |node_off| {
24312391 const tree = try src_loc.file_scope.getTree(gpa);
24322392 const node_datas = tree.nodes.items(.data);
24332393 const parent_node = src_loc.declRelativeToNodeIndex(node_off);
2434 const node = node_datas[parent_node].rhs;
2435 const main_tokens = tree.nodes.items(.main_token);
2436 const tok_index = main_tokens[node];
2437 const token_starts = tree.tokens.items(.start);
2438 return token_starts[tok_index];
2394 return nodeToSpan(tree, node_datas[parent_node].rhs);
24392395 },
24402396
24412397 .node_offset_lib_name => |node_off| {
......@@ -2462,8 +2418,9 @@ pub const SrcLoc = struct {
24622418 else => unreachable,
24632419 };
24642420 const tok_index = full.lib_name.?;
2465 const token_starts = tree.tokens.items(.start);
2466 return token_starts[tok_index];
2421 const start = tree.tokens.items(.start)[tok_index];
2422 const end = start + @intCast(u32, tree.tokenSlice(tok_index).len);
2423 return Span{ .start = start, .end = end, .main = start };
24672424 },
24682425
24692426 .node_offset_array_type_len => |node_off| {
......@@ -2476,11 +2433,7 @@ pub const SrcLoc = struct {
24762433 .array_type_sentinel => tree.arrayTypeSentinel(parent_node),
24772434 else => unreachable,
24782435 };
2479 const node = full.ast.elem_count;
2480 const main_tokens = tree.nodes.items(.main_token);
2481 const tok_index = main_tokens[node];
2482 const token_starts = tree.tokens.items(.start);
2483 return token_starts[tok_index];
2436 return nodeToSpan(tree, full.ast.elem_count);
24842437 },
24852438 .node_offset_array_type_sentinel => |node_off| {
24862439 const tree = try src_loc.file_scope.getTree(gpa);
......@@ -2492,11 +2445,7 @@ pub const SrcLoc = struct {
24922445 .array_type_sentinel => tree.arrayTypeSentinel(parent_node),
24932446 else => unreachable,
24942447 };
2495 const node = full.ast.sentinel;
2496 const main_tokens = tree.nodes.items(.main_token);
2497 const tok_index = main_tokens[node];
2498 const token_starts = tree.tokens.items(.start);
2499 return token_starts[tok_index];
2448 return nodeToSpan(tree, full.ast.sentinel);
25002449 },
25012450 .node_offset_array_type_elem => |node_off| {
25022451 const tree = try src_loc.file_scope.getTree(gpa);
......@@ -2508,21 +2457,14 @@ pub const SrcLoc = struct {
25082457 .array_type_sentinel => tree.arrayTypeSentinel(parent_node),
25092458 else => unreachable,
25102459 };
2511 const node = full.ast.elem_type;
2512 const main_tokens = tree.nodes.items(.main_token);
2513 const tok_index = main_tokens[node];
2514 const token_starts = tree.tokens.items(.start);
2515 return token_starts[tok_index];
2460 return nodeToSpan(tree, full.ast.elem_type);
25162461 },
25172462 .node_offset_un_op => |node_off| {
25182463 const tree = try src_loc.file_scope.getTree(gpa);
25192464 const node_datas = tree.nodes.items(.data);
25202465 const node = src_loc.declRelativeToNodeIndex(node_off);
25212466
2522 const main_tokens = tree.nodes.items(.main_token);
2523 const tok_index = main_tokens[node_datas[node].lhs];
2524 const token_starts = tree.tokens.items(.start);
2525 return token_starts[tok_index];
2467 return nodeToSpan(tree, node_datas[node].lhs);
25262468 },
25272469 }
25282470 }
......@@ -2532,7 +2474,7 @@ pub const SrcLoc = struct {
25322474 gpa: Allocator,
25332475 node_off: i32,
25342476 arg_index: u32,
2535 ) !u32 {
2477 ) !Span {
25362478 const tree = try src_loc.file_scope.getTree(gpa);
25372479 const node_datas = tree.nodes.items(.data);
25382480 const node_tags = tree.nodes.items(.tag);
......@@ -2546,10 +2488,36 @@ pub const SrcLoc = struct {
25462488 .builtin_call, .builtin_call_comma => tree.extra_data[node_datas[node].lhs + arg_index],
25472489 else => unreachable,
25482490 };
2549 const main_tokens = tree.nodes.items(.main_token);
2550 const tok_index = main_tokens[param];
2491 return nodeToSpan(tree, param);
2492 }
2493
2494 pub fn nodeToSpan(tree: *const Ast, node: u32) Span {
2495 return tokensToSpan(
2496 tree,
2497 tree.firstToken(node),
2498 tree.lastToken(node),
2499 tree.nodes.items(.main_token)[node],
2500 );
2501 }
2502
2503 fn tokensToSpan(tree: *const Ast, start: Ast.TokenIndex, end: Ast.TokenIndex, main: Ast.TokenIndex) Span {
25512504 const token_starts = tree.tokens.items(.start);
2552 return token_starts[tok_index];
2505 var start_tok = start;
2506 var end_tok = end;
2507
2508 if (tree.tokensOnSameLine(start, end)) {
2509 // do nothing
2510 } else if (tree.tokensOnSameLine(start, main)) {
2511 end_tok = main;
2512 } else if (tree.tokensOnSameLine(main, end)) {
2513 start_tok = main;
2514 } else {
2515 start_tok = main;
2516 end_tok = main;
2517 }
2518 const start_off = token_starts[start_tok];
2519 const end_off = token_starts[end_tok] + @intCast(u32, tree.tokenSlice(end_tok).len);
2520 return Span{ .start = start_off, .end = end_off, .main = token_starts[main] };
25532521 }
25542522};
25552523
......@@ -2603,10 +2571,9 @@ pub const LazySrcLoc = union(enum) {
26032571 /// from its containing Decl node AST index.
26042572 /// The Decl is determined contextually.
26052573 node_offset: TracedOffset,
2606 /// The source location points to two tokens left of the first token of an AST node,
2607 /// which is this value offset from its containing Decl node AST index.
2574 /// The source location points to the beginning of a struct initializer.
26082575 /// The Decl is determined contextually.
2609 node_offset_back2tok: i32,
2576 node_offset_initializer: i32,
26102577 /// The source location points to a variable declaration type expression,
26112578 /// found by taking this AST node index offset from the containing
26122579 /// Decl AST node, which points to a variable declaration AST node. Next, navigate
......@@ -2802,7 +2769,7 @@ pub const LazySrcLoc = union(enum) {
28022769 .byte_offset,
28032770 .token_offset,
28042771 .node_offset,
2805 .node_offset_back2tok,
2772 .node_offset_initializer,
28062773 .node_offset_var_decl_ty,
28072774 .node_offset_for_cond,
28082775 .node_offset_builtin_call_arg0,
......@@ -3313,7 +3280,11 @@ pub fn astGenFile(mod: *Module, file: *File) !void {
33133280 .src_loc = .{
33143281 .file_scope = file,
33153282 .parent_decl_node = 0,
3316 .lazy = .{ .byte_abs = token_starts[parse_err.token] + extra_offset },
3283 .lazy = if (extra_offset == 0) .{
3284 .token_abs = parse_err.token,
3285 } else .{
3286 .byte_abs = token_starts[parse_err.token] + extra_offset,
3287 },
33173288 },
33183289 .msg = msg.toOwnedSlice(),
33193290 };
......@@ -3336,7 +3307,7 @@ pub fn astGenFile(mod: *Module, file: *File) !void {
33363307 .src_loc = .{
33373308 .file_scope = file,
33383309 .parent_decl_node = 0,
3339 .lazy = .{ .byte_abs = token_starts[note.token] },
3310 .lazy = .{ .token_abs = note.token },
33403311 },
33413312 .msg = msg.toOwnedSlice(),
33423313 };
src/Sema.zig+8-8
......@@ -3497,7 +3497,7 @@ fn validateUnionInit(
34973497
34983498 for (instrs[1..]) |inst| {
34993499 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
3500 const inst_src: LazySrcLoc = .{ .node_offset_back2tok = inst_data.src_node };
3500 const inst_src: LazySrcLoc = .{ .node_offset_initializer = inst_data.src_node };
35013501 try sema.errNote(block, inst_src, msg, "additional initializer here", .{});
35023502 }
35033503 try sema.addDeclaredHereNote(msg, union_ty);
......@@ -3515,7 +3515,7 @@ fn validateUnionInit(
35153515
35163516 const field_ptr = instrs[0];
35173517 const field_ptr_data = sema.code.instructions.items(.data)[field_ptr].pl_node;
3518 const field_src: LazySrcLoc = .{ .node_offset_back2tok = field_ptr_data.src_node };
3518 const field_src: LazySrcLoc = .{ .node_offset_initializer = field_ptr_data.src_node };
35193519 const field_ptr_extra = sema.code.extraData(Zir.Inst.Field, field_ptr_data.payload_index).data;
35203520 const field_name = sema.code.nullTerminatedString(field_ptr_extra.field_name_start);
35213521 const field_index = try sema.unionFieldIndex(block, union_ty, field_name, field_src);
......@@ -3617,7 +3617,7 @@ fn validateStructInit(
36173617
36183618 for (instrs) |field_ptr| {
36193619 const field_ptr_data = sema.code.instructions.items(.data)[field_ptr].pl_node;
3620 const field_src: LazySrcLoc = .{ .node_offset_back2tok = field_ptr_data.src_node };
3620 const field_src: LazySrcLoc = .{ .node_offset_initializer = field_ptr_data.src_node };
36213621 const field_ptr_extra = sema.code.extraData(Zir.Inst.Field, field_ptr_data.payload_index).data;
36223622 struct_ptr_zir_ref = field_ptr_extra.lhs;
36233623 const field_name = sema.code.nullTerminatedString(field_ptr_extra.field_name_start);
......@@ -3625,7 +3625,7 @@ fn validateStructInit(
36253625 if (found_fields[field_index] != 0) {
36263626 const other_field_ptr = found_fields[field_index];
36273627 const other_field_ptr_data = sema.code.instructions.items(.data)[other_field_ptr].pl_node;
3628 const other_field_src: LazySrcLoc = .{ .node_offset_back2tok = other_field_ptr_data.src_node };
3628 const other_field_src: LazySrcLoc = .{ .node_offset_initializer = other_field_ptr_data.src_node };
36293629 const msg = msg: {
36303630 const msg = try sema.errMsg(block, field_src, "duplicate field", .{});
36313631 errdefer msg.destroy(gpa);
......@@ -3700,7 +3700,7 @@ fn validateStructInit(
37003700 field: for (found_fields) |field_ptr, i| {
37013701 if (field_ptr != 0) {
37023702 const field_ptr_data = sema.code.instructions.items(.data)[field_ptr].pl_node;
3703 const field_src: LazySrcLoc = .{ .node_offset_back2tok = field_ptr_data.src_node };
3703 const field_src: LazySrcLoc = .{ .node_offset_initializer = field_ptr_data.src_node };
37043704
37053705 // Determine whether the value stored to this pointer is comptime-known.
37063706 const field_ty = struct_ty.structFieldType(i);
......@@ -14096,14 +14096,14 @@ fn zirStructInit(
1409614096 extra_index = item.end;
1409714097
1409814098 const field_type_data = zir_datas[item.data.field_type].pl_node;
14099 const field_src: LazySrcLoc = .{ .node_offset_back2tok = field_type_data.src_node };
14099 const field_src: LazySrcLoc = .{ .node_offset_initializer = field_type_data.src_node };
1410014100 const field_type_extra = sema.code.extraData(Zir.Inst.FieldType, field_type_data.payload_index).data;
1410114101 const field_name = sema.code.nullTerminatedString(field_type_extra.name_start);
1410214102 const field_index = try sema.structFieldIndex(block, resolved_ty, field_name, field_src);
1410314103 if (field_inits[field_index] != .none) {
1410414104 const other_field_type = found_fields[field_index];
1410514105 const other_field_type_data = zir_datas[other_field_type].pl_node;
14106 const other_field_src: LazySrcLoc = .{ .node_offset_back2tok = other_field_type_data.src_node };
14106 const other_field_src: LazySrcLoc = .{ .node_offset_initializer = other_field_type_data.src_node };
1410714107 const msg = msg: {
1410814108 const msg = try sema.errMsg(block, field_src, "duplicate field", .{});
1410914109 errdefer msg.destroy(gpa);
......@@ -14125,7 +14125,7 @@ fn zirStructInit(
1412514125 const item = sema.code.extraData(Zir.Inst.StructInit.Item, extra.end);
1412614126
1412714127 const field_type_data = zir_datas[item.data.field_type].pl_node;
14128 const field_src: LazySrcLoc = .{ .node_offset_back2tok = field_type_data.src_node };
14128 const field_src: LazySrcLoc = .{ .node_offset_initializer = field_type_data.src_node };
1412914129 const field_type_extra = sema.code.extraData(Zir.Inst.FieldType, field_type_data.payload_index).data;
1413014130 const field_name = sema.code.nullTerminatedString(field_type_extra.name_start);
1413114131 const field_index = try sema.unionFieldIndex(block, resolved_ty, field_name, field_src);
src/main.zig+13-3
......@@ -4387,7 +4387,7 @@ fn printErrsMsgToStdErr(
43874387 .msg = try std.fmt.allocPrint(arena, "invalid byte: '{'}'", .{
43884388 std.zig.fmtEscapes(tree.source[byte_offset..][0..1]),
43894389 }),
4390 .byte_offset = byte_offset,
4390 .span = .{ .start = byte_offset, .end = byte_offset + 1, .main = byte_offset },
43914391 .line = @intCast(u32, start_loc.line),
43924392 .column = @intCast(u32, start_loc.column) + bad_off,
43934393 .source_line = source_line,
......@@ -4402,11 +4402,16 @@ fn printErrsMsgToStdErr(
44024402 text_buf.items.len = 0;
44034403 try tree.renderError(note, writer);
44044404 const note_loc = tree.tokenLocation(0, note.token);
4405 const byte_offset = @intCast(u32, note_loc.line_start);
44054406 notes_buffer[notes_len] = .{
44064407 .src = .{
44074408 .src_path = path,
44084409 .msg = try arena.dupe(u8, text_buf.items),
4409 .byte_offset = @intCast(u32, note_loc.line_start),
4410 .span = .{
4411 .start = byte_offset,
4412 .end = byte_offset + @intCast(u32, tree.tokenSlice(note.token).len),
4413 .main = byte_offset,
4414 },
44104415 .line = @intCast(u32, note_loc.line),
44114416 .column = @intCast(u32, note_loc.column),
44124417 .source_line = tree.source[note_loc.line_start..note_loc.line_end],
......@@ -4417,11 +4422,16 @@ fn printErrsMsgToStdErr(
44174422 }
44184423
44194424 const extra_offset = tree.errorOffset(parse_error);
4425 const byte_offset = @intCast(u32, start_loc.line_start) + extra_offset;
44204426 const message: Compilation.AllErrors.Message = .{
44214427 .src = .{
44224428 .src_path = path,
44234429 .msg = text,
4424 .byte_offset = @intCast(u32, start_loc.line_start) + extra_offset,
4430 .span = .{
4431 .start = byte_offset,
4432 .end = byte_offset + @intCast(u32, tree.tokenSlice(lok_token).len),
4433 .main = byte_offset,
4434 },
44254435 .line = @intCast(u32, start_loc.line),
44264436 .column = @intCast(u32, start_loc.column) + extra_offset,
44274437 .source_line = source_line,
src/print_zir.zig+6-4
......@@ -2381,10 +2381,12 @@ const Writer = struct {
23812381 .parent_decl_node = self.parent_decl_node,
23822382 .lazy = src,
23832383 };
2384 const abs_byte_off = src_loc.byteOffset(self.gpa) catch unreachable;
2385 const delta_line = std.zig.findLineColumn(tree.source, abs_byte_off);
2386 try stream.print("{s}:{d}:{d}", .{
2387 @tagName(src), delta_line.line + 1, delta_line.column + 1,
2384 const src_span = src_loc.span(self.gpa) catch unreachable;
2385 const start = std.zig.findLineColumn(tree.source, src_span.start);
2386 const end = std.zig.findLineColumn(tree.source, src_span.end);
2387 try stream.print("{s}:{d}:{d} to :{d}:{d}", .{
2388 @tagName(src), start.line + 1, start.column + 1,
2389 end.line + 1, end.column + 1,
23882390 });
23892391 }
23902392 }
test/compile_errors.zig+9
......@@ -174,6 +174,15 @@ pub fn addCases(ctx: *TestContext) !void {
174174 });
175175 }
176176
177 {
178 const case = ctx.obj("missing semicolon at EOF", .{});
179 case.addError(
180 \\const foo = 1
181 , &[_][]const u8{
182 \\:1:14: error: expected ';' after declaration
183 });
184 }
185
177186 // TODO test this in stage2, but we won't even try in stage1
178187 //ctx.objErrStage1("inline fn calls itself indirectly",
179188 // \\export fn foo() void {