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

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

Open
kpreid opened this issue Jan 17, 2025 · 0 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@kpreid
Copy link
Contributor

kpreid commented Jan 17, 2025

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
@kpreid kpreid added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jan 17, 2025
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 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

1 participant