Open
Description
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.
Metadata
Metadata
Assignees
Labels
Type
Projects
Milestone
Relationships
Development
No branches or pull requests
Activity
char
for pointing at defs #41214estebank commentedon Mar 15, 2018
Current output is still mistifying:
peterjoel commentedon Apr 15, 2018
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 commentedon Apr 15, 2018
That OP has edited their question to no longer be meaningful to this discussion. This is the relevant revision:
estebank commentedon Sep 23, 2019
Triage: no change for the OP. For the case brought up in StackOverflow, the current output is
and if you follow the compiler advice you get
which nudges you in the right direction:
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.
impl
item doesn't conform totrait
#65068Rollup merge of rust-lang#65068 - estebank:trait-impl-lt-mismatch, r=…
Rollup merge of rust-lang#65068 - estebank:trait-impl-lt-mismatch, r=…
Rollup merge of rust-lang#65068 - estebank:trait-impl-lt-mismatch, r=…
Auto merge of #65068 - estebank:trait-impl-lt-mismatch, r=nikomatsakis
estebank commentedon Nov 14, 2019
The current output still fails to differentiate the lifetimes involved:
RegionKind::ReVar
lifetimes #67460estebank commentedon Jun 10, 2020
Current output:
estebank commentedon Jun 8, 2022
Current output: