Skip to content

self param with ampersand (&) and explicit type yields noisy parsing diagnostics which could be more helpful #122086

Closed
@Veykril

Description

@Veykril

Code

struct S;

impl S {
    fn f(&self: Self) {}
}

Current output

Compiling playground v0.0.1 (/playground)
error: expected parameter name, found `:`
 --> src/lib.rs:4:15
  |
4 |     fn f(&self: Self) {}
  |               ^ expected parameter name

error: expected one of `)` or `,`, found `:`
 --> src/lib.rs:4:15
  |
4 |     fn f(&self: Self) {}
  |               ^
  |               |
  |               expected one of `)` or `,`
  |               help: missing `,`

error: could not compile `playground` (lib) due to 2 previous errors

Desired output

Compiling playground v0.0.1 (/playground)
error: explicit self parameter type cannot be declared with `&`
 --> src/lib.rs:4:15
  |
4 |     fn f(&self: Self) {}
  |          ^ use `self: &Self` instead

error: could not compile `playground` (lib) due to 1 previous errors

Rationale and extra context

No response

Other cases

No response

Rust Version

rustc 1.78.0-nightly (0ecbd0605 2024-02-25)
binary: rustc
commit-hash: 0ecbd0605770f45c9151715e66ba2b3cae367fcb
commit-date: 2024-02-25
host: x86_64-pc-windows-msvc
release: 1.78.0-nightly
LLVM version: 18.1.0

Anything else?

Desired output is rough, but something along those lines would probably be better.

Metadata

Metadata

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`D-confusingDiagnostics: Confusing error or lint that should be reworked.D-terseDiagnostics: An error or lint that doesn't give enough information about the problem at hand.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