Skip to content

Provide a structured suggestion for _-as-parameter-type in trait impls #95097

Closed
@scottmcm

Description

@scottmcm

Given the following code: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=91ba3d382d71310c4e6239fc5d4da92f

struct Foo;

impl From<Vec<Vec<Vec<i32>>>> for Foo {
    fn from(x: _) -> Self { Self }
}

The current output is:

error[E0121]: the placeholder `_` is not allowed within types on item signatures for functions
 --> src/lib.rs:4:16
  |
4 |     fn from(x: _) -> Self { Self }
  |                ^ not allowed in type signatures
  |
help: use type parameters instead
  |
4 |     fn from<T>(x: T) -> Self { Self }
  |            +++    ~

That's a particularly-bad help in a trait impl, since the associated function is not generic, and thus adding a generic parameter definitely won't work.

Ideally the output should look like:

error[E0121]: the placeholder `_` is not allowed within types on item signatures for functions
 --> src/lib.rs:4:16
  |
4 |     fn from(x: _) -> Self { Self }
  |                ^ not allowed in type signatures
  |
help: use the type required by the trait instead
  |
4 |     fn from(x: Vec<Vec<Vec<i32>>>) -> Self { Self }
  |                ~~~~~~~~~~~~~~~~~~

So that, like happens with -> _, I can just accept the structured suggestion instead of needing to type out the long type again.

Metadata

Metadata

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsT-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