Skip to content

Commit 284ec9c

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 be01dab commit 284ec9c

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

Diff for: compiler/rustc_trait_selection/src/error_reporting/traits/fulfillment_errors.rs

+14-5
Original file line numberDiff line numberDiff line change
@@ -1818,23 +1818,32 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
18181818
if impl_trait_ref.references_error() {
18191819
return false;
18201820
}
1821+
let self_ty = impl_trait_ref.self_ty().to_string();
18211822
err.highlighted_help(vec![
18221823
StringPart::normal(format!(
18231824
"the trait `{}` ",
18241825
impl_trait_ref.print_trait_sugared()
18251826
)),
18261827
StringPart::highlighted("is"),
18271828
StringPart::normal(" implemented for `"),
1828-
StringPart::highlighted(impl_trait_ref.self_ty().to_string()),
1829+
if let [TypeError::Sorts(_)] = &terrs[..] {
1830+
StringPart::normal(self_ty)
1831+
} else {
1832+
StringPart::highlighted(self_ty)
1833+
},
18291834
StringPart::normal("`"),
18301835
]);
18311836

18321837
if let [TypeError::Sorts(exp_found)] = &terrs[..] {
18331838
let exp_found = self.resolve_vars_if_possible(*exp_found);
1834-
err.help(format!(
1835-
"for that trait implementation, expected `{}`, found `{}`",
1836-
exp_found.expected, exp_found.found
1837-
));
1839+
err.highlighted_help(vec![
1840+
StringPart::normal("for that trait implementation, "),
1841+
StringPart::normal("expected `"),
1842+
StringPart::highlighted(exp_found.expected.to_string()),
1843+
StringPart::normal("`, found `"),
1844+
StringPart::highlighted(exp_found.found.to_string()),
1845+
StringPart::normal("`"),
1846+
]);
18381847
}
18391848

18401849
true

0 commit comments

Comments
 (0)