Skip to content
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

Detect type parameters that were meant to be impl Trait in return position #78906

Closed
estebank opened this issue Nov 9, 2020 · 1 comment
Closed
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-impl-trait Area: impl Trait. Universally / existentially quantified anonymous types with static dispatch. A-suggestion-diagnostics Area: suggestions generated by the compiler applied by cargo fix A-typesystem Area: The type system D-newcomer-roadblock Confusing diagnostic error hard to understand for new users T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@estebank
Copy link
Contributor

estebank commented Nov 9, 2020

Given fn foo<T: Any>() -> T { "" } the output is:

error[E0308]: mismatched types
 --> src/main.rs:3:5
  |
2 | fn foo<T: Any>() -> T {
  |        -            - expected `T` because of return type
  |        |
  |        this type parameter
3 |     ""
  |     ^^ expected type parameter `T`, found `&str`
  |
  = note: expected type parameter `T`
                  found reference `&'static str`

We should be suggesting the use of impl Trait while explaining that the callers of foo will only be able to access whatever the trait bounds allow: fn foo() -> impl Any { "" }. We should also likely add an additional note for the special case of an Any bound letting people know that it is very unlikely what they want to do.

(Taken from a subset of https://users.rust-lang.org/t/how-can-we-return-a-anonymous-type-in-result-trait/51258)

@estebank estebank added A-typesystem Area: The type system A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. A-impl-trait Area: impl Trait. Universally / existentially quantified anonymous types with static dispatch. A-suggestion-diagnostics Area: suggestions generated by the compiler applied by cargo fix D-newcomer-roadblock Confusing diagnostic error hard to understand for new users labels Nov 9, 2020
@estebank
Copy link
Contributor Author

estebank commented Jan 7, 2023

Fixed:

error[E0308]: mismatched types
 --> src/main.rs:3:5
  |
2 | fn foo<T: Any>() -> T {
  |        -            -
  |        |            |
  |        |            expected `T` because of return type
  |        |            help: consider using an impl return type: `impl Any`
  |        this type parameter
3 |     ""
  |     ^^ expected type parameter `T`, found `&str`
  |
  = note: expected type parameter `T`
                  found reference `&'static str`

@estebank estebank closed this as completed Jan 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-impl-trait Area: impl Trait. Universally / existentially quantified anonymous types with static dispatch. A-suggestion-diagnostics Area: suggestions generated by the compiler applied by cargo fix A-typesystem Area: The type system D-newcomer-roadblock Confusing diagnostic error hard to understand for new users T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

1 participant