Skip to content

Misleading error message with unnamed lifetimes #30790

@Amanieu

Description

@Amanieu
Member

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.

Activity

added
A-diagnosticsArea: Messages for errors, warnings, and lints
A-lifetimesArea: Lifetimes / regions
on Jan 9, 2016
GuillaumeGomez

GuillaumeGomez commented on Jan 9, 2016

@GuillaumeGomez
Member

If there is no lifetime specified, should we print:

A lifetime parameter is needed to bind `&Trait`

?

Amanieu

Amanieu commented on Jan 9, 2016

@Amanieu
MemberAuthor

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.

removed
T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
on Mar 9, 2017
estebank

estebank commented on Jan 27, 2018

@estebank
Contributor

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
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 / regionsC-enhancementCategory: An issue proposing an enhancement or a PR with one.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @steveklabnik@Amanieu@huonw@estebank@GuillaumeGomez

        Issue actions

          Misleading error message with unnamed lifetimes · Issue #30790 · rust-lang/rust