Skip to content

Commit 68ea7e4

Browse files
authored
Rollup merge of rust-lang#57294 - estebank:point-copy-less, r=nikomatsakis
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. ``` error[E0382]: use of moved value: `x` --> $DIR/issue-34721.rs:27:9 | LL | pub fn baz<T: Foo>(x: T) -> T { | - - move occurs because `x` has type `T`, which does not implement the `Copy` trait | | | consider adding a `Copy` constraint to this type argument LL | if 0 == 1 { LL | bar::bar(x.zero()) | - value moved here LL | } else { LL | x.zero() | - value moved here LL | }; LL | x.zero() | ^ value used here after move ``` Fix rust-lang#34721.
2 parents 7164a9f + 09006d8 commit 68ea7e4

23 files changed

+175
-87
lines changed

src/librustc_mir/borrow_check/error_reporting.rs

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -198,19 +198,38 @@ 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+
if let ty::TyKind::Param(param_ty) = ty.sty {
207+
let tcx = self.infcx.tcx;
208+
let generics = tcx.generics_of(self.mir_def_id);
209+
let def_id = generics.type_param(&param_ty, tcx).def_id;
210+
if let Some(sp) = tcx.hir().span_if_local(def_id) {
211+
err.span_label(
212+
sp,
213+
"consider adding a `Copy` constraint to this type argument",
214+
);
215+
}
216+
}
217+
if let Place::Local(local) = place {
218+
let decl = &self.mir.local_decls[*local];
219+
err.span_label(
220+
decl.source_info.span,
221+
format!(
222+
"move occurs because {} has type `{}`, \
223+
which does not implement the `Copy` trait",
224+
note_msg, ty,
225+
));
226+
} else {
227+
err.note(&format!(
228+
"move occurs because {} has type `{}`, \
229+
which does not implement the `Copy` trait",
230+
note_msg, ty
231+
));
232+
}
214233
}
215234

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

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

Lines changed: 6 additions & 4 deletions
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

Lines changed: 3 additions & 2 deletions
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

Lines changed: 4 additions & 4 deletions
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

Lines changed: 3 additions & 2 deletions
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

Lines changed: 3 additions & 2 deletions
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-54499-field-mutation-of-moved-out-with-mut.nll.stderr

Lines changed: 6 additions & 6 deletions
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

Lines changed: 6 additions & 6 deletions
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/borrowck/two-phase-nonrecv-autoref.nll.stderr

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ LL | f(f(10));
1010
error[E0382]: use of moved value: `*f`
1111
--> $DIR/two-phase-nonrecv-autoref.rs:69:11
1212
|
13+
LL | fn twice_ten_so<F: FnOnce(i32) -> i32>(f: Box<F>) {
14+
| - consider adding a `Copy` constraint to this type argument
1315
LL | f(f(10));
1416
| - ^ value used here after move
1517
| |

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

Lines changed: 3 additions & 2 deletions
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

Lines changed: 3 additions & 2 deletions
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/issues/issue-34721.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#![feature(nll)]
2+
3+
pub trait Foo {
4+
fn zero(self) -> Self;
5+
}
6+
7+
impl Foo for u32 {
8+
fn zero(self) -> u32 { 0u32 }
9+
}
10+
11+
pub mod bar {
12+
pub use Foo;
13+
pub fn bar<T: Foo>(x: T) -> T {
14+
x.zero()
15+
}
16+
}
17+
18+
mod baz {
19+
use bar;
20+
use Foo;
21+
pub fn baz<T: Foo>(x: T) -> T {
22+
if 0 == 1 {
23+
bar::bar(x.zero())
24+
} else {
25+
x.zero()
26+
};
27+
x.zero()
28+
//~^ ERROR use of moved value
29+
}
30+
}
31+
32+
fn main() {
33+
let _ = baz::baz(0u32);
34+
}

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
error[E0382]: use of moved value: `x`
2+
--> $DIR/issue-34721.rs:27:9
3+
|
4+
LL | pub fn baz<T: Foo>(x: T) -> T {
5+
| - - move occurs because `x` has type `T`, which does not implement the `Copy` trait
6+
| |
7+
| consider adding a `Copy` constraint to this type argument
8+
LL | if 0 == 1 {
9+
LL | bar::bar(x.zero())
10+
| - value moved here
11+
LL | } else {
12+
LL | x.zero()
13+
| - value moved here
14+
LL | };
15+
LL | x.zero()
16+
| ^ value used here after move
17+
18+
error: aborting due to previous error
19+
20+
For more information about this error, try `rustc --explain E0382`.

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

Lines changed: 2 additions & 2 deletions
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

0 commit comments

Comments
 (0)