Skip to content

Misleading error message with unnamed lifetimes #30790

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

Closed
Amanieu opened this issue Jan 9, 2016 · 3 comments
Closed

Misleading error message with unnamed lifetimes #30790

Amanieu opened this issue Jan 9, 2016 · 3 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-lifetimes Area: Lifetimes / regions C-enhancement Category: An issue proposing an enhancement or a PR with one.

Comments

@Amanieu
Copy link
Member

Amanieu commented Jan 9, 2016

This code results in a horrible error message:

use std::ops::Deref;
trait Trait {}

struct Struct;

impl Deref for Struct {
    type Target = Trait;
    fn deref(&self) -> &Trait {
        unimplemented!();
    }
}
<anon>:8:5: 10:6 error: method `deref` has an incompatible type for trait:
 expected bound lifetime parameter ,
    found concrete lifetime [E0053]
<anon>: 8     fn deref(&self) -> &Trait {
<anon>: 9         unimplemented!();
<anon>:10     }
<anon>:8:5: 10:6 help: see the detailed explanation for E0053

In particular, note the missing lifetime name on the second line of the error message.

@huonw huonw added A-diagnostics Area: Messages for errors, warnings, and lints A-lifetimes Area: Lifetimes / regions labels Jan 9, 2016
@GuillaumeGomez
Copy link
Member

If there is no lifetime specified, should we print:

A lifetime parameter is needed to bind `&Trait`

?

@Amanieu
Copy link
Member Author

Amanieu commented Jan 9, 2016

The correct code in this case is to specify the return value of the function as &(Trait + 'static). What happens here is that Target = Trait gets expanded by the compiler into Target = Trait + 'static, but the error message doesn't make this clear.

@steveklabnik steveklabnik removed the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Mar 9, 2017
@Mark-Simulacrum Mark-Simulacrum added the C-enhancement Category: An issue proposing an enhancement or a PR with one. label Jul 24, 2017
@estebank
Copy link
Contributor

estebank commented Jan 27, 2018

Proposed output in #47791:

error[E0495]: cannot infer an appropriate lifetime for lifetime parameter in generic type due to conflicting requirements
  --> $DIR/mismatched_trait_impl-2.rs:18:5
   |
18 | /     fn deref(&self) -> &Trait {
19 | |         unimplemented!();
20 | |     }
   | |_____^
   |
note: first, the lifetime cannot outlive the anonymous lifetime #1 defined on the method body at 18:5...
  --> $DIR/mismatched_trait_impl-2.rs:18:5
   |
18 | /     fn deref(&self) -> &Trait {
19 | |         unimplemented!();
20 | |     }
   | |_____^
   = note: ...but the lifetime must also be valid for the static lifetime...
   = note: ...so that the method type is compatible with trait:
           expected fn(&Struct) -> &Trait + 'static
              found fn(&Struct) -> &Trait

bors added a commit that referenced this issue Feb 3, 2018
Tweak presentation on lifetime trait mismatch

 - On trait/impl method discrepancy, add label pointing at trait signature.
 - Point only at method definition when referring to named lifetimes on lifetime mismatch.
 - When the sub and sup expectations are the same, tweak the output to avoid repeated spans.

Fix #30790, CC #18759.
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 C-enhancement Category: An issue proposing an enhancement or a PR with one.
Projects
None yet
Development

No branches or pull requests

6 participants