forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of rust-lang#133292 - dianne:e0277-suggest-deref, r=este…
…bank E0277: suggest dereferencing function arguments in more cases This unifies and generalizes some of the logic in `TypeErrCtxt::suggest_dereferences` so that it will suggest dereferencing arguments to function/method calls in order to satisfy trait bounds in more cases. Previously it would only fire on reference types, and it had two separate cases (one specifically to get through custom `Deref` impls when passing by-reference, and one specifically to catch rust-lang#87437). I've based the new checks loosely on what's done for `E0308` in `FnCtxt::suggest_deref_or_ref`: it will suggest dereferences to satisfy trait bounds whenever the referent is `Copy`, is boxed (& so can be moved out of the boxes), or is being passed by reference. This doesn't make the suggestion fire in contexts other than function arguments or binary operators (which are in a separate case that this doesn't touch), and doesn't make it suggest a combination of `&`-removal and dereferences. Those would require a bit more restructuring, so I figured just doing this would be a decent first step. Closes rust-lang#90997
- Loading branch information
Showing
6 changed files
with
206 additions
and
113 deletions.
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`. |