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

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

Closed
Tracked by #42516
jdm opened this issue Mar 29, 2017 · 2 comments · Fixed by #65068
Closed
Tracked by #42516

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

jdm opened this issue Mar 29, 2017 · 2 comments · Fixed by #65068
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-lifetimes Area: Lifetimes / regions A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@jdm
Copy link
Contributor

jdm commented Mar 29, 2017

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?

@jdm jdm added A-diagnostics Area: Messages for errors, warnings, and lints A-lifetimes Area: Lifetimes / regions labels Mar 29, 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

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
Copy link
Contributor

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

@estebank estebank added A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Oct 2, 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 bors closed this as completed in 0b7e28a Oct 30, 2019
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: Lifetimes / regions A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants