-
-
Notifications
You must be signed in to change notification settings - Fork 14.8k
Fix deref field pattern suggestions and improve error messages #154543
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
chenyukang
wants to merge
1
commit into
rust-lang:main
Choose a base branch
from
chenyukang:yukang-fix-146995-deref-field-suggestion
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
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
40 changes: 40 additions & 0 deletions
40
tests/ui/borrowck/deref-field-pattern-ref-suggestion-issue-146995.fixed
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 @@ | ||
| //@ run-rustfix | ||
| #![allow(dead_code)] | ||
|
|
||
| use std::ops::{Deref, DerefMut}; | ||
|
|
||
| fn take(mut wrap: Wrap<Struct>) { | ||
| if let Some(ref mut val) = wrap.field { | ||
| //~^ ERROR cannot move out of dereference of `Wrap<Struct>` | ||
| val.0 = (); | ||
| } | ||
| } | ||
|
|
||
| fn take_mut_ref_base(mut wrap: Wrap<Struct>) { | ||
| if let Some(ref mut val) = (&mut wrap).field { | ||
| //~^ ERROR cannot move out of dereference of `Wrap<Struct>` | ||
| val.0 = (); | ||
| } | ||
| } | ||
|
|
||
| struct Wrap<T>(T); | ||
| struct Struct { | ||
| field: Option<NonCopy>, | ||
| } | ||
| struct NonCopy(()); | ||
|
|
||
| impl<T> Deref for Wrap<T> { | ||
| type Target = T; | ||
|
|
||
| fn deref(&self) -> &Self::Target { | ||
| &self.0 | ||
| } | ||
| } | ||
|
|
||
| impl<T> DerefMut for Wrap<T> { | ||
| fn deref_mut(&mut self) -> &mut Self::Target { | ||
| &mut self.0 | ||
| } | ||
| } | ||
|
|
||
| fn main() {} |
40 changes: 40 additions & 0 deletions
40
tests/ui/borrowck/deref-field-pattern-ref-suggestion-issue-146995.rs
|
chenyukang marked this conversation as resolved.
|
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 @@ | ||
| //@ run-rustfix | ||
| #![allow(dead_code)] | ||
|
|
||
| use std::ops::{Deref, DerefMut}; | ||
|
|
||
| fn take(mut wrap: Wrap<Struct>) { | ||
| if let Some(mut val) = wrap.field { | ||
| //~^ ERROR cannot move out of dereference of `Wrap<Struct>` | ||
| val.0 = (); | ||
| } | ||
| } | ||
|
|
||
| fn take_mut_ref_base(mut wrap: Wrap<Struct>) { | ||
| if let Some(mut val) = (&mut wrap).field { | ||
| //~^ ERROR cannot move out of dereference of `Wrap<Struct>` | ||
| val.0 = (); | ||
| } | ||
| } | ||
|
|
||
| struct Wrap<T>(T); | ||
| struct Struct { | ||
| field: Option<NonCopy>, | ||
| } | ||
| struct NonCopy(()); | ||
|
|
||
| impl<T> Deref for Wrap<T> { | ||
| type Target = T; | ||
|
|
||
| fn deref(&self) -> &Self::Target { | ||
| &self.0 | ||
| } | ||
| } | ||
|
|
||
| impl<T> DerefMut for Wrap<T> { | ||
| fn deref_mut(&mut self) -> &mut Self::Target { | ||
| &mut self.0 | ||
| } | ||
| } | ||
|
|
||
| fn main() {} |
29 changes: 29 additions & 0 deletions
29
tests/ui/borrowck/deref-field-pattern-ref-suggestion-issue-146995.stderr
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,29 @@ | ||
| error[E0507]: cannot move out of dereference of `Wrap<Struct>` | ||
| --> $DIR/deref-field-pattern-ref-suggestion-issue-146995.rs:7:28 | ||
| | | ||
| LL | if let Some(mut val) = wrap.field { | ||
| | ------- ^^^^^^^^^^ | ||
| | | | ||
| | data moved here because `val` has type `NonCopy`, which does not implement the `Copy` trait | ||
| | | ||
| help: consider borrowing the pattern binding | ||
| | | ||
| LL | if let Some(ref mut val) = wrap.field { | ||
| | +++ | ||
|
|
||
| error[E0507]: cannot move out of dereference of `Wrap<Struct>` | ||
| --> $DIR/deref-field-pattern-ref-suggestion-issue-146995.rs:14:28 | ||
| | | ||
| LL | if let Some(mut val) = (&mut wrap).field { | ||
| | ------- ^^^^^^^^^^^^^^^^^ | ||
| | | | ||
| | data moved here because `val` has type `NonCopy`, which does not implement the `Copy` trait | ||
| | | ||
| help: consider borrowing the pattern binding | ||
| | | ||
| LL | if let Some(ref mut val) = (&mut wrap).field { | ||
| | +++ | ||
|
|
||
| error: aborting due to 2 previous errors | ||
|
|
||
| For more information about this error, try `rustc --explain E0507`. |
37 changes: 37 additions & 0 deletions
37
tests/ui/borrowck/deref-index-pattern-ref-suggestion.fixed
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,37 @@ | ||
| //@ run-rustfix | ||
| #![allow(dead_code)] | ||
|
|
||
| use std::ops::{Deref, DerefMut}; | ||
|
|
||
| fn take(mut wrap: Wrap<[Option<NonCopy>; 1]>) { | ||
| if let Some(ref mut val) = wrap[0] { | ||
| //~^ ERROR cannot move out of type `[Option<NonCopy>; 1]`, a non-copy array | ||
| val.0 = (); | ||
| } | ||
| } | ||
|
|
||
| fn take_mut_ref_base(mut wrap: Wrap<[Option<NonCopy>; 1]>) { | ||
| if let Some(ref mut val) = (&mut wrap)[0] { | ||
| //~^ ERROR cannot move out of type `[Option<NonCopy>; 1]`, a non-copy array | ||
| val.0 = (); | ||
| } | ||
| } | ||
|
|
||
| struct Wrap<T>(T); | ||
| struct NonCopy(()); | ||
|
|
||
| impl<T> Deref for Wrap<T> { | ||
| type Target = T; | ||
|
|
||
| fn deref(&self) -> &Self::Target { | ||
| &self.0 | ||
| } | ||
| } | ||
|
|
||
| impl<T> DerefMut for Wrap<T> { | ||
| fn deref_mut(&mut self) -> &mut Self::Target { | ||
| &mut self.0 | ||
| } | ||
| } | ||
|
|
||
| fn main() {} |
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.