-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Suggest impl Trait
return type when incorrectly using a generic return type
#89892
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
Merged
Merged
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
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,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 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,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 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,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 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,30 @@ | ||
// run-rustfix | ||
|
||
trait Trait {} | ||
impl Trait for () {} | ||
|
||
// this works | ||
fn foo() -> impl Trait { | ||
() | ||
} | ||
|
||
fn bar<T: Trait + std::marker::Sync>() -> T | ||
Noratrieb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
where | ||
Noratrieb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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 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,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`. |
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.