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

Misleading error when a trait method implementation has explicit lifetime parameters but the trait signature does not. #41343

Open
peterjoel opened this issue Apr 17, 2017 · 7 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-lifetimes Area: lifetime related A-traits Area: Trait system C-enhancement Category: An issue proposing an enhancement or a PR with one. D-papercut Diagnostics: An error or lint that needs small tweaks. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@peterjoel
Copy link
Contributor

Consider this example (playground):

use std::vec::Vec;
use std::option::Option;

trait MyTrait {
    fn bar(&self, vec: &Vec<u32>) -> Option<&u32>;
}

struct Foo;

impl MyTrait for Foo {
    fn bar<'a>(&self, vec: &'a Vec<u32>) -> Option<&'a u32> {
        vec.get(0)
    }
}

The error message is:

rustc 1.18.0-nightly (7627e3d31 2017-04-16)
error[E0495]: cannot infer an appropriate lifetime for lifetime parameter 'a in generic type due to conflicting requirements
  --> <anon>:11:5
   |
11 |       fn bar<'a>(&self, vec: &'a Vec<u32>) -> Option<&'a u32> {
   |  _____^ starting here...
12 | |         vec.get(0)
13 | |     }
   | |_____^ ...ending here
   |
note: first, the lifetime cannot outlive the anonymous lifetime #2 defined on the body at 11:60...
  --> <anon>:11:61
   |
11 |       fn bar<'a>(&self, vec: &'a Vec<u32>) -> Option<&'a u32> {
   |  _____________________________________________________________^ starting here...
12 | |         vec.get(0)
13 | |     }
   | |_____^ ...ending here
note: ...so that method type is compatible with trait (expected fn(&Foo, &std::vec::Vec<u32>) -> std::option::Option<&u32>, found fn(&Foo, &std::vec::Vec<u32>) -> std::option::Option<&u32>)
  --> <anon>:11:5
   |
11 |       fn bar<'a>(&self, vec: &'a Vec<u32>) -> Option<&'a u32> {
   |  _____^ starting here...
12 | |         vec.get(0)
13 | |     }
   | |_____^ ...ending here
note: but, the lifetime must be valid for the anonymous lifetime #1 defined on the body at 11:60...
  --> <anon>:11:61
   |
11 |       fn bar<'a>(&self, vec: &'a Vec<u32>) -> Option<&'a u32> {
   |  _____________________________________________________________^ starting here...
12 | |         vec.get(0)
13 | |     }
   | |_____^ ...ending here
note: ...so that method type is compatible with trait (expected fn(&Foo, &std::vec::Vec<u32>) -> std::option::Option<&u32>, found fn(&Foo, &std::vec::Vec<u32>) -> std::option::Option<&u32>)
  --> <anon>:11:5
   |
11 |       fn bar<'a>(&self, vec: &'a Vec<u32>) -> Option<&'a u32> {
   |  _____^ starting here...
12 | |         vec.get(0)
13 | |     }
   | |_____^ ...ending here

error: aborting due to previous error

All of the text of the error is focussed on the body of the function, even though there are no lifetime problems in the body with respect to the signature of the surrounding function. The actual error is that the signatures of the trait method and its implementation do not match.

@TimNN TimNN added the A-diagnostics Area: Messages for errors, warnings, and lints label Apr 17, 2017
@Mark-Simulacrum Mark-Simulacrum added the C-enhancement Category: An issue proposing an enhancement or a PR with one. label Jul 26, 2017
@estebank
Copy link
Contributor

estebank commented Mar 15, 2018

Current output is still mistifying:

error[E0495]: cannot infer an appropriate lifetime for lifetime parameter 'a in generic type due to conflicting requirements
  --> src/main.rs:11:5
   |
11 |     fn bar<'a>(&self, vec: &'a Vec<u32>) -> Option<&'a u32> {
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
note: first, the lifetime cannot outlive the anonymous lifetime #2 defined on the method body at 11:5...
  --> src/main.rs:11:5
   |
11 | /     fn bar<'a>(&self, vec: &'a Vec<u32>) -> Option<&'a u32> {
12 | |         vec.get(0)
13 | |     }
   | |_____^
note: ...but the lifetime must also be valid for the anonymous lifetime #1 defined on the method body at 11:5...
  --> src/main.rs:11:5
   |
11 | /     fn bar<'a>(&self, vec: &'a Vec<u32>) -> Option<&'a u32> {
12 | |         vec.get(0)
13 | |     }
   | |_____^
   = note: ...so that the method type is compatible with trait:
           expected fn(&Foo, &std::vec::Vec<u32>) -> std::option::Option<&u32>
              found fn(&Foo, &std::vec::Vec<u32>) -> std::option::Option<&u32>

@peterjoel
Copy link
Contributor Author

The error from this Stack Overflow question might be related:
https://stackoverflow.com/questions/49844281/why-do-i-get-missing-lifetime-in-this-pice-of-code

OP is seeing an error with lifetimes but actually he has just got the trait signature wrong.

@shepmaster
Copy link
Member

shepmaster commented Apr 15, 2018

That OP has edited their question to no longer be meaningful to this discussion. This is the relevant revision:

use std::ops::Index;

enum J_List<T> {
    Cons(T, Box<J_List<T>>),
    Nil,
}

struct List<T> {
    value: J_List<T>,
}

impl<T> Index<usize> for List<T> {
    type Output = T;

    fn index(self, x: usize) -> &T {
        unimplemented!();
    }
}

@estebank
Copy link
Contributor

Triage: no change for the OP. For the case brought up in StackOverflow, the current output is

error[E0106]: missing lifetime specifier
  --> src/lib.rs:15:33
   |
15 |     fn index(self, x: usize) -> &T {
   |                                 ^ help: consider giving it an explicit bounded or 'static lifetime: `&'static`
   |
   = help: this function's return type contains a borrowed value with an elided lifetime, but the lifetime cannot be derived from the arguments

and if you follow the compiler advice you get

error[E0053]: method `index` has an incompatible type for trait
  --> src/lib.rs:15:5
   |
15 |     fn index(self, x: usize) -> &'static T {
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected &List<T>, found struct `List`
   |
   = note: expected type `fn(&List<T>, usize) -> &T`
              found type `fn(List<T>, usize) -> &'static T`

which nudges you in the right direction:

impl<T> Index<usize> for List<T> {
    type Output = T;

    fn index(&self, x: usize) -> &T {
        unimplemented!();
    }
}

The first message already mentions "this function's return type contains a borrowed value with an elided lifetime, but the lifetime cannot be derived from the arguments", which could use a rephrase, but tells you what's wrong here.

@estebank estebank added A-traits Area: Trait system T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. A-lifetimes Area: lifetime related labels Sep 23, 2019
JohnTitor added a commit to JohnTitor/rust that referenced this issue Oct 28, 2019
…nikomatsakis

Custom lifetime error for `impl` item doesn't conform to `trait`

Partly addresses rust-lang#42706, rust-lang#41343, fix rust-lang#40900.
Centril added a commit to Centril/rust that referenced this issue Oct 29, 2019
…nikomatsakis

Custom lifetime error for `impl` item doesn't conform to `trait`

Partly addresses rust-lang#42706, rust-lang#41343, fix rust-lang#40900.
tmandry added a commit to tmandry/rust that referenced this issue Oct 30, 2019
…nikomatsakis

Custom lifetime error for `impl` item doesn't conform to `trait`

Partly addresses rust-lang#42706, rust-lang#41343, fix rust-lang#40900.
bors added a commit that referenced this issue Oct 30, 2019
Custom lifetime error for `impl` item doesn't conform to `trait`

Partly addresses #42706, #41343, fix #40900.
@estebank
Copy link
Contributor

The current output still fails to differentiate the lifetimes involved:

error: `impl` item signature doesn't match `trait` item signature
  --> src/main.rs:11:5
   |
5  |     fn bar(&self, vec: &Vec<u32>) -> Option<&u32>;
   |     ---------------------------------------------- expected fn(&Foo, &std::vec::Vec<u32>) -> std::option::Option<&u32>
...
11 |     fn bar<'a>(&self, vec: &'a Vec<u32>) -> Option<&'a u32> {
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ found fn(&Foo, &std::vec::Vec<u32>) -> std::option::Option<&u32>
   |
   = note: expected `fn(&Foo, &std::vec::Vec<u32>) -> std::option::Option<&u32>`
              found `fn(&Foo, &std::vec::Vec<u32>) -> std::option::Option<&u32>`

@estebank
Copy link
Contributor

Current output:

error: `impl` item signature doesn't match `trait` item signature
  --> src/main.rs:11:5
   |
5  |     fn bar(&self, vec: &Vec<u32>) -> Option<&u32>;
   |     ---------------------------------------------- expected `fn(&Foo, &std::vec::Vec<u32>) -> std::option::Option<&u32>`
...
11 |     fn bar<'a>(&self, vec: &'a Vec<u32>) -> Option<&'a u32> {
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ found `fn(&Foo, &std::vec::Vec<u32>) -> std::option::Option<&u32>`
   |
   = note: expected `fn(&Foo, &std::vec::Vec<u32>) -> std::option::Option<&u32>`
              found `fn(&Foo, &std::vec::Vec<u32>) -> std::option::Option<&u32>`
   = help: the lifetime requirements from the `impl` do not correspond to the requirements in the `trait`
   = help: verify the lifetime relationships in the `trait` and `impl` between the `self` argument, the other inputs and its output

@estebank estebank added the D-papercut Diagnostics: An error or lint that needs small tweaks. label Jun 12, 2020
@estebank
Copy link
Contributor

estebank commented Jun 8, 2022

Current output:

error: `impl` item signature doesn't match `trait` item signature
  --> src/main.rs:11:5
   |
5  |     fn bar(&self, vec: &Vec<u32>) -> Option<&u32>;
   |     ---------------------------------------------- expected `fn(&'1 Foo, &'2 Vec<u32>) -> Option<&'1 u32>`
...
11 |     fn bar<'a>(&self, vec: &'a Vec<u32>) -> Option<&'a u32> {
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ found `fn(&'1 Foo, &'2 Vec<u32>) -> Option<&'2 u32>`
   |
   = note: expected `fn(&'1 Foo, &'2 Vec<u32>) -> Option<&'1 u32>`
              found `fn(&'1 Foo, &'2 Vec<u32>) -> Option<&'2 u32>`
   = help: the lifetime requirements from the `impl` do not correspond to the requirements in the `trait`
   = help: verify the lifetime relationships in the `trait` and `impl` between the `self` argument, the other inputs and its output

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 A-lifetimes Area: lifetime related A-traits Area: Trait system C-enhancement Category: An issue proposing an enhancement or a PR with one. D-papercut Diagnostics: An error or lint that needs small tweaks. 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

5 participants