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

When using value after move, point at span of local #57294

Merged
merged 6 commits into from
Jan 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 30 additions & 11 deletions src/librustc_mir/borrow_check/error_reporting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,19 +198,38 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
let place = &self.move_data.move_paths[mpi].place;

let ty = place.ty(self.mir, self.infcx.tcx).to_ty(self.infcx.tcx);
let note_msg = match self.describe_place_with_options(
place,
IncludingDowncast(true),
) {
Some(name) => format!("`{}`", name),
let opt_name = self.describe_place_with_options(place, IncludingDowncast(true));
let note_msg = match opt_name {
Some(ref name) => format!("`{}`", name),
None => "value".to_owned(),
};

err.note(&format!(
"move occurs because {} has type `{}`, \
which does not implement the `Copy` trait",
note_msg, ty
));
if let ty::TyKind::Param(param_ty) = ty.sty {
let tcx = self.infcx.tcx;
let generics = tcx.generics_of(self.mir_def_id);
let def_id = generics.type_param(&param_ty, tcx).def_id;
if let Some(sp) = tcx.hir().span_if_local(def_id) {
err.span_label(
sp,
"consider adding a `Copy` constraint to this type argument",
estebank marked this conversation as resolved.
Show resolved Hide resolved
);
}
}
if let Place::Local(local) = place {
let decl = &self.mir.local_decls[*local];
err.span_label(
decl.source_info.span,
format!(
"move occurs because {} has type `{}`, \
which does not implement the `Copy` trait",
note_msg, ty,
));
} else {
err.note(&format!(
"move occurs because {} has type `{}`, \
which does not implement the `Copy` trait",
note_msg, ty
));
}
}

if let Some((_, mut old_err)) = self.move_error_reported
Expand Down
253 changes: 253 additions & 0 deletions src/test/ui/binop/binop-consume-args.nll.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,253 @@
error[E0382]: use of moved value: `lhs`
--> $DIR/binop-consume-args.rs:7:10
|
LL | fn add<A: Add<B, Output=()>, B>(lhs: A, rhs: B) {
| - --- move occurs because `lhs` has type `A`, which does not implement the `Copy` trait
| |
| consider adding a `Copy` constraint to this type argument
LL | lhs + rhs;
| --- value moved here
LL | drop(lhs); //~ ERROR use of moved value: `lhs`
| ^^^ value used here after move

error[E0382]: use of moved value: `rhs`
--> $DIR/binop-consume-args.rs:8:10
|
LL | fn add<A: Add<B, Output=()>, B>(lhs: A, rhs: B) {
| - --- move occurs because `rhs` has type `B`, which does not implement the `Copy` trait
| |
| consider adding a `Copy` constraint to this type argument
LL | lhs + rhs;
| --- value moved here
LL | drop(lhs); //~ ERROR use of moved value: `lhs`
LL | drop(rhs); //~ ERROR use of moved value: `rhs`
| ^^^ value used here after move

error[E0382]: use of moved value: `lhs`
--> $DIR/binop-consume-args.rs:13:10
|
LL | fn sub<A: Sub<B, Output=()>, B>(lhs: A, rhs: B) {
| - --- move occurs because `lhs` has type `A`, which does not implement the `Copy` trait
| |
| consider adding a `Copy` constraint to this type argument
LL | lhs - rhs;
| --- value moved here
LL | drop(lhs); //~ ERROR use of moved value: `lhs`
| ^^^ value used here after move

error[E0382]: use of moved value: `rhs`
--> $DIR/binop-consume-args.rs:14:10
|
LL | fn sub<A: Sub<B, Output=()>, B>(lhs: A, rhs: B) {
| - --- move occurs because `rhs` has type `B`, which does not implement the `Copy` trait
| |
| consider adding a `Copy` constraint to this type argument
LL | lhs - rhs;
| --- value moved here
LL | drop(lhs); //~ ERROR use of moved value: `lhs`
LL | drop(rhs); //~ ERROR use of moved value: `rhs`
| ^^^ value used here after move

error[E0382]: use of moved value: `lhs`
--> $DIR/binop-consume-args.rs:19:10
|
LL | fn mul<A: Mul<B, Output=()>, B>(lhs: A, rhs: B) {
| - --- move occurs because `lhs` has type `A`, which does not implement the `Copy` trait
| |
| consider adding a `Copy` constraint to this type argument
LL | lhs * rhs;
| --- value moved here
LL | drop(lhs); //~ ERROR use of moved value: `lhs`
| ^^^ value used here after move

error[E0382]: use of moved value: `rhs`
--> $DIR/binop-consume-args.rs:20:10
|
LL | fn mul<A: Mul<B, Output=()>, B>(lhs: A, rhs: B) {
| - --- move occurs because `rhs` has type `B`, which does not implement the `Copy` trait
| |
| consider adding a `Copy` constraint to this type argument
LL | lhs * rhs;
| --- value moved here
LL | drop(lhs); //~ ERROR use of moved value: `lhs`
LL | drop(rhs); //~ ERROR use of moved value: `rhs`
| ^^^ value used here after move

error[E0382]: use of moved value: `lhs`
--> $DIR/binop-consume-args.rs:25:10
|
LL | fn div<A: Div<B, Output=()>, B>(lhs: A, rhs: B) {
| - --- move occurs because `lhs` has type `A`, which does not implement the `Copy` trait
| |
| consider adding a `Copy` constraint to this type argument
LL | lhs / rhs;
| --- value moved here
LL | drop(lhs); //~ ERROR use of moved value: `lhs`
| ^^^ value used here after move

error[E0382]: use of moved value: `rhs`
--> $DIR/binop-consume-args.rs:26:10
|
LL | fn div<A: Div<B, Output=()>, B>(lhs: A, rhs: B) {
| - --- move occurs because `rhs` has type `B`, which does not implement the `Copy` trait
| |
| consider adding a `Copy` constraint to this type argument
LL | lhs / rhs;
| --- value moved here
LL | drop(lhs); //~ ERROR use of moved value: `lhs`
LL | drop(rhs); //~ ERROR use of moved value: `rhs`
| ^^^ value used here after move

error[E0382]: use of moved value: `lhs`
--> $DIR/binop-consume-args.rs:31:10
|
LL | fn rem<A: Rem<B, Output=()>, B>(lhs: A, rhs: B) {
| - --- move occurs because `lhs` has type `A`, which does not implement the `Copy` trait
| |
| consider adding a `Copy` constraint to this type argument
LL | lhs % rhs;
| --- value moved here
LL | drop(lhs); //~ ERROR use of moved value: `lhs`
| ^^^ value used here after move

error[E0382]: use of moved value: `rhs`
--> $DIR/binop-consume-args.rs:32:10
|
LL | fn rem<A: Rem<B, Output=()>, B>(lhs: A, rhs: B) {
| - --- move occurs because `rhs` has type `B`, which does not implement the `Copy` trait
| |
| consider adding a `Copy` constraint to this type argument
LL | lhs % rhs;
| --- value moved here
LL | drop(lhs); //~ ERROR use of moved value: `lhs`
LL | drop(rhs); //~ ERROR use of moved value: `rhs`
| ^^^ value used here after move

error[E0382]: use of moved value: `lhs`
--> $DIR/binop-consume-args.rs:37:10
|
LL | fn bitand<A: BitAnd<B, Output=()>, B>(lhs: A, rhs: B) {
| - --- move occurs because `lhs` has type `A`, which does not implement the `Copy` trait
| |
| consider adding a `Copy` constraint to this type argument
LL | lhs & rhs;
| --- value moved here
LL | drop(lhs); //~ ERROR use of moved value: `lhs`
| ^^^ value used here after move

error[E0382]: use of moved value: `rhs`
--> $DIR/binop-consume-args.rs:38:10
|
LL | fn bitand<A: BitAnd<B, Output=()>, B>(lhs: A, rhs: B) {
| - --- move occurs because `rhs` has type `B`, which does not implement the `Copy` trait
| |
| consider adding a `Copy` constraint to this type argument
LL | lhs & rhs;
| --- value moved here
LL | drop(lhs); //~ ERROR use of moved value: `lhs`
LL | drop(rhs); //~ ERROR use of moved value: `rhs`
| ^^^ value used here after move

error[E0382]: use of moved value: `lhs`
--> $DIR/binop-consume-args.rs:43:10
|
LL | fn bitor<A: BitOr<B, Output=()>, B>(lhs: A, rhs: B) {
| - --- move occurs because `lhs` has type `A`, which does not implement the `Copy` trait
| |
| consider adding a `Copy` constraint to this type argument
LL | lhs | rhs;
| --- value moved here
LL | drop(lhs); //~ ERROR use of moved value: `lhs`
| ^^^ value used here after move

error[E0382]: use of moved value: `rhs`
--> $DIR/binop-consume-args.rs:44:10
|
LL | fn bitor<A: BitOr<B, Output=()>, B>(lhs: A, rhs: B) {
| - --- move occurs because `rhs` has type `B`, which does not implement the `Copy` trait
| |
| consider adding a `Copy` constraint to this type argument
LL | lhs | rhs;
| --- value moved here
LL | drop(lhs); //~ ERROR use of moved value: `lhs`
LL | drop(rhs); //~ ERROR use of moved value: `rhs`
| ^^^ value used here after move

error[E0382]: use of moved value: `lhs`
--> $DIR/binop-consume-args.rs:49:10
|
LL | fn bitxor<A: BitXor<B, Output=()>, B>(lhs: A, rhs: B) {
| - --- move occurs because `lhs` has type `A`, which does not implement the `Copy` trait
| |
| consider adding a `Copy` constraint to this type argument
LL | lhs ^ rhs;
| --- value moved here
LL | drop(lhs); //~ ERROR use of moved value: `lhs`
| ^^^ value used here after move

error[E0382]: use of moved value: `rhs`
--> $DIR/binop-consume-args.rs:50:10
|
LL | fn bitxor<A: BitXor<B, Output=()>, B>(lhs: A, rhs: B) {
| - --- move occurs because `rhs` has type `B`, which does not implement the `Copy` trait
| |
| consider adding a `Copy` constraint to this type argument
LL | lhs ^ rhs;
| --- value moved here
LL | drop(lhs); //~ ERROR use of moved value: `lhs`
LL | drop(rhs); //~ ERROR use of moved value: `rhs`
| ^^^ value used here after move

error[E0382]: use of moved value: `lhs`
--> $DIR/binop-consume-args.rs:55:10
|
LL | fn shl<A: Shl<B, Output=()>, B>(lhs: A, rhs: B) {
| - --- move occurs because `lhs` has type `A`, which does not implement the `Copy` trait
| |
| consider adding a `Copy` constraint to this type argument
LL | lhs << rhs;
| --- value moved here
LL | drop(lhs); //~ ERROR use of moved value: `lhs`
| ^^^ value used here after move

error[E0382]: use of moved value: `rhs`
--> $DIR/binop-consume-args.rs:56:10
|
LL | fn shl<A: Shl<B, Output=()>, B>(lhs: A, rhs: B) {
| - --- move occurs because `rhs` has type `B`, which does not implement the `Copy` trait
| |
| consider adding a `Copy` constraint to this type argument
LL | lhs << rhs;
| --- value moved here
LL | drop(lhs); //~ ERROR use of moved value: `lhs`
LL | drop(rhs); //~ ERROR use of moved value: `rhs`
| ^^^ value used here after move

error[E0382]: use of moved value: `lhs`
--> $DIR/binop-consume-args.rs:61:10
|
LL | fn shr<A: Shr<B, Output=()>, B>(lhs: A, rhs: B) {
| - --- move occurs because `lhs` has type `A`, which does not implement the `Copy` trait
| |
| consider adding a `Copy` constraint to this type argument
LL | lhs >> rhs;
| --- value moved here
LL | drop(lhs); //~ ERROR use of moved value: `lhs`
| ^^^ value used here after move

error[E0382]: use of moved value: `rhs`
--> $DIR/binop-consume-args.rs:62:10
|
LL | fn shr<A: Shr<B, Output=()>, B>(lhs: A, rhs: B) {
| - --- move occurs because `rhs` has type `B`, which does not implement the `Copy` trait
| |
| consider adding a `Copy` constraint to this type argument
LL | lhs >> rhs;
| --- value moved here
LL | drop(lhs); //~ ERROR use of moved value: `lhs`
LL | drop(rhs); //~ ERROR use of moved value: `rhs`
| ^^^ value used here after move

error: aborting due to 20 previous errors

For more information about this error, try `rustc --explain E0382`.
12 changes: 8 additions & 4 deletions src/test/ui/binop/binop-move-semantics.nll.stderr
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
error[E0382]: use of moved value: `x`
--> $DIR/binop-move-semantics.rs:8:5
|
LL | fn double_move<T: Add<Output=()>>(x: 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 | x
| - value moved here
LL | +
LL | x; //~ ERROR: use of moved value
| ^ value used here after move
|
= note: move occurs because `x` has type `T`, which does not implement the `Copy` trait

error[E0382]: borrow of moved value: `x`
--> $DIR/binop-move-semantics.rs:14:5
|
LL | fn move_then_borrow<T: Add<Output=()> + Clone>(x: 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 | x
| - value moved here
LL | +
LL | x.clone(); //~ ERROR: use of moved value
| ^ value borrowed here after move
|
= note: move occurs because `x` has type `T`, which does not implement the `Copy` trait

error[E0505]: cannot move out of `x` because it is borrowed
--> $DIR/binop-move-semantics.rs:21:5
Expand Down
10 changes: 6 additions & 4 deletions src/test/ui/borrowck/borrowck-asm.ast.nll.stderr
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
error[E0382]: use of moved value: `x`
--> $DIR/borrowck-asm.rs:27:17
|
LL | let x = &mut 0isize;
| - move occurs because `x` has type `&mut isize`, which does not implement the `Copy` trait
LL | unsafe {
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
|
= note: move occurs because `x` has type `&mut isize`, which does not implement the `Copy` trait

error[E0503]: cannot use `x` because it was mutably borrowed
--> $DIR/borrowck-asm.rs:35:32
Expand Down Expand Up @@ -66,12 +67,13 @@ LL | let z = y;
error[E0382]: use of moved value: `x`
--> $DIR/borrowck-asm.rs:86:40
|
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 moved here
|
= note: move occurs because `x` has type `&mut i32`, which does not implement the `Copy` trait

error: aborting due to 7 previous errors

Expand Down
Loading