rust-lang / rust-lang/rust

Suboptimal interaction between lifetime oriented and value oriented type system in the presence of closures.

Open
#125,250 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-lifetimes C-discussion S-has-mcve T-lang T-types
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

This code compiles with the comments and does not compile without the comments.

fn main() {
    fn subparsing_lifetime<'s, 'r>(args: &'s Vec<&'s str>) -> (&'r ()/*, &'s ()*/) { unreachable!() }
    let closure = |args| subparsing_lifetime(&args);
}

It runs in the compiler error "cannot return value referencing function parameter".

This highlights, to me, suboptimal interaction between the lifetime oriented type system and the value oriented type system.

The typing guarantees that the function parameter is not referenced in the values.

Moreover, there are use cases, bumpalo arenas, for instance, where it is perfectly legitimate to have lifetimes shared between arguments and returned values, with no dependencies between one and the other.

So this code should compile if uncommented.

One of the interest I have in rust is precisely the ability to share lifetimes between independent references.

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

Start by compiling the minimal Rust closure example exactly as shown, both with and without the commented return reference. Investigate how closure parameter and return lifetimes are inferred, including the stated bumpalo arena use case; done means establishing whether the uncommented version should compile and documenting or implementing the required compiler behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.