Skip to content

Error message for inferred lifetime failure isn't great #40900

Closed
Listed in
@jdm

Description

@jdm
Contributor

This code does not work:

use std::borrow::Borrow;

struct S;
trait T {}
impl T for S {}

impl Borrow<T> for S {
    fn borrow(&self) -> &T {
        self as &T
    }
}

fn main() {
    let s = S;
    let _t: &T = s.borrow();
}

The error message I get is:

error[E0495]: cannot infer an appropriate lifetime for lifetime parameter in generic type due to conflicting requirements
  --> <anon>:8:5
   |
8  |       fn borrow(&self) -> &T {
   |  _____^ starting here...
9  | |         self as &T
10 | |     }
   | |_____^ ...ending here
   |
note: first, the lifetime cannot outlive the anonymous lifetime #1 defined on the body at 8:27...
  --> <anon>:8:28
   |
8  |       fn borrow(&self) -> &T {
   |  ____________________________^ starting here...
9  | |         self as &T
10 | |     }
   | |_____^ ...ending here
note: ...so that method type is compatible with trait (expected fn(&S) -> &T + 'static, found fn(&S) -> &T)
  --> <anon>:8:5
   |
8  |       fn borrow(&self) -> &T {
   |  _____^ starting here...
9  | |         self as &T
10 | |     }
   | |_____^ ...ending here
   = note: but, the lifetime must be valid for the static lifetime...
note: ...so that method type is compatible with trait (expected fn(&S) -> &T + 'static, found fn(&S) -> &T)
  --> <anon>:8:5
   |
8  |       fn borrow(&self) -> &T {
   |  _____^ starting here...
9  | |         self as &T
10 | |     }
   | |_____^ ...ending here

error: aborting due to previous error

This code does work:

use std::borrow::Borrow;

struct S;
trait T {}
impl T for S {}

impl<'a> Borrow<T + 'a> for S {
    fn borrow(&self) -> &(T + 'a) {
        self as &T
    }
}

fn main() {
    let s = S;
    let _t: &T = s.borrow();
}

The only clue I got was this note:

note: ...so that method type is compatible with trait (expected fn(&S) -> &T + 'static, found fn(&S) -> &T)

which even then I wasn't clear if my change would make a difference or not. Is there some change we could make to the output that would make the solution clearer?

Activity

added
A-diagnosticsArea: Messages for errors, warnings, and lints
A-lifetimesArea: Lifetimes / regions
on Mar 29, 2017
estebank

estebank commented on Feb 10, 2018

@estebank
Contributor

Current output:

error[E0495]: cannot infer an appropriate lifetime for lifetime parameter in generic type due to conflicting requirements
 --> src/main.rs:8:5
  |
8 |     fn borrow(&self) -> &T {
  |     ^^^^^^^^^^^^^^^^^^^^^^
  |
note: first, the lifetime cannot outlive the anonymous lifetime #1 defined on the method body at 8:5...
 --> src/main.rs:8:5
  |
8 | /     fn borrow(&self) -> &T {
9 | |         self as &T
10| |     }
  | |_____^
  = note: ...but the lifetime must also be valid for the static lifetime...
  = note: ...so that the method type is compatible with trait:
          expected fn(&S) -> &T + 'static
             found fn(&S) -> &T
estebank

estebank commented on Sep 23, 2019

@estebank
Contributor

Triage: no change other than &T -> &dyn T.

added
A-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`
T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
on Oct 2, 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

0b7e28a
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-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`C-enhancementCategory: An issue proposing an enhancement or a PR with one.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

      Participants

      @jdm@estebank@Mark-Simulacrum

      Issue actions

        Error message for inferred lifetime failure isn't great · Issue #40900 · rust-lang/rust