Skip to content

Inferred lifetimes in static trait method resolve to 'static, even though a local lifetime would suffice #69472

@Byron

Description

@Byron

With a static function sf on a trait implemented on struct S, then calling S::sf(&value) with value being (&str, …), the borrow checker will report that the string within the tuple doesn't live long enough. It behaves like it expects &str to be static, and indeed works only with &'static strings. However, if S::sf is made into a standalone function sf, it works as expected with non-static strings.

I tried to run cargo check on this code, which caused these error messages:

Screenshot 2020-02-26 at 08 02 37

Instead I expected the check to pass.

When commenting in this line and commenting out this line sf is used instead, and cargo check passes as expected.

How to reproduce

The following snippet takes 1min 10s to execute on a 12core laptop.

git clone https://github.com/crates-io/criner && \
cd criner && \
git checkout 2c6314397b347d4b3ddd4dcf6e3f0278bcaa447b && \
cargo check

Observations

The error message provided by the borrow checker is sparse compared to how helpful it usually is. Also I do suspect it is related to interactions of using references in static trait functions.

Meta

rustc --version --verbose:

rustc 1.41.0 (5e1a79984 2020-01-27)
binary: rustc
commit-hash: 5e1a799842ba6ed4a57e91f7ab9435947482f7d8
commit-date: 2020-01-27
host: x86_64-apple-darwin
release: 1.41.0
LLVM version: 9.0

Also tested on nightly - the issue persists:
rustc +nightly --version --verbose

rustc 1.43.0-nightly (834bc5650 2020-02-24)
binary: rustc
commit-hash: 834bc5650acf7019a53b409db68986857822812c
commit-date: 2020-02-24
host: x86_64-apple-darwin
release: 1.43.0-nightly
LLVM version: 9.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-inferenceArea: Type inferenceA-lifetimesArea: Lifetimes / regionsC-bugCategory: This is a bug.E-needs-mcveCall for participation: This issue has a repro, but needs a Minimal Complete and Verifiable ExampleT-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