Skip to content

Commit

Permalink
Maintain highlighting in note and help even when they have a span
Browse files Browse the repository at this point in the history
  • Loading branch information
estebank committed Aug 6, 2024
1 parent 60d1465 commit db6c05f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
21 changes: 21 additions & 0 deletions compiler/rustc_errors/src/diagnostic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -741,6 +741,16 @@ impl<'a, G: EmissionGuarantee> Diag<'a, G> {
self
}

#[rustc_lint_diagnostics]
pub fn highlighted_span_note(
&mut self,
span: impl Into<MultiSpan>,
msg: Vec<StringPart>,
) -> &mut Self {
self.sub_with_highlights(Level::Note, msg, span.into());
self
}

/// This is like [`Diag::note()`], but it's only printed once.
#[rustc_lint_diagnostics]
pub fn note_once(&mut self, msg: impl Into<SubdiagMessage>) -> &mut Self {
Expand Down Expand Up @@ -815,6 +825,17 @@ impl<'a, G: EmissionGuarantee> Diag<'a, G> {
self
}

/// Add a help message attached to this diagnostic with a customizable highlighted message.
#[rustc_lint_diagnostics]
pub fn highlighted_span_help(
&mut self,
span: impl Into<MultiSpan>,
msg: Vec<StringPart>,
) -> &mut Self {
self.sub_with_highlights(Level::Help, msg, span.into());
self
}

/// Prints the span with some help above it.
/// This is like [`Diag::help()`], but it gets its own span.
#[rustc_lint_diagnostics]
Expand Down
7 changes: 5 additions & 2 deletions compiler/rustc_errors/src/emitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1347,7 +1347,7 @@ impl HumanEmitter {
label_width += 2;
}
let mut line = 0;
for (text, _) in msgs.iter() {
for (text, style) in msgs.iter() {
let text = self.translate_message(text, args).map_err(Report::new).unwrap();
// Account for newlines to align output to its label.
for text in normalize_whitespace(&text).lines() {
Expand All @@ -1358,7 +1358,10 @@ impl HumanEmitter {
if line == 0 { String::new() } else { " ".repeat(label_width) },
text
),
header_style,
match style {
Style::Highlight => *style,
_ => header_style,
},
);
line += 1;
}
Expand Down

0 comments on commit db6c05f

Please sign in to comment.