-
-
Notifications
You must be signed in to change notification settings - Fork 14.7k
Fix 'assign to data in an index of' collection suggestions #152216
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
Open
GTimothy
wants to merge
9
commits into
rust-lang:main
Choose a base branch
from
GTimothy:map-diagnostics-fix
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
efb2ca0
Fix 'assign to data in an index of' collection suggestions
GTimothy 9b0864d
restore missing case, method call on value of map
GTimothy 5d997ef
rebless the ui tests
GTimothy 193b59c
second version
GTimothy 2e22102
fix: the same borrowed type (ignoring lifetime differences) for index…
GTimothy 7b1ebf1
remove unnecessary format! calls in favour of .to_string calls
GTimothy 062ab4f
add test for index mut suggestion causing borrow issue
GTimothy 1b13ca4
remove copy/clone check on type of index
GTimothy 9fea208
refactor documentation/comments
GTimothy 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
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,84 @@ | ||
| // When mutably indexing a type that implements `Index` but not `IndexMut`, a | ||
| // special 'help' message is added to the output. | ||
| // | ||
| // The suggestions have different requirements on the index type. | ||
| // map[&idx] = val; | ||
| // If idx is later used, it results in a borrow issue if both: | ||
| // - the the suggestion that was chosen requires the idx, not a ref to it. | ||
| // - idx's type is not Copy. | ||
| // | ||
| // For now, we suggest all options, regardless of the Copy-ness of the idx. | ||
| use std::collections::HashMap; | ||
|
|
||
| /// With a copy type, subsequent reuse of idx is not an issue. | ||
| fn copy_type() { | ||
| // ===&str===, a copy type. | ||
|
|
||
| let mut map = HashMap::<&str, u32>::new(); | ||
| // earlier, peter is initialised with 22. | ||
| // map["peter"] = 22; //ERROR | ||
| map.insert("peter", 22); | ||
|
|
||
| // at some point, if we get a &str variable peter again | ||
| let peter = "peter"; | ||
| // and we want to use it to update the map but still want to use it later? | ||
| // map[&peter] = 23; // ERROR | ||
| // we could insert again, and because &T are copy, we can use peter even if we use peter later. | ||
| map.insert(peter, 23); // WORKS | ||
| println!("my name is {peter}"); // WORKS because &str is Copy | ||
| // and we could use a &&str too in this case, because &str:Borrow<&str> (because T:Borrow<T>) | ||
| if let Some(val) = map.get_mut(&peter) { | ||
| *val = 23; | ||
| }; // WORKS | ||
| println!("my name is {peter}"); // WORKS because &str is Copy | ||
| // even a &str directly, (because rust auto borrows peter -> &peter ?) | ||
| if let Some(val) = map.get_mut(peter) { | ||
| *val = 24; | ||
| }; // WORKS | ||
| } | ||
|
|
||
| /// With a non-copy type, subsequent reuse of idx is an issue for `insert` and `entry`. | ||
| fn non_copy_type_insert() { | ||
| // ===STRING===, a non-copy type | ||
|
|
||
| let mut map = HashMap::<String, u32>::new(); | ||
| // earlier, peter is initialised with 22. | ||
| // map[&"peter".to_string()] = 22; // ERROR cannot assign | ||
| map.insert("peter".to_string(), 22); | ||
|
|
||
| // at some point, if we get a String variable peter again | ||
| let peter = "peter".to_string(); | ||
| // and we want to use it to update the map but still want to use it later? | ||
| // map[&peter] = 23; // ERROR cannot assign | ||
| // we could insert again, but we cannot use peter after. | ||
| map.insert(peter, 23); // WORKS | ||
| println!("my name is {peter}"); //~ ERROR: borrow of moved value: `peter` [E0382] | ||
| } | ||
|
|
||
| /// With a non-copy type, subsequent reuse of idx is not an issue for `get_mut`. | ||
| fn non_copy_type_get_mut() { | ||
| // ===STRING===, a non-copy type | ||
|
|
||
| let mut map = HashMap::<String, u32>::new(); | ||
| // earlier, peter is initialised with 22. | ||
| // map["peter".to_string()] = 22; // ERROR cannot assign | ||
| map.insert("peter".to_string(), 22); | ||
|
|
||
| // at some point, if we get a String variable peter again | ||
| let peter = "peter".to_string(); | ||
| // and we want to use it to update the map but still want to use it later? | ||
| // map[&peter] = 23; // ERROR cannot assign | ||
| // we can use a &String in this case, so get_mut is always fine. | ||
| if let Some(val) = map.get_mut(&peter) { | ||
| *val = 23; | ||
| }; // WORKS | ||
| println!("my name is {peter}"); // WORKS | ||
| // or a &str because String:Borrow<str>) and "peter" is &str. | ||
|
|
||
| if let Some(val) = map.get_mut("peter") { | ||
| *val = 24; | ||
| }; // WORKS | ||
| println!("my name is {peter}"); // WORKS | ||
| } | ||
|
|
||
| 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,19 @@ | ||
| error[E0382]: borrow of moved value: `peter` | ||
| --> $DIR/index-mut-suggest-borrow-issue.rs:55:27 | ||
| | | ||
| LL | let peter = "peter".to_string(); | ||
| | ----- move occurs because `peter` has type `String`, which does not implement the `Copy` trait | ||
| ... | ||
| LL | map.insert(peter, 23); // WORKS | ||
| | ----- value moved here | ||
| LL | println!("my name is {peter}"); | ||
| | ^^^^^ value borrowed here after move | ||
| | | ||
| help: consider cloning the value if the performance cost is acceptable | ||
| | | ||
| LL | map.insert(peter.clone(), 23); // WORKS | ||
| | ++++++++ | ||
|
|
||
| error: aborting due to 1 previous error | ||
|
|
||
| For more information about this error, try `rustc --explain E0382`. |
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
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.