Skip to content

Commit a6888b4

Browse files
committed
Maintain highlighting in note and help even when they have a span
1 parent bcf1f6d commit a6888b4

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

compiler/rustc_errors/src/diagnostic.rs

+21
Original file line numberDiff line numberDiff line change
@@ -740,6 +740,16 @@ impl<'a, G: EmissionGuarantee> Diag<'a, G> {
740740
self
741741
}
742742

743+
#[rustc_lint_diagnostics]
744+
pub fn highlighted_span_note(
745+
&mut self,
746+
span: impl Into<MultiSpan>,
747+
msg: Vec<StringPart>,
748+
) -> &mut Self {
749+
self.sub_with_highlights(Level::Note, msg, span.into());
750+
self
751+
}
752+
743753
/// This is like [`Diag::note()`], but it's only printed once.
744754
#[rustc_lint_diagnostics]
745755
pub fn note_once(&mut self, msg: impl Into<SubdiagMessage>) -> &mut Self {
@@ -814,6 +824,17 @@ impl<'a, G: EmissionGuarantee> Diag<'a, G> {
814824
self
815825
}
816826

827+
/// Add a help message attached to this diagnostic with a customizable highlighted message.
828+
#[rustc_lint_diagnostics]
829+
pub fn highlighted_span_help(
830+
&mut self,
831+
span: impl Into<MultiSpan>,
832+
msg: Vec<StringPart>,
833+
) -> &mut Self {
834+
self.sub_with_highlights(Level::Help, msg, span.into());
835+
self
836+
}
837+
817838
/// Prints the span with some help above it.
818839
/// This is like [`Diag::help()`], but it gets its own span.
819840
#[rustc_lint_diagnostics]

compiler/rustc_errors/src/emitter.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -1353,7 +1353,7 @@ impl HumanEmitter {
13531353
buffer.append(0, ": ", header_style);
13541354
label_width += 2;
13551355
}
1356-
for (text, _) in msgs.iter() {
1356+
for (text, style) in msgs.iter() {
13571357
let text = self.translate_message(text, args).map_err(Report::new).unwrap();
13581358
// Account for newlines to align output to its label.
13591359
for (line, text) in normalize_whitespace(&text).lines().enumerate() {
@@ -1364,7 +1364,10 @@ impl HumanEmitter {
13641364
if line == 0 { String::new() } else { " ".repeat(label_width) },
13651365
text
13661366
),
1367-
header_style,
1367+
match style {
1368+
Style::Highlight => *style,
1369+
_ => header_style,
1370+
},
13681371
);
13691372
}
13701373
}

0 commit comments

Comments
 (0)