Allow lifetime polymorphism in local bindings
Nobody has claimed this yet.
- Dominant language
- Markdown
- Stars
- 6.6k
- Forks
- 1.7k
- Avg merge
- 16h 14m
- Merged PRs (30d)
- 1
Description
The following example doesn't compile in current Rust:
fn main() {
let id = |x: &i32| x;
{
let a: Box<i32> = Box::new(42);
id(&a);
}
{
let b: Box<i32> = Box::new(43);
id(&b);
}
}
This is because a and b have non-overlapping lifetimes, so id cannot be fixed to either of them.
The typical solution is to lift id into a fn, making the lifetime parameter polymorphic:
fn id<'a>(x: &'a i32) -> &'a i32 { x }
// ...
But then we lose the ability to close over the environment.
Ideally, I'd like the compiler to accept something like this:
let id = for<'a> |x: &'a i32| -> &'a i32 { x };
that is, a closure which is polymorphic over the lifetime rather than fixed to a particular one.
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the closure examples and lifetime explanation in the issue, then investigate how Rust represents closure lifetimes and environment captures. Determine the design and implementation scope for lifetime-polymorphic local closures. Done requires a settled approach and corresponding compiler or RFC work that supports the requested behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 30/100