Skip to content

Commit 677381a

Browse files
committed
On type mismatch error highlight & when type matches
When the only difference between the two types in a type error is that one is a reference to the other type (`T` vs `&T`) or both are references differing only in their mutability (`&T` vs `&mut T`), don't highlight the type (`T`).
1 parent ea51b19 commit 677381a

File tree

1 file changed

+58
-0
lines changed
  • src/librustc/infer/error_reporting

1 file changed

+58
-0
lines changed

src/librustc/infer/error_reporting/mod.rs

+58
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,20 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
555555
fn cmp(&self, t1: Ty<'tcx>, t2: Ty<'tcx>)
556556
-> (DiagnosticStyledString, DiagnosticStyledString)
557557
{
558+
fn equals<'tcx>(a: &Ty<'tcx>, b: &Ty<'tcx>) -> bool {
559+
match (&a.sty, &b.sty) {
560+
(a, b) if *a == *b => true,
561+
(&ty::TyInt(_), &ty::TyInfer(ty::InferTy::IntVar(_))) |
562+
(&ty::TyInfer(ty::InferTy::IntVar(_)), &ty::TyInt(_)) |
563+
(&ty::TyInfer(ty::InferTy::IntVar(_)), &ty::TyInfer(ty::InferTy::IntVar(_))) |
564+
(&ty::TyFloat(_), &ty::TyInfer(ty::InferTy::FloatVar(_))) |
565+
(&ty::TyInfer(ty::InferTy::FloatVar(_)), &ty::TyFloat(_)) |
566+
(&ty::TyInfer(ty::InferTy::FloatVar(_)),
567+
&ty::TyInfer(ty::InferTy::FloatVar(_))) => true,
568+
_ => false,
569+
}
570+
}
571+
558572
match (&t1.sty, &t2.sty) {
559573
(&ty::TyAdt(def1, sub1), &ty::TyAdt(def2, sub2)) => {
560574
let mut values = (DiagnosticStyledString::new(), DiagnosticStyledString::new());
@@ -672,6 +686,50 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
672686
DiagnosticStyledString::highlighted(format!("{}", t2)))
673687
}
674688
}
689+
690+
// When finding T != &T, hightlight only the borrow
691+
(&ty::TyRef(_, ref tnm1), _) if equals(&tnm1.ty, &t2) => {
692+
let mut values = (DiagnosticStyledString::new(), DiagnosticStyledString::new());
693+
values.0.push_highlighted(format!("&{}", if tnm1.mutbl == hir::MutMutable {
694+
"mut "
695+
} else {
696+
""
697+
}));
698+
values.0.push_normal(format!("{}", tnm1.ty));
699+
values.1.push_normal(format!("{}", t2));
700+
values
701+
}
702+
(_, &ty::TyRef(_, ref tnm2)) if equals(&t1, &tnm2.ty) => {
703+
let mut values = (DiagnosticStyledString::new(), DiagnosticStyledString::new());
704+
values.1.push_highlighted(format!("&{}", if tnm2.mutbl == hir::MutMutable {
705+
"mut "
706+
} else {
707+
""
708+
}));
709+
values.0.push_normal(format!("{}", t1));
710+
values.1.push_normal(format!("{}", tnm2.ty));
711+
values
712+
}
713+
714+
// When encountering &T != &mut T, highlight only the borrow
715+
(&ty::TyRef(_, ref tnm1), &ty::TyRef(_, ref tnm2)) if equals(&tnm1.ty, &tnm2.ty) => {
716+
let mut values = (DiagnosticStyledString::new(), DiagnosticStyledString::new());
717+
values.0.push_highlighted(format!("&{}", if tnm1.mutbl == hir::MutMutable {
718+
"mut "
719+
} else {
720+
""
721+
}));
722+
values.1.push_highlighted(format!("&{}", if tnm2.mutbl == hir::MutMutable {
723+
"mut "
724+
} else {
725+
""
726+
}));
727+
728+
values.0.push_normal(format!("{}", tnm1.ty));
729+
values.1.push_normal(format!("{}", tnm2.ty));
730+
values
731+
}
732+
675733
_ => {
676734
if t1 == t2 {
677735
// The two types are the same, elide and don't highlight.

0 commit comments

Comments
 (0)