Explain method-call move errors in loops

PR #73708 added a more detailed explanation of move errors that occur
due to a call to a method that takes `self`. This PR extends that logic
to work when a move error occurs due to a method call in the previous
iteration of a loop.
This commit is contained in:
Aaron Hill 2020-12-22 22:15:40 -05:00
parent ddf2cc7f8e
commit de90afc72e
No known key found for this signature in database
GPG key ID: B4087E510E98B164
4 changed files with 100 additions and 86 deletions

View file

@ -69,6 +69,11 @@ fn move_out(val: Container) {
let container = Container(vec![]);
for _val in container.custom_into_iter() {}
container; //~ ERROR use of moved
let foo2 = Foo;
loop {
foo2.use_self(); //~ ERROR use of moved
}
}
fn main() {}

View file

@ -152,7 +152,16 @@ note: this function consumes the receiver `self` by taking ownership of it, whic
LL | fn custom_into_iter(self) -> impl Iterator<Item = bool> {
| ^^^^
error: aborting due to 11 previous errors
error[E0382]: use of moved value: `foo2`
--> $DIR/move-fn-self-receiver.rs:75:9
|
LL | let foo2 = Foo;
| ---- move occurs because `foo2` has type `Foo`, which does not implement the `Copy` trait
LL | loop {
LL | foo2.use_self();
| ^^^^ ---------- `foo2` moved due to this method call, in previous iteration of loop
error: aborting due to 12 previous errors
Some errors have detailed explanations: E0382, E0505.
For more information about an error, try `rustc --explain E0382`.

View file

@ -15,8 +15,14 @@ LL | for i in &a {
LL | for j in a {
| ^
| |
| value moved here, in previous iteration of loop
| `a` moved due to this implicit call to `.into_iter()`, in previous iteration of loop
| help: consider borrowing to avoid moving into the for loop: `&a`
|
note: this function consumes the receiver `self` by taking ownership of it, which moves `a`
--> $SRC_DIR/core/src/iter/traits/collect.rs:LL:COL
|
LL | fn into_iter(self) -> Self::IntoIter;
| ^^^^
error: aborting due to 2 previous errors