From 6fd1a9fff79d906ecadcc9eab3962d84d38c7061 Mon Sep 17 00:00:00 2001 From: ashtneoi Date: Tue, 10 Jul 2018 23:14:12 -0700 Subject: [PATCH] Don't try to suggest `ref mut` for implicit `ref` --- src/librustc_mir/borrow_check/mod.rs | 34 +++++++++++-------- .../enum.nll.stderr | 6 ++-- .../explicit-mut.nll.stderr | 6 ++-- 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 @@ 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 + } } } 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 -- 2.44.0