Repository navigation
Open
Labels
A-async-awaitArea: Async & AwaitArea: Async & AwaitA-impl-traitArea: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch.Area: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch.AsyncAwait-TriagedAsync-await issues that have been triaged during a working group meeting.Async-await issues that have been triaged during a working group meeting.T-langRelevant to the language teamRelevant to the language team
Description
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?
Reacted by Anatoly Strashkevich, Aspen Smith, Esper Thomson, Sam Van Overmeire and YjyJeff
Metadata
Metadata
Assignees
Labels
A-async-awaitArea: Async & AwaitArea: Async & AwaitA-impl-traitArea: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch.Area: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch.AsyncAwait-TriagedAsync-await issues that have been triaged during a working group meeting.Async-await issues that have been triaged during a working group meeting.T-langRelevant to the language teamRelevant to the language team
Activity
Mark-Simulacrum commentedon Oct 18, 2019
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
nikomatsakis commentedon Oct 22, 2019
I think my expectation is that the explicit
type = impl Traitversion should work. I'm not 100% sure why it doesn't, but I can do some investigation.nikomatsakis commentedon Oct 22, 2019
Assigning to myself to investigate and document the cause of the issue, at least.
Arnavion commentedon Nov 16, 2019
Related to / dupe of #42940
glittershark commentedon Apr 14, 2021
I hate bumping issues but... any movement on this? Just ran into it myself in a very similar context
nikomatsakis commentedon Apr 19, 2021
No progress I know of
ranile commentedon Jun 19, 2022
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.SOF3 commentedon Sep 10, 2022
Reproduced with a more minimal example: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=319e66180308bede359b022cda8ed0bf
traviscross commentedon Mar 31, 2023
Here's the minimum
asyncexample of this problem:Playground link.
traviscross commentedon Mar 31, 2023
[-]impl Trait that does not capture generic argument[/-][+]Closure-like blocks capture all generic type and const parameters[/+]Dylan-DPC commentedon Nov 5, 2025
WaffleLapkin commentedon Nov 5, 2025
The output is unchanged actually. It's just that since #128440 you need to add an attribute to define taits:
[play]
[play]