@@ -338,7 +338,7 @@ pub const AllErrors = struct {
338
338
src_path : []const u8 ,
339
339
line : u32 ,
340
340
column : u32 ,
341
- byte_offset : u32 ,
341
+ span : Module.SrcLoc.Span ,
342
342
/// Usually one, but incremented for redundant messages.
343
343
count : u32 = 1 ,
344
344
/// Does not include the trailing newline.
@@ -429,7 +429,10 @@ pub const AllErrors = struct {
429
429
try stderr .writeByte ('\n ' );
430
430
try stderr .writeByteNTimes (' ' , src .column );
431
431
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 ' );
433
436
ttyconf .setColor (stderr , .Reset );
434
437
}
435
438
}
@@ -469,7 +472,8 @@ pub const AllErrors = struct {
469
472
hasher .update (src .src_path );
470
473
std .hash .autoHash (& hasher , src .line );
471
474
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 );
473
477
},
474
478
.plain = > | plain | {
475
479
hasher .update (plain .msg );
@@ -488,7 +492,8 @@ pub const AllErrors = struct {
488
492
mem .eql (u8 , a_src .src_path , b_src .src_path ) and
489
493
a_src .line == b_src .line and
490
494
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 ;
492
497
},
493
498
.plain = > return false ,
494
499
},
@@ -527,20 +532,20 @@ pub const AllErrors = struct {
527
532
std .hash_map .default_max_load_percentage ,
528
533
).init (allocator );
529
534
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 );
532
537
533
538
for (module_err_msg .notes ) | module_note | {
534
539
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 );
537
542
const file_path = try module_note .src_loc .file_scope .fullPath (allocator );
538
543
const note = & notes_buf [note_i ];
539
544
note .* = .{
540
545
.src = .{
541
546
.src_path = file_path ,
542
547
.msg = try allocator .dupe (u8 , module_note .msg ),
543
- .byte_offset = byte_offset ,
548
+ .span = span ,
544
549
.line = @intCast (u32 , loc .line ),
545
550
.column = @intCast (u32 , loc .column ),
546
551
.source_line = if (err_loc .eql (loc )) null else try allocator .dupe (u8 , loc .source_line ),
@@ -566,7 +571,7 @@ pub const AllErrors = struct {
566
571
.src = .{
567
572
.src_path = file_path ,
568
573
.msg = try allocator .dupe (u8 , module_err_msg .msg ),
569
- .byte_offset = err_byte_offset ,
574
+ .span = err_span ,
570
575
.line = @intCast (u32 , err_loc .line ),
571
576
.column = @intCast (u32 , err_loc .column ),
572
577
.notes = notes_buf [0.. note_i ],
@@ -593,16 +598,15 @@ pub const AllErrors = struct {
593
598
while (item_i < items_len ) : (item_i += 1 ) {
594
599
const item = file .zir .extraData (Zir .Inst .CompileErrors .Item , extra_index );
595
600
extra_index = item .end ;
596
- const err_byte_offset = blk : {
597
- const token_starts = file .tree .tokens .items (.start );
601
+ const err_span = blk : {
598
602
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 );
602
604
}
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 };
604
608
};
605
- const err_loc = std .zig .findLineColumn (file .source , err_byte_offset );
609
+ const err_loc = std .zig .findLineColumn (file .source , err_span . start );
606
610
607
611
var notes : []Message = &[0 ]Message {};
608
612
if (item .data .notes != 0 ) {
@@ -612,22 +616,21 @@ pub const AllErrors = struct {
612
616
for (notes ) | * note , i | {
613
617
const note_item = file .zir .extraData (Zir .Inst .CompileErrors .Item , body [i ]);
614
618
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 : {
617
620
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 );
621
622
}
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 };
623
626
};
624
- const loc = std .zig .findLineColumn (file .source , byte_offset );
627
+ const loc = std .zig .findLineColumn (file .source , span . start );
625
628
626
629
note .* = .{
627
630
.src = .{
628
631
.src_path = try file .fullPath (arena ),
629
632
.msg = try arena .dupe (u8 , msg ),
630
- .byte_offset = byte_offset ,
633
+ .span = span ,
631
634
.line = @intCast (u32 , loc .line ),
632
635
.column = @intCast (u32 , loc .column ),
633
636
.notes = &.{}, // TODO rework this function to be recursive
@@ -642,7 +645,7 @@ pub const AllErrors = struct {
642
645
.src = .{
643
646
.src_path = try file .fullPath (arena ),
644
647
.msg = try arena .dupe (u8 , msg ),
645
- .byte_offset = err_byte_offset ,
648
+ .span = err_span ,
646
649
.line = @intCast (u32 , err_loc .line ),
647
650
.column = @intCast (u32 , err_loc .column ),
648
651
.notes = notes ,
@@ -688,7 +691,7 @@ pub const AllErrors = struct {
688
691
.src_path = try arena .dupe (u8 , src .src_path ),
689
692
.line = src .line ,
690
693
.column = src .column ,
691
- .byte_offset = src .byte_offset ,
694
+ .span = src .span ,
692
695
.source_line = if (src .source_line ) | s | try arena .dupe (u8 , s ) else null ,
693
696
.notes = try dupeList (src .notes , arena ),
694
697
} },
@@ -2662,7 +2665,7 @@ pub fn getAllErrorsAlloc(self: *Compilation) !AllErrors {
2662
2665
.msg = try std .fmt .allocPrint (arena_allocator , "unable to build C object: {s}" , .{
2663
2666
err_msg .msg ,
2664
2667
}),
2665
- .byte_offset = 0 ,
2668
+ .span = .{ . start = 0 , . end = 1 } ,
2666
2669
.line = err_msg .line ,
2667
2670
.column = err_msg .column ,
2668
2671
.source_line = null , // TODO
0 commit comments