Skip to content

Commit a35cf79

Browse files
committed
Auto merge of #56843 - csmoe:non-copy, r=davidtwco
Add a note describing the type of the non-Copy moved variable Closes #56654 r?@davidtwco
2 parents 5918318 + 48de0ff commit a35cf79

23 files changed

+94
-95
lines changed

src/librustc_mir/borrow_check/error_reporting.rs

Lines changed: 29 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -181,38 +181,36 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
181181
);
182182
}
183183

184-
if let Some(ty) = self.retrieve_type_for_place(used_place) {
185-
let needs_note = match ty.sty {
186-
ty::Closure(id, _) => {
187-
let tables = self.infcx.tcx.typeck_tables_of(id);
188-
let node_id = self.infcx.tcx.hir().as_local_node_id(id).unwrap();
189-
let hir_id = self.infcx.tcx.hir().node_to_hir_id(node_id);
190-
191-
tables.closure_kind_origins().get(hir_id).is_none()
192-
}
193-
_ => true,
194-
};
184+
let ty = used_place.ty(self.mir, self.infcx.tcx).to_ty(self.infcx.tcx);
185+
let needs_note = match ty.sty {
186+
ty::Closure(id, _) => {
187+
let tables = self.infcx.tcx.typeck_tables_of(id);
188+
let node_id = self.infcx.tcx.hir().as_local_node_id(id).unwrap();
189+
let hir_id = self.infcx.tcx.hir().node_to_hir_id(node_id);
195190

196-
if needs_note {
197-
let mpi = self.move_data.moves[move_out_indices[0]].path;
198-
let place = &self.move_data.move_paths[mpi].place;
199-
200-
if let Some(ty) = self.retrieve_type_for_place(place) {
201-
let note_msg = match self.describe_place_with_options(
202-
place,
203-
IncludingDowncast(true),
204-
) {
205-
Some(name) => format!("`{}`", name),
206-
None => "value".to_owned(),
207-
};
208-
209-
err.note(&format!(
210-
"move occurs because {} has type `{}`, \
211-
which does not implement the `Copy` trait",
212-
note_msg, ty
213-
));
214-
}
191+
tables.closure_kind_origins().get(hir_id).is_none()
215192
}
193+
_ => true,
194+
};
195+
196+
if needs_note {
197+
let mpi = self.move_data.moves[move_out_indices[0]].path;
198+
let place = &self.move_data.move_paths[mpi].place;
199+
200+
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),
206+
None => "value".to_owned(),
207+
};
208+
209+
err.note(&format!(
210+
"move occurs because {} has type `{}`, \
211+
which does not implement the `Copy` trait",
212+
note_msg, ty
213+
));
216214
}
217215

218216
if let Some((_, mut old_err)) = self.move_error_reported
@@ -1558,7 +1556,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
15581556
)?;
15591557
buf.push_str("[");
15601558
if self.append_local_to_string(index, buf).is_err() {
1561-
buf.push_str("..");
1559+
buf.push_str("_");
15621560
}
15631561
buf.push_str("]");
15641562
}
@@ -1663,22 +1661,6 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
16631661
}
16641662
}
16651663

1666-
/// Retrieve type of a place for the current MIR representation
1667-
fn retrieve_type_for_place(&self, place: &Place<'tcx>) -> Option<ty::Ty> {
1668-
match place {
1669-
Place::Local(local) => {
1670-
let local = &self.mir.local_decls[*local];
1671-
Some(local.ty)
1672-
}
1673-
Place::Promoted(ref prom) => Some(prom.1),
1674-
Place::Static(ref st) => Some(st.ty),
1675-
Place::Projection(ref proj) => match proj.elem {
1676-
ProjectionElem::Field(_, ty) => Some(ty),
1677-
_ => None,
1678-
},
1679-
}
1680-
}
1681-
16821664
/// Check if a place is a thread-local static.
16831665
pub fn is_place_thread_local(&self, place: &Place<'tcx>) -> bool {
16841666
if let Place::Static(statik) = place {

src/test/ui/borrowck/borrowck-describe-lvalue.ast.nll.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ LL | v[0].y;
316316
LL | drop(x);
317317
| - borrow later used here
318318

319-
error[E0503]: cannot use `v[..].y` because it was mutably borrowed
319+
error[E0503]: cannot use `v[_].y` because it was mutably borrowed
320320
--> $DIR/borrowck-describe-lvalue.rs:261:9
321321
|
322322
LL | let x = &mut v;

src/test/ui/borrowck/borrowck-describe-lvalue.mir.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ LL | v[0].y;
316316
LL | drop(x);
317317
| - borrow later used here
318318

319-
error[E0503]: cannot use `v[..].y` because it was mutably borrowed
319+
error[E0503]: cannot use `v[_].y` because it was mutably borrowed
320320
--> $DIR/borrowck-describe-lvalue.rs:261:9
321321
|
322322
LL | let x = &mut v;

src/test/ui/borrowck/borrowck-describe-lvalue.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ fn main() {
260260
let x = &mut v;
261261
v[0].y;
262262
//[ast]~^ ERROR cannot use `v[..].y` because it was mutably borrowed
263-
//[mir]~^^ ERROR cannot use `v[..].y` because it was mutably borrowed
263+
//[mir]~^^ ERROR cannot use `v[_].y` because it was mutably borrowed
264264
//[mir]~| ERROR cannot use `*v` because it was mutably borrowed
265265
drop(x);
266266
}

src/test/ui/borrowck/borrowck-field-sensitivity.nll.stderr

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ LL | drop(x.b);
55
| --- value moved here
66
LL | drop(*x.b); //~ ERROR use of moved value: `*x.b`
77
| ^^^^ value used here after move
8+
|
9+
= note: move occurs because `x.b` has type `std::boxed::Box<isize>`, which does not implement the `Copy` trait
810

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

1721
error[E0382]: borrow of moved value: `x.b`
1822
--> $DIR/borrowck-field-sensitivity.rs:20:13

src/test/ui/borrowck/borrowck-move-out-from-array.ast.nll.stderr

Lines changed: 0 additions & 19 deletions
This file was deleted.

src/test/ui/borrowck/borrowck-move-out-from-array.mir.stderr

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ LL | let [_, _x] = a;
55
| -- value moved here
66
LL | let [.., _y] = a; //[ast]~ ERROR [E0382]
77
| ^^ value used here after move
8+
|
9+
= note: move occurs because `a[..]` has type `std::boxed::Box<i32>`, which does not implement the `Copy` trait
810

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

1721
error: aborting due to 2 previous errors
1822

src/test/ui/borrowck/borrowck-vec-pattern-move-tail.ast.nll.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
error[E0506]: cannot assign to `a[..]` because it is borrowed
1+
error[E0506]: cannot assign to `a[_]` because it is borrowed
22
--> $DIR/borrowck-vec-pattern-move-tail.rs:16:5
33
|
44
LL | [1, 2, ref tail..] => tail,
5-
| -------- borrow of `a[..]` occurs here
5+
| -------- borrow of `a[_]` occurs here
66
...
77
LL | a[2] = 0; //[ast]~ ERROR cannot assign to `a[..]` because it is borrowed
8-
| ^^^^^^^^ assignment to borrowed `a[..]` occurs here
8+
| ^^^^^^^^ assignment to borrowed `a[_]` occurs here
99
...
1010
LL | println!("t[0]: {}", t[0]);
1111
| ---- borrow later used here

src/test/ui/borrowck/borrowck-vec-pattern-move-tail.cmp.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ LL | [1, 2, ref tail..] => tail,
77
LL | a[2] = 0; //[ast]~ ERROR cannot assign to `a[..]` because it is borrowed
88
| ^^^^^^^^ assignment to borrowed `a[..]` occurs here
99

10-
error[E0506]: cannot assign to `a[..]` because it is borrowed (Mir)
10+
error[E0506]: cannot assign to `a[_]` because it is borrowed (Mir)
1111
--> $DIR/borrowck-vec-pattern-move-tail.rs:16:5
1212
|
1313
LL | [1, 2, ref tail..] => tail,
14-
| -------- borrow of `a[..]` occurs here
14+
| -------- borrow of `a[_]` occurs here
1515
...
1616
LL | a[2] = 0; //[ast]~ ERROR cannot assign to `a[..]` because it is borrowed
17-
| ^^^^^^^^ assignment to borrowed `a[..]` occurs here
17+
| ^^^^^^^^ assignment to borrowed `a[_]` occurs here
1818
...
1919
LL | println!("t[0]: {}", t[0]);
2020
| ---- borrow later used here

src/test/ui/borrowck/borrowck-vec-pattern-move-tail.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ fn main() {
1515
println!("t[0]: {}", t[0]);
1616
a[2] = 0; //[ast]~ ERROR cannot assign to `a[..]` because it is borrowed
1717
//[cmp]~^ ERROR cannot assign to `a[..]` because it is borrowed (Ast)
18-
//[cmp]~| ERROR cannot assign to `a[..]` because it is borrowed (Mir)
18+
//[cmp]~| ERROR cannot assign to `a[_]` because it is borrowed (Mir)
1919
println!("t[0]: {}", t[0]);
2020
t[0];
2121
}

0 commit comments

Comments
 (0)