Skip to content

Commit 87191fa

Browse files
committed
Maintain highlighting in note and help even when they have a span
1 parent faefc61 commit 87191fa

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
@@ -737,6 +737,16 @@ impl<'a, G: EmissionGuarantee> Diag<'a, G> {
737737
self
738738
}
739739

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

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

compiler/rustc_errors/src/emitter.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -1328,7 +1328,7 @@ impl HumanEmitter {
13281328
buffer.append(0, ": ", header_style);
13291329
label_width += 2;
13301330
}
1331-
for (text, _) in msgs.iter() {
1331+
for (text, style) in msgs.iter() {
13321332
let text = self.translate_message(text, args).map_err(Report::new).unwrap();
13331333
// Account for newlines to align output to its label.
13341334
for (line, text) in normalize_whitespace(&text).lines().enumerate() {
@@ -1339,7 +1339,10 @@ impl HumanEmitter {
13391339
if line == 0 { String::new() } else { " ".repeat(label_width) },
13401340
text
13411341
),
1342-
header_style,
1342+
match style {
1343+
Style::Highlight => *style,
1344+
_ => header_style,
1345+
},
13431346
);
13441347
}
13451348
}

0 commit comments

Comments
 (0)