Skip to content

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

Open
@peterjoel

Description

@peterjoel
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.

Activity

added
A-diagnosticsArea: Messages for errors, warnings, and lints
on Apr 17, 2017
estebank

estebank commented on Mar 15, 2018

@estebank
Contributor

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

peterjoel commented on Apr 15, 2018

@peterjoel
ContributorAuthor

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

shepmaster commented on Apr 15, 2018

@shepmaster
Member

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

estebank commented on Sep 23, 2019

@estebank
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.

added
T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
A-lifetimesArea: Lifetimes / regions
on Sep 23, 2019
added a commit that references this issue on Oct 28, 2019

Rollup merge of rust-lang#65068 - estebank:trait-impl-lt-mismatch, r=…

05ff8db
added a commit that references this issue on Oct 29, 2019

Rollup merge of rust-lang#65068 - estebank:trait-impl-lt-mismatch, r=…

1437592
added a commit that references this issue on Oct 30, 2019

Rollup merge of rust-lang#65068 - estebank:trait-impl-lt-mismatch, r=…

ab9b789
added a commit that references this issue on Oct 30, 2019

Auto merge of #65068 - estebank:trait-impl-lt-mismatch, r=nikomatsakis

estebank

estebank commented on Nov 14, 2019

@estebank
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

estebank commented on Jun 10, 2020

@estebank
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
added
D-papercutDiagnostics: An error or lint that needs small tweaks.
on Jun 12, 2020
estebank

estebank commented on Jun 8, 2022

@estebank
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(&'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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-lifetimesArea: Lifetimes / regionsA-trait-systemArea: Trait systemC-enhancementCategory: An issue proposing an enhancement or a PR with one.D-papercutDiagnostics: An error or lint that needs small tweaks.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @peterjoel@shepmaster@TimNN@estebank@Mark-Simulacrum

        Issue actions

          Misleading error when a trait method implementation has explicit lifetime parameters but the trait signature does not. · Issue #41343 · rust-lang/rust