Skip to content

Commit

Permalink
Auto merge of #56843 - csmoe:non-copy, r=davidtwco
Browse files Browse the repository at this point in the history
Add a note describing the type of the non-Copy moved variable

Closes #56654
r?@davidtwco
  • Loading branch information
bors committed Dec 29, 2018
2 parents 5918318 + 48de0ff commit a35cf79
Show file tree
Hide file tree
Showing 23 changed files with 94 additions and 95 deletions.
76 changes: 29 additions & 47 deletions src/librustc_mir/borrow_check/error_reporting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,38 +181,36 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
);
}

if let Some(ty) = self.retrieve_type_for_place(used_place) {
let needs_note = match ty.sty {
ty::Closure(id, _) => {
let tables = self.infcx.tcx.typeck_tables_of(id);
let node_id = self.infcx.tcx.hir().as_local_node_id(id).unwrap();
let hir_id = self.infcx.tcx.hir().node_to_hir_id(node_id);

tables.closure_kind_origins().get(hir_id).is_none()
}
_ => true,
};
let ty = used_place.ty(self.mir, self.infcx.tcx).to_ty(self.infcx.tcx);
let needs_note = match ty.sty {
ty::Closure(id, _) => {
let tables = self.infcx.tcx.typeck_tables_of(id);
let node_id = self.infcx.tcx.hir().as_local_node_id(id).unwrap();
let hir_id = self.infcx.tcx.hir().node_to_hir_id(node_id);

if needs_note {
let mpi = self.move_data.moves[move_out_indices[0]].path;
let place = &self.move_data.move_paths[mpi].place;

if let Some(ty) = self.retrieve_type_for_place(place) {
let note_msg = match self.describe_place_with_options(
place,
IncludingDowncast(true),
) {
Some(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
));
}
tables.closure_kind_origins().get(hir_id).is_none()
}
_ => true,
};

if needs_note {
let mpi = self.move_data.moves[move_out_indices[0]].path;
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),
None => "value".to_owned(),
};

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 Expand Up @@ -1558,7 +1556,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
)?;
buf.push_str("[");
if self.append_local_to_string(index, buf).is_err() {
buf.push_str("..");
buf.push_str("_");
}
buf.push_str("]");
}
Expand Down Expand Up @@ -1663,22 +1661,6 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
}
}

/// Retrieve type of a place for the current MIR representation
fn retrieve_type_for_place(&self, place: &Place<'tcx>) -> Option<ty::Ty> {
match place {
Place::Local(local) => {
let local = &self.mir.local_decls[*local];
Some(local.ty)
}
Place::Promoted(ref prom) => Some(prom.1),
Place::Static(ref st) => Some(st.ty),
Place::Projection(ref proj) => match proj.elem {
ProjectionElem::Field(_, ty) => Some(ty),
_ => None,
},
}
}

/// Check if a place is a thread-local static.
pub fn is_place_thread_local(&self, place: &Place<'tcx>) -> bool {
if let Place::Static(statik) = place {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ LL | v[0].y;
LL | drop(x);
| - borrow later used here

error[E0503]: cannot use `v[..].y` because it was mutably borrowed
error[E0503]: cannot use `v[_].y` because it was mutably borrowed
--> $DIR/borrowck-describe-lvalue.rs:261:9
|
LL | let x = &mut v;
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 @@ -316,7 +316,7 @@ LL | v[0].y;
LL | drop(x);
| - borrow later used here

error[E0503]: cannot use `v[..].y` because it was mutably borrowed
error[E0503]: cannot use `v[_].y` because it was mutably borrowed
--> $DIR/borrowck-describe-lvalue.rs:261:9
|
LL | let x = &mut v;
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/borrowck/borrowck-describe-lvalue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ fn main() {
let x = &mut v;
v[0].y;
//[ast]~^ ERROR cannot use `v[..].y` because it was mutably borrowed
//[mir]~^^ ERROR cannot use `v[..].y` because it was mutably borrowed
//[mir]~^^ ERROR cannot use `v[_].y` because it was mutably borrowed
//[mir]~| ERROR cannot use `*v` because it was mutably borrowed
drop(x);
}
Expand Down
4 changes: 4 additions & 0 deletions src/test/ui/borrowck/borrowck-field-sensitivity.nll.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ LL | drop(x.b);
| --- value moved here
LL | drop(*x.b); //~ ERROR use of moved value: `*x.b`
| ^^^^ value used here after move
|
= note: move occurs because `x.b` has type `std::boxed::Box<isize>`, which does not implement the `Copy` trait

error[E0382]: use of moved value: `x.b`
--> $DIR/borrowck-field-sensitivity.rs:14:10
Expand All @@ -13,6 +15,8 @@ LL | let y = A { a: 3, .. x };
| ---------------- value moved here
LL | drop(*x.b); //~ ERROR use of moved value: `*x.b`
| ^^^^ value used here after move
|
= note: move occurs because `x.b` has type `std::boxed::Box<isize>`, which does not implement the `Copy` trait

error[E0382]: borrow of moved value: `x.b`
--> $DIR/borrowck-field-sensitivity.rs:20:13
Expand Down
19 changes: 0 additions & 19 deletions src/test/ui/borrowck/borrowck-move-out-from-array.ast.nll.stderr

This file was deleted.

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

error[E0382]: use of moved value: `a[..]`
--> $DIR/borrowck-move-out-from-array.rs:17:10
Expand All @@ -13,6 +15,8 @@ LL | let [_x, _] = a;
| -- value moved here
LL | let [_y..] = a; //[ast]~ ERROR [E0382]
| ^^ value used here after move
|
= note: move occurs because `a[..]` has type `std::boxed::Box<i32>`, which does not implement the `Copy` trait

error: aborting due to 2 previous errors

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
error[E0506]: cannot assign to `a[..]` because it is borrowed
error[E0506]: cannot assign to `a[_]` because it is borrowed
--> $DIR/borrowck-vec-pattern-move-tail.rs:16:5
|
LL | [1, 2, ref tail..] => tail,
| -------- borrow of `a[..]` occurs here
| -------- borrow of `a[_]` occurs here
...
LL | a[2] = 0; //[ast]~ ERROR cannot assign to `a[..]` because it is borrowed
| ^^^^^^^^ assignment to borrowed `a[..]` occurs here
| ^^^^^^^^ assignment to borrowed `a[_]` occurs here
...
LL | println!("t[0]: {}", t[0]);
| ---- borrow later used here
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ LL | [1, 2, ref tail..] => tail,
LL | a[2] = 0; //[ast]~ ERROR cannot assign to `a[..]` because it is borrowed
| ^^^^^^^^ assignment to borrowed `a[..]` occurs here

error[E0506]: cannot assign to `a[..]` because it is borrowed (Mir)
error[E0506]: cannot assign to `a[_]` because it is borrowed (Mir)
--> $DIR/borrowck-vec-pattern-move-tail.rs:16:5
|
LL | [1, 2, ref tail..] => tail,
| -------- borrow of `a[..]` occurs here
| -------- borrow of `a[_]` occurs here
...
LL | a[2] = 0; //[ast]~ ERROR cannot assign to `a[..]` because it is borrowed
| ^^^^^^^^ assignment to borrowed `a[..]` occurs here
| ^^^^^^^^ assignment to borrowed `a[_]` occurs here
...
LL | println!("t[0]: {}", t[0]);
| ---- borrow later used here
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/borrowck/borrowck-vec-pattern-move-tail.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ fn main() {
println!("t[0]: {}", t[0]);
a[2] = 0; //[ast]~ ERROR cannot assign to `a[..]` because it is borrowed
//[cmp]~^ ERROR cannot assign to `a[..]` because it is borrowed (Ast)
//[cmp]~| ERROR cannot assign to `a[..]` because it is borrowed (Mir)
//[cmp]~| ERROR cannot assign to `a[_]` because it is borrowed (Mir)
println!("t[0]: {}", t[0]);
t[0];
}
12 changes: 6 additions & 6 deletions src/test/ui/borrowck/borrowck-vec-pattern-nesting.nll.stderr
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
error[E0506]: cannot assign to `vec[..]` because it is borrowed
error[E0506]: cannot assign to `vec[_]` because it is borrowed
--> $DIR/borrowck-vec-pattern-nesting.rs:10:13
|
LL | [box ref _a, _, _] => {
| ------ borrow of `vec[..]` occurs here
| ------ borrow of `vec[_]` occurs here
LL | //~^ borrow of `vec[..]` occurs here
LL | vec[0] = box 4; //~ ERROR cannot assign
| ^^^^^^ assignment to borrowed `vec[..]` occurs here
| ^^^^^^ assignment to borrowed `vec[_]` occurs here
LL | //~^ assignment to borrowed `vec[..]` occurs here
LL | _a.use_ref();
| -- borrow later used here

error[E0506]: cannot assign to `vec[..]` because it is borrowed
error[E0506]: cannot assign to `vec[_]` because it is borrowed
--> $DIR/borrowck-vec-pattern-nesting.rs:23:13
|
LL | &mut [ref _b..] => {
| ------ borrow of `vec[..]` occurs here
| ------ borrow of `vec[_]` occurs here
LL | //~^ borrow of `vec[..]` occurs here
LL | vec[0] = box 4; //~ ERROR cannot assign
| ^^^^^^ assignment to borrowed `vec[..]` occurs here
| ^^^^^^ assignment to borrowed `vec[_]` occurs here
LL | //~^ assignment to borrowed `vec[..]` occurs here
LL | _b.use_ref();
| -- borrow later used here
Expand Down
4 changes: 4 additions & 0 deletions src/test/ui/borrowck/two-phase-nonrecv-autoref.ast.nll.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ LL | f(f(10));
| - ^ value used here after move
| |
| value moved here
|
= note: move occurs because `*f` has type `F`, which does not implement the `Copy` trait

error[E0499]: cannot borrow `*f` as mutable more than once at a time
--> $DIR/two-phase-nonrecv-autoref.rs:76:11
Expand Down Expand Up @@ -43,6 +45,8 @@ LL | f(f(10));
| - ^ value used here after move
| |
| value moved here
|
= note: move occurs because `*f` has type `dyn std::ops::FnOnce(i32) -> i32`, which does not implement the `Copy` trait

error[E0502]: cannot borrow `a` as immutable because it is also borrowed as mutable
--> $DIR/two-phase-nonrecv-autoref.rs:129:27
Expand Down
4 changes: 4 additions & 0 deletions src/test/ui/borrowck/two-phase-nonrecv-autoref.nll.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ LL | f(f(10));
| - ^ value used here after move
| |
| value moved here
|
= note: move occurs because `*f` has type `F`, which does not implement the `Copy` trait

error[E0499]: cannot borrow `*f` as mutable more than once at a time
--> $DIR/two-phase-nonrecv-autoref.rs:76:11
Expand Down Expand Up @@ -43,6 +45,8 @@ LL | f(f(10));
| - ^ value used here after move
| |
| value moved here
|
= note: move occurs because `*f` has type `dyn std::ops::FnOnce(i32) -> i32`, which does not implement the `Copy` trait

error[E0502]: cannot borrow `a` as immutable because it is also borrowed as mutable
--> $DIR/two-phase-nonrecv-autoref.rs:129:27
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ LL | _ if { (|| { let bar = b; *bar = false; })();
LL | false } => { },
LL | &mut true => { println!("You might think we should get here"); },
| ^^^^ value used here after move
|
= note: move occurs because `b` has type `&mut bool`, which does not implement the `Copy` trait

error: aborting due to previous error

Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/issues/issue-42344.nll.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error[E0596]: cannot borrow `*TAB[..]` as mutable, as `TAB` is an immutable static item
error[E0596]: cannot borrow `*TAB[_]` as mutable, as `TAB` is an immutable static item
--> $DIR/issue-42344.rs:4:5
|
LL | TAB[0].iter_mut(); //~ ERROR cannot borrow data mutably in a `&` reference [E0389]
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/issues/issue-46604.ast.nll.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error[E0017]: references in statics may only refer to immutable values
LL | static buf: &mut [u8] = &mut [1u8,2,3,4,5,7]; //[ast]~ ERROR E0017
| ^^^^^^^^^^^^^^^^^^^^ statics require immutable values

error[E0594]: cannot assign to `buf[..]`, as `buf` is an immutable static item
error[E0594]: cannot assign to `buf[_]`, as `buf` is an immutable static item
--> $DIR/issue-46604.rs:10:5
|
LL | buf[0]=2; //[ast]~ ERROR E0389
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/issues/issue-46604.mir.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error[E0017]: references in statics may only refer to immutable values
LL | static buf: &mut [u8] = &mut [1u8,2,3,4,5,7]; //[ast]~ ERROR E0017
| ^^^^^^^^^^^^^^^^^^^^ statics require immutable values

error[E0594]: cannot assign to `buf[..]`, as `buf` is an immutable static item
error[E0594]: cannot assign to `buf[_]`, as `buf` is an immutable static item
--> $DIR/issue-46604.rs:10:5
|
LL | buf[0]=2; //[ast]~ ERROR E0389
Expand Down
2 changes: 2 additions & 0 deletions src/test/ui/liveness/liveness-use-after-move.nll.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ LL | let y = x;
| - value moved here
LL | println!("{}", *x); //~ ERROR use of moved value: `*x`
| ^^ value borrowed here after move
|
= note: move occurs because `x` has type `std::boxed::Box<i32>`, which does not implement the `Copy` trait

error: aborting due to previous error

Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/nll/drop-no-may-dangle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ fn main() {
use_x(*p.value);
} else {
use_x(22);
v[0] += 1; //~ ERROR cannot assign to `v[..]` because it is borrowed
v[0] += 1; //~ ERROR cannot assign to `v[_]` because it is borrowed
}

v[0] += 1; //~ ERROR cannot assign to `v[..]` because it is borrowed
v[0] += 1; //~ ERROR cannot assign to `v[_]` because it is borrowed
}

struct WrapMayNotDangle<T> {
Expand Down
16 changes: 8 additions & 8 deletions src/test/ui/nll/drop-no-may-dangle.stderr
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
error[E0506]: cannot assign to `v[..]` because it is borrowed
error[E0506]: cannot assign to `v[_]` because it is borrowed
--> $DIR/drop-no-may-dangle.rs:20:9
|
LL | let p: WrapMayNotDangle<&usize> = WrapMayNotDangle { value: &v[0] };
| ----- borrow of `v[..]` occurs here
| ----- borrow of `v[_]` occurs here
...
LL | v[0] += 1; //~ ERROR cannot assign to `v[..]` because it is borrowed
| ^^^^^^^^^ assignment to borrowed `v[..]` occurs here
LL | v[0] += 1; //~ ERROR cannot assign to `v[_]` because it is borrowed
| ^^^^^^^^^ assignment to borrowed `v[_]` occurs here
...
LL | }
| - borrow might be used here, when `p` is dropped and runs the `Drop` code for type `WrapMayNotDangle`

error[E0506]: cannot assign to `v[..]` because it is borrowed
error[E0506]: cannot assign to `v[_]` because it is borrowed
--> $DIR/drop-no-may-dangle.rs:23:5
|
LL | let p: WrapMayNotDangle<&usize> = WrapMayNotDangle { value: &v[0] };
| ----- borrow of `v[..]` occurs here
| ----- borrow of `v[_]` occurs here
...
LL | v[0] += 1; //~ ERROR cannot assign to `v[..]` because it is borrowed
| ^^^^^^^^^ assignment to borrowed `v[..]` occurs here
LL | v[0] += 1; //~ ERROR cannot assign to `v[_]` because it is borrowed
| ^^^^^^^^^ assignment to borrowed `v[_]` occurs here
LL | }
| - borrow might be used here, when `p` is dropped and runs the `Drop` code for type `WrapMayNotDangle`

Expand Down
Loading

0 comments on commit a35cf79

Please sign in to comment.