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

Unclear lifetime mismatch error with anonymous lifetimes #125143

Open
cyrgani opened this issue May 15, 2024 · 1 comment
Open

Unclear lifetime mismatch error with anonymous lifetimes #125143

cyrgani opened this issue May 15, 2024 · 1 comment
Assignees
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

@cyrgani
Copy link

cyrgani commented May 15, 2024

Code

struct X;

impl std::ops::Add<&X> for &X {
    type Output = X;

    fn add(self, _rhs: Self) -> Self::Output {
        X
    }
}

Current output

error[E0308]: method not compatible with trait
 --> src/main.rs:8:5
  |
8 |     fn add(self, _rhs: Self) -> Self::Output {
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ lifetime mismatch
  |
  = note: expected signature `fn(&X, &X) -> X`
             found signature `fn(&X, &X) -> X`
note: the anonymous lifetime as defined here...
 --> src/main.rs:5:20
  |
5 | impl std::ops::Add<&X> for &X {
  |                    ^
note: ...does not necessarily outlive the anonymous lifetime as defined here
 --> src/main.rs:5:20
  |
5 | impl std::ops::Add<&X> for &X {
  |                    ^

For more information about this error, try `rustc --explain E0308`.

Desired output

In the first part:

  = note: expected signature `fn(&X, &X) -> X`
             found signature `fn(&X, &X) -> X`

The different lifetimes should be shown since no difference is visible right now.

In the second part, one arrow should point at the right &X.

Rationale and extra context

No response

Other cases

When annotating the impl with two different lifetimes, the error message becomes more explanatory:

impl<'a, 'b> std::ops::Add<&'a X> for &'b X {
    type Output = X;

    fn add(self, _rhs: Self) -> Self::Output {
        X
    }
}

produces

error[E0308]: method not compatible with trait
  --> src/main.rs:16:5
   |
16 |     fn add(self, _rhs: Self) -> Self::Output {
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ lifetime mismatch
   |
   = note: expected signature `fn(&'b X, &'a X) -> X`
              found signature `fn(&'b X, &'b X) -> X`
note: the lifetime `'a` as defined here...
  --> src/main.rs:13:6
   |
13 | impl<'a, 'b> std::ops::Add<&'a X> for &'b X {
   |      ^^
note: ...does not necessarily outlive the lifetime `'b` as defined here
  --> src/main.rs:13:10
   |
13 | impl<'a, 'b> std::ops::Add<&'a X> for &'b X {
   |          ^^

For more information about this error, try `rustc --explain E0308`.

Rust Version

rustc 1.78.0 (9b00956e5 2024-04-29)
binary: rustc
commit-hash: 9b00956e56009bab2aa15d7bff10916599e3d6d6
commit-date: 2024-04-29
host: x86_64-unknown-linux-gnu
release: 1.78.0
LLVM version: 18.1.2

Anything else?

No response

@cyrgani cyrgani 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 May 15, 2024
@bovinebuddha
Copy link
Contributor

@rustbot claim

bovinebuddha added a commit to bovinebuddha/rust that referenced this issue May 17, 2024
 - More than one unnamed lifetimes gave incorrect spans when lookup
   was done by name (since they both had name "'_"). Use DefId instead
   to get correct spans.
bovinebuddha added a commit to bovinebuddha/rust that referenced this issue May 17, 2024
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

2 participants