Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add better error message for partial move #58199

Merged
merged 7 commits into from
Feb 23, 2019
7 changes: 5 additions & 2 deletions src/librustc_mir/borrow_check/error_reporting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,13 +130,15 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
);

let mut is_loop_move = false;
let mut is_partial_move = false;
for move_site in &move_site_vec {
let move_out = self.move_data.moves[(*move_site).moi];
let moved_place = &self.move_data.move_paths[move_out.path].place;

let move_spans = self.move_spans(moved_place, move_out.source);
let move_span = move_spans.args_or_use();

is_partial_move = used_place.is_prefix_of(moved_place);
davidtwco marked this conversation as resolved.
Show resolved Hide resolved
let move_msg = if move_spans.for_closure() {
" into closure"
} else {
Expand Down Expand Up @@ -175,8 +177,9 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
err.span_label(
span,
format!(
"value {} here after move",
desired_action.as_verb_in_past_tense()
"value {} here {}",
desired_action.as_verb_in_past_tense(),
if is_partial_move { "after partial move" } else { "after move" },
),
);
}
Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/borrowck/borrowck-asm.mir.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ LL | asm!("nop" : : "r"(x));
| - value moved here
LL | }
LL | let z = x; //[ast]~ ERROR use of moved value: `x`
| ^ value used here after move
| ^ value used here after partial move

error[E0503]: cannot use `x` because it was mutably borrowed
--> $DIR/borrowck-asm.rs:35:32
Expand Down Expand Up @@ -71,7 +71,7 @@ LL | let x = &mut 2;
| - move occurs because `x` has type `&mut i32`, which does not implement the `Copy` trait
LL | unsafe {
LL | asm!("nop" : : "r"(x), "r"(x) ); //[ast]~ ERROR use of moved value
| - ^ value used here after move
| - ^ value used here after partial move
| |
| value moved here

Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/borrowck/borrowck-describe-lvalue.mir.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ error[E0382]: use of moved value: `x`
LL | drop(x);
| - value moved here
LL | drop(x); //[ast]~ ERROR use of moved value: `x`
| ^ value used here after move
davidtwco marked this conversation as resolved.
Show resolved Hide resolved
| ^ value used here after partial move
|
= note: move occurs because `x` has type `std::vec::Vec<i32>`, which does not implement the `Copy` trait

Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/borrowck/borrowck-drop-from-guard.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ LL | Some(_) if { drop(my_str); false } => {}
| ------ value moved here
LL | Some(_) => {}
LL | None => { foo(my_str); } //~ ERROR [E0382]
| ^^^^^^ value used here after move
| ^^^^^^ value used here after partial move

error: aborting due to previous error

Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/borrowck/borrowck-issue-48962.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ LL | let mut src = &mut node;
LL | {src};
| --- value moved here
LL | src.next = None; //~ ERROR use of moved value: `src` [E0382]
| ^^^^^^^^ value used here after move
| ^^^^^^^^ value used here after partial move
davidtwco marked this conversation as resolved.
Show resolved Hide resolved

error[E0382]: use of moved value: `src`
--> $DIR/borrowck-issue-48962.rs:22:5
Expand All @@ -16,7 +16,7 @@ LL | let mut src = &mut (22, 44);
LL | {src};
| --- value moved here
LL | src.0 = 66; //~ ERROR use of moved value: `src` [E0382]
| ^^^^^^^^^^ value used here after move
| ^^^^^^^^^^ value used here after partial move

error: aborting due to 2 previous errors

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ LL | call_f(move|| { *t + 1 });
LL | call_f(move|| { *t + 1 }); //[ast]~ ERROR capture of moved value
| ^^^^^^ - use occurs due to use in closure
| |
| value used here after move
| value used here after partial move

error: aborting due to previous error

Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/borrowck/borrowck-move-out-from-array.mir.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error[E0382]: use of moved value: `a[..]`
LL | let [_, _x] = a;
| -- value moved here
LL | let [.., _y] = a; //[ast]~ ERROR [E0382]
| ^^ value used here after move
| ^^ value used here after partial move
|
= note: move occurs because `a[..]` has type `std::boxed::Box<i32>`, which does not implement the `Copy` trait

Expand All @@ -14,7 +14,7 @@ error[E0382]: use of moved value: `a[..]`
LL | let [_x, _] = a;
| -- value moved here
LL | let [_y..] = a; //[ast]~ ERROR [E0382]
| ^^ value used here after move
| ^^ value used here after partial move
|
= note: move occurs because `a[..]` has type `std::boxed::Box<i32>`, which does not implement the `Copy` trait

Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/borrowck/borrowck-reinit.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ LL | let mut x = Box::new(0);
LL | drop(x);
| - value moved here
LL | let _ = (1,x); //~ ERROR use of moved value: `x` (Ast)
| ^ value used here after move
| ^ value used here after partial move

error: aborting due to 2 previous errors

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ error[E0382]: use of moved value: `line2`
LL | let _moved = (line2.origin, line2.middle);
| ------------ value moved here
LL | line2.consume(); //[ast]~ ERROR use of partially moved value: `line2` [E0382]
| ^^^^^ value used here after move
| ^^^^^ value used here after partial move
|
= note: move occurs because `line2.middle` has type `Point`, which does not implement the `Copy` trait

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ LL | let mut t: Tuple = (S(0), 0);
LL | drop(t);
| - value moved here
LL | t.0 = S(1);
| ^^^^^^^^^^ value partially assigned here after move
| ^^^^^^^^^^ value partially assigned here after partial move

error[E0382]: assign to part of moved value: `u`
--> $DIR/issue-54499-field-mutation-of-moved-out-with-mut.rs:34:9
Expand All @@ -16,7 +16,7 @@ LL | let mut u: Tpair = Tpair(S(0), 0);
LL | drop(u);
| - value moved here
LL | u.0 = S(1);
| ^^^^^^^^^^ value partially assigned here after move
| ^^^^^^^^^^ value partially assigned here after partial move

error[E0382]: assign to part of moved value: `v`
--> $DIR/issue-54499-field-mutation-of-moved-out-with-mut.rs:45:9
Expand All @@ -26,7 +26,7 @@ LL | let mut v: Spair = Spair { x: S(0), y: 0 };
LL | drop(v);
| - value moved here
LL | v.x = S(1);
| ^^^^^^^^^^ value partially assigned here after move
| ^^^^^^^^^^ value partially assigned here after partial move

error: aborting due to 3 previous errors

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ LL | let t: Tuple = (S(0), 0);
LL | drop(t);
| - value moved here
LL | t.0 = S(1);
| ^^^^^^^^^^ value partially assigned here after move
| ^^^^^^^^^^ value partially assigned here after partial move

error[E0594]: cannot assign to `t.1`, as `t` is not declared as mutable
--> $DIR/issue-54499-field-mutation-of-moved-out.rs:27:9
Expand Down Expand Up @@ -43,7 +43,7 @@ LL | let u: Tpair = Tpair(S(0), 0);
LL | drop(u);
| - value moved here
LL | u.0 = S(1);
| ^^^^^^^^^^ value partially assigned here after move
| ^^^^^^^^^^ value partially assigned here after partial move

error[E0594]: cannot assign to `u.1`, as `u` is not declared as mutable
--> $DIR/issue-54499-field-mutation-of-moved-out.rs:42:9
Expand Down Expand Up @@ -71,7 +71,7 @@ LL | let v: Spair = Spair { x: S(0), y: 0 };
LL | drop(v);
| - value moved here
LL | v.x = S(1);
| ^^^^^^^^^^ value partially assigned here after move
| ^^^^^^^^^^ value partially assigned here after partial move

error[E0594]: cannot assign to `v.y`, as `v` is not declared as mutable
--> $DIR/issue-54499-field-mutation-of-moved-out.rs:57:9
Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/borrowck/two-phase-nonrecv-autoref.nll.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ error[E0382]: use of moved value: `*f`
LL | fn twice_ten_so<F: FnOnce(i32) -> i32>(f: Box<F>) {
| - consider adding a `Copy` constraint to this type argument
LL | f(f(10));
| - ^ value used here after move
| - ^ value used here after partial move
| |
| value moved here
|
Expand Down Expand Up @@ -44,7 +44,7 @@ error[E0382]: use of moved value: `*f`
--> $DIR/two-phase-nonrecv-autoref.rs:85:11
|
LL | f(f(10));
| - ^ value used here after move
| - ^ value used here after partial move
| |
| value moved here
|
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/issues/issue-29723.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ LL | 0 if { drop(s); false } => String::from("oops"),
| - value moved here
...
LL | s
| ^ value used here after move
| ^ value used here after partial move

error: aborting due to previous error

Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/issues/issue-34721.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ LL | x.zero()
| - value moved here
LL | };
LL | x.zero()
| ^ value used here after move
| ^ value used here after partial move

error: aborting due to previous error

Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/moves/moves-based-on-type-tuple.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ error[E0382]: use of moved value: `x` (Mir)
LL | fn dup(x: Box<isize>) -> Box<(Box<isize>,Box<isize>)> {
| - move occurs because `x` has type `std::boxed::Box<isize>`, which does not implement the `Copy` trait
LL | box (x, x)
| - ^ value used here after move
| - ^ value used here after partial move
| |
| value moved here

Expand Down
8 changes: 4 additions & 4 deletions src/test/ui/nll/closure-access-spans.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ LL | let r = x;
LL | || x.len(); //~ ERROR
| ^^ - borrow occurs due to use in closure
| |
| value borrowed here after move
| value borrowed here after partial move

error[E0382]: borrow of moved value: `x`
--> $DIR/closure-access-spans.rs:42:5
Expand All @@ -78,7 +78,7 @@ LL | let r = x;
LL | || x = String::new(); //~ ERROR
| ^^ - borrow occurs due to use in closure
| |
| value borrowed here after move
| value borrowed here after partial move

error[E0382]: borrow of moved value: `x`
--> $DIR/closure-access-spans.rs:47:5
Expand All @@ -90,7 +90,7 @@ LL | let r = x;
LL | || *x = String::new(); //~ ERROR
| ^^ - borrow occurs due to use in closure
| |
| value borrowed here after move
| value borrowed here after partial move

error[E0382]: use of moved value: `x`
--> $DIR/closure-access-spans.rs:52:5
Expand All @@ -102,7 +102,7 @@ LL | let r = x;
LL | || x; //~ ERROR
| ^^ - use occurs due to use in closure
| |
| value used here after move
| value used here after partial move

error: aborting due to 9 previous errors

Expand Down
6 changes: 3 additions & 3 deletions src/test/ui/nll/closure-move-spans.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ LL | || x;
| |
| value moved into closure here
LL | let y = x; //~ ERROR
| ^ value used here after move
| ^ value used here after partial move

error[E0382]: borrow of moved value: `x`
--> $DIR/closure-move-spans.rs:12:13
Expand All @@ -20,7 +20,7 @@ LL | || x;
| |
| value moved into closure here
LL | let y = &x; //~ ERROR
| ^^ value borrowed here after move
| ^^ value borrowed here after partial move

error[E0382]: borrow of moved value: `x`
--> $DIR/closure-move-spans.rs:17:13
Expand All @@ -32,7 +32,7 @@ LL | || x;
| |
| value moved into closure here
LL | let y = &mut x; //~ ERROR
| ^^^^^^ value borrowed here after move
| ^^^^^^ value borrowed here after partial move

error: aborting due to 3 previous errors

Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/nll/decl-macro-illegal-copy.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ LL | $wrapper.inner
| -------------- value moved here
...
LL | wrapper.inner,
| ^^^^^^^^^^^^^ value used here after move
| ^^^^^^^^^^^^^ value used here after partial move
|
= note: move occurs because `wrapper.inner` has type `NonCopy`, which does not implement the `Copy` trait

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ LL | let mut d = D { x: 0, s: S{ y: 0, z: 0 } };
LL | drop(d);
| - value moved here
LL | d.x = 10;
| ^^^^^^^^ value assigned here after move
| ^^^^^^^^ value assigned here after partial move

error[E0381]: assign to part of possibly uninitialized variable: `d`
--> $DIR/issue-21232-partial-init-and-erroneous-use.rs:49:5
Expand Down
10 changes: 5 additions & 5 deletions src/test/ui/nll/issue-21232-partial-init-and-use.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ LL | let mut s: S<B> = S::new(); drop(s);
| |
| move occurs because `s` has type `S<std::boxed::Box<u32>>`, which does not implement the `Copy` trait
LL | s.x = 10; s.y = Box::new(20);
| ^^^^^^^^ value partially assigned here after move
| ^^^^^^^^ value partially assigned here after partial move

error[E0382]: assign to part of moved value: `t`
--> $DIR/issue-21232-partial-init-and-use.rs:120:5
Expand All @@ -28,7 +28,7 @@ LL | let mut t: T = (0, Box::new(0)); drop(t);
| |
| move occurs because `t` has type `(u32, std::boxed::Box<u32>)`, which does not implement the `Copy` trait
LL | t.0 = 10; t.1 = Box::new(20);
| ^^^^^^^^ value partially assigned here after move
| ^^^^^^^^ value partially assigned here after partial move

error[E0381]: assign to part of possibly uninitialized variable: `s`
--> $DIR/issue-21232-partial-init-and-use.rs:127:5
Expand All @@ -50,7 +50,7 @@ LL | let mut s: S<B> = S::new(); drop(s);
| |
| move occurs because `s` has type `S<std::boxed::Box<u32>>`, which does not implement the `Copy` trait
LL | s.x = 10;
| ^^^^^^^^ value partially assigned here after move
| ^^^^^^^^ value partially assigned here after partial move

error[E0382]: assign to part of moved value: `t`
--> $DIR/issue-21232-partial-init-and-use.rs:148:5
Expand All @@ -60,7 +60,7 @@ LL | let mut t: T = (0, Box::new(0)); drop(t);
| |
| move occurs because `t` has type `(u32, std::boxed::Box<u32>)`, which does not implement the `Copy` trait
LL | t.0 = 10;
| ^^^^^^^^ value partially assigned here after move
| ^^^^^^^^ value partially assigned here after partial move

error[E0381]: assign to part of possibly uninitialized variable: `s`
--> $DIR/issue-21232-partial-init-and-use.rs:155:5
Expand Down Expand Up @@ -159,7 +159,7 @@ LL | match c {
LL | c2 => {
| -- value moved here
LL | c.0 = 2; //~ ERROR assign to part of moved value
| ^^^^^^^ value partially assigned here after move
| ^^^^^^^ value partially assigned here after partial move

error[E0382]: assign to part of moved value: `c`
--> $DIR/issue-21232-partial-init-and-use.rs:269:13
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/nll/move-subpaths-moves-root.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error[E0382]: use of moved value: `x`
LL | drop(x.0);
| --- value moved here
LL | drop(x); //~ ERROR use of moved value
| ^ value used here after move
| ^ value used here after partial move
|
= note: move occurs because `x.0` has type `std::vec::Vec<i32>`, which does not implement the `Copy` trait

Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/try-block/try-block-bad-lifetime.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ LL | Err(k) ?;
| - value moved here
...
LL | ::std::mem::drop(k); //~ ERROR use of moved value: `k`
| ^ value used here after move
| ^ value used here after partial move

error[E0506]: cannot assign to `i` because it is borrowed
--> $DIR/try-block-bad-lifetime.rs:32:9
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/try-block/try-block-maybe-bad-lifetime.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ LL | ::std::mem::drop(x);
| - value moved here
LL | };
LL | println!("{}", x); //~ ERROR borrow of moved value: `x`
| ^ value borrowed here after move
| ^ value borrowed here after partial move

error[E0506]: cannot assign to `i` because it is borrowed
--> $DIR/try-block-maybe-bad-lifetime.rs:40:9
Expand Down