forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of rust-lang#85686 - ptrojahn:loop_reinitialize, r=estebank
Add help on reinitialization between move and access Fixes rust-lang#83760
- Loading branch information
Showing
3 changed files
with
176 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
struct Struct; | ||
|
||
fn test1() { | ||
let mut val = Some(Struct); | ||
while let Some(foo) = val { //~ ERROR use of moved value | ||
if true { | ||
val = None; | ||
} else { | ||
|
||
} | ||
} | ||
} | ||
|
||
fn test2() { | ||
let mut foo = Some(Struct); | ||
let _x = foo.unwrap(); | ||
if true { | ||
foo = Some(Struct); | ||
} else { | ||
} | ||
let _y = foo; //~ ERROR use of moved value: `foo` | ||
} | ||
|
||
fn test3() { | ||
let mut foo = Some(Struct); | ||
let _x = foo.unwrap(); | ||
if true { | ||
foo = Some(Struct); | ||
} else if true { | ||
foo = Some(Struct); | ||
} else if true { | ||
foo = Some(Struct); | ||
} else if true { | ||
foo = Some(Struct); | ||
} else { | ||
} | ||
let _y = foo; //~ ERROR use of moved value: `foo` | ||
} | ||
|
||
fn main() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
error[E0382]: use of moved value | ||
--> $DIR/issue-83760.rs:5:20 | ||
| | ||
LL | while let Some(foo) = val { | ||
| ^^^ value moved here, in previous iteration of loop | ||
LL | if true { | ||
LL | val = None; | ||
| ---------- this reinitialization might get skipped | ||
| | ||
= note: move occurs because value has type `Struct`, which does not implement the `Copy` trait | ||
|
||
error[E0382]: use of moved value: `foo` | ||
--> $DIR/issue-83760.rs:21:14 | ||
| | ||
LL | let mut foo = Some(Struct); | ||
| ------- move occurs because `foo` has type `Option<Struct>`, which does not implement the `Copy` trait | ||
LL | let _x = foo.unwrap(); | ||
| -------- `foo` moved due to this method call | ||
LL | if true { | ||
LL | foo = Some(Struct); | ||
| ------------------ this reinitialization might get skipped | ||
... | ||
LL | let _y = foo; | ||
| ^^^ value used here after move | ||
| | ||
note: this function takes ownership of the receiver `self`, which moves `foo` | ||
--> $SRC_DIR/core/src/option.rs:LL:COL | ||
| | ||
LL | pub const fn unwrap(self) -> T { | ||
| ^^^^ | ||
|
||
error[E0382]: use of moved value: `foo` | ||
--> $DIR/issue-83760.rs:37:14 | ||
| | ||
LL | let mut foo = Some(Struct); | ||
| ------- move occurs because `foo` has type `Option<Struct>`, which does not implement the `Copy` trait | ||
LL | let _x = foo.unwrap(); | ||
| -------- `foo` moved due to this method call | ||
... | ||
LL | let _y = foo; | ||
| ^^^ value used here after move | ||
| | ||
note: these 3 reinitializations and 1 other might get skipped | ||
--> $DIR/issue-83760.rs:30:9 | ||
| | ||
LL | foo = Some(Struct); | ||
| ^^^^^^^^^^^^^^^^^^ | ||
LL | } else if true { | ||
LL | foo = Some(Struct); | ||
| ^^^^^^^^^^^^^^^^^^ | ||
LL | } else if true { | ||
LL | foo = Some(Struct); | ||
| ^^^^^^^^^^^^^^^^^^ | ||
note: this function takes ownership of the receiver `self`, which moves `foo` | ||
--> $SRC_DIR/core/src/option.rs:LL:COL | ||
| | ||
LL | pub const fn unwrap(self) -> T { | ||
| ^^^^ | ||
|
||
error: aborting due to 3 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0382`. |