about summary refs log tree commit diff
diff options
context:
space:
mode:
authorEsteban Küber <esteban@kuber.com.ar>2017-11-29 08:55:45 -0800
committerEsteban Küber <esteban@kuber.com.ar>2017-11-29 08:55:45 -0800
commit02808f1e9e5315addbc3c3fa3f12116d366323b9 (patch)
tree4386c04519271802d912232e47bf6c8809c981e1
parent677381a0c1788d459c010b891fbf21e80b4a28a3 (diff)
downloadrust-02808f1e9e5315addbc3c3fa3f12116d366323b9.tar.gz
rust-02808f1e9e5315addbc3c3fa3f12116d366323b9.zip
Include lifetime on highlighted ref type mismatch
-rw-r--r--src/librustc/infer/error_reporting/mod.rs54
1 files changed, 26 insertions, 28 deletions
diff --git a/src/librustc/infer/error_reporting/mod.rs b/src/librustc/infer/error_reporting/mod.rs
index 5ef1c37135b..514b29120a9 100644
--- a/src/librustc/infer/error_reporting/mod.rs
+++ b/src/librustc/infer/error_reporting/mod.rs
@@ -569,6 +569,25 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
             }
         }
 
+        fn push_ty_ref<'tcx>(r: &ty::Region<'tcx>,
+                             tnm: &ty::TypeAndMut<'tcx>,
+                             s: &mut DiagnosticStyledString) {
+            let r = &format!("{}", r);
+            s.push_highlighted(format!("&{}{}{}",
+                                       r,
+                                       if r == "" {
+                                           ""
+                                       } else {
+                                           " "
+                                       },
+                                       if tnm.mutbl == hir::MutMutable {
+                                          "mut "
+                                       } else {
+                                           ""
+                                       }));
+            s.push_normal(format!("{}", tnm.ty));
+        }
+
         match (&t1.sty, &t2.sty) {
             (&ty::TyAdt(def1, sub1), &ty::TyAdt(def2, sub2)) => {
                 let mut values = (DiagnosticStyledString::new(), DiagnosticStyledString::new());
@@ -688,45 +707,24 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
             }
 
             // When finding T != &T, hightlight only the borrow
-            (&ty::TyRef(_, ref tnm1), _) if equals(&tnm1.ty, &t2) => {
+            (&ty::TyRef(r1, ref tnm1), _) if equals(&tnm1.ty, &t2) => {
                 let mut values = (DiagnosticStyledString::new(), DiagnosticStyledString::new());
-                values.0.push_highlighted(format!("&{}", if tnm1.mutbl == hir::MutMutable {
-                    "mut "
-                 } else {
-                     ""
-                 }));
-                values.0.push_normal(format!("{}", tnm1.ty));
+                push_ty_ref(&r1, tnm1, &mut values.0);
                 values.1.push_normal(format!("{}", t2));
                 values
             }
-            (_, &ty::TyRef(_, ref tnm2)) if equals(&t1, &tnm2.ty) => {
+            (_, &ty::TyRef(r2, ref tnm2)) if equals(&t1, &tnm2.ty) => {
                 let mut values = (DiagnosticStyledString::new(), DiagnosticStyledString::new());
-                values.1.push_highlighted(format!("&{}", if tnm2.mutbl == hir::MutMutable {
-                    "mut "
-                 } else {
-                     ""
-                 }));
                 values.0.push_normal(format!("{}", t1));
-                values.1.push_normal(format!("{}", tnm2.ty));
+                push_ty_ref(&r2, tnm2, &mut values.1);
                 values
             }
 
             // When encountering &T != &mut T, highlight only the borrow
-            (&ty::TyRef(_, ref tnm1), &ty::TyRef(_, ref tnm2)) if equals(&tnm1.ty, &tnm2.ty) => {
+            (&ty::TyRef(r1, ref tnm1), &ty::TyRef(r2, ref tnm2)) if equals(&tnm1.ty, &tnm2.ty) => {
                 let mut values = (DiagnosticStyledString::new(), DiagnosticStyledString::new());
-                values.0.push_highlighted(format!("&{}", if tnm1.mutbl == hir::MutMutable {
-                    "mut "
-                 } else {
-                     ""
-                 }));
-                values.1.push_highlighted(format!("&{}", if tnm2.mutbl == hir::MutMutable {
-                    "mut "
-                 } else {
-                     ""
-                 }));
-
-                values.0.push_normal(format!("{}", tnm1.ty));
-                values.1.push_normal(format!("{}", tnm2.ty));
+                push_ty_ref(&r1, tnm1, &mut values.0);
+                push_ty_ref(&r2, tnm2, &mut values.1);
                 values
             }