1
Fork 0

Rollup merge of #136415 - estebank:highlight-clarification, r=compiler-errors

Highlight clarifying information in "expected/found" error

When the expected and found types have the same textual representation, we add clarifying in parentheses. We now visually highlight it in the output.

Detect a corner case where the clarifying information would be the same for both types and skip it, as it doesn't add anything useful.

![Screenshot of the rustc highlighted output on the terminal](https://github.com/user-attachments/assets/aa4b9433-5332-4941-b2c2-1a43e5cadff7)
This commit is contained in:
Matthias Krüger 2025-02-02 18:05:24 +01:00 committed by GitHub
commit 7e3d872bc0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 160 additions and 27 deletions

View file

@ -644,7 +644,14 @@ impl<'a, G: EmissionGuarantee> Diag<'a, G> {
found_label: &dyn fmt::Display,
found: DiagStyledString,
) -> &mut Self {
self.note_expected_found_extra(expected_label, expected, found_label, found, &"", &"")
self.note_expected_found_extra(
expected_label,
expected,
found_label,
found,
DiagStyledString::normal(""),
DiagStyledString::normal(""),
)
}
#[rustc_lint_diagnostics]
@ -654,8 +661,8 @@ impl<'a, G: EmissionGuarantee> Diag<'a, G> {
expected: DiagStyledString,
found_label: &dyn fmt::Display,
found: DiagStyledString,
expected_extra: &dyn fmt::Display,
found_extra: &dyn fmt::Display,
expected_extra: DiagStyledString,
found_extra: DiagStyledString,
) -> &mut Self {
let expected_label = expected_label.to_string();
let expected_label = if expected_label.is_empty() {
@ -680,10 +687,13 @@ impl<'a, G: EmissionGuarantee> Diag<'a, G> {
expected_label
))];
msg.extend(expected.0);
msg.push(StringPart::normal(format!("`{expected_extra}\n")));
msg.push(StringPart::normal(format!("`")));
msg.extend(expected_extra.0);
msg.push(StringPart::normal(format!("\n")));
msg.push(StringPart::normal(format!("{}{} `", " ".repeat(found_padding), found_label)));
msg.extend(found.0);
msg.push(StringPart::normal(format!("`{found_extra}")));
msg.push(StringPart::normal(format!("`")));
msg.extend(found_extra.0);
// For now, just attach these as notes.
self.highlighted_note(msg);