Skip to content

Commit b159eeb

Browse files
committed
Auto merge of rust-lang#46949 - davidtwco:issue-46631, r=arielb1
MIR borrowck: no "move occurs because `X` is not Copy` error Fixes rust-lang#46631. r? @arielb1
2 parents 0cd6758 + 6710bd8 commit b159eeb

File tree

4 files changed

+75
-2
lines changed

4 files changed

+75
-2
lines changed

src/librustc_mir/borrow_check/error_reporting.rs

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,34 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
8181
err.span_label(move_span, format!("value moved{} here", move_msg));
8282
};
8383
}
84-
//FIXME: add note for closure
84+
85+
if let Some(ty) = self.retrieve_type_for_place(place) {
86+
let needs_note = match ty.sty {
87+
ty::TypeVariants::TyClosure(id, _) => {
88+
let tables = self.tcx.typeck_tables_of(id);
89+
let node_id = self.tcx.hir.as_local_node_id(id).unwrap();
90+
let hir_id = self.tcx.hir.node_to_hir_id(node_id);
91+
if let Some(_) = tables.closure_kind_origins().get(hir_id) {
92+
false
93+
} else {
94+
true
95+
}
96+
},
97+
_ => true,
98+
};
99+
100+
if needs_note {
101+
let note_msg = match self.describe_place(place) {
102+
Some(name) => format!("`{}`", name),
103+
None => "value".to_owned(),
104+
};
105+
106+
err.note(&format!("move occurs because {} has type `{}`, \
107+
which does not implement the `Copy` trait",
108+
note_msg, ty));
109+
}
110+
}
111+
85112
err.emit();
86113
}
87114
}
@@ -721,4 +748,21 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
721748
fn retrieve_borrow_span(&self, borrow: &BorrowData) -> Span {
722749
self.mir.source_info(borrow.location).span
723750
}
751+
752+
// Retrieve type of a place for the current MIR representation
753+
fn retrieve_type_for_place(&self, place: &Place<'tcx>) -> Option<ty::Ty> {
754+
match place {
755+
Place::Local(local) => {
756+
let local = &self.mir.local_decls[*local];
757+
Some(local.ty)
758+
},
759+
Place::Static(ref st) => Some(st.ty),
760+
Place::Projection(ref proj) => {
761+
match proj.elem {
762+
ProjectionElem::Field(_, ty) => Some(ty),
763+
_ => None,
764+
}
765+
},
766+
}
767+
}
724768
}

src/test/ui/borrowck/borrowck-reinit.stderr

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ error[E0382]: use of moved value: `x` (Mir)
1515
| - value moved here
1616
18 | let _ = (1,x); //~ ERROR use of moved value: `x` (Ast)
1717
| ^ value used here after move
18+
|
19+
= note: move occurs because `x` has type `std::boxed::Box<i32>`, which does not implement the `Copy` trait
1820

1921
error: aborting due to 2 previous errors
2022

src/test/compile-fail/moves-based-on-type-tuple.rs renamed to src/test/ui/moves-based-on-type-tuple.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,14 @@
1010

1111
#![feature(box_syntax)]
1212

13+
// compile-flags: -Z emit-end-regions -Z borrowck=compare
14+
1315
fn dup(x: Box<isize>) -> Box<(Box<isize>,Box<isize>)> {
14-
box (x, x) //~ ERROR use of moved value
16+
box (x, x)
17+
//~^ use of moved value: `x` (Ast) [E0382]
18+
//~| use of moved value: `x` (Mir) [E0382]
1519
}
20+
1621
fn main() {
1722
dup(box 3);
1823
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
error[E0382]: use of moved value: `x` (Ast)
2+
--> $DIR/moves-based-on-type-tuple.rs:16:13
3+
|
4+
16 | box (x, x)
5+
| - ^ value used here after move
6+
| |
7+
| value moved here
8+
|
9+
= note: move occurs because `x` has type `std::boxed::Box<isize>`, which does not implement the `Copy` trait
10+
11+
error[E0382]: use of moved value: `x` (Mir)
12+
--> $DIR/moves-based-on-type-tuple.rs:16:13
13+
|
14+
16 | box (x, x)
15+
| - ^ value used here after move
16+
| |
17+
| value moved here
18+
|
19+
= note: move occurs because `x` has type `std::boxed::Box<isize>`, which does not implement the `Copy` trait
20+
21+
error: aborting due to 2 previous errors
22+

0 commit comments

Comments
 (0)