diff --git a/src/librustc_borrowck/borrowck/mod.rs b/src/librustc_borrowck/borrowck/mod.rs index 0d475e12571..8f2e5deb92c 100644 --- a/src/librustc_borrowck/borrowck/mod.rs +++ b/src/librustc_borrowck/borrowck/mod.rs @@ -705,8 +705,19 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> { ol, moved_lp_msg, pat_ty)); - self.tcx.sess.fileline_help(span, - "use `ref` to override"); + match self.tcx.sess.codemap().span_to_snippet(span) { + Ok(string) => { + self.tcx.sess.span_suggestion( + span, + &format!("if you would like to borrow the value instead, \ + use a `ref` binding as shown:"), + format!("ref {}", string)); + }, + Err(_) => { + self.tcx.sess.fileline_help(span, + "use `ref` to override"); + }, + } } move_data::Captured => { diff --git a/src/test/compile-fail/ref-suggestion.rs b/src/test/compile-fail/ref-suggestion.rs new file mode 100644 index 00000000000..815f7526632 --- /dev/null +++ b/src/test/compile-fail/ref-suggestion.rs @@ -0,0 +1,33 @@ +// Copyright 2015 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +fn main() { + let x = vec![1]; + let y = x; + //~^ HELP use a `ref` binding as shown + //~| SUGGESTION let ref y = x; + x; //~ ERROR use of moved value + + let x = vec![1]; + let mut y = x; + //~^ HELP use a `ref` binding as shown + //~| SUGGESTION let ref mut y = x; + x; //~ ERROR use of moved value + + let x = (Some(vec![1]), ()); + + match x { + (Some(y), ()) => {}, + //~^ HELP use a `ref` binding as shown + //~| SUGGESTION (Some(ref y), ()) => {}, + _ => {}, + } + x; //~ ERROR use of partially moved value +}