Skip to content

Commit 9053fdd

Browse files
committed
Auto merge of #46349 - estebank:highlight-ref, r=arielb1
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`).
2 parents 7a64987 + 02808f1 commit 9053fdd

File tree

1 file changed

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

1 file changed

+56
-0
lines changed

src/librustc/infer/error_reporting/mod.rs

+56
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,39 @@ 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+
572+
fn push_ty_ref<'tcx>(r: &ty::Region<'tcx>,
573+
tnm: &ty::TypeAndMut<'tcx>,
574+
s: &mut DiagnosticStyledString) {
575+
let r = &format!("{}", r);
576+
s.push_highlighted(format!("&{}{}{}",
577+
r,
578+
if r == "" {
579+
""
580+
} else {
581+
" "
582+
},
583+
if tnm.mutbl == hir::MutMutable {
584+
"mut "
585+
} else {
586+
""
587+
}));
588+
s.push_normal(format!("{}", tnm.ty));
589+
}
590+
558591
match (&t1.sty, &t2.sty) {
559592
(&ty::TyAdt(def1, sub1), &ty::TyAdt(def2, sub2)) => {
560593
let mut values = (DiagnosticStyledString::new(), DiagnosticStyledString::new());
@@ -672,6 +705,29 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
672705
DiagnosticStyledString::highlighted(format!("{}", t2)))
673706
}
674707
}
708+
709+
// When finding T != &T, hightlight only the borrow
710+
(&ty::TyRef(r1, ref tnm1), _) if equals(&tnm1.ty, &t2) => {
711+
let mut values = (DiagnosticStyledString::new(), DiagnosticStyledString::new());
712+
push_ty_ref(&r1, tnm1, &mut values.0);
713+
values.1.push_normal(format!("{}", t2));
714+
values
715+
}
716+
(_, &ty::TyRef(r2, ref tnm2)) if equals(&t1, &tnm2.ty) => {
717+
let mut values = (DiagnosticStyledString::new(), DiagnosticStyledString::new());
718+
values.0.push_normal(format!("{}", t1));
719+
push_ty_ref(&r2, tnm2, &mut values.1);
720+
values
721+
}
722+
723+
// When encountering &T != &mut T, highlight only the borrow
724+
(&ty::TyRef(r1, ref tnm1), &ty::TyRef(r2, ref tnm2)) if equals(&tnm1.ty, &tnm2.ty) => {
725+
let mut values = (DiagnosticStyledString::new(), DiagnosticStyledString::new());
726+
push_ty_ref(&r1, tnm1, &mut values.0);
727+
push_ty_ref(&r2, tnm2, &mut values.1);
728+
values
729+
}
730+
675731
_ => {
676732
if t1 == t2 {
677733
// The two types are the same, elide and don't highlight.

0 commit comments

Comments
 (0)