Skip to content

Closure-like blocks capture all generic type and const parameters #65442

Open

Description

@jonhoo

I'd like to write the following code:

use std::future::Future;
struct Foo<A>(A);
impl<A> Foo<A> {
    fn bar<Q, R: Default>(&mut self, q: Q) -> impl Future<Output = R> {
        let _ = q;
        async move {
            R::default()
        }
    }
    
    fn baz(&mut self, x: &str) -> impl Future<Output = usize> {
        self.bar(x)
    }
}

In particular, I would like to have the impl Trait returned by baz not be tied to the lifetime of its &str argument. Since impl Trait captures the lifetimes of all generic arguments (as per RFC 1951), I can't write the code this way though. So instead, I tried

#![feature(type_alias_impl_trait)]
use std::future::Future;
struct Foo<A>(A);

type BarFut<A, R> = impl Future<Output = R>;
impl<A> Foo<A> {
    fn bar<Q, R: Default>(&mut self, q: Q) -> BarFut<A, R> {
        let _ = q;
        async move {
            R::default()
        }
    }
    
    fn baz(&mut self, x: &str) -> impl Future<Output = usize> {
        self.bar(x)
    }
}

However, with this, I get the error:

error: type parameter `Q` is part of concrete type but not used in parameter list for the `impl Trait` type alias

This seems odd, since Q is (intentionally) not used in the async block. I can work around this by adding an async fn and calling that instead of using async move, but that seems like an odd hack:

async fn make_r_fut<R: Default>() -> R {
    R::default()
}
// ...
    fn bar<Q, R: Default>(&mut self, q: Q) -> BarFut<A, R> {
        let _ = q;
        make_r_fut()
    }
// ...

Is it intentional that the async block "captures" Q here, even though it never contains a Q?

Activity

  1. added
    A-impl-traitArea: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch.
    T-langRelevant to the language team
    on Oct 15, 2019
  2. Mark-Simulacrum commented on Oct 18, 2019

    @Mark-Simulacrum
    Member

    I suspect the answer is no -- cc @rust-lang/wg-async-await -- this might contribute to "not Send/Sync" errors if we're capturing what we shouldn't be

  3. nikomatsakis commented on Oct 22, 2019

    @nikomatsakis
    Contributor

    I think my expectation is that the explicit type = impl Trait version should work. I'm not 100% sure why it doesn't, but I can do some investigation.

  4. added
    AsyncAwait-TriagedAsync-await issues that have been triaged during a working group meeting.
    on Oct 22, 2019
  5. self-assigned this
    on Oct 22, 2019
  6. nikomatsakis commented on Oct 22, 2019

    @nikomatsakis
    Contributor

    Assigning to myself to investigate and document the cause of the issue, at least.

  7. Arnavion commented on Nov 16, 2019

    @Arnavion
    Contributor

    Related to / dupe of #42940

  8. glittershark commented on Apr 14, 2021

    @glittershark
    Contributor

    I hate bumping issues but... any movement on this? Just ran into it myself in a very similar context

  9. nikomatsakis commented on Apr 19, 2021

    @nikomatsakis
    Contributor

    No progress I know of

  10. ranile commented on Jun 19, 2022

    @ranile
    Contributor

    Note that this bug does not require async to trigger. Playground where this triggered using type_alias_impl_trait. I'm not sure if/how this is related #42940 (as mentioned in the comment above). Looking through the discussion there, I couldn't find any mentions of the error returned by the compiler.

  11. SOF3 commented on Sep 10, 2022

    @SOF3
    Contributor

    Reproduced with a more minimal example: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=319e66180308bede359b022cda8ed0bf

    #![feature(type_alias_impl_trait)]
    
    type Ty = impl Fn();
    
    fn run<E>(_: E) -> Ty {
        || ()
    }
       Compiling playground v0.0.1 (/playground)
    error: type parameter `E` is part of concrete type but not used in parameter list for the `impl Trait` type alias
     --> src/lib.rs:6:5
      |
    6 |     || ()
      |     ^^^^^
    
    error: could not compile `playground` due to previous error```
    
  12. traviscross commented on Mar 31, 2023

    @traviscross
    Contributor

    Here's the minimum async example of this problem:

    #![feature(type_alias_impl_trait)]
    
    use std::future::Future;
    
    type Deferred = impl Future<Output = ()> + 'static;
    
    fn future_out<F>(_: F) -> Deferred {
        async move { }
    }
    error: type parameter `F` is part of concrete type but not used in parameter list for the `impl Trait` type alias
     --> src/lib.rs:8:5
      |
    8 |     async move { }
      |     ^^^^^^^^^^^^^^
    
    error: could not compile `playground` (lib) due to previous error
    

    Playground link.

  13. traviscross commented on Mar 31, 2023

    @traviscross
  14. changed the title [-]impl Trait that does not capture generic argument[/-] [+]Closure-like blocks capture all generic type and const parameters[/+] on Jun 14, 2024
  15. Dylan-DPC commented on Nov 5, 2025

    @Dylan-DPC
  16. WaffleLapkin commented on Nov 5, 2025

    @WaffleLapkin
    Member

    The output is unchanged actually. It's just that since #128440 you need to add an attribute to define taits:

    #![feature(type_alias_impl_trait)]
    
    type Ty = impl Fn();
    
    #[define_opaque(Ty)]
    fn run<E>(_: E) -> Ty {
        || ()
    }
    error: type parameter `E` is part of concrete type but not used in parameter list for the `impl Trait` type alias
     --> src/lib.rs:6:20
      |
    6 | fn run<E>(_: E) -> Ty {
      |                    ^^
    

    [play]


    #![feature(type_alias_impl_trait)]
    
    use std::future::Future;
    
    type Deferred = impl Future<Output = ()> + 'static;
    
    #[define_opaque(Deferred)]
    fn future_identity<F>(_: F) -> Deferred {
        async move { }
    }
    error: type parameter `F` is part of concrete type but not used in parameter list for the `impl Trait` type alias
     --> src/lib.rs:8:32
      |
    8 | fn future_identity<F>(_: F) -> Deferred {
      |                                ^^^^^^^^
    

    [play]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

A-async-awaitArea: Async & AwaitA-impl-traitArea: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch.AsyncAwait-TriagedAsync-await issues that have been triaged during a working group meeting.T-langRelevant to the language team

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions