]> git.lizzy.rs Git - rust.git/commitdiff
Don't try to suggest `ref mut` for implicit `ref`
authorashtneoi <ashtneoi@gmail.com>
Wed, 11 Jul 2018 06:14:12 +0000 (23:14 -0700)
committerashtneoi <ashtneoi@gmail.com>
Fri, 13 Jul 2018 05:51:30 +0000 (22:51 -0700)
src/librustc_mir/borrow_check/mod.rs
src/test/ui/rfc-2005-default-binding-mode/enum.nll.stderr
src/test/ui/rfc-2005-default-binding-mode/explicit-mut.nll.stderr

index 243f378377f9a5ddbacf50bb26901085fb4d7fe0..95601f1337139cd5a2b7456fb543dd69b415088d 100644 (file)
@@ -1841,9 +1841,9 @@ enum AccessKind {
                 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 @@ enum AccessKind {
                         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 @@ enum AccessKind {
                     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 @@ fn suggest_ampmut_type<'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
+            }
         }
     }
 
index 8aa7e8a417c2bea3aeb5d778768278e29720e664..a9b2bca434cbac08a5409d17cc837dd4c7c323ae 100644 (file)
@@ -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
 
index 4e00dec761621ac58d317944e99a1275c270ea28..4c6149a8b7b30e8d93031a76839e67808bd07d62 100644 (file)
@@ -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