Skip to content

Commit cb0db7f

Browse files
committed
Highlight more differences between fn pointer and fn item and constructor
1 parent 65eb340 commit cb0db7f

File tree

1 file changed

+20
-16
lines changed
  • compiler/rustc_infer/src/infer/error_reporting

1 file changed

+20
-16
lines changed

compiler/rustc_infer/src/infer/error_reporting/mod.rs

+20-16
Original file line numberDiff line numberDiff line change
@@ -1116,13 +1116,23 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
11161116
s.push_normal(ty.to_string());
11171117
}
11181118

1119-
fn fn_def_pretty_prefix(tcx: TyCtxt<'_>, did: DefId) -> DiagnosticStyledString {
1120-
let description = match tcx.def_kind(did) {
1119+
fn fn_def_pretty_prefixes<const N: usize>(
1120+
tcx: TyCtxt<'_>,
1121+
dids: [DefId; N],
1122+
) -> [DiagnosticStyledString; N] {
1123+
assert!(N == 1 || N == 2);
1124+
let descriptions = dids.map(|did| match tcx.def_kind(did) {
11211125
hir::def::DefKind::Ctor(_, hir::def::CtorKind::Fn) => "constructor of",
11221126
hir::def::DefKind::Fn => "fn item",
11231127
_ => unreachable!(),
1124-
};
1125-
DiagnosticStyledString::normal(format!("[{description} {{"))
1128+
});
1129+
let highlight = N == 1 || descriptions[0] != descriptions[1];
1130+
descriptions.map(|description| {
1131+
let mut styled_string = DiagnosticStyledString::normal("[");
1132+
styled_string.push(description.to_string(), highlight);
1133+
styled_string.push_normal(" {");
1134+
styled_string
1135+
})
11261136
}
11271137

11281138
// process starts here
@@ -1380,10 +1390,8 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
13801390
(&ty::FnDef(did1, substs1), &ty::FnDef(did2, substs2)) => {
13811391
let sig1 = self.tcx.bound_fn_sig(did1).subst(self.tcx, substs1);
13821392
let sig2 = self.tcx.bound_fn_sig(did2).subst(self.tcx, substs2);
1383-
let mut values = (
1384-
fn_def_pretty_prefix(self.tcx, did1),
1385-
fn_def_pretty_prefix(self.tcx, did2),
1386-
);
1393+
let [v1, v2] = fn_def_pretty_prefixes(self.tcx, [did1, did2]);
1394+
let mut values = (v1, v2);
13871395
let path1 = self.tcx.def_path_str_with_substs(did1, substs1);
13881396
let path2 = self.tcx.def_path_str_with_substs(did2, substs2);
13891397
let same_path = path1 == path2;
@@ -1399,10 +1407,8 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
13991407

14001408
(&ty::FnDef(did1, substs1), ty::FnPtr(sig2)) => {
14011409
let sig1 = self.tcx.bound_fn_sig(did1).subst(self.tcx, substs1);
1402-
let mut values = (
1403-
fn_def_pretty_prefix(self.tcx, did1),
1404-
DiagnosticStyledString::new(),
1405-
);
1410+
let [v1] = fn_def_pretty_prefixes(self.tcx, [did1]);
1411+
let mut values = (v1, DiagnosticStyledString::new());
14061412
values.0.push_highlighted(self.tcx.def_path_str_with_substs(did1, substs1));
14071413
values.0.push_normal("}: ");
14081414
self.cmp_fn_sig(&sig1, sig2, &mut values, true);
@@ -1412,10 +1418,8 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
14121418

14131419
(ty::FnPtr(sig1), &ty::FnDef(did2, substs2)) => {
14141420
let sig2 = self.tcx.bound_fn_sig(did2).subst(self.tcx, substs2);
1415-
let mut values = (
1416-
DiagnosticStyledString::new(),
1417-
fn_def_pretty_prefix(self.tcx, did2),
1418-
);
1421+
let [v2] = fn_def_pretty_prefixes(self.tcx, [did2]);
1422+
let mut values = (DiagnosticStyledString::new(), v2);
14191423
values.1.push_highlighted(self.tcx.def_path_str_with_substs(did2, substs2));
14201424
values.1.push_normal("}: ");
14211425
self.cmp_fn_sig(sig1, &sig2, &mut values, true);

0 commit comments

Comments
 (0)