Skip to content

E0562 should mention impl Trait in argument position #80476

Closed
@kpreid

Description

@kpreid

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-impl-traitArea: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch.C-bugCategory: This is a bug.D-incorrectDiagnostics: A diagnostic that is giving misleading or incorrect information.D-papercutDiagnostics: An error or lint that needs small tweaks.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions