Skip to content

Commit 8982c7f

Browse files
committed
Maintain highlighting in note and help even when they have a span
1 parent d371d17 commit 8982c7f

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

compiler/rustc_errors/src/diagnostic.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -735,6 +735,16 @@ impl<'a, G: EmissionGuarantee> Diag<'a, G> {
735735
self
736736
}
737737

738+
#[rustc_lint_diagnostics]
739+
pub fn highlighted_span_note(
740+
&mut self,
741+
span: impl Into<MultiSpan>,
742+
msg: Vec<StringPart>,
743+
) -> &mut Self {
744+
self.sub_with_highlights(Level::Note, msg, span.into());
745+
self
746+
}
747+
738748
/// This is like [`Diag::note()`], but it's only printed once.
739749
#[rustc_lint_diagnostics]
740750
pub fn note_once(&mut self, msg: impl Into<SubdiagMessage>) -> &mut Self {
@@ -809,6 +819,17 @@ impl<'a, G: EmissionGuarantee> Diag<'a, G> {
809819
self
810820
}
811821

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

compiler/rustc_errors/src/emitter.rs

Lines changed: 5 additions & 2 deletions
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)