Skip to content

Commit 063e4a2

Browse files
authored
Rollup merge of rust-lang#56843 - csmoe:non-copy, r=davidtwco
Add a note describing the type of the non-Copy moved variable Closes rust-lang#56654 r?@davidtwco
2 parents 7b70b0b + 37c3561 commit 063e4a2

13 files changed

+61
-68
lines changed

src/librustc_mir/borrow_check/error_reporting.rs

Lines changed: 29 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -191,38 +191,36 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
191191
);
192192
}
193193

194-
if let Some(ty) = self.retrieve_type_for_place(used_place) {
195-
let needs_note = match ty.sty {
196-
ty::Closure(id, _) => {
197-
let tables = self.infcx.tcx.typeck_tables_of(id);
198-
let node_id = self.infcx.tcx.hir().as_local_node_id(id).unwrap();
199-
let hir_id = self.infcx.tcx.hir().node_to_hir_id(node_id);
200-
201-
tables.closure_kind_origins().get(hir_id).is_none()
202-
}
203-
_ => true,
204-
};
194+
let ty = used_place.ty(self.mir, self.infcx.tcx).to_ty(self.infcx.tcx);
195+
let needs_note = match ty.sty {
196+
ty::Closure(id, _) => {
197+
let tables = self.infcx.tcx.typeck_tables_of(id);
198+
let node_id = self.infcx.tcx.hir().as_local_node_id(id).unwrap();
199+
let hir_id = self.infcx.tcx.hir().node_to_hir_id(node_id);
205200

206-
if needs_note {
207-
let mpi = self.move_data.moves[move_out_indices[0]].path;
208-
let place = &self.move_data.move_paths[mpi].place;
209-
210-
if let Some(ty) = self.retrieve_type_for_place(place) {
211-
let note_msg = match self.describe_place_with_options(
212-
place,
213-
IncludingDowncast(true),
214-
) {
215-
Some(name) => format!("`{}`", name),
216-
None => "value".to_owned(),
217-
};
218-
219-
err.note(&format!(
220-
"move occurs because {} has type `{}`, \
221-
which does not implement the `Copy` trait",
222-
note_msg, ty
223-
));
224-
}
201+
tables.closure_kind_origins().get(hir_id).is_none()
225202
}
203+
_ => true,
204+
};
205+
206+
if needs_note {
207+
let mpi = self.move_data.moves[move_out_indices[0]].path;
208+
let place = &self.move_data.move_paths[mpi].place;
209+
210+
let ty = place.ty(self.mir, self.infcx.tcx).to_ty(self.infcx.tcx);
211+
let note_msg = match self.describe_place_with_options(
212+
place,
213+
IncludingDowncast(true),
214+
) {
215+
Some(name) => format!("`{}`", name),
216+
None => "value".to_owned(),
217+
};
218+
219+
err.note(&format!(
220+
"move occurs because {} has type `{}`, \
221+
which does not implement the `Copy` trait",
222+
note_msg, ty
223+
));
226224
}
227225

228226
if let Some((_, mut old_err)) = self.move_error_reported
@@ -1568,7 +1566,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
15681566
)?;
15691567
buf.push_str("[");
15701568
if self.append_local_to_string(index, buf).is_err() {
1571-
buf.push_str("..");
1569+
buf.push_str("_");
15721570
}
15731571
buf.push_str("]");
15741572
}
@@ -1673,22 +1671,6 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
16731671
}
16741672
}
16751673

1676-
/// Retrieve type of a place for the current MIR representation
1677-
fn retrieve_type_for_place(&self, place: &Place<'tcx>) -> Option<ty::Ty> {
1678-
match place {
1679-
Place::Local(local) => {
1680-
let local = &self.mir.local_decls[*local];
1681-
Some(local.ty)
1682-
}
1683-
Place::Promoted(ref prom) => Some(prom.1),
1684-
Place::Static(ref st) => Some(st.ty),
1685-
Place::Projection(ref proj) => match proj.elem {
1686-
ProjectionElem::Field(_, ty) => Some(ty),
1687-
_ => None,
1688-
},
1689-
}
1690-
}
1691-
16921674
/// Check if a place is a thread-local static.
16931675
pub fn is_place_thread_local(&self, place: &Place<'tcx>) -> bool {
16941676
if let Place::Static(statik) = place {

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:271: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
@@ -270,7 +270,7 @@ fn main() {
270270
let x = &mut v;
271271
v[0].y;
272272
//[ast]~^ ERROR cannot use `v[..].y` because it was mutably borrowed
273-
//[mir]~^^ ERROR cannot use `v[..].y` because it was mutably borrowed
273+
//[mir]~^^ ERROR cannot use `v[_].y` because it was mutably borrowed
274274
//[mir]~| ERROR cannot use `*v` because it was mutably borrowed
275275
drop(x);
276276
}

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:27: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.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:24: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
@@ -23,7 +23,7 @@ fn main() {
2323
println!("t[0]: {}", t[0]);
2424
a[2] = 0; //[ast]~ ERROR cannot assign to `a[..]` because it is borrowed
2525
//[cmp]~^ ERROR cannot assign to `a[..]` because it is borrowed (Ast)
26-
//[cmp]~| ERROR cannot assign to `a[..]` because it is borrowed (Mir)
26+
//[cmp]~| ERROR cannot assign to `a[_]` because it is borrowed (Mir)
2727
println!("t[0]: {}", t[0]);
2828
t[0];
2929
}

src/test/ui/borrowck/two-phase-nonrecv-autoref.nll.stderr

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ LL | f(f(10));
1414
| - ^ value used here after move
1515
| |
1616
| value moved here
17+
|
18+
= note: move occurs because `*f` has type `F`, which does not implement the `Copy` trait
1719

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

4751
error[E0502]: cannot borrow `a` as immutable because it is also borrowed as mutable
4852
--> $DIR/two-phase-nonrecv-autoref.rs:139:27

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ LL | _ if { (|| { let bar = b; *bar = false; })();
88
LL | false } => { },
99
LL | &mut true => { println!("You might think we should get here"); },
1010
| ^^^^ value used here after move
11+
|
12+
= note: move occurs because `b` has type `&mut bool`, which does not implement the `Copy` trait
1113

1214
error: aborting due to previous error
1315

src/test/ui/issues/issue-46604.mir.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0017]: references in statics may only refer to immutable values
44
LL | static buf: &mut [u8] = &mut [1u8,2,3,4,5,7]; //[ast]~ ERROR E0017
55
| ^^^^^^^^^^^^^^^^^^^^ statics require immutable values
66

7-
error[E0594]: cannot assign to `buf[..]`, as `buf` is an immutable static item
7+
error[E0594]: cannot assign to `buf[_]`, as `buf` is an immutable static item
88
--> $DIR/issue-46604.rs:20:5
99
|
1010
LL | buf[0]=2; //[ast]~ ERROR E0389

src/test/ui/nll/drop-no-may-dangle.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ fn main() {
2727
use_x(*p.value);
2828
} else {
2929
use_x(22);
30-
v[0] += 1; //~ ERROR cannot assign to `v[..]` because it is borrowed
30+
v[0] += 1; //~ ERROR cannot assign to `v[_]` because it is borrowed
3131
}
3232

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

3636
struct WrapMayNotDangle<T> {

src/test/ui/nll/drop-no-may-dangle.stderr

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
1-
error[E0506]: cannot assign to `v[..]` because it is borrowed
1+
error[E0506]: cannot assign to `v[_]` because it is borrowed
22
--> $DIR/drop-no-may-dangle.rs:30:9
33
|
44
LL | let p: WrapMayNotDangle<&usize> = WrapMayNotDangle { value: &v[0] };
5-
| ----- borrow of `v[..]` occurs here
5+
| ----- borrow of `v[_]` occurs here
66
...
7-
LL | v[0] += 1; //~ ERROR cannot assign to `v[..]` because it is borrowed
8-
| ^^^^^^^^^ assignment to borrowed `v[..]` occurs here
7+
LL | v[0] += 1; //~ ERROR cannot assign to `v[_]` because it is borrowed
8+
| ^^^^^^^^^ assignment to borrowed `v[_]` occurs here
99
...
1010
LL | }
1111
| - borrow might be used here, when `p` is dropped and runs the `Drop` code for type `WrapMayNotDangle`
1212

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

src/test/ui/use/use-after-move-self.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
//compile-flags: -Z borrowck=mir
1112
#![feature(box_syntax)]
1213

1314
struct S {
@@ -17,7 +18,7 @@ struct S {
1718
impl S {
1819
pub fn foo(self) -> isize {
1920
self.bar();
20-
return *self.x; //~ ERROR use of moved value: `*self.x`
21+
return *self.x; //~ ERROR use of moved value: `self`
2122
}
2223

2324
pub fn bar(self) {}

src/test/ui/use/use-after-move-self.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
error[E0382]: use of moved value: `*self.x`
2-
--> $DIR/use-after-move-self.rs:20:16
1+
error[E0382]: use of moved value: `self`
2+
--> $DIR/use-after-move-self.rs:21:16
33
|
44
LL | self.bar();
55
| ---- value moved here
6-
LL | return *self.x; //~ ERROR use of moved value: `*self.x`
6+
LL | return *self.x; //~ ERROR use of moved value: `self`
77
| ^^^^^^^ value used here after move
88
|
99
= note: move occurs because `self` has type `S`, which does not implement the `Copy` trait

0 commit comments

Comments
 (0)