-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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 #89892 - Nilstrieb:suggest-return-impl-trait, r=jackh726
Suggest `impl Trait` return type when incorrectly using a generic return type Address #85991 When there is a type mismatch error and the return type is generic, and that generic parameter is not used in the function parameters, suggest replacing that generic with the `impl Trait` syntax. r? `@estebank`
- 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`. |