Closures needlessly capture all generic parameters from parent item in TAIT
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 119k
- Forks
- 16.2k
- PR merge metrics
- PR metrics pending
Description
I tried this code:
#![feature(type_alias_impl_trait)]
type Tait = impl Sized + 'static;
fn capture<T>() -> Tait {
|| 0u8
}
I expected to see this happen:
Successful compilation, since the generic parameter T is not used in the returned closure.
Instead, this happened:
Compiling playground v0.0.1 (/playground)
error: type parameter `T` is part of concrete type but not used in parameter list for the `impl Trait` type alias
--> src/lib.rs:6:5
|
6 | || 0u8
| ^^^^^^
This error also happens when returning async blocks:
// same error
fn capture_async<T>() -> Tait {
async {
0u8
}
}
Also, this appears to only affect generic type parameters, as lifetimes aren't captured if the closure doesn't use them:
// compiles
fn capture_lt<'a>(_: &'a ()) -> Tait {
|| 0u8
}
Meta
Version:
rustc 1.76.0-nightly (2023-11-19 9a66e4471f71283fd54d)
Contributor guide
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 reproducer in src/lib.rs using type_alias_impl_trait, then compare the closure and async-block cases with the lifetime example. Done means the generic parameter is not captured when unused and the examples compile successfully, with regression coverage for the reported behavior.
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
- 42/100