Skip to content

Commit 828ebb8

Browse files
committed
Rollup merge of #26819 - P1start:ref-suggestion, r=nikomatsakis
The error now looks like this: ``` <anon>:4:9: 4:10 error: use of moved value: `x` [E0382] <anon>:4 foo(x); ^ <anon>:3:9: 3:10 note: `x` moved here because it has type `Box<i32>`, which is moved by default <anon>:3 let y = x; ^ <anon>:3:9: 3:10 help: use `ref` to take a reference instead: <anon>: let ref y = x; ```
2 parents 43bb514 + 0bf9fbb commit 828ebb8

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

src/librustc_borrowck/borrowck/mod.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -705,8 +705,19 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
705705
ol,
706706
moved_lp_msg,
707707
pat_ty));
708-
self.tcx.sess.fileline_help(span,
709-
"use `ref` to override");
708+
match self.tcx.sess.codemap().span_to_snippet(span) {
709+
Ok(string) => {
710+
self.tcx.sess.span_suggestion(
711+
span,
712+
&format!("if you would like to borrow the value instead, \
713+
use a `ref` binding as shown:"),
714+
format!("ref {}", string));
715+
},
716+
Err(_) => {
717+
self.tcx.sess.fileline_help(span,
718+
"use `ref` to override");
719+
},
720+
}
710721
}
711722

712723
move_data::Captured => {
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
fn main() {
12+
let x = vec![1];
13+
let y = x;
14+
//~^ HELP use a `ref` binding as shown
15+
//~| SUGGESTION let ref y = x;
16+
x; //~ ERROR use of moved value
17+
18+
let x = vec![1];
19+
let mut y = x;
20+
//~^ HELP use a `ref` binding as shown
21+
//~| SUGGESTION let ref mut y = x;
22+
x; //~ ERROR use of moved value
23+
24+
let x = (Some(vec![1]), ());
25+
26+
match x {
27+
(Some(y), ()) => {},
28+
//~^ HELP use a `ref` binding as shown
29+
//~| SUGGESTION (Some(ref y), ()) => {},
30+
_ => {},
31+
}
32+
x; //~ ERROR use of partially moved value
33+
}

0 commit comments

Comments
 (0)