Skip to content

Commit ba13bda

Browse files
committed
Tweak highlighting when trait is available for different type
When printing ``` = help: the trait `chumsky::private::ParserSealed<'_, &'a str, ((), ()), chumsky::extra::Full<EmptyErr, (), ()>>` is implemented for `Then<Ignored<chumsky::combinator::Filter<chumsky::primitive::Any<&str, chumsky::extra::Full<EmptyErr, (), ()>>, {closure@src/main.rs:9:17: 9:27}>, char>, chumsky::combinator::Map<impl CSTParser<'a, O>, O, {closure@src/main.rs:11:24: 11:27}>, (), (), chumsky::extra::Full<EmptyErr, (), ()>>` = help: for that trait implementation, expected `((), ())`, found `()` ``` Highlight only the `expected` and `found` types, instead of the full type in the first `help`.
1 parent 4e6146e commit ba13bda

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

compiler/rustc_trait_selection/src/error_reporting/traits/fulfillment_errors.rs

+14-5
Original file line numberDiff line numberDiff line change
@@ -1835,23 +1835,32 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
18351835
if impl_trait_ref.references_error() {
18361836
return false;
18371837
}
1838+
let self_ty = impl_trait_ref.self_ty().to_string();
18381839
err.highlighted_help(vec![
18391840
StringPart::normal(format!(
18401841
"the trait `{}` ",
18411842
impl_trait_ref.print_trait_sugared()
18421843
)),
18431844
StringPart::highlighted("is"),
18441845
StringPart::normal(" implemented for `"),
1845-
StringPart::highlighted(impl_trait_ref.self_ty().to_string()),
1846+
if let [TypeError::Sorts(_)] = &terrs[..] {
1847+
StringPart::normal(self_ty)
1848+
} else {
1849+
StringPart::highlighted(self_ty)
1850+
},
18461851
StringPart::normal("`"),
18471852
]);
18481853

18491854
if let [TypeError::Sorts(exp_found)] = &terrs[..] {
18501855
let exp_found = self.resolve_vars_if_possible(*exp_found);
1851-
err.help(format!(
1852-
"for that trait implementation, expected `{}`, found `{}`",
1853-
exp_found.expected, exp_found.found
1854-
));
1856+
err.highlighted_help(vec![
1857+
StringPart::normal("for that trait implementation, "),
1858+
StringPart::normal("expected `"),
1859+
StringPart::highlighted(exp_found.expected.to_string()),
1860+
StringPart::normal("`, found `"),
1861+
StringPart::highlighted(exp_found.found.to_string()),
1862+
StringPart::normal("`"),
1863+
]);
18551864
}
18561865

18571866
true

tests/ui/impl-trait/diagnostics/highlight-difference-between-expected-trait-and-found-trait.svg

+2-2
Loading

0 commit comments

Comments
 (0)