rust-lang / rust-lang/rust

Error on bounds for unused lifetime

Open
#130,014 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-lifetimes C-bug P-low regression-from-stable-to-stable T-types
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

The following fails to build:

struct Foo<'a, 'b: 'a, 'c: 'a>(&'a mut &'a (), &'b (), &'c ());
fn with_foo<'b>(_f: impl FnOnce(Foo<'_, 'b, '_>)) {}

struct Bar;
impl Bar {
    fn baz<'a>(&'a self, _foo: Foo<'a, '_, '_>) {}

    fn call_baz<'b>(&'b self) {
        with_foo::<'b>(|foo| self.baz(foo))
    }
}
error[E0521]: borrowed data escapes outside of method
 --> src/lib.rs:9:30
  |
8 |     fn call_baz<'b>(&'b self) {
  |                 --  -------- `self` is a reference that is only valid in the method body
  |                 |
  |                 lifetime `'b` defined here
9 |         with_foo::<'b>(|foo| self.baz(foo))
  |                              ^^^^^^^^^^^^^
  |                              |
  |                              `self` escapes the method body here
  |                              argument requires that `'b` must outlive `'static`

For more information about this error, try `rustc --explain E0521`.
error: could not compile `playground` (lib) due to 1 previous error

However, it works if the bounds on 'c argument on the declaration of Foo are changed to either 'c: 'b, or simply an unbounded lifetime 'c. This seems unexpected because 'c: 'b is a stricter bound than 'c: 'a (and 'c is a weaker bound than 'c: 'a). It's even stranger because the 'c lifetime is unused in this example.

This seems to be either a bug, unexpected limitation, or as another user suggested, an NLL regression. I created a post on URLO that has some further discussion.

Meta

rustc --version --verbose:

rustc 1.81.0 (eeb90cda1 2024-09-04)
binary: rustc
commit-hash: eeb90cda1969383f56a2637cbd3037bdf598841c
commit-date: 2024-09-04
host: x86_64-unknown-linux-gnu
release: 1.81.0
LLVM version: 18.1.7

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the failing lifetime example shown in src/lib.rs using the reported rustc 1.81.0 toolchain, then compare it with the variants where the 'c bound is changed or removed. Trace the compiler's handling of the unused lifetime and determine whether the borrow-checking result is incorrect; done means a justified compiler fix or a documented expected limitation with regression coverage.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.