From db6c05f9b93ffa2375773d8a5e69ca59b22d3c85 Mon Sep 17 00:00:00 2001 From: Esteban Küber Date: Thu, 9 May 2024 20:55:32 +0000 Subject: Maintain highlighting in `note` and `help` even when they have a span --- compiler/rustc_errors/src/diagnostic.rs | 21 +++++++++++++++++++++ compiler/rustc_errors/src/emitter.rs | 7 +++++-- 2 files changed, 26 insertions(+), 2 deletions(-) (limited to 'compiler/rustc_errors') diff --git a/compiler/rustc_errors/src/diagnostic.rs b/compiler/rustc_errors/src/diagnostic.rs index e1dcbf5b280..67ca6d50cca 100644 --- a/compiler/rustc_errors/src/diagnostic.rs +++ b/compiler/rustc_errors/src/diagnostic.rs @@ -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, + msg: Vec, + ) -> &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) -> &mut Self { @@ -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, + msg: Vec, + ) -> &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] diff --git a/compiler/rustc_errors/src/emitter.rs b/compiler/rustc_errors/src/emitter.rs index 483b757f20c..8df3b685829 100644 --- a/compiler/rustc_errors/src/emitter.rs +++ b/compiler/rustc_errors/src/emitter.rs @@ -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() { @@ -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; } -- cgit 1.4.1-3-g733a5 From 034b73ba54f8221c97f70828d7bd529d64b8fe8f Mon Sep 17 00:00:00 2001 From: Esteban Küber Date: Tue, 6 Aug 2024 18:36:43 +0000 Subject: fix test --- compiler/rustc_errors/src/emitter.rs | 8 ++++++++ tests/run-make/crate-loading/rmake.rs | 26 +++++++++++--------------- 2 files changed, 19 insertions(+), 15 deletions(-) (limited to 'compiler/rustc_errors') diff --git a/compiler/rustc_errors/src/emitter.rs b/compiler/rustc_errors/src/emitter.rs index 8df3b685829..88ed3128164 100644 --- a/compiler/rustc_errors/src/emitter.rs +++ b/compiler/rustc_errors/src/emitter.rs @@ -1365,6 +1365,14 @@ impl HumanEmitter { ); line += 1; } + // We add lines above, but if the last line has no explicit newline (which would + // yield an empty line), then we revert one line up to continue with the next + // styled text chunk on the same line as the last one from the prior one. Otherwise + // every `text` would appear on their own line (because even though they didn't end + // in '\n', they advanced `line` by one). + if line > 0 { + line -= 1; + } } if self.short_message { let labels = msp diff --git a/tests/run-make/crate-loading/rmake.rs b/tests/run-make/crate-loading/rmake.rs index 2aa396fd2d9..fd5b66ae879 100644 --- a/tests/run-make/crate-loading/rmake.rs +++ b/tests/run-make/crate-loading/rmake.rs @@ -13,19 +13,15 @@ fn main() { .input("multiple-dep-versions.rs") .extern_("dependency", rust_lib_name("dependency")) .extern_("dep_2_reexport", rust_lib_name("dependency2")) - .inspect(|cmd| eprintln!("{cmd:?}")) - .run_fail(); - let stderr = out.stderr_utf8(); - assert_contains( - &stderr, - "you have multiple different versions of crate `dependency` in your dependency graph", - ); - assert_contains( - &stderr, - "two types coming from two different versions of the same crate are different types even \ - if they look the same", - ); - assert_contains(&stderr, "this type doesn't implement the required trait"); - assert_contains(&stderr, "this type implements the required trait"); - assert_contains(&stderr, "this is the required trait"); + .run_fail() + .assert_stderr_contains( + "you have multiple different versions of crate `dependency` in your dependency graph", + ) + .assert_stderr_contains( + "two types coming from two different versions of the same crate are different types \ + even if they look the same", + ) + .assert_stderr_contains("this type doesn't implement the required trait") + .assert_stderr_contains("this type implements the required trait") + .assert_stderr_contains("this is the required trait"); } -- cgit 1.4.1-3-g733a5