Skip to content

Handle ADTs with implicit lifetime params resulting a lifetime mismatch in fn definition #40990

Description

@TomasTomecek

I have this function:

use git2::Reference;
fn resolve_symbolic_reference(&self, reference: Option<Reference>) -> Option<Reference> {
    return reference;
    // here's more logic, this is just shortened for clarity
}

Here's the source of the struct.

And I get this from compiler:

error[E0308]: mismatched types
  --> src/backend.rs:71:16
   |
71 |         return reference;
   |                ^^^^^^^^^ lifetime mismatch
   |
   = note: expected type `std::option::Option<git2::Reference<'_>>`
              found type `std::option::Option<git2::Reference<'_>>`
note: the anonymous lifetime #2 defined on the body at 70:92...
  --> src/backend.rs:70:93
   |
70 |     fn resolve_symbolic_reference(&self, reference: Option<Reference>) -> Option<Reference> {
   |                                                                                             ^
note: ...does not necessarily outlive the anonymous lifetime #1 defined on the body at 70:92
  --> src/backend.rs:70:93
   |
70 |     fn resolve_symbolic_reference(&self, reference: Option<Reference>) -> Option<Reference> {
   |                                                                                             ^

error: aborting due to previous error

First of all: I don't understand what's wrong with my code: isn't the lifetime of the function argument problem of the caller of my function?
Second: the compiler's error message is not helpful, especially:

   = note: expected type `std::option::Option<git2::Reference<'_>>`
              found type `std::option::Option<git2::Reference<'_>>`

I run rust unstable installed via rustup:

$ rustc 1.17.0-nightly (0aeb9c129 2017-03-15)
binary: rustc
commit-hash: 0aeb9c12979e6da753701a798d04105b6b1a8c28
commit-date: 2017-03-15
host: x86_64-unknown-linux-gnu
release: 1.17.0-nightly
LLVM version: 3.9

I have also reported this to the library's upstream.

Activity

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.D-newcomer-roadblockDiagnostics: Confusing error or lint; hard to understand for new users.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

      Issue actions