about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorashtneoi <ashtneoi@gmail.com>2018-07-10 23:14:12 -0700
committerashtneoi <ashtneoi@gmail.com>2018-07-12 22:51:30 -0700
commit6fd1a9fff79d906ecadcc9eab3962d84d38c7061 (patch)
treee77f59500f70b41fb1954cdffa1c38f15bfcbdaf /src
parent52d6ae854d0f4c33aa65ad1c151bf7e1700de476 (diff)
Don't try to suggest `ref mut` for implicit `ref`
Diffstat (limited to 'src')
-rw-r--r--src/librustc_mir/borrow_check/mod.rs34
-rw-r--r--src/test/ui/rfc-2005-default-binding-mode/enum.nll.stderr6
-rw-r--r--src/test/ui/rfc-2005-default-binding-mode/explicit-mut.nll.stderr6
3 files changed, 26 insertions, 20 deletions
diff --git a/src/librustc_mir/borrow_check/mod.rs b/src/librustc_mir/borrow_check/mod.rs
index 243f378377f..95601f13371 100644
--- a/src/librustc_mir/borrow_check/mod.rs
+++ b/src/librustc_mir/borrow_check/mod.rs
@@ -1841,9 +1841,9 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
                 elem: ProjectionElem::Deref,
             }) if self.mir.local_decls[*local].is_user_variable.is_some() => {
                 let local_decl = &self.mir.local_decls[*local];
-                let (err_help_span, suggested_code) = match local_decl.is_user_variable {
+                let suggestion = match local_decl.is_user_variable {
                     Some(ClearCrossCrate::Set(mir::BindingForm::ImplicitSelf)) => {
-                        suggest_ampmut_self(local_decl)
+                        Some(suggest_ampmut_self(local_decl))
                     },
 
                     Some(ClearCrossCrate::Set(mir::BindingForm::Var(mir::VarBindingForm {
@@ -1854,9 +1854,9 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
                         if let Some(x) = try_suggest_ampmut_rhs(
                             self.tcx, self.mir, *local,
                         ) {
-                            x
+                            Some(x)
                         } else {
-                            suggest_ampmut_type(local_decl, opt_ty_info)
+                            Some(suggest_ampmut_type(local_decl, opt_ty_info))
                         }
                     },
 
@@ -1872,11 +1872,13 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
                     None => bug!(),
                 };
 
-                err.span_suggestion(
-                    err_help_span,
-                    "consider changing this to be a mutable reference",
-                    suggested_code,
-                );
+                if let Some((err_help_span, suggested_code)) = suggestion {
+                    err.span_suggestion(
+                        err_help_span,
+                        "consider changing this to be a mutable reference",
+                        suggested_code,
+                    );
+                }
 
                 if let Some(name) = local_decl.name {
                     err.span_label(
@@ -1967,13 +1969,17 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
         fn suggest_ref_mut<'cx, 'gcx, 'tcx>(
             tcx: TyCtxt<'cx, 'gcx, 'tcx>,
             local_decl: &mir::LocalDecl<'tcx>,
-        ) -> (Span, String) {
+        ) -> Option<(Span, String)> {
             let hi_span = local_decl.source_info.span;
             let hi_src = tcx.sess.codemap().span_to_snippet(hi_span).unwrap();
-            assert!(hi_src.starts_with("ref"));
-            assert!(hi_src["ref".len()..].starts_with(Pattern_White_Space));
-            let suggestion = format!("ref mut{}", &hi_src["ref".len()..]);
-            (hi_span, suggestion)
+            if hi_src.starts_with("ref")
+                && hi_src["ref".len()..].starts_with(Pattern_White_Space)
+            {
+                let suggestion = format!("ref mut{}", &hi_src["ref".len()..]);
+                Some((hi_span, suggestion))
+            } else {
+                None
+            }
         }
     }
 
diff --git a/src/test/ui/rfc-2005-default-binding-mode/enum.nll.stderr b/src/test/ui/rfc-2005-default-binding-mode/enum.nll.stderr
index 8aa7e8a417c..a9b2bca434c 100644
--- a/src/test/ui/rfc-2005-default-binding-mode/enum.nll.stderr
+++ b/src/test/ui/rfc-2005-default-binding-mode/enum.nll.stderr
@@ -2,19 +2,19 @@ error[E0594]: cannot assign to `*x` which is behind a `&` reference
   --> $DIR/enum.rs:19:5
    |
 LL |     *x += 1; //~ ERROR cannot assign to immutable
-   |     ^^^^^^^ cannot assign
+   |     ^^^^^^^ `x` is a `&` reference, so the data it refers to cannot be written
 
 error[E0594]: cannot assign to `*x` which is behind a `&` reference
   --> $DIR/enum.rs:23:9
    |
 LL |         *x += 1; //~ ERROR cannot assign to immutable
-   |         ^^^^^^^ cannot assign
+   |         ^^^^^^^ `x` is a `&` reference, so the data it refers to cannot be written
 
 error[E0594]: cannot assign to `*x` which is behind a `&` reference
   --> $DIR/enum.rs:29:9
    |
 LL |         *x += 1; //~ ERROR cannot assign to immutable
-   |         ^^^^^^^ cannot assign
+   |         ^^^^^^^ `x` is a `&` reference, so the data it refers to cannot be written
 
 error: aborting due to 3 previous errors
 
diff --git a/src/test/ui/rfc-2005-default-binding-mode/explicit-mut.nll.stderr b/src/test/ui/rfc-2005-default-binding-mode/explicit-mut.nll.stderr
index 4e00dec7616..4c6149a8b7b 100644
--- a/src/test/ui/rfc-2005-default-binding-mode/explicit-mut.nll.stderr
+++ b/src/test/ui/rfc-2005-default-binding-mode/explicit-mut.nll.stderr
@@ -2,19 +2,19 @@ error[E0594]: cannot assign to `*n` which is behind a `&` reference
   --> $DIR/explicit-mut.rs:17:13
    |
 LL |             *n += 1; //~ ERROR cannot assign to immutable
-   |             ^^^^^^^ cannot assign
+   |             ^^^^^^^ `n` is a `&` reference, so the data it refers to cannot be written
 
 error[E0594]: cannot assign to `*n` which is behind a `&` reference
   --> $DIR/explicit-mut.rs:25:13
    |
 LL |             *n += 1; //~ ERROR cannot assign to immutable
-   |             ^^^^^^^ cannot assign
+   |             ^^^^^^^ `n` is a `&` reference, so the data it refers to cannot be written
 
 error[E0594]: cannot assign to `*n` which is behind a `&` reference
   --> $DIR/explicit-mut.rs:33:13
    |
 LL |             *n += 1; //~ ERROR cannot assign to immutable
-   |             ^^^^^^^ cannot assign
+   |             ^^^^^^^ `n` is a `&` reference, so the data it refers to cannot be written
 
 error: aborting due to 3 previous errors