Skip to content

Commit 63bf9e1

Browse files
committed
Maintain highlighting in note and help even when they have a span
1 parent e57f309 commit 63bf9e1

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
@@ -741,6 +741,16 @@ impl<'a, G: EmissionGuarantee> Diag<'a, G> {
741741
self
742742
}
743743

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

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

compiler/rustc_errors/src/emitter.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1346,7 +1346,7 @@ impl HumanEmitter {
13461346
buffer.append(0, ": ", header_style);
13471347
label_width += 2;
13481348
}
1349-
for (text, _) in msgs.iter() {
1349+
for (text, style) in msgs.iter() {
13501350
let text = self.translate_message(text, args).map_err(Report::new).unwrap();
13511351
// Account for newlines to align output to its label.
13521352
for (line, text) in normalize_whitespace(&text).lines().enumerate() {
@@ -1357,7 +1357,10 @@ impl HumanEmitter {
13571357
if line == 0 { String::new() } else { " ".repeat(label_width) },
13581358
text
13591359
),
1360-
header_style,
1360+
match style {
1361+
Style::Highlight => *style,
1362+
_ => header_style,
1363+
},
13611364
);
13621365
}
13631366
}

0 commit comments

Comments
 (0)