-
Notifications
You must be signed in to change notification settings - Fork 13.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Address #85991 Suggest the `impl Trait` return type syntax if the user tried to return a generic parameter and we get a type mismatch The suggestion is not emitted if the param appears in the function parameters, and only get the bounds that actually involve `T: ` directly It also checks whether the generic param is contained in any where bound (where it isn't the self type), and if one is found (like `Option<T>: Send`), it is not suggested. This also adds `TyS::contains`, which recursively vistits the type and looks if the other type is contained anywhere
- Loading branch information
Showing
7 changed files
with
321 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
trait Trait {} | ||
impl Trait for () {} | ||
|
||
fn bad_echo<T>(_t: T) -> T { | ||
"this should not suggest impl Trait" //~ ERROR mismatched types | ||
} | ||
|
||
fn bad_echo_2<T: Trait>(_t: T) -> T { | ||
"this will not suggest it, because that would probably be wrong" //~ ERROR mismatched types | ||
} | ||
|
||
fn other_bounds_bad<T>() -> T | ||
where | ||
T: Send, | ||
Option<T>: Send, | ||
{ | ||
"don't suggest this, because Option<T> places additional constraints" //~ ERROR mismatched types | ||
} | ||
|
||
// FIXME: implement this check | ||
trait GenericTrait<T> {} | ||
|
||
fn used_in_trait<T>() -> T | ||
where | ||
T: Send, | ||
(): GenericTrait<T>, | ||
{ | ||
"don't suggest this, because the generic param is used in the bound." //~ ERROR mismatched types | ||
} | ||
|
||
fn main() {} |
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,59 @@ | ||
error[E0308]: mismatched types | ||
--> $DIR/return-impl-trait-bad.rs:5:5 | ||
| | ||
LL | fn bad_echo<T>(_t: T) -> T { | ||
| - - expected `T` because of return type | ||
| | | ||
| this type parameter | ||
LL | "this should not suggest impl Trait" | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected type parameter `T`, found `&str` | ||
| | ||
= note: expected type parameter `T` | ||
found reference `&'static str` | ||
|
||
error[E0308]: mismatched types | ||
--> $DIR/return-impl-trait-bad.rs:9:5 | ||
| | ||
LL | fn bad_echo_2<T: Trait>(_t: T) -> T { | ||
| - - expected `T` because of return type | ||
| | | ||
| this type parameter | ||
LL | "this will not suggest it, because that would probably be wrong" | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected type parameter `T`, found `&str` | ||
| | ||
= note: expected type parameter `T` | ||
found reference `&'static str` | ||
|
||
error[E0308]: mismatched types | ||
--> $DIR/return-impl-trait-bad.rs:17:5 | ||
| | ||
LL | fn other_bounds_bad<T>() -> T | ||
| - - expected `T` because of return type | ||
| | | ||
| this type parameter | ||
... | ||
LL | "don't suggest this, because Option<T> places additional constraints" | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected type parameter `T`, found `&str` | ||
| | ||
= note: expected type parameter `T` | ||
found reference `&'static str` | ||
|
||
error[E0308]: mismatched types | ||
--> $DIR/return-impl-trait-bad.rs:28:5 | ||
| | ||
LL | fn used_in_trait<T>() -> T | ||
| - - | ||
| | | | ||
| | expected `T` because of return type | ||
| | help: consider using an impl return type: `impl Send` | ||
| this type parameter | ||
... | ||
LL | "don't suggest this, because the generic param is used in the bound." | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected type parameter `T`, found `&str` | ||
| | ||
= note: expected type parameter `T` | ||
found reference `&'static str` | ||
|
||
error: aborting due to 4 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0308`. |
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,30 @@ | ||
// run-rustfix | ||
|
||
trait Trait {} | ||
impl Trait for () {} | ||
|
||
// this works | ||
fn foo() -> impl Trait { | ||
() | ||
} | ||
|
||
fn bar<T: Trait + std::marker::Sync>() -> impl Trait + std::marker::Sync + Send | ||
where | ||
T: Send, | ||
{ | ||
() //~ ERROR mismatched types | ||
} | ||
|
||
fn other_bounds<T>() -> impl Trait | ||
where | ||
T: Trait, | ||
Vec<usize>: Clone, | ||
{ | ||
() //~ ERROR mismatched types | ||
} | ||
|
||
fn main() { | ||
foo(); | ||
bar::<()>(); | ||
other_bounds::<()>(); | ||
} |
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,30 @@ | ||
// run-rustfix | ||
|
||
trait Trait {} | ||
impl Trait for () {} | ||
|
||
// this works | ||
fn foo() -> impl Trait { | ||
() | ||
} | ||
|
||
fn bar<T: Trait + std::marker::Sync>() -> T | ||
where | ||
T: Send, | ||
{ | ||
() //~ ERROR mismatched types | ||
} | ||
|
||
fn other_bounds<T>() -> T | ||
where | ||
T: Trait, | ||
Vec<usize>: Clone, | ||
{ | ||
() //~ ERROR mismatched types | ||
} | ||
|
||
fn main() { | ||
foo(); | ||
bar::<()>(); | ||
other_bounds::<()>(); | ||
} |
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,34 @@ | ||
error[E0308]: mismatched types | ||
--> $DIR/return-impl-trait.rs:15:5 | ||
| | ||
LL | fn bar<T: Trait + std::marker::Sync>() -> T | ||
| - - | ||
| | | | ||
| | expected `T` because of return type | ||
| this type parameter help: consider using an impl return type: `impl Trait + std::marker::Sync + Send` | ||
... | ||
LL | () | ||
| ^^ expected type parameter `T`, found `()` | ||
| | ||
= note: expected type parameter `T` | ||
found unit type `()` | ||
|
||
error[E0308]: mismatched types | ||
--> $DIR/return-impl-trait.rs:23:5 | ||
| | ||
LL | fn other_bounds<T>() -> T | ||
| - - | ||
| | | | ||
| | expected `T` because of return type | ||
| | help: consider using an impl return type: `impl Trait` | ||
| this type parameter | ||
... | ||
LL | () | ||
| ^^ expected type parameter `T`, found `()` | ||
| | ||
= note: expected type parameter `T` | ||
found unit type `()` | ||
|
||
error: aborting due to 2 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0308`. |