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 {...@@ -338,7 +338,7 @@ pub const AllErrors = struct {
338 src_path: []const u8,338 src_path: []const u8,
339 line: u32,339 line: u32,
340 column: u32,340 column: u32,
341 byte_offset: u32,341 span: Module.SrcLoc.Span,
342 /// Usually one, but incremented for redundant messages.342 /// Usually one, but incremented for redundant messages.
343 count: u32 = 1,343 count: u32 = 1,
344 /// Does not include the trailing newline.344 /// Does not include the trailing newline.
...@@ -427,9 +427,16 @@ pub const AllErrors = struct {...@@ -427,9 +427,16 @@ pub const AllErrors = struct {
427 else => try stderr.writeByte(b),427 else => try stderr.writeByte(b),
428 };428 };
429 try stderr.writeByte('\n');429 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);
431 ttyconf.setColor(stderr, .Green);435 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');
433 ttyconf.setColor(stderr, .Reset);440 ttyconf.setColor(stderr, .Reset);
434 }441 }
435 }442 }
...@@ -469,7 +476,7 @@ pub const AllErrors = struct {...@@ -469,7 +476,7 @@ pub const AllErrors = struct {
469 hasher.update(src.src_path);476 hasher.update(src.src_path);
470 std.hash.autoHash(&hasher, src.line);477 std.hash.autoHash(&hasher, src.line);
471 std.hash.autoHash(&hasher, src.column);478 std.hash.autoHash(&hasher, src.column);
472 std.hash.autoHash(&hasher, src.byte_offset);479 std.hash.autoHash(&hasher, src.span.main);
473 },480 },
474 .plain => |plain| {481 .plain => |plain| {
475 hasher.update(plain.msg);482 hasher.update(plain.msg);
...@@ -488,7 +495,7 @@ pub const AllErrors = struct {...@@ -488,7 +495,7 @@ pub const AllErrors = struct {
488 mem.eql(u8, a_src.src_path, b_src.src_path) and495 mem.eql(u8, a_src.src_path, b_src.src_path) and
489 a_src.line == b_src.line and496 a_src.line == b_src.line and
490 a_src.column == b_src.column and497 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;
492 },499 },
493 .plain => return false,500 .plain => return false,
494 },501 },
...@@ -527,20 +534,20 @@ pub const AllErrors = struct {...@@ -527,20 +534,20 @@ pub const AllErrors = struct {
527 std.hash_map.default_max_load_percentage,534 std.hash_map.default_max_load_percentage,
528 ).init(allocator);535 ).init(allocator);
529 const err_source = try module_err_msg.src_loc.file_scope.getSource(module.gpa);536 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);537 const err_span = try module_err_msg.src_loc.span(module.gpa);
531 const err_loc = std.zig.findLineColumn(err_source.bytes, err_byte_offset);538 const err_loc = std.zig.findLineColumn(err_source.bytes, err_span.main);
532539
533 for (module_err_msg.notes) |module_note| {540 for (module_err_msg.notes) |module_note| {
534 const source = try module_note.src_loc.file_scope.getSource(module.gpa);541 const source = try module_note.src_loc.file_scope.getSource(module.gpa);
535 const byte_offset = try module_note.src_loc.byteOffset(module.gpa);542 const span = try module_note.src_loc.span(module.gpa);
536 const loc = std.zig.findLineColumn(source.bytes, byte_offset);543 const loc = std.zig.findLineColumn(source.bytes, span.main);
537 const file_path = try module_note.src_loc.file_scope.fullPath(allocator);544 const file_path = try module_note.src_loc.file_scope.fullPath(allocator);
538 const note = &notes_buf[note_i];545 const note = &notes_buf[note_i];
539 note.* = .{546 note.* = .{
540 .src = .{547 .src = .{
541 .src_path = file_path,548 .src_path = file_path,
542 .msg = try allocator.dupe(u8, module_note.msg),549 .msg = try allocator.dupe(u8, module_note.msg),
543 .byte_offset = byte_offset,550 .span = span,
544 .line = @intCast(u32, loc.line),551 .line = @intCast(u32, loc.line),
545 .column = @intCast(u32, loc.column),552 .column = @intCast(u32, loc.column),
546 .source_line = if (err_loc.eql(loc)) null else try allocator.dupe(u8, loc.source_line),553 .source_line = if (err_loc.eql(loc)) null else try allocator.dupe(u8, loc.source_line),
...@@ -566,7 +573,7 @@ pub const AllErrors = struct {...@@ -566,7 +573,7 @@ pub const AllErrors = struct {
566 .src = .{573 .src = .{
567 .src_path = file_path,574 .src_path = file_path,
568 .msg = try allocator.dupe(u8, module_err_msg.msg),575 .msg = try allocator.dupe(u8, module_err_msg.msg),
569 .byte_offset = err_byte_offset,576 .span = err_span,
570 .line = @intCast(u32, err_loc.line),577 .line = @intCast(u32, err_loc.line),
571 .column = @intCast(u32, err_loc.column),578 .column = @intCast(u32, err_loc.column),
572 .notes = notes_buf[0..note_i],579 .notes = notes_buf[0..note_i],
...@@ -593,16 +600,16 @@ pub const AllErrors = struct {...@@ -593,16 +600,16 @@ pub const AllErrors = struct {
593 while (item_i < items_len) : (item_i += 1) {600 while (item_i < items_len) : (item_i += 1) {
594 const item = file.zir.extraData(Zir.Inst.CompileErrors.Item, extra_index);601 const item = file.zir.extraData(Zir.Inst.CompileErrors.Item, extra_index);
595 extra_index = item.end;602 extra_index = item.end;
596 const err_byte_offset = blk: {603 const err_span = blk: {
597 const token_starts = file.tree.tokens.items(.start);
598 if (item.data.node != 0) {604 if (item.data.node != 0) {
599 const main_tokens = file.tree.nodes.items(.main_token);605 break :blk Module.SrcLoc.nodeToSpan(&file.tree, item.data.node);
600 const main_token = main_tokens[item.data.node];
601 break :blk token_starts[main_token];
602 }606 }
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 };
604 };611 };
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
607 var notes: []Message = &[0]Message{};614 var notes: []Message = &[0]Message{};
608 if (item.data.notes != 0) {615 if (item.data.notes != 0) {
...@@ -612,22 +619,22 @@ pub const AllErrors = struct {...@@ -612,22 +619,22 @@ pub const AllErrors = struct {
612 for (notes) |*note, i| {619 for (notes) |*note, i| {
613 const note_item = file.zir.extraData(Zir.Inst.CompileErrors.Item, body[i]);620 const note_item = file.zir.extraData(Zir.Inst.CompileErrors.Item, body[i]);
614 const msg = file.zir.nullTerminatedString(note_item.data.msg);621 const msg = file.zir.nullTerminatedString(note_item.data.msg);
615 const byte_offset = blk: {622 const span = blk: {
616 const token_starts = file.tree.tokens.items(.start);
617 if (note_item.data.node != 0) {623 if (note_item.data.node != 0) {
618 const main_tokens = file.tree.nodes.items(.main_token);624 break :blk Module.SrcLoc.nodeToSpan(&file.tree, note_item.data.node);
619 const main_token = main_tokens[note_item.data.node];
620 break :blk token_starts[main_token];
621 }625 }
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 };
623 };630 };
624 const loc = std.zig.findLineColumn(file.source, byte_offset);631 const loc = std.zig.findLineColumn(file.source, span.main);
625632
626 note.* = .{633 note.* = .{
627 .src = .{634 .src = .{
628 .src_path = try file.fullPath(arena),635 .src_path = try file.fullPath(arena),
629 .msg = try arena.dupe(u8, msg),636 .msg = try arena.dupe(u8, msg),
630 .byte_offset = byte_offset,637 .span = span,
631 .line = @intCast(u32, loc.line),638 .line = @intCast(u32, loc.line),
632 .column = @intCast(u32, loc.column),639 .column = @intCast(u32, loc.column),
633 .notes = &.{}, // TODO rework this function to be recursive640 .notes = &.{}, // TODO rework this function to be recursive
...@@ -642,7 +649,7 @@ pub const AllErrors = struct {...@@ -642,7 +649,7 @@ pub const AllErrors = struct {
642 .src = .{649 .src = .{
643 .src_path = try file.fullPath(arena),650 .src_path = try file.fullPath(arena),
644 .msg = try arena.dupe(u8, msg),651 .msg = try arena.dupe(u8, msg),
645 .byte_offset = err_byte_offset,652 .span = err_span,
646 .line = @intCast(u32, err_loc.line),653 .line = @intCast(u32, err_loc.line),
647 .column = @intCast(u32, err_loc.column),654 .column = @intCast(u32, err_loc.column),
648 .notes = notes,655 .notes = notes,
...@@ -688,7 +695,7 @@ pub const AllErrors = struct {...@@ -688,7 +695,7 @@ pub const AllErrors = struct {
688 .src_path = try arena.dupe(u8, src.src_path),695 .src_path = try arena.dupe(u8, src.src_path),
689 .line = src.line,696 .line = src.line,
690 .column = src.column,697 .column = src.column,
691 .byte_offset = src.byte_offset,698 .span = src.span,
692 .source_line = if (src.source_line) |s| try arena.dupe(u8, s) else null,699 .source_line = if (src.source_line) |s| try arena.dupe(u8, s) else null,
693 .notes = try dupeList(src.notes, arena),700 .notes = try dupeList(src.notes, arena),
694 } },701 } },
...@@ -2662,7 +2669,7 @@ pub fn getAllErrorsAlloc(self: *Compilation) !AllErrors {...@@ -2662,7 +2669,7 @@ pub fn getAllErrorsAlloc(self: *Compilation) !AllErrors {
2662 .msg = try std.fmt.allocPrint(arena_allocator, "unable to build C object: {s}", .{2669 .msg = try std.fmt.allocPrint(arena_allocator, "unable to build C object: {s}", .{
2663 err_msg.msg,2670 err_msg.msg,
2664 }),2671 }),
2665 .byte_offset = 0,2672 .span = .{ .start = 0, .end = 1, .main = 0 },
2666 .line = err_msg.line,2673 .line = err_msg.line,
2667 .column = err_msg.column,2674 .column = err_msg.column,
2668 .source_line = null, // TODO2675 .source_line = null, // TODO
src/Module.zig+107-136
...@@ -2082,60 +2082,65 @@ pub const SrcLoc = struct {...@@ -2082,60 +2082,65 @@ pub const SrcLoc = struct {
2082 return @bitCast(Ast.Node.Index, offset + @bitCast(i32, src_loc.parent_decl_node));2082 return @bitCast(Ast.Node.Index, offset + @bitCast(i32, src_loc.parent_decl_node));
2083 }2083 }
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 {
2086 switch (src_loc.lazy) {2092 switch (src_loc.lazy) {
2087 .unneeded => unreachable,2093 .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
2092 .token_abs => |tok_index| {2098 .token_abs => |tok_index| {
2093 const tree = try src_loc.file_scope.getTree(gpa);2099 const tree = try src_loc.file_scope.getTree(gpa);
2094 const token_starts = tree.tokens.items(.start);2100 const start = tree.tokens.items(.start)[tok_index];
2095 return token_starts[tok_index];2101 const end = start + @intCast(u32, tree.tokenSlice(tok_index).len);
2102 return Span{ .start = start, .end = end, .main = start };
2096 },2103 },
2097 .node_abs => |node| {2104 .node_abs => |node| {
2098 const tree = try src_loc.file_scope.getTree(gpa);2105 const tree = try src_loc.file_scope.getTree(gpa);
2099 const token_starts = tree.tokens.items(.start);2106 return nodeToSpan(tree, node);
2100 const tok_index = tree.firstToken(node);
2101 return token_starts[tok_index];
2102 },2107 },
2103 .byte_offset => |byte_off| {2108 .byte_offset => |byte_off| {
2104 const tree = try src_loc.file_scope.getTree(gpa);2109 const tree = try src_loc.file_scope.getTree(gpa);
2105 const token_starts = tree.tokens.items(.start);2110 const tok_index = src_loc.declSrcToken();
2106 return token_starts[src_loc.declSrcToken()] + byte_off;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 };
2107 },2114 },
2108 .token_offset => |tok_off| {2115 .token_offset => |tok_off| {
2109 const tree = try src_loc.file_scope.getTree(gpa);2116 const tree = try src_loc.file_scope.getTree(gpa);
2110 const tok_index = src_loc.declSrcToken() + tok_off;2117 const tok_index = src_loc.declSrcToken() + tok_off;
2111 const token_starts = tree.tokens.items(.start);2118 const start = tree.tokens.items(.start)[tok_index];
2112 return token_starts[tok_index];2119 const end = start + @intCast(u32, tree.tokenSlice(tok_index).len);
2120 return Span{ .start = start, .end = end, .main = start };
2113 },2121 },
2114 .node_offset => |traced_off| {2122 .node_offset => |traced_off| {
2115 const node_off = traced_off.x;2123 const node_off = traced_off.x;
2116 const tree = try src_loc.file_scope.getTree(gpa);2124 const tree = try src_loc.file_scope.getTree(gpa);
2117 const node = src_loc.declRelativeToNodeIndex(node_off);2125 const node = src_loc.declRelativeToNodeIndex(node_off);
2118 assert(src_loc.file_scope.tree_loaded);2126 assert(src_loc.file_scope.tree_loaded);
2119 const main_tokens = tree.nodes.items(.main_token);2127 return nodeToSpan(tree, node);
2120 const tok_index = main_tokens[node];
2121 const token_starts = tree.tokens.items(.start);
2122 return token_starts[tok_index];
2123 },2128 },
2124 .node_offset_bin_op => |node_off| {2129 .node_offset_bin_op => |node_off| {
2125 const tree = try src_loc.file_scope.getTree(gpa);2130 const tree = try src_loc.file_scope.getTree(gpa);
2126 const node = src_loc.declRelativeToNodeIndex(node_off);2131 const node = src_loc.declRelativeToNodeIndex(node_off);
2127 assert(src_loc.file_scope.tree_loaded);2132 assert(src_loc.file_scope.tree_loaded);
2128 const main_tokens = tree.nodes.items(.main_token);2133 return nodeToSpan(tree, node);
2129 const tok_index = main_tokens[node];
2130 const token_starts = tree.tokens.items(.start);
2131 return token_starts[tok_index];
2132 },2134 },
2133 .node_offset_back2tok => |node_off| {2135 .node_offset_initializer => |node_off| {
2134 const tree = try src_loc.file_scope.getTree(gpa);2136 const tree = try src_loc.file_scope.getTree(gpa);
2135 const node = src_loc.declRelativeToNodeIndex(node_off);2137 const node = src_loc.declRelativeToNodeIndex(node_off);
2136 const tok_index = tree.firstToken(node) - 2;2138 return tokensToSpan(
2137 const token_starts = tree.tokens.items(.start);2139 tree,
2138 return token_starts[tok_index];2140 tree.firstToken(node) - 3,
2141 tree.lastToken(node),
2142 tree.nodes.items(.main_token)[node] - 2,
2143 );
2139 },2144 },
2140 .node_offset_var_decl_ty => |node_off| {2145 .node_offset_var_decl_ty => |node_off| {
2141 const tree = try src_loc.file_scope.getTree(gpa);2146 const tree = try src_loc.file_scope.getTree(gpa);
...@@ -2148,14 +2153,13 @@ pub const SrcLoc = struct {...@@ -2148,14 +2153,13 @@ pub const SrcLoc = struct {
2148 .aligned_var_decl => tree.alignedVarDecl(node),2153 .aligned_var_decl => tree.alignedVarDecl(node),
2149 else => unreachable,2154 else => unreachable,
2150 };2155 };
2151 const tok_index = if (full.ast.type_node != 0) blk: {2156 if (full.ast.type_node != 0) {
2152 const main_tokens = tree.nodes.items(.main_token);2157 return nodeToSpan(tree, full.ast.type_node);
2153 break :blk main_tokens[full.ast.type_node];2158 }
2154 } else blk: {2159 const tok_index = full.ast.mut_token + 1; // the name token
2155 break :blk full.ast.mut_token + 1; // the name token2160 const start = tree.tokens.items(.start)[tok_index];
2156 };2161 const end = start + @intCast(u32, tree.tokenSlice(tok_index).len);
2157 const token_starts = tree.tokens.items(.start);2162 return Span{ .start = start, .end = end, .main = start };
2158 return token_starts[tok_index];
2159 },2163 },
2160 .node_offset_builtin_call_arg0 => |n| return src_loc.byteOffsetBuiltinCallArg(gpa, n, 0),2164 .node_offset_builtin_call_arg0 => |n| return src_loc.byteOffsetBuiltinCallArg(gpa, n, 0),
2161 .node_offset_builtin_call_arg1 => |n| return src_loc.byteOffsetBuiltinCallArg(gpa, n, 1),2165 .node_offset_builtin_call_arg1 => |n| return src_loc.byteOffsetBuiltinCallArg(gpa, n, 1),
...@@ -2167,10 +2171,7 @@ pub const SrcLoc = struct {...@@ -2167,10 +2171,7 @@ pub const SrcLoc = struct {
2167 const tree = try src_loc.file_scope.getTree(gpa);2171 const tree = try src_loc.file_scope.getTree(gpa);
2168 const node_datas = tree.nodes.items(.data);2172 const node_datas = tree.nodes.items(.data);
2169 const node = src_loc.declRelativeToNodeIndex(node_off);2173 const node = src_loc.declRelativeToNodeIndex(node_off);
2170 const main_tokens = tree.nodes.items(.main_token);2174 return nodeToSpan(tree, node_datas[node].rhs);
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 },2175 },
2175 .node_offset_slice_ptr,2176 .node_offset_slice_ptr,
2176 .node_offset_slice_start,2177 .node_offset_slice_start,
...@@ -2186,18 +2187,14 @@ pub const SrcLoc = struct {...@@ -2186,18 +2187,14 @@ pub const SrcLoc = struct {
2186 .slice_sentinel => tree.sliceSentinel(node),2187 .slice_sentinel => tree.sliceSentinel(node),
2187 else => unreachable,2188 else => unreachable,
2188 };2189 };
2189 const main_tokens = tree.nodes.items(.main_token);2190 const part_node = switch (src_loc.lazy) {
2190 const tok_index = main_tokens[2191 .node_offset_slice_ptr => full.ast.sliced,
2191 switch (src_loc.lazy) {2192 .node_offset_slice_start => full.ast.start,
2192 .node_offset_slice_ptr => full.ast.sliced,2193 .node_offset_slice_end => full.ast.end,
2193 .node_offset_slice_start => full.ast.start,2194 .node_offset_slice_sentinel => full.ast.sentinel,
2194 .node_offset_slice_end => full.ast.end,2195 else => unreachable,
2195 .node_offset_slice_sentinel => full.ast.sentinel,2196 };
2196 else => unreachable,2197 return nodeToSpan(tree, part_node);
2197 }
2198 ];
2199 const token_starts = tree.tokens.items(.start);
2200 return token_starts[tok_index];
2201 },2198 },
2202 .node_offset_call_func => |node_off| {2199 .node_offset_call_func => |node_off| {
2203 const tree = try src_loc.file_scope.getTree(gpa);2200 const tree = try src_loc.file_scope.getTree(gpa);
...@@ -2219,10 +2216,7 @@ pub const SrcLoc = struct {...@@ -2219,10 +2216,7 @@ pub const SrcLoc = struct {
22192216
2220 else => unreachable,2217 else => unreachable,
2221 };2218 };
2222 const main_tokens = tree.nodes.items(.main_token);2219 return nodeToSpan(tree, full.ast.fn_expr);
2223 const tok_index = main_tokens[full.ast.fn_expr];
2224 const token_starts = tree.tokens.items(.start);
2225 return token_starts[tok_index];
2226 },2220 },
2227 .node_offset_field_name => |node_off| {2221 .node_offset_field_name => |node_off| {
2228 const tree = try src_loc.file_scope.getTree(gpa);2222 const tree = try src_loc.file_scope.getTree(gpa);
...@@ -2233,16 +2227,14 @@ pub const SrcLoc = struct {...@@ -2233,16 +2227,14 @@ pub const SrcLoc = struct {
2233 .field_access => node_datas[node].rhs,2227 .field_access => node_datas[node].rhs,
2234 else => tree.firstToken(node) - 2,2228 else => tree.firstToken(node) - 2,
2235 };2229 };
2236 const token_starts = tree.tokens.items(.start);2230 const start = tree.tokens.items(.start)[tok_index];
2237 return token_starts[tok_index];2231 const end = start + @intCast(u32, tree.tokenSlice(tok_index).len);
2232 return Span{ .start = start, .end = end, .main = start };
2238 },2233 },
2239 .node_offset_deref_ptr => |node_off| {2234 .node_offset_deref_ptr => |node_off| {
2240 const tree = try src_loc.file_scope.getTree(gpa);2235 const tree = try src_loc.file_scope.getTree(gpa);
2241 const node_datas = tree.nodes.items(.data);
2242 const node = src_loc.declRelativeToNodeIndex(node_off);2236 const node = src_loc.declRelativeToNodeIndex(node_off);
2243 const tok_index = node_datas[node].lhs;2237 return nodeToSpan(tree, node);
2244 const token_starts = tree.tokens.items(.start);
2245 return token_starts[tok_index];
2246 },2238 },
2247 .node_offset_asm_source => |node_off| {2239 .node_offset_asm_source => |node_off| {
2248 const tree = try src_loc.file_scope.getTree(gpa);2240 const tree = try src_loc.file_scope.getTree(gpa);
...@@ -2253,10 +2245,7 @@ pub const SrcLoc = struct {...@@ -2253,10 +2245,7 @@ pub const SrcLoc = struct {
2253 .@"asm" => tree.asmFull(node),2245 .@"asm" => tree.asmFull(node),
2254 else => unreachable,2246 else => unreachable,
2255 };2247 };
2256 const main_tokens = tree.nodes.items(.main_token);2248 return nodeToSpan(tree, full.ast.template);
2257 const tok_index = main_tokens[full.ast.template];
2258 const token_starts = tree.tokens.items(.start);
2259 return token_starts[tok_index];
2260 },2249 },
2261 .node_offset_asm_ret_ty => |node_off| {2250 .node_offset_asm_ret_ty => |node_off| {
2262 const tree = try src_loc.file_scope.getTree(gpa);2251 const tree = try src_loc.file_scope.getTree(gpa);
...@@ -2269,11 +2258,7 @@ pub const SrcLoc = struct {...@@ -2269,11 +2258,7 @@ pub const SrcLoc = struct {
2269 };2258 };
2270 const asm_output = full.outputs[0];2259 const asm_output = full.outputs[0];
2271 const node_datas = tree.nodes.items(.data);2260 const node_datas = tree.nodes.items(.data);
2272 const ret_ty_node = node_datas[asm_output].lhs;2261 return nodeToSpan(tree, 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];
2277 },2262 },
22782263
2279 .node_offset_for_cond, .node_offset_if_cond => |node_off| {2264 .node_offset_for_cond, .node_offset_if_cond => |node_off| {
...@@ -2290,41 +2275,26 @@ pub const SrcLoc = struct {...@@ -2290,41 +2275,26 @@ pub const SrcLoc = struct {
2290 .@"for" => tree.forFull(node).ast.cond_expr,2275 .@"for" => tree.forFull(node).ast.cond_expr,
2291 else => unreachable,2276 else => unreachable,
2292 };2277 };
2293 const main_tokens = tree.nodes.items(.main_token);2278 return nodeToSpan(tree, src_node);
2294 const tok_index = main_tokens[src_node];
2295 const token_starts = tree.tokens.items(.start);
2296 return token_starts[tok_index];
2297 },2279 },
2298 .node_offset_bin_lhs => |node_off| {2280 .node_offset_bin_lhs => |node_off| {
2299 const tree = try src_loc.file_scope.getTree(gpa);2281 const tree = try src_loc.file_scope.getTree(gpa);
2300 const node = src_loc.declRelativeToNodeIndex(node_off);2282 const node = src_loc.declRelativeToNodeIndex(node_off);
2301 const node_datas = tree.nodes.items(.data);2283 const node_datas = tree.nodes.items(.data);
2302 const src_node = node_datas[node].lhs;2284 return nodeToSpan(tree, 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];
2307 },2285 },
2308 .node_offset_bin_rhs => |node_off| {2286 .node_offset_bin_rhs => |node_off| {
2309 const tree = try src_loc.file_scope.getTree(gpa);2287 const tree = try src_loc.file_scope.getTree(gpa);
2310 const node = src_loc.declRelativeToNodeIndex(node_off);2288 const node = src_loc.declRelativeToNodeIndex(node_off);
2311 const node_datas = tree.nodes.items(.data);2289 const node_datas = tree.nodes.items(.data);
2312 const src_node = node_datas[node].rhs;2290 return nodeToSpan(tree, 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];
2317 },2291 },
23182292
2319 .node_offset_switch_operand => |node_off| {2293 .node_offset_switch_operand => |node_off| {
2320 const tree = try src_loc.file_scope.getTree(gpa);2294 const tree = try src_loc.file_scope.getTree(gpa);
2321 const node = src_loc.declRelativeToNodeIndex(node_off);2295 const node = src_loc.declRelativeToNodeIndex(node_off);
2322 const node_datas = tree.nodes.items(.data);2296 const node_datas = tree.nodes.items(.data);
2323 const src_node = node_datas[node].lhs;2297 return nodeToSpan(tree, 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];
2328 },2298 },
23292299
2330 .node_offset_switch_special_prong => |node_off| {2300 .node_offset_switch_special_prong => |node_off| {
...@@ -2347,9 +2317,7 @@ pub const SrcLoc = struct {...@@ -2347,9 +2317,7 @@ pub const SrcLoc = struct {
2347 mem.eql(u8, tree.tokenSlice(main_tokens[case.ast.values[0]]), "_"));2317 mem.eql(u8, tree.tokenSlice(main_tokens[case.ast.values[0]]), "_"));
2348 if (!is_special) continue;2318 if (!is_special) continue;
23492319
2350 const tok_index = main_tokens[case_node];2320 return nodeToSpan(tree, case_node);
2351 const token_starts = tree.tokens.items(.start);
2352 return token_starts[tok_index];
2353 } else unreachable;2321 } else unreachable;
2354 },2322 },
23552323
...@@ -2375,9 +2343,7 @@ pub const SrcLoc = struct {...@@ -2375,9 +2343,7 @@ pub const SrcLoc = struct {
23752343
2376 for (case.ast.values) |item_node| {2344 for (case.ast.values) |item_node| {
2377 if (node_tags[item_node] == .switch_range) {2345 if (node_tags[item_node] == .switch_range) {
2378 const tok_index = main_tokens[item_node];2346 return nodeToSpan(tree, item_node);
2379 const token_starts = tree.tokens.items(.start);
2380 return token_starts[tok_index];
2381 }2347 }
2382 }2348 }
2383 } else unreachable;2349 } else unreachable;
...@@ -2403,10 +2369,7 @@ pub const SrcLoc = struct {...@@ -2403,10 +2369,7 @@ pub const SrcLoc = struct {
2403 },2369 },
2404 else => unreachable,2370 else => unreachable,
2405 };2371 };
2406 const main_tokens = tree.nodes.items(.main_token);2372 return nodeToSpan(tree, full.ast.callconv_expr);
2407 const tok_index = main_tokens[full.ast.callconv_expr];
2408 const token_starts = tree.tokens.items(.start);
2409 return token_starts[tok_index];
2410 },2373 },
24112374
2412 .node_offset_fn_type_ret_ty => |node_off| {2375 .node_offset_fn_type_ret_ty => |node_off| {
...@@ -2421,21 +2384,14 @@ pub const SrcLoc = struct {...@@ -2421,21 +2384,14 @@ pub const SrcLoc = struct {
2421 .fn_proto => tree.fnProto(node),2384 .fn_proto => tree.fnProto(node),
2422 else => unreachable,2385 else => unreachable,
2423 };2386 };
2424 const main_tokens = tree.nodes.items(.main_token);2387 return nodeToSpan(tree, full.ast.return_type);
2425 const tok_index = main_tokens[full.ast.return_type];
2426 const token_starts = tree.tokens.items(.start);
2427 return token_starts[tok_index];
2428 },2388 },
24292389
2430 .node_offset_anyframe_type => |node_off| {2390 .node_offset_anyframe_type => |node_off| {
2431 const tree = try src_loc.file_scope.getTree(gpa);2391 const tree = try src_loc.file_scope.getTree(gpa);
2432 const node_datas = tree.nodes.items(.data);2392 const node_datas = tree.nodes.items(.data);
2433 const parent_node = src_loc.declRelativeToNodeIndex(node_off);2393 const parent_node = src_loc.declRelativeToNodeIndex(node_off);
2434 const node = node_datas[parent_node].rhs;2394 return nodeToSpan(tree, 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];
2439 },2395 },
24402396
2441 .node_offset_lib_name => |node_off| {2397 .node_offset_lib_name => |node_off| {
...@@ -2462,8 +2418,9 @@ pub const SrcLoc = struct {...@@ -2462,8 +2418,9 @@ pub const SrcLoc = struct {
2462 else => unreachable,2418 else => unreachable,
2463 };2419 };
2464 const tok_index = full.lib_name.?;2420 const tok_index = full.lib_name.?;
2465 const token_starts = tree.tokens.items(.start);2421 const start = tree.tokens.items(.start)[tok_index];
2466 return token_starts[tok_index];2422 const end = start + @intCast(u32, tree.tokenSlice(tok_index).len);
2423 return Span{ .start = start, .end = end, .main = start };
2467 },2424 },
24682425
2469 .node_offset_array_type_len => |node_off| {2426 .node_offset_array_type_len => |node_off| {
...@@ -2476,11 +2433,7 @@ pub const SrcLoc = struct {...@@ -2476,11 +2433,7 @@ pub const SrcLoc = struct {
2476 .array_type_sentinel => tree.arrayTypeSentinel(parent_node),2433 .array_type_sentinel => tree.arrayTypeSentinel(parent_node),
2477 else => unreachable,2434 else => unreachable,
2478 };2435 };
2479 const node = full.ast.elem_count;2436 return nodeToSpan(tree, 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];
2484 },2437 },
2485 .node_offset_array_type_sentinel => |node_off| {2438 .node_offset_array_type_sentinel => |node_off| {
2486 const tree = try src_loc.file_scope.getTree(gpa);2439 const tree = try src_loc.file_scope.getTree(gpa);
...@@ -2492,11 +2445,7 @@ pub const SrcLoc = struct {...@@ -2492,11 +2445,7 @@ pub const SrcLoc = struct {
2492 .array_type_sentinel => tree.arrayTypeSentinel(parent_node),2445 .array_type_sentinel => tree.arrayTypeSentinel(parent_node),
2493 else => unreachable,2446 else => unreachable,
2494 };2447 };
2495 const node = full.ast.sentinel;2448 return nodeToSpan(tree, 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];
2500 },2449 },
2501 .node_offset_array_type_elem => |node_off| {2450 .node_offset_array_type_elem => |node_off| {
2502 const tree = try src_loc.file_scope.getTree(gpa);2451 const tree = try src_loc.file_scope.getTree(gpa);
...@@ -2508,21 +2457,14 @@ pub const SrcLoc = struct {...@@ -2508,21 +2457,14 @@ pub const SrcLoc = struct {
2508 .array_type_sentinel => tree.arrayTypeSentinel(parent_node),2457 .array_type_sentinel => tree.arrayTypeSentinel(parent_node),
2509 else => unreachable,2458 else => unreachable,
2510 };2459 };
2511 const node = full.ast.elem_type;2460 return nodeToSpan(tree, 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];
2516 },2461 },
2517 .node_offset_un_op => |node_off| {2462 .node_offset_un_op => |node_off| {
2518 const tree = try src_loc.file_scope.getTree(gpa);2463 const tree = try src_loc.file_scope.getTree(gpa);
2519 const node_datas = tree.nodes.items(.data);2464 const node_datas = tree.nodes.items(.data);
2520 const node = src_loc.declRelativeToNodeIndex(node_off);2465 const node = src_loc.declRelativeToNodeIndex(node_off);
25212466
2522 const main_tokens = tree.nodes.items(.main_token);2467 return nodeToSpan(tree, node_datas[node].lhs);
2523 const tok_index = main_tokens[node_datas[node].lhs];
2524 const token_starts = tree.tokens.items(.start);
2525 return token_starts[tok_index];
2526 },2468 },
2527 }2469 }
2528 }2470 }
...@@ -2532,7 +2474,7 @@ pub const SrcLoc = struct {...@@ -2532,7 +2474,7 @@ pub const SrcLoc = struct {
2532 gpa: Allocator,2474 gpa: Allocator,
2533 node_off: i32,2475 node_off: i32,
2534 arg_index: u32,2476 arg_index: u32,
2535 ) !u32 {2477 ) !Span {
2536 const tree = try src_loc.file_scope.getTree(gpa);2478 const tree = try src_loc.file_scope.getTree(gpa);
2537 const node_datas = tree.nodes.items(.data);2479 const node_datas = tree.nodes.items(.data);
2538 const node_tags = tree.nodes.items(.tag);2480 const node_tags = tree.nodes.items(.tag);
...@@ -2546,10 +2488,36 @@ pub const SrcLoc = struct {...@@ -2546,10 +2488,36 @@ pub const SrcLoc = struct {
2546 .builtin_call, .builtin_call_comma => tree.extra_data[node_datas[node].lhs + arg_index],2488 .builtin_call, .builtin_call_comma => tree.extra_data[node_datas[node].lhs + arg_index],
2547 else => unreachable,2489 else => unreachable,
2548 };2490 };
2549 const main_tokens = tree.nodes.items(.main_token);2491 return nodeToSpan(tree, param);
2550 const tok_index = main_tokens[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 {
2551 const token_starts = tree.tokens.items(.start);2504 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] };
2553 }2521 }
2554};2522};
25552523
...@@ -2603,10 +2571,9 @@ pub const LazySrcLoc = union(enum) {...@@ -2603,10 +2571,9 @@ pub const LazySrcLoc = union(enum) {
2603 /// from its containing Decl node AST index.2571 /// from its containing Decl node AST index.
2604 /// The Decl is determined contextually.2572 /// The Decl is determined contextually.
2605 node_offset: TracedOffset,2573 node_offset: TracedOffset,
2606 /// The source location points to two tokens left of the first token of an AST node,2574 /// The source location points to the beginning of a struct initializer.
2607 /// which is this value offset from its containing Decl node AST index.
2608 /// The Decl is determined contextually.2575 /// The Decl is determined contextually.
2609 node_offset_back2tok: i32,2576 node_offset_initializer: i32,
2610 /// The source location points to a variable declaration type expression,2577 /// The source location points to a variable declaration type expression,
2611 /// found by taking this AST node index offset from the containing2578 /// found by taking this AST node index offset from the containing
2612 /// Decl AST node, which points to a variable declaration AST node. Next, navigate2579 /// Decl AST node, which points to a variable declaration AST node. Next, navigate
...@@ -2802,7 +2769,7 @@ pub const LazySrcLoc = union(enum) {...@@ -2802,7 +2769,7 @@ pub const LazySrcLoc = union(enum) {
2802 .byte_offset,2769 .byte_offset,
2803 .token_offset,2770 .token_offset,
2804 .node_offset,2771 .node_offset,
2805 .node_offset_back2tok,2772 .node_offset_initializer,
2806 .node_offset_var_decl_ty,2773 .node_offset_var_decl_ty,
2807 .node_offset_for_cond,2774 .node_offset_for_cond,
2808 .node_offset_builtin_call_arg0,2775 .node_offset_builtin_call_arg0,
...@@ -3313,7 +3280,11 @@ pub fn astGenFile(mod: *Module, file: *File) !void {...@@ -3313,7 +3280,11 @@ pub fn astGenFile(mod: *Module, file: *File) !void {
3313 .src_loc = .{3280 .src_loc = .{
3314 .file_scope = file,3281 .file_scope = file,
3315 .parent_decl_node = 0,3282 .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 },
3317 },3288 },
3318 .msg = msg.toOwnedSlice(),3289 .msg = msg.toOwnedSlice(),
3319 };3290 };
...@@ -3336,7 +3307,7 @@ pub fn astGenFile(mod: *Module, file: *File) !void {...@@ -3336,7 +3307,7 @@ pub fn astGenFile(mod: *Module, file: *File) !void {
3336 .src_loc = .{3307 .src_loc = .{
3337 .file_scope = file,3308 .file_scope = file,
3338 .parent_decl_node = 0,3309 .parent_decl_node = 0,
3339 .lazy = .{ .byte_abs = token_starts[note.token] },3310 .lazy = .{ .token_abs = note.token },
3340 },3311 },
3341 .msg = msg.toOwnedSlice(),3312 .msg = msg.toOwnedSlice(),
3342 };3313 };
src/Sema.zig+8-8
...@@ -3497,7 +3497,7 @@ fn validateUnionInit(...@@ -3497,7 +3497,7 @@ fn validateUnionInit(
34973497
3498 for (instrs[1..]) |inst| {3498 for (instrs[1..]) |inst| {
3499 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;3499 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 };
3501 try sema.errNote(block, inst_src, msg, "additional initializer here", .{});3501 try sema.errNote(block, inst_src, msg, "additional initializer here", .{});
3502 }3502 }
3503 try sema.addDeclaredHereNote(msg, union_ty);3503 try sema.addDeclaredHereNote(msg, union_ty);
...@@ -3515,7 +3515,7 @@ fn validateUnionInit(...@@ -3515,7 +3515,7 @@ fn validateUnionInit(
35153515
3516 const field_ptr = instrs[0];3516 const field_ptr = instrs[0];
3517 const field_ptr_data = sema.code.instructions.items(.data)[field_ptr].pl_node;3517 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 };
3519 const field_ptr_extra = sema.code.extraData(Zir.Inst.Field, field_ptr_data.payload_index).data;3519 const field_ptr_extra = sema.code.extraData(Zir.Inst.Field, field_ptr_data.payload_index).data;
3520 const field_name = sema.code.nullTerminatedString(field_ptr_extra.field_name_start);3520 const field_name = sema.code.nullTerminatedString(field_ptr_extra.field_name_start);
3521 const field_index = try sema.unionFieldIndex(block, union_ty, field_name, field_src);3521 const field_index = try sema.unionFieldIndex(block, union_ty, field_name, field_src);
...@@ -3617,7 +3617,7 @@ fn validateStructInit(...@@ -3617,7 +3617,7 @@ fn validateStructInit(
36173617
3618 for (instrs) |field_ptr| {3618 for (instrs) |field_ptr| {
3619 const field_ptr_data = sema.code.instructions.items(.data)[field_ptr].pl_node;3619 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 };
3621 const field_ptr_extra = sema.code.extraData(Zir.Inst.Field, field_ptr_data.payload_index).data;3621 const field_ptr_extra = sema.code.extraData(Zir.Inst.Field, field_ptr_data.payload_index).data;
3622 struct_ptr_zir_ref = field_ptr_extra.lhs;3622 struct_ptr_zir_ref = field_ptr_extra.lhs;
3623 const field_name = sema.code.nullTerminatedString(field_ptr_extra.field_name_start);3623 const field_name = sema.code.nullTerminatedString(field_ptr_extra.field_name_start);
...@@ -3625,7 +3625,7 @@ fn validateStructInit(...@@ -3625,7 +3625,7 @@ fn validateStructInit(
3625 if (found_fields[field_index] != 0) {3625 if (found_fields[field_index] != 0) {
3626 const other_field_ptr = found_fields[field_index];3626 const other_field_ptr = found_fields[field_index];
3627 const other_field_ptr_data = sema.code.instructions.items(.data)[other_field_ptr].pl_node;3627 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 };
3629 const msg = msg: {3629 const msg = msg: {
3630 const msg = try sema.errMsg(block, field_src, "duplicate field", .{});3630 const msg = try sema.errMsg(block, field_src, "duplicate field", .{});
3631 errdefer msg.destroy(gpa);3631 errdefer msg.destroy(gpa);
...@@ -3700,7 +3700,7 @@ fn validateStructInit(...@@ -3700,7 +3700,7 @@ fn validateStructInit(
3700 field: for (found_fields) |field_ptr, i| {3700 field: for (found_fields) |field_ptr, i| {
3701 if (field_ptr != 0) {3701 if (field_ptr != 0) {
3702 const field_ptr_data = sema.code.instructions.items(.data)[field_ptr].pl_node;3702 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
3705 // Determine whether the value stored to this pointer is comptime-known.3705 // Determine whether the value stored to this pointer is comptime-known.
3706 const field_ty = struct_ty.structFieldType(i);3706 const field_ty = struct_ty.structFieldType(i);
...@@ -14096,14 +14096,14 @@ fn zirStructInit(...@@ -14096,14 +14096,14 @@ fn zirStructInit(
14096 extra_index = item.end;14096 extra_index = item.end;
1409714097
14098 const field_type_data = zir_datas[item.data.field_type].pl_node;14098 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 };
14100 const field_type_extra = sema.code.extraData(Zir.Inst.FieldType, field_type_data.payload_index).data;14100 const field_type_extra = sema.code.extraData(Zir.Inst.FieldType, field_type_data.payload_index).data;
14101 const field_name = sema.code.nullTerminatedString(field_type_extra.name_start);14101 const field_name = sema.code.nullTerminatedString(field_type_extra.name_start);
14102 const field_index = try sema.structFieldIndex(block, resolved_ty, field_name, field_src);14102 const field_index = try sema.structFieldIndex(block, resolved_ty, field_name, field_src);
14103 if (field_inits[field_index] != .none) {14103 if (field_inits[field_index] != .none) {
14104 const other_field_type = found_fields[field_index];14104 const other_field_type = found_fields[field_index];
14105 const other_field_type_data = zir_datas[other_field_type].pl_node;14105 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 };
14107 const msg = msg: {14107 const msg = msg: {
14108 const msg = try sema.errMsg(block, field_src, "duplicate field", .{});14108 const msg = try sema.errMsg(block, field_src, "duplicate field", .{});
14109 errdefer msg.destroy(gpa);14109 errdefer msg.destroy(gpa);
...@@ -14125,7 +14125,7 @@ fn zirStructInit(...@@ -14125,7 +14125,7 @@ fn zirStructInit(
14125 const item = sema.code.extraData(Zir.Inst.StructInit.Item, extra.end);14125 const item = sema.code.extraData(Zir.Inst.StructInit.Item, extra.end);
1412614126
14127 const field_type_data = zir_datas[item.data.field_type].pl_node;14127 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 };
14129 const field_type_extra = sema.code.extraData(Zir.Inst.FieldType, field_type_data.payload_index).data;14129 const field_type_extra = sema.code.extraData(Zir.Inst.FieldType, field_type_data.payload_index).data;
14130 const field_name = sema.code.nullTerminatedString(field_type_extra.name_start);14130 const field_name = sema.code.nullTerminatedString(field_type_extra.name_start);
14131 const field_index = try sema.unionFieldIndex(block, resolved_ty, field_name, field_src);14131 const field_index = try sema.unionFieldIndex(block, resolved_ty, field_name, field_src);
src/main.zig+13-3
...@@ -4387,7 +4387,7 @@ fn printErrsMsgToStdErr(...@@ -4387,7 +4387,7 @@ fn printErrsMsgToStdErr(
4387 .msg = try std.fmt.allocPrint(arena, "invalid byte: '{'}'", .{4387 .msg = try std.fmt.allocPrint(arena, "invalid byte: '{'}'", .{
4388 std.zig.fmtEscapes(tree.source[byte_offset..][0..1]),4388 std.zig.fmtEscapes(tree.source[byte_offset..][0..1]),
4389 }),4389 }),
4390 .byte_offset = byte_offset,4390 .span = .{ .start = byte_offset, .end = byte_offset + 1, .main = byte_offset },
4391 .line = @intCast(u32, start_loc.line),4391 .line = @intCast(u32, start_loc.line),
4392 .column = @intCast(u32, start_loc.column) + bad_off,4392 .column = @intCast(u32, start_loc.column) + bad_off,
4393 .source_line = source_line,4393 .source_line = source_line,
...@@ -4402,11 +4402,16 @@ fn printErrsMsgToStdErr(...@@ -4402,11 +4402,16 @@ fn printErrsMsgToStdErr(
4402 text_buf.items.len = 0;4402 text_buf.items.len = 0;
4403 try tree.renderError(note, writer);4403 try tree.renderError(note, writer);
4404 const note_loc = tree.tokenLocation(0, note.token);4404 const note_loc = tree.tokenLocation(0, note.token);
4405 const byte_offset = @intCast(u32, note_loc.line_start);
4405 notes_buffer[notes_len] = .{4406 notes_buffer[notes_len] = .{
4406 .src = .{4407 .src = .{
4407 .src_path = path,4408 .src_path = path,
4408 .msg = try arena.dupe(u8, text_buf.items),4409 .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 },
4410 .line = @intCast(u32, note_loc.line),4415 .line = @intCast(u32, note_loc.line),
4411 .column = @intCast(u32, note_loc.column),4416 .column = @intCast(u32, note_loc.column),
4412 .source_line = tree.source[note_loc.line_start..note_loc.line_end],4417 .source_line = tree.source[note_loc.line_start..note_loc.line_end],
...@@ -4417,11 +4422,16 @@ fn printErrsMsgToStdErr(...@@ -4417,11 +4422,16 @@ fn printErrsMsgToStdErr(
4417 }4422 }
44184423
4419 const extra_offset = tree.errorOffset(parse_error);4424 const extra_offset = tree.errorOffset(parse_error);
4425 const byte_offset = @intCast(u32, start_loc.line_start) + extra_offset;
4420 const message: Compilation.AllErrors.Message = .{4426 const message: Compilation.AllErrors.Message = .{
4421 .src = .{4427 .src = .{
4422 .src_path = path,4428 .src_path = path,
4423 .msg = text,4429 .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 },
4425 .line = @intCast(u32, start_loc.line),4435 .line = @intCast(u32, start_loc.line),
4426 .column = @intCast(u32, start_loc.column) + extra_offset,4436 .column = @intCast(u32, start_loc.column) + extra_offset,
4427 .source_line = source_line,4437 .source_line = source_line,
src/print_zir.zig+6-4
...@@ -2381,10 +2381,12 @@ const Writer = struct {...@@ -2381,10 +2381,12 @@ const Writer = struct {
2381 .parent_decl_node = self.parent_decl_node,2381 .parent_decl_node = self.parent_decl_node,
2382 .lazy = src,2382 .lazy = src,
2383 };2383 };
2384 const abs_byte_off = src_loc.byteOffset(self.gpa) catch unreachable;2384 const src_span = src_loc.span(self.gpa) catch unreachable;
2385 const delta_line = std.zig.findLineColumn(tree.source, abs_byte_off);2385 const start = std.zig.findLineColumn(tree.source, src_span.start);
2386 try stream.print("{s}:{d}:{d}", .{2386 const end = std.zig.findLineColumn(tree.source, src_span.end);
2387 @tagName(src), delta_line.line + 1, delta_line.column + 1,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,
2388 });2390 });
2389 }2391 }
2390 }2392 }
test/compile_errors.zig+9
...@@ -174,6 +174,15 @@ pub fn addCases(ctx: *TestContext) !void {...@@ -174,6 +174,15 @@ pub fn addCases(ctx: *TestContext) !void {
174 });174 });
175 }175 }
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
177 // TODO test this in stage2, but we won't even try in stage1186 // TODO test this in stage2, but we won't even try in stage1
178 //ctx.objErrStage1("inline fn calls itself indirectly",187 //ctx.objErrStage1("inline fn calls itself indirectly",
179 // \\export fn foo() void {188 // \\export fn foo() void {