-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Add help on reinitialization between move and access #85686
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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`. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this happen unconditionally, even in the happy path? We might want to do a perf run just to be sure we don't regress things too much.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The only place where get_moved_indexes is called is in report_use_of_moved_or_uninitialized. I am pretty sure that this function is only called if we know for sure that an error occurred. This shouldn't run in normal error free compilation.