Skip to content

Commit a6be8a3

Browse files
committed
fix: Match rustc's annotation overlap
1 parent 2033e8f commit a6be8a3

File tree

3 files changed

+48
-13
lines changed

3 files changed

+48
-13
lines changed

src/renderer/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1167,7 +1167,7 @@ impl Renderer {
11671167
// otherwise the lines would end up needing to go over a message.
11681168

11691169
let mut annotations = line_info.annotations.clone();
1170-
annotations.sort_by_key(|a| Reverse(a.start.display));
1170+
annotations.sort_by_key(|a| Reverse(a.start));
11711171

11721172
// First, figure out where each label will be positioned.
11731173
//
@@ -1249,8 +1249,8 @@ impl Renderer {
12491249
{
12501250
// If we're overlapping with an un-labelled annotation with the same span
12511251
// we can just merge them in the output
1252-
if next.start.display == annotation.start.display
1253-
&& next.end.display == annotation.end.display
1252+
if next.start == annotation.start
1253+
&& next.end == annotation.end
12541254
&& !next.has_label()
12551255
{
12561256
continue;
@@ -1284,7 +1284,7 @@ impl Renderer {
12841284
&& next.takes_space())
12851285
|| (annotation.takes_space() && next.takes_space())
12861286
|| (overlaps(next, annotation, l)
1287-
&& next.end.display <= annotation.end.display
1287+
&& next.end <= annotation.end
12881288
&& next.has_label()
12891289
&& p == 0)
12901290
// Avoid #42595.

src/renderer/source_map.rs

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::renderer::{char_width, is_different, num_overlap, LineAnnotation, LineAnnotationType};
22
use crate::{Annotation, AnnotationKind, Patch};
33
use std::borrow::Cow;
4-
use std::cmp::{max, min};
4+
use std::cmp::{max, min, Ordering};
55
use std::ops::Range;
66

77
#[derive(Debug)]
@@ -294,12 +294,16 @@ impl<'a> SourceMap<'a> {
294294
.map_or(ann.start.line, |(line, _)| line);
295295
for line in ann.start.line + 1..until {
296296
// Every `|` that joins the beginning of the span (`___^`) to the end (`|__^`).
297-
self.add_annotation_to_file(&mut annotated_line_infos, line, ann.as_line());
297+
self.add_annotation_to_file(&mut annotated_line_infos, line, ann.as_line(line));
298298
}
299299
let line_end = ann.end.line - 1;
300300
let end_is_empty = self.get_line(line_end).map_or(false, |s| !filter(s));
301301
if middle < line_end && !end_is_empty {
302-
self.add_annotation_to_file(&mut annotated_line_infos, line_end, ann.as_line());
302+
self.add_annotation_to_file(
303+
&mut annotated_line_infos,
304+
line_end,
305+
ann.as_line(line_end),
306+
);
303307
}
304308
}
305309
self.add_annotation_to_file(&mut annotated_line_infos, end_ann.end.line, end_ann);
@@ -575,10 +579,16 @@ impl<'a> MultilineAnnotation<'a> {
575579
}
576580
}
577581

578-
pub(crate) fn as_line(&self) -> LineAnnotation<'a> {
582+
pub(crate) fn as_line(&self, line: usize) -> LineAnnotation<'a> {
579583
LineAnnotation {
580-
start: Loc::default(),
581-
end: Loc::default(),
584+
start: Loc {
585+
line,
586+
..Default::default()
587+
},
588+
end: Loc {
589+
line,
590+
..Default::default()
591+
},
582592
kind: self.kind,
583593
label: None,
584594
annotation_type: LineAnnotationType::MultilineLine(self.depth),
@@ -604,7 +614,7 @@ pub(crate) struct AnnotatedLineInfo<'a> {
604614
}
605615

606616
/// A source code location used for error reporting.
607-
#[derive(Clone, Copy, Debug, Default, PartialOrd, Ord, PartialEq, Eq)]
617+
#[derive(Clone, Copy, Debug, Default, Eq)]
608618
pub(crate) struct Loc {
609619
/// The (1-based) line number.
610620
pub(crate) line: usize,
@@ -616,6 +626,30 @@ pub(crate) struct Loc {
616626
pub(crate) byte: usize,
617627
}
618628

629+
impl PartialEq for Loc {
630+
fn eq(&self, other: &Self) -> bool {
631+
self.line.eq(&other.line) && self.display.eq(&other.display) && self.char.eq(&other.char)
632+
}
633+
}
634+
635+
impl Ord for Loc {
636+
fn cmp(&self, other: &Self) -> Ordering {
637+
match self.line.cmp(&other.line) {
638+
Ordering::Equal => match self.display.cmp(&other.display) {
639+
Ordering::Equal => self.char.cmp(&other.char),
640+
c => c,
641+
},
642+
c => c,
643+
}
644+
}
645+
}
646+
647+
impl PartialOrd for Loc {
648+
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
649+
Some(self.cmp(other))
650+
}
651+
}
652+
619653
struct CursorLines<'a>(&'a str);
620654

621655
impl CursorLines<'_> {

tests/rustc_tests.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3500,8 +3500,9 @@ error: extern blocks should be unsafe
35003500
--> $DIR/unsafe-extern-suggestion.rs:6:1
35013501
|
35023502
LL | extern "C" {
3503-
| ^ help: needs `unsafe` before the extern keyword: `unsafe`
3504-
| _|
3503+
| ^
3504+
| |
3505+
| _help: needs `unsafe` before the extern keyword: `unsafe`
35053506
| |
35063507
LL | | //~^ ERROR extern blocks should be unsafe [missing_unsafe_on_extern]
35073508
LL | | //~| WARN this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2024!

0 commit comments

Comments
 (0)