-
-
Notifications
You must be signed in to change notification settings - Fork 14.9k
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
Merged
+592
−47
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
cb37077
Fix 'assign to data in an index of' collection suggestions
GTimothy f2bf6a8
rebless the ui tests
GTimothy 9de80d4
add index-mut test case where mut index access to a map produces E027…
GTimothy 5f1e962
fix: wrong assumption could cause panic
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,153 @@ | ||
| // When mutably indexing a type that implements `Index` but not `IndexMut`, a | ||
| // special 'help' message is added to the output. | ||
| // ... Except when it is not! | ||
| use std::borrow::Borrow; | ||
| use std::collections::HashMap; | ||
|
|
||
| #[derive(Hash, Eq, PartialEq)] | ||
| struct A {} | ||
| #[derive(Hash, Eq, PartialEq, Copy, Clone)] | ||
| struct ACopy {} | ||
| #[derive(Hash, Eq, PartialEq)] | ||
| struct B {} | ||
| #[derive(Hash, Eq, PartialEq)] | ||
| struct C {} | ||
| #[derive(Hash, Eq, PartialEq)] | ||
| struct D {} | ||
|
|
||
| impl Borrow<C> for D { | ||
| fn borrow(&self) -> &C { | ||
| &C {} | ||
| } | ||
| } | ||
|
|
||
| /// In this test the key is a A type. | ||
| fn test_a_index() { | ||
| let mut map = HashMap::<A, u32>::new(); | ||
|
|
||
| let index = &&&&&A {}; | ||
| // index gets autodereferenced | ||
| map[index] = 23; //~ ERROR E0594 | ||
| map[&&&&&&&&index] = 23; //~ ERROR E0594 | ||
| } | ||
| /// In this test the key is a A type. Auto-dereferencing to &A works but dereferencing to A need | ||
| /// the exact amount of `*` | ||
| fn test_a_2() { | ||
| let mut map = HashMap::<A, u32>::new(); | ||
|
|
||
| let index = &&&&&A {}; | ||
| // complete dereferencing needs to be exact | ||
| map.insert(*index, 23); //~ ERROR E0308 | ||
| // index gets autodereferenced | ||
| if let Some(val) = map.get_mut(index) { | ||
| *val = 23; | ||
| } // passes | ||
| } | ||
| /// In this test the key is a A type. Could not be merged with 2 because compiler only shows error | ||
| /// 0308 | ||
| fn test_a_3() { | ||
| let mut map = HashMap::<A, u32>::new(); | ||
|
|
||
| let index = &&&&&A {}; | ||
| // A does not implement Copy so a Clone might be required | ||
| map.insert(*****index, 23); //~ ERROR E0507 | ||
| } | ||
|
|
||
| /// In this test the key is a ACopy type. | ||
| fn test_acopy_index() { | ||
| let mut map = HashMap::<ACopy, u32>::new(); | ||
|
|
||
| let index = &&&&&ACopy {}; | ||
| // index gets autodereferenced | ||
| map[index] = 23; //~ ERROR E0594 | ||
| } | ||
| /// In this test the key is a ACopy type. Auto-dereferencing to &A works but dereferencing to A | ||
| /// need the exact amount of `*`. | ||
| fn test_acopy_2() { | ||
| let mut map = HashMap::<ACopy, u32>::new(); | ||
|
|
||
| let index = &&&&&ACopy {}; | ||
| // complete dereferencing needs to be exact | ||
| map.insert(*index, 23); //~ ERROR E0308 | ||
|
|
||
| // index gets autodereferenced | ||
| if let Some(val) = map.get_mut(index) { | ||
| *val = 23; | ||
| } // passes | ||
| } | ||
| /// In this test the key is a ACopy type. Could not be merged with 2 because compiler only shows | ||
| /// error 0308 | ||
| fn test_acopy_3() { | ||
| let mut map = HashMap::<ACopy, u32>::new(); | ||
|
|
||
| let index = &&&&&ACopy {}; | ||
| map.insert(*****index, 23); // no E057 error in this case because ACopy is Copy | ||
| } | ||
|
|
||
| /// In this test the key type is B-reference. The autodereferencing does not work in this case for | ||
| /// both for the map[index] part and `get_mut` call. | ||
| /// This leads to E0277 errors. | ||
| fn test_b() { | ||
| let mut map = HashMap::<&B, u32>::new(); | ||
|
|
||
| let index = &&&&&B {}; | ||
| // index does NOT get autorederefenced | ||
| map[index] = 23; //~ ERROR E0277 | ||
|
|
||
| // index does NOT get autorederefenced | ||
| if let Some(val) = map.get_mut(index) { //~ ERROR E0277 | ||
| *val = 23; | ||
| } | ||
| if let Some(val) = map.get_mut(***index) { | ||
| *val = 23; | ||
| } // passes | ||
| } | ||
|
|
||
|
|
||
| /// In this test the key type is C. | ||
| /// The `Borrow<C> for D` implementation changes nothing here, same error as for test_a. | ||
| fn test_c() { | ||
| let mut map = HashMap::<C, u32>::new(); | ||
|
|
||
| let index = &&&&&C {}; | ||
| // index gets autodereferenced | ||
| map[index] = 23; //~ ERROR E0594 | ||
|
|
||
| // index gets autodereferenced | ||
| if let Some(val) = map.get_mut(index) { | ||
| *val = 23; | ||
| } // passes | ||
| } | ||
|
|
||
| /// In this test the key type is D. The `Borrow<C> for D` implementation seems to prevent | ||
| /// autodereferencing. | ||
| /// The autodereferencing does not work in this case for both for the map[index] part and `get_mut` | ||
| /// call. | ||
| /// This leads to E0277 errors. | ||
| fn test_d() { | ||
| let mut map = HashMap::<D, u32>::new(); | ||
|
|
||
| let index = &&&&&D {}; | ||
| // index does NOT get autorederefenced | ||
| map[index] = 23; //~ ERROR E0277 | ||
|
|
||
| // index does NOT get autorederefenced | ||
| if let Some(val) = map.get_mut(index) {//~ ERROR E0277 | ||
| *val = 23; | ||
| } | ||
| if let Some(val) = map.get_mut(****index) { | ||
| *val = 23; | ||
| } // passes | ||
| } | ||
|
|
||
| fn main() { | ||
| test_a_index(); | ||
| test_a_2(); | ||
| test_a_3(); | ||
| test_acopy_index(); | ||
| test_acopy_2(); | ||
| test_acopy_3(); | ||
| test_b(); | ||
| test_c(); | ||
| test_d(); | ||
| } |
Oops, something went wrong.
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.