-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
E0277: suggest dereferencing function arguments in more cases #133292
Open
dianne
wants to merge
1
commit into
rust-lang:master
Choose a base branch
from
dianne:e0277-suggest-deref
base: master
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.
+206
−113
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 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
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,37 @@ | ||
//@ run-rustfix | ||
//! diagnostic test for #90997. | ||
//! test that E0277 suggests dereferences to satisfy bounds when the referent is `Copy` or boxed. | ||
use std::ops::Deref; | ||
|
||
trait Test { | ||
fn test(self); | ||
} | ||
fn consume_test(x: impl Test) { x.test() } | ||
|
||
impl Test for u32 { | ||
fn test(self) {} | ||
} | ||
struct MyRef(u32); | ||
impl Deref for MyRef { | ||
type Target = u32; | ||
fn deref(&self) -> &Self::Target { | ||
&self.0 | ||
} | ||
} | ||
|
||
struct NonCopy; | ||
impl Test for NonCopy { | ||
fn test(self) {} | ||
} | ||
|
||
fn main() { | ||
let my_ref = MyRef(0); | ||
consume_test(*my_ref); | ||
//~^ ERROR the trait bound `MyRef: Test` is not satisfied | ||
//~| SUGGESTION * | ||
|
||
let nested_box = Box::new(Box::new(Box::new(NonCopy))); | ||
consume_test(***nested_box); | ||
//~^ ERROR the trait bound `Box<Box<Box<NonCopy>>>: Test` is not satisfied | ||
//~| SUGGESTION *** | ||
} |
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,37 @@ | ||
//@ run-rustfix | ||
//! diagnostic test for #90997. | ||
//! test that E0277 suggests dereferences to satisfy bounds when the referent is `Copy` or boxed. | ||
use std::ops::Deref; | ||
|
||
trait Test { | ||
fn test(self); | ||
} | ||
fn consume_test(x: impl Test) { x.test() } | ||
|
||
impl Test for u32 { | ||
fn test(self) {} | ||
} | ||
struct MyRef(u32); | ||
impl Deref for MyRef { | ||
type Target = u32; | ||
fn deref(&self) -> &Self::Target { | ||
&self.0 | ||
} | ||
} | ||
|
||
struct NonCopy; | ||
impl Test for NonCopy { | ||
fn test(self) {} | ||
} | ||
|
||
fn main() { | ||
let my_ref = MyRef(0); | ||
consume_test(my_ref); | ||
//~^ ERROR the trait bound `MyRef: Test` is not satisfied | ||
//~| SUGGESTION * | ||
|
||
let nested_box = Box::new(Box::new(Box::new(NonCopy))); | ||
consume_test(nested_box); | ||
//~^ ERROR the trait bound `Box<Box<Box<NonCopy>>>: Test` is not satisfied | ||
//~| SUGGESTION *** | ||
} |
39 changes: 39 additions & 0 deletions
39
tests/ui/traits/suggest-dereferences/deref-argument.stderr
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,39 @@ | ||
error[E0277]: the trait bound `MyRef: Test` is not satisfied | ||
--> $DIR/deref-argument.rs:29:18 | ||
| | ||
LL | consume_test(my_ref); | ||
| ------------ ^^^^^^ the trait `Test` is not implemented for `MyRef` | ||
| | | ||
| required by a bound introduced by this call | ||
| | ||
note: required by a bound in `consume_test` | ||
--> $DIR/deref-argument.rs:9:25 | ||
| | ||
LL | fn consume_test(x: impl Test) { x.test() } | ||
| ^^^^ required by this bound in `consume_test` | ||
help: consider dereferencing here | ||
| | ||
LL | consume_test(*my_ref); | ||
| + | ||
|
||
error[E0277]: the trait bound `Box<Box<Box<NonCopy>>>: Test` is not satisfied | ||
--> $DIR/deref-argument.rs:34:18 | ||
| | ||
LL | consume_test(nested_box); | ||
| ------------ ^^^^^^^^^^ the trait `Test` is not implemented for `Box<Box<Box<NonCopy>>>` | ||
| | | ||
| required by a bound introduced by this call | ||
| | ||
note: required by a bound in `consume_test` | ||
--> $DIR/deref-argument.rs:9:25 | ||
| | ||
LL | fn consume_test(x: impl Test) { x.test() } | ||
| ^^^^ required by this bound in `consume_test` | ||
help: consider dereferencing here | ||
| | ||
LL | consume_test(***nested_box); | ||
| +++ | ||
|
||
error: aborting due to 2 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0277`. |
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.
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
ty.is_numeric()
case is admittedly a hack fortests/ui/traits/suggest-dereferences/suggest-dereferencing-receiver-argument.rs
, since it wants a deref suggestion for an&{integer}
. I think a slightly more principled (still hacky) way to do this could be to walk throughbase_ty
and addCopy
bounds to theParamEnv
for any numeric inference vars. It should maybe also downgrade the suggestion toMaybeIncorrect
if any type inference vars are present.{integer}: Sized
does appear to be provable already, at least.