diff --git a/codespan-reporting/src/diagnostic.rs b/codespan-reporting/src/diagnostic.rs index 8f017ba..828aff1 100644 --- a/codespan-reporting/src/diagnostic.rs +++ b/codespan-reporting/src/diagnostic.rs @@ -113,7 +113,7 @@ impl Label { /// Represents a diagnostic message that can provide information like errors and /// warnings to the user. /// -/// The position of a Diagnostic is considered to be the position of the [`Label`] with a style of [`LabelStyle::primary`] that has the smallest start position. +/// The position of a Diagnostic is considered to be the position of the [`Label`] that has the earliest starting position and has the highest style which appears in all the labels of the diagnostic. #[derive(Clone, Debug)] #[cfg_attr(feature = "serialization", derive(Serialize, Deserialize))] pub struct Diagnostic { diff --git a/codespan-reporting/src/term/views.rs b/codespan-reporting/src/term/views.rs index 59cdcc5..076a237 100644 --- a/codespan-reporting/src/term/views.rs +++ b/codespan-reporting/src/term/views.rs @@ -119,10 +119,11 @@ where { Some(labeled_file) => { // another diagnostic also referenced this file - if labeled_file.start > label.range.start - && labeled_file.max_label_style >= label.style + if labeled_file.max_label_style > label.style + || (labeled_file.max_label_style == label.style + && labeled_file.start > label.range.start) { - // this label indicates an earlier start and has at least the same level of style + // this label has a higher style or has the same style but starts earlier labeled_file.start = label.range.start; labeled_file.location = files .location(label.file_id, label.range.start) diff --git a/codespan-reporting/tests/term.rs b/codespan-reporting/tests/term.rs index 4a0fa26..8b1bf2d 100644 --- a/codespan-reporting/tests/term.rs +++ b/codespan-reporting/tests/term.rs @@ -569,9 +569,10 @@ mod multiline_overlapping { .with_message("match arms have incompatible types") .with_code("E0308") .with_labels(vec![ + // this secondary label is before the primary label to test the locus calculation (see issue #259) + Label::secondary((), 89..134).with_message("this is found to be of type `Result`"), Label::primary((), 230..351).with_message("expected enum `Result`, found struct `LineIndexOutOfBoundsError`"), Label::secondary((), 8..362).with_message("`match` arms have incompatible types"), - Label::secondary((), 89..134).with_message("this is found to be of type `Result`"), Label::secondary((), 167..195).with_message("this is found to be of type `Result`"), ]) .with_notes(vec![unindent::unindent(