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

Suggest using -> impl Trait when returning type parameter -> T where T: Trait #85991

Closed
estebank opened this issue Jun 4, 2021 · 2 comments
Closed
Assignees
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 D-newcomer-roadblock Confusing diagnostic error hard to understand for new users D-papercut Diagnostic error that needs small tweaks P-low Low priority T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@estebank
Copy link
Contributor

estebank commented Jun 4, 2021

Some newcomers are confused by impl Trait in return position because they have first internalized impl Trait as an alias for type parameters in argument position. Because of this, some people try the following:

trait Trait {}
impl Trait for () {}

// this works
fn foo() -> impl Trait {
    ()
}

// this doesn't
fn bar<T: Trait>() -> T {
    ()
}

We don't currently point them in the right direction, like we do in other cases.

Took inspiration from https://twitter.com/JaapRood/status/1400782159812628482

@estebank estebank added A-diagnostics Area: Messages for errors, warnings, and lints P-low Low priority 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-papercut Diagnostic error that needs small tweaks D-newcomer-roadblock Confusing diagnostic error hard to understand for new users labels Jun 4, 2021
@Nilstrieb
Copy link
Member

@rustbot claim

Nilstrieb added a commit to Nilstrieb/rust that referenced this issue Feb 18, 2022
Address rust-lang#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
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Feb 18, 2022
…, r=jackh726

Suggest `impl Trait` return type when incorrectly using a generic return type

Address rust-lang#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`
@jackh726
Copy link
Contributor

Should have been closed by #89892

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 D-newcomer-roadblock Confusing diagnostic error hard to understand for new users D-papercut Diagnostic error that needs small tweaks P-low Low priority 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

3 participants