rust-lang / rust-lang/rust

E0106 "missing lifetime specifier" should be omitted when the signature is incorrect in an `impl` (E0186)

Open
#135,659 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-diagnostics T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Code
trait Foo {
    fn foo(&self) -> &str;
}

struct Example;
impl Foo for Example {
    fn foo() -> &str {
        "example"
    }
}
Current output
error[E0106]: missing lifetime specifier
 --> src/lib.rs:7:17
  |
7 |     fn foo() -> &str {
  |                 ^ expected named lifetime parameter
  |
  = help: this function's return type contains a borrowed value, but there is no value for it to be borrowed from
help: consider using the `'static` lifetime, but this is uncommon unless you're returning a borrowed value from a `const` or a `static`
  |
7 |     fn foo() -> &'static str {
  |                  +++++++
help: instead, you are more likely to want to return an owned value
  |
7 |     fn foo() -> String {
  |                 ~~~~~~

error[E0186]: method `foo` has a `&self` declaration in the trait, but not in the impl
 --> src/lib.rs:7:5
  |
2 |     fn foo(&self) -> &str;
  |     ---------------------- `&self` used in trait
...
7 |     fn foo() -> &str {
  |     ^^^^^^^^^^^^^^^^ expected `&self` in impl
Desired output
error[E0186]: method `foo` has a `&self` declaration in the trait, but not in the impl
 --> src/lib.rs:7:5
  |
2 |     fn foo(&self) -> &str;
  |     ---------------------- `&self` used in trait
...
7 |     fn foo() -> &str {
  |     ^^^^^^^^^^^^^^^^ expected `&self` in impl
Rationale and extra context

When the function is a trait associated function, the signature must match (or refine) the trait’s, so E0106 and any other errors that would propose changes to the signature are misleading or at least not nearly as relevant as E0186 (or E0050 or E0053 or any other such mismatch). Therefore, they should be hidden.

This is particularly important for situations involving a missing self parameter, because (in my personal experience) it’s easy to forget to write self in the parameter list.

Rust Version
1.84.0

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by compiling the provided Rust reproducer and compare the emitted E0106 and E0186 diagnostics. Trace the compiler diagnostic path for mismatched trait and impl method signatures; done means the misleading E0106 signature suggestions are omitted while the relevant mismatch diagnostic remains.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.