Skip to content

Trait methods with implicit lifetime results  #116107

Open
@AmesCornish

Description

@AmesCornish

I had a problem getting the compiler to accept an implicit lifetime trait method implemented for an explicit lifetime object. @estebank encouraged me to submit this as a bug, though I did find a workaround.

After fixing a bunch of compiler errors in my code, I finally got to see "error: lifetime may not live long enough" for code like in this playground.
It would be nice if the compiler could figure out that the lifetimes here are actually OK (which I think they are), but it is complicated since the trait has no lifetime, the target implementation type does not have a lifetime, but the method result does have a lifetime.

I tried fixing with various hints, but the only fix that worked for me was to change the Trait to include a lifetime, which got rid of the error, as in this playground code. This is actually OK, but it meant that I had to do a whole bunch of code editing since I had to change the Trait method signature, and most callers had to be modified.

Meta

This behavior happens in both stable and nightly on the playground.

rustc --version --verbose:

rustc 1.72.1 (d5c2e9c34 2023-09-13)
binary: rustc
commit-hash: d5c2e9c342b358556da91d61ed4133f6f50fc0c3
commit-date: 2023-09-13
host: x86_64-unknown-linux-gnu
release: 1.72.1
LLVM version: 16.0.5

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-lifetimesArea: Lifetimes / regionsC-bugCategory: This is a bug.D-newcomer-roadblockDiagnostics: Confusing error or lint; hard to understand for new users.D-terseDiagnostics: An error or lint that doesn't give enough information about the problem at hand.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