Skip to content

Closures needlessly capture all generic parameters from parent item in TAIT #118116

Open
@jwong101

Description

@jwong101

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-closuresArea: Closures (`|…| { … }`)A-impl-traitArea: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch.C-enhancementCategory: An issue proposing an enhancement or a PR with one.T-typesRelevant to the types 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