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

E0562 should mention impl Trait in argument position #80476

Open
kpreid opened this issue Dec 29, 2020 · 0 comments
Open

E0562 should mention impl Trait in argument position #80476

kpreid opened this issue Dec 29, 2020 · 0 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-impl-trait Area: impl Trait. Universally / existentially quantified anonymous types with static dispatch. C-bug Category: This is a bug. D-incorrect A diagnostic that is giving misleading or incorrect information D-papercut Diagnostic error that needs small tweaks T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@kpreid
Copy link
Contributor

kpreid commented Dec 29, 2020

impl Trait as a type is permitted in function parameter lists. However, the error message E0562 incorrectly states that it is only permitted in function or inherent method return positions.

/// OK
fn a(x: impl Into<String>) {}
/// OK
fn b() -> impl Into<String> { "bees" }
/// Error
struct C {
    x: impl Into<String>,
}
error[E0562]: `impl Trait` not allowed outside of function and inherent method return types
 --> src/lib.rs:4:8
  |
4 |     x: impl Into<String>,
  |        ^^^^^^^^^^^^^^^^^

Being precise in this message will assist beginners (such as myself a few months ago) in understanding the boundaries of the language. Particularly, one might reasonably imagine equivalence between a function's parameter list and a single parameter whose type is a struct bundling several parameters, but impl Trait is available in the former and not the latter — whereas the error message implies that it is allowed in neither of those places.

Tangentially, it would be neat if the compiler gave suggestions to rewrite disallowed impl Trait as either an explicit parameter,struct C<S: impl Into<String>> { x: S }, or (especially in the case of impl Into) a constructor function, but that's much more work and more dependent on the context and perhaps even the specific trait.

Meta

This occurs on both stable 1.48.0 and nightly 1.50.0-nightly (2020-12-22 bb1fbbf84455fbad9afd), and the message is present in the same form in the current source code.

@kpreid kpreid added the C-bug Category: This is a bug. label Dec 29, 2020
@JohnTitor JohnTitor added A-diagnostics Area: Messages for errors, warnings, and lints A-impl-trait Area: impl Trait. Universally / existentially quantified anonymous types with static dispatch. D-incorrect A diagnostic that is giving misleading or incorrect information D-papercut Diagnostic error that needs small tweaks T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Dec 31, 2020
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. C-bug Category: This is a bug. D-incorrect A diagnostic that is giving misleading or incorrect information D-papercut Diagnostic error that needs small tweaks 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

2 participants