Skip to content

Commit b5a8382

Browse files
committed
stage2: point to error location using spans
1 parent a455927 commit b5a8382

File tree

4 files changed

+128
-150
lines changed

4 files changed

+128
-150
lines changed

src/Compilation.zig

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
@@ -429,7 +429,10 @@ pub const AllErrors = struct {
429429
try stderr.writeByte('\n');
430430
try stderr.writeByteNTimes(' ', src.column);
431431
ttyconf.setColor(stderr, .Green);
432-
try stderr.writeAll("^\n");
432+
try stderr.writeByte('^');
433+
// TODO basic unicode code point monospace width
434+
try stderr.writeByteNTimes('~', src.span.end - src.span.start - 1);
435+
try stderr.writeByte('\n');
433436
ttyconf.setColor(stderr, .Reset);
434437
}
435438
}
@@ -469,7 +472,8 @@ pub const AllErrors = struct {
469472
hasher.update(src.src_path);
470473
std.hash.autoHash(&hasher, src.line);
471474
std.hash.autoHash(&hasher, src.column);
472-
std.hash.autoHash(&hasher, src.byte_offset);
475+
std.hash.autoHash(&hasher, src.span.start);
476+
std.hash.autoHash(&hasher, src.span.end);
473477
},
474478
.plain => |plain| {
475479
hasher.update(plain.msg);
@@ -488,7 +492,8 @@ pub const AllErrors = struct {
488492
mem.eql(u8, a_src.src_path, b_src.src_path) and
489493
a_src.line == b_src.line and
490494
a_src.column == b_src.column and
491-
a_src.byte_offset == b_src.byte_offset;
495+
a_src.span.start == b_src.span.start and
496+
a_src.span.end == b_src.span.end;
492497
},
493498
.plain => return false,
494499
},
@@ -527,20 +532,20 @@ pub const AllErrors = struct {
527532
std.hash_map.default_max_load_percentage,
528533
).init(allocator);
529534
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);
535+
const err_span = try module_err_msg.src_loc.span(module.gpa);
536+
const err_loc = std.zig.findLineColumn(err_source.bytes, err_span.start);
532537

533538
for (module_err_msg.notes) |module_note| {
534539
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);
540+
const span = try module_note.src_loc.span(module.gpa);
541+
const loc = std.zig.findLineColumn(source.bytes, span.start);
537542
const file_path = try module_note.src_loc.file_scope.fullPath(allocator);
538543
const note = &notes_buf[note_i];
539544
note.* = .{
540545
.src = .{
541546
.src_path = file_path,
542547
.msg = try allocator.dupe(u8, module_note.msg),
543-
.byte_offset = byte_offset,
548+
.span = span,
544549
.line = @intCast(u32, loc.line),
545550
.column = @intCast(u32, loc.column),
546551
.source_line = if (err_loc.eql(loc)) null else try allocator.dupe(u8, loc.source_line),
@@ -566,7 +571,7 @@ pub const AllErrors = struct {
566571
.src = .{
567572
.src_path = file_path,
568573
.msg = try allocator.dupe(u8, module_err_msg.msg),
569-
.byte_offset = err_byte_offset,
574+
.span = err_span,
570575
.line = @intCast(u32, err_loc.line),
571576
.column = @intCast(u32, err_loc.column),
572577
.notes = notes_buf[0..note_i],
@@ -593,16 +598,15 @@ pub const AllErrors = struct {
593598
while (item_i < items_len) : (item_i += 1) {
594599
const item = file.zir.extraData(Zir.Inst.CompileErrors.Item, extra_index);
595600
extra_index = item.end;
596-
const err_byte_offset = blk: {
597-
const token_starts = file.tree.tokens.items(.start);
601+
const err_span = blk: {
598602
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];
603+
break :blk Module.SrcLoc.nodeToSpan(&file.tree, item.data.node);
602604
}
603-
break :blk token_starts[item.data.token] + item.data.byte_offset;
605+
const token_starts = file.tree.tokens.items(.start);
606+
const start = token_starts[item.data.token] + item.data.byte_offset;
607+
break :blk Module.SrcLoc.Span{ .start = start, .end = start + 1 };
604608
};
605-
const err_loc = std.zig.findLineColumn(file.source, err_byte_offset);
609+
const err_loc = std.zig.findLineColumn(file.source, err_span.start);
606610

607611
var notes: []Message = &[0]Message{};
608612
if (item.data.notes != 0) {
@@ -612,22 +616,21 @@ pub const AllErrors = struct {
612616
for (notes) |*note, i| {
613617
const note_item = file.zir.extraData(Zir.Inst.CompileErrors.Item, body[i]);
614618
const msg = file.zir.nullTerminatedString(note_item.data.msg);
615-
const byte_offset = blk: {
616-
const token_starts = file.tree.tokens.items(.start);
619+
const span = blk: {
617620
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];
621+
break :blk Module.SrcLoc.nodeToSpan(&file.tree, note_item.data.node);
621622
}
622-
break :blk token_starts[note_item.data.token] + note_item.data.byte_offset;
623+
const token_starts = file.tree.tokens.items(.start);
624+
const start = token_starts[note_item.data.token] + note_item.data.byte_offset;
625+
break :blk Module.SrcLoc.Span{ .start = start, .end = start + 1 };
623626
};
624-
const loc = std.zig.findLineColumn(file.source, byte_offset);
627+
const loc = std.zig.findLineColumn(file.source, span.start);
625628

626629
note.* = .{
627630
.src = .{
628631
.src_path = try file.fullPath(arena),
629632
.msg = try arena.dupe(u8, msg),
630-
.byte_offset = byte_offset,
633+
.span = span,
631634
.line = @intCast(u32, loc.line),
632635
.column = @intCast(u32, loc.column),
633636
.notes = &.{}, // TODO rework this function to be recursive
@@ -642,7 +645,7 @@ pub const AllErrors = struct {
642645
.src = .{
643646
.src_path = try file.fullPath(arena),
644647
.msg = try arena.dupe(u8, msg),
645-
.byte_offset = err_byte_offset,
648+
.span = err_span,
646649
.line = @intCast(u32, err_loc.line),
647650
.column = @intCast(u32, err_loc.column),
648651
.notes = notes,
@@ -688,7 +691,7 @@ pub const AllErrors = struct {
688691
.src_path = try arena.dupe(u8, src.src_path),
689692
.line = src.line,
690693
.column = src.column,
691-
.byte_offset = src.byte_offset,
694+
.span = src.span,
692695
.source_line = if (src.source_line) |s| try arena.dupe(u8, s) else null,
693696
.notes = try dupeList(src.notes, arena),
694697
} },
@@ -2662,7 +2665,7 @@ pub fn getAllErrorsAlloc(self: *Compilation) !AllErrors {
26622665
.msg = try std.fmt.allocPrint(arena_allocator, "unable to build C object: {s}", .{
26632666
err_msg.msg,
26642667
}),
2665-
.byte_offset = 0,
2668+
.span = .{ .start = 0, .end = 1 },
26662669
.line = err_msg.line,
26672670
.column = err_msg.column,
26682671
.source_line = null, // TODO

0 commit comments

Comments
 (0)