Skip to content

Commit eb5a253

Browse files
committed
When using value after move, point at span of local
When trying to use a value after move, instead of using a note, point at the local declaration that has a type that doesn't implement `Copy` trait.
1 parent daa53a5 commit eb5a253

21 files changed

+116
-88
lines changed

src/librustc_mir/borrow_check/error_reporting.rs

+23-11
Original file line numberDiff line numberDiff line change
@@ -198,19 +198,31 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
198198
let place = &self.move_data.move_paths[mpi].place;
199199

200200
let ty = place.ty(self.mir, self.infcx.tcx).to_ty(self.infcx.tcx);
201-
let note_msg = match self.describe_place_with_options(
202-
place,
203-
IncludingDowncast(true),
204-
) {
205-
Some(name) => format!("`{}`", name),
201+
let opt_name = self.describe_place_with_options(place, IncludingDowncast(true));
202+
let note_msg = match opt_name {
203+
Some(ref name) => format!("`{}`", name),
206204
None => "value".to_owned(),
207205
};
208-
209-
err.note(&format!(
210-
"move occurs because {} has type `{}`, \
211-
which does not implement the `Copy` trait",
212-
note_msg, ty
213-
));
206+
let mut note = true;
207+
for decl in &self.mir.local_decls {
208+
if decl.ty == ty && decl.name.map(|x| x.to_string()) == opt_name {
209+
err.span_label(
210+
decl.source_info.span,
211+
format!(
212+
"move occurs because {} has type `{}`, \
213+
which does not implement the `Copy` trait",
214+
note_msg, ty,
215+
));
216+
note = false;
217+
}
218+
}
219+
if note {
220+
err.note(&format!(
221+
"move occurs because {} has type `{}`, \
222+
which does not implement the `Copy` trait",
223+
note_msg, ty
224+
));
225+
}
214226
}
215227

216228
if let Some((_, mut old_err)) = self.move_error_reported

src/test/ui/borrowck/borrowck-asm.mir.stderr

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
error[E0382]: use of moved value: `x`
22
--> $DIR/borrowck-asm.rs:27:17
33
|
4+
LL | let x = &mut 0isize;
5+
| - move occurs because `x` has type `&mut isize`, which does not implement the `Copy` trait
6+
LL | unsafe {
47
LL | asm!("nop" : : "r"(x));
58
| - value moved here
69
LL | }
710
LL | let z = x; //[ast]~ ERROR use of moved value: `x`
811
| ^ value used here after move
9-
|
10-
= note: move occurs because `x` has type `&mut isize`, which does not implement the `Copy` trait
1112

1213
error[E0503]: cannot use `x` because it was mutably borrowed
1314
--> $DIR/borrowck-asm.rs:35:32
@@ -66,12 +67,13 @@ LL | let z = y;
6667
error[E0382]: use of moved value: `x`
6768
--> $DIR/borrowck-asm.rs:86:40
6869
|
70+
LL | let x = &mut 2;
71+
| - move occurs because `x` has type `&mut i32`, which does not implement the `Copy` trait
72+
LL | unsafe {
6973
LL | asm!("nop" : : "r"(x), "r"(x) ); //[ast]~ ERROR use of moved value
7074
| - ^ value used here after move
7175
| |
7276
| value moved here
73-
|
74-
= note: move occurs because `x` has type `&mut i32`, which does not implement the `Copy` trait
7577

7678
error: aborting due to 7 previous errors
7779

src/test/ui/borrowck/borrowck-drop-from-guard.stderr

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
error[E0382]: use of moved value: `my_str`
22
--> $DIR/borrowck-drop-from-guard.rs:11:23
33
|
4+
LL | let my_str = "hello".to_owned();
5+
| ------ move occurs because `my_str` has type `std::string::String`, which does not implement the `Copy` trait
6+
LL | match Some(42) {
47
LL | Some(_) if { drop(my_str); false } => {}
58
| ------ value moved here
69
LL | Some(_) => {}
710
LL | None => { foo(my_str); } //~ ERROR [E0382]
811
| ^^^^^^ value used here after move
9-
|
10-
= note: move occurs because `my_str` has type `std::string::String`, which does not implement the `Copy` trait
1112

1213
error: aborting due to previous error
1314

src/test/ui/borrowck/borrowck-issue-48962.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
error[E0382]: use of moved value: `src`
22
--> $DIR/borrowck-issue-48962.rs:16:5
33
|
4+
LL | let mut src = &mut node;
5+
| ------- move occurs because `src` has type `&mut Node`, which does not implement the `Copy` trait
46
LL | {src};
57
| --- value moved here
68
LL | src.next = None; //~ ERROR use of moved value: `src` [E0382]
79
| ^^^^^^^^ value used here after move
8-
|
9-
= note: move occurs because `src` has type `&mut Node`, which does not implement the `Copy` trait
1010

1111
error[E0382]: use of moved value: `src`
1212
--> $DIR/borrowck-issue-48962.rs:22:5
1313
|
14+
LL | let mut src = &mut (22, 44);
15+
| ------- move occurs because `src` has type `&mut (i32, i32)`, which does not implement the `Copy` trait
1416
LL | {src};
1517
| --- value moved here
1618
LL | src.0 = 66; //~ ERROR use of moved value: `src` [E0382]
1719
| ^^^^^^^^^^ value used here after move
18-
|
19-
= note: move occurs because `src` has type `&mut (i32, i32)`, which does not implement the `Copy` trait
2020

2121
error: aborting due to 2 previous errors
2222

src/test/ui/borrowck/borrowck-move-moved-value-into-closure.mir.stderr

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
error[E0382]: use of moved value: `t`
22
--> $DIR/borrowck-move-moved-value-into-closure.rs:14:12
33
|
4+
LL | let t: Box<_> = box 3;
5+
| - move occurs because `t` has type `std::boxed::Box<isize>`, which does not implement the `Copy` trait
6+
LL |
47
LL | call_f(move|| { *t + 1 });
58
| ------ - variable moved due to use in closure
69
| |
@@ -9,8 +12,6 @@ LL | call_f(move|| { *t + 1 }); //[ast]~ ERROR capture of moved value
912
| ^^^^^^ - use occurs due to use in closure
1013
| |
1114
| value used here after move
12-
|
13-
= note: move occurs because `t` has type `std::boxed::Box<isize>`, which does not implement the `Copy` trait
1415

1516
error: aborting due to previous error
1617

src/test/ui/borrowck/borrowck-reinit.stderr

+3-2
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,13 @@ LL | let _ = (1,x); //~ ERROR use of moved value: `x` (Ast)
1111
error[E0382]: use of moved value: `x` (Mir)
1212
--> $DIR/borrowck-reinit.rs:8:16
1313
|
14+
LL | let mut x = Box::new(0);
15+
| ----- move occurs because `x` has type `std::boxed::Box<i32>`, which does not implement the `Copy` trait
16+
...
1417
LL | drop(x);
1518
| - value moved here
1619
LL | let _ = (1,x); //~ ERROR use of moved value: `x` (Ast)
1720
| ^ value used here after move
18-
|
19-
= note: move occurs because `x` has type `std::boxed::Box<i32>`, which does not implement the `Copy` trait
2021

2122
error: aborting due to 2 previous errors
2223

src/test/ui/borrowck/issue-41962.stderr

+4-1
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,13 @@ LL | if let Some(thing) = maybe {
1919
error[E0382]: use of moved value (Mir)
2020
--> $DIR/issue-41962.rs:7:21
2121
|
22+
LL | let maybe = Some(vec![true, true]);
23+
| ---------------- move occurs because value has type `std::vec::Vec<bool>`, which does not implement the `Copy` trait
24+
...
2225
LL | if let Some(thing) = maybe {
2326
| ^^^^^ value moved here, in previous iteration of loop
2427
|
25-
= note: move occurs because value has type `std::vec::Vec<bool>`, which does not implement the `Copy` trait
28+
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)
2629

2730
error: aborting due to 3 previous errors
2831

src/test/ui/borrowck/issue-54499-field-mutation-of-moved-out-with-mut.nll.stderr

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,32 @@
11
error[E0382]: assign to part of moved value: `t`
22
--> $DIR/issue-54499-field-mutation-of-moved-out-with-mut.rs:23:9
33
|
4+
LL | let mut t: Tuple = (S(0), 0);
5+
| ----- move occurs because `t` has type `(S, i32)`, which does not implement the `Copy` trait
46
LL | drop(t);
57
| - value moved here
68
LL | t.0 = S(1);
79
| ^^^^^^^^^^ value partially assigned here after move
8-
|
9-
= note: move occurs because `t` has type `(S, i32)`, which does not implement the `Copy` trait
1010

1111
error[E0382]: assign to part of moved value: `u`
1212
--> $DIR/issue-54499-field-mutation-of-moved-out-with-mut.rs:34:9
1313
|
14+
LL | let mut u: Tpair = Tpair(S(0), 0);
15+
| ----- move occurs because `u` has type `Tpair`, which does not implement the `Copy` trait
1416
LL | drop(u);
1517
| - value moved here
1618
LL | u.0 = S(1);
1719
| ^^^^^^^^^^ value partially assigned here after move
18-
|
19-
= note: move occurs because `u` has type `Tpair`, which does not implement the `Copy` trait
2020

2121
error[E0382]: assign to part of moved value: `v`
2222
--> $DIR/issue-54499-field-mutation-of-moved-out-with-mut.rs:45:9
2323
|
24+
LL | let mut v: Spair = Spair { x: S(0), y: 0 };
25+
| ----- move occurs because `v` has type `Spair`, which does not implement the `Copy` trait
2426
LL | drop(v);
2527
| - value moved here
2628
LL | v.x = S(1);
2729
| ^^^^^^^^^^ value partially assigned here after move
28-
|
29-
= note: move occurs because `v` has type `Spair`, which does not implement the `Copy` trait
3030

3131
error: aborting due to 3 previous errors
3232

src/test/ui/borrowck/issue-54499-field-mutation-of-moved-out.nll.stderr

+6-6
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ LL | t.0 = S(1);
1010
error[E0382]: assign to part of moved value: `t`
1111
--> $DIR/issue-54499-field-mutation-of-moved-out.rs:23:9
1212
|
13+
LL | let t: Tuple = (S(0), 0);
14+
| - move occurs because `t` has type `(S, i32)`, which does not implement the `Copy` trait
1315
LL | drop(t);
1416
| - value moved here
1517
LL | t.0 = S(1);
1618
| ^^^^^^^^^^ value partially assigned here after move
17-
|
18-
= note: move occurs because `t` has type `(S, i32)`, which does not implement the `Copy` trait
1919

2020
error[E0594]: cannot assign to `t.1`, as `t` is not declared as mutable
2121
--> $DIR/issue-54499-field-mutation-of-moved-out.rs:27:9
@@ -38,12 +38,12 @@ LL | u.0 = S(1);
3838
error[E0382]: assign to part of moved value: `u`
3939
--> $DIR/issue-54499-field-mutation-of-moved-out.rs:38:9
4040
|
41+
LL | let u: Tpair = Tpair(S(0), 0);
42+
| - move occurs because `u` has type `Tpair`, which does not implement the `Copy` trait
4143
LL | drop(u);
4244
| - value moved here
4345
LL | u.0 = S(1);
4446
| ^^^^^^^^^^ value partially assigned here after move
45-
|
46-
= note: move occurs because `u` has type `Tpair`, which does not implement the `Copy` trait
4747

4848
error[E0594]: cannot assign to `u.1`, as `u` is not declared as mutable
4949
--> $DIR/issue-54499-field-mutation-of-moved-out.rs:42:9
@@ -66,12 +66,12 @@ LL | v.x = S(1);
6666
error[E0382]: assign to part of moved value: `v`
6767
--> $DIR/issue-54499-field-mutation-of-moved-out.rs:53:9
6868
|
69+
LL | let v: Spair = Spair { x: S(0), y: 0 };
70+
| - move occurs because `v` has type `Spair`, which does not implement the `Copy` trait
6971
LL | drop(v);
7072
| - value moved here
7173
LL | v.x = S(1);
7274
| ^^^^^^^^^^ value partially assigned here after move
73-
|
74-
= note: move occurs because `v` has type `Spair`, which does not implement the `Copy` trait
7575

7676
error[E0594]: cannot assign to `v.y`, as `v` is not declared as mutable
7777
--> $DIR/issue-54499-field-mutation-of-moved-out.rs:57:9

src/test/ui/issues/issue-27282-move-match-input-into-guard.stderr

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
error[E0382]: use of moved value: `b`
22
--> $DIR/issue-27282-move-match-input-into-guard.rs:18:14
33
|
4+
LL | let b = &mut true;
5+
| - move occurs because `b` has type `&mut bool`, which does not implement the `Copy` trait
6+
...
47
LL | _ if { (|| { let bar = b; *bar = false; })();
58
| -- - variable moved due to use in closure
69
| |
710
| value moved into closure here
811
LL | false } => { },
912
LL | &mut true => { println!("You might think we should get here"); },
1013
| ^^^^ value used here after move
11-
|
12-
= note: move occurs because `b` has type `&mut bool`, which does not implement the `Copy` trait
1314

1415
error: aborting due to previous error
1516

src/test/ui/issues/issue-29723.stderr

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
error[E0382]: use of moved value: `s`
22
--> $DIR/issue-29723.rs:12:13
33
|
4+
LL | let s = String::new();
5+
| - move occurs because `s` has type `std::string::String`, which does not implement the `Copy` trait
6+
LL | let _s = match 0 {
47
LL | 0 if { drop(s); false } => String::from("oops"),
58
| - value moved here
69
...
710
LL | s
811
| ^ value used here after move
9-
|
10-
= note: move occurs because `s` has type `std::string::String`, which does not implement the `Copy` trait
1112

1213
error: aborting due to previous error
1314

src/test/ui/moves/moves-based-on-type-tuple.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ LL | box (x, x)
1111
error[E0382]: use of moved value: `x` (Mir)
1212
--> $DIR/moves-based-on-type-tuple.rs:6:13
1313
|
14+
LL | fn dup(x: Box<isize>) -> Box<(Box<isize>,Box<isize>)> {
15+
| - move occurs because `x` has type `std::boxed::Box<isize>`, which does not implement the `Copy` trait
1416
LL | box (x, x)
1517
| - ^ value used here after move
1618
| |
1719
| value moved here
18-
|
19-
= note: move occurs because `x` has type `std::boxed::Box<isize>`, which does not implement the `Copy` trait
2020

2121
error: aborting due to 2 previous errors
2222

src/test/ui/nll/closure-access-spans.stderr

+8-8
Original file line numberDiff line numberDiff line change
@@ -59,50 +59,50 @@ LL | r.use_ref();
5959
error[E0382]: borrow of moved value: `x`
6060
--> $DIR/closure-access-spans.rs:37:5
6161
|
62+
LL | fn closure_imm_capture_moved(mut x: String) {
63+
| ----- move occurs because `x` has type `std::string::String`, which does not implement the `Copy` trait
6264
LL | let r = x;
6365
| - value moved here
6466
LL | || x.len(); //~ ERROR
6567
| ^^ - borrow occurs due to use in closure
6668
| |
6769
| value borrowed here after move
68-
|
69-
= note: move occurs because `x` has type `std::string::String`, which does not implement the `Copy` trait
7070

7171
error[E0382]: borrow of moved value: `x`
7272
--> $DIR/closure-access-spans.rs:42:5
7373
|
74+
LL | fn closure_mut_capture_moved(mut x: String) {
75+
| ----- move occurs because `x` has type `std::string::String`, which does not implement the `Copy` trait
7476
LL | let r = x;
7577
| - value moved here
7678
LL | || x = String::new(); //~ ERROR
7779
| ^^ - borrow occurs due to use in closure
7880
| |
7981
| value borrowed here after move
80-
|
81-
= note: move occurs because `x` has type `std::string::String`, which does not implement the `Copy` trait
8282

8383
error[E0382]: borrow of moved value: `x`
8484
--> $DIR/closure-access-spans.rs:47:5
8585
|
86+
LL | fn closure_unique_capture_moved(x: &mut String) {
87+
| - move occurs because `x` has type `&mut std::string::String`, which does not implement the `Copy` trait
8688
LL | let r = x;
8789
| - value moved here
8890
LL | || *x = String::new(); //~ ERROR
8991
| ^^ - borrow occurs due to use in closure
9092
| |
9193
| value borrowed here after move
92-
|
93-
= note: move occurs because `x` has type `&mut std::string::String`, which does not implement the `Copy` trait
9494

9595
error[E0382]: use of moved value: `x`
9696
--> $DIR/closure-access-spans.rs:52:5
9797
|
98+
LL | fn closure_move_capture_moved(x: &mut String) {
99+
| - move occurs because `x` has type `&mut std::string::String`, which does not implement the `Copy` trait
98100
LL | let r = x;
99101
| - value moved here
100102
LL | || x; //~ ERROR
101103
| ^^ - use occurs due to use in closure
102104
| |
103105
| value used here after move
104-
|
105-
= note: move occurs because `x` has type `&mut std::string::String`, which does not implement the `Copy` trait
106106

107107
error: aborting due to 9 previous errors
108108

0 commit comments

Comments
 (0)