You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Async closure argument inference is broken with functions taking FnOnce() -> futures::TryFuture #127781
While the problem from first message in issue seems to be fixed, there are still cases when this fails.
One of examples is when using try_for_each instead of for_each, when closure is expected to return Result:
#![feature(async_closure)]use anyhow::Error;use futures::{stream::repeat_with,StreamExt,TryStreamExt};fnaccept_str(_:&str){}fnaccept_string(_:&String){}#[tokio::main(flavor = "current_thread")]asyncfnmain() -> Result<(),Error>{repeat_with(|| "foo".to_owned()).take(1).map(Result::<_,Error>::Ok).try_for_each(asyncmove |value| {// error on whole closure, rust thinks that type of value is `str`accept_str(&value);// type annotations needed, cannot infer typeaccept_str(value.as_str());// this worksaccept_string(&value);// okOk(())}).await?;Ok(())}
I expected to see this happen: All options used to work in past versions of rust
Instead, this happened: Without explicit type hint it fails
Version it worked on
It most recently worked on: Not 100% sure, some explanations have been made in original issue
We only extract an async closure bound from cases where there is Future<Output = ???> bound on the trait, which TryFuture is not. There isn't even a supertrait bound that constrains the output type.
I also do not expect this to ever be fixed, because that would require overly complicating the closure signature inference algorithm in ways that make me quite uncomfortable12. The correct solution is to rework the futures crate to have first-class async closure support, so for now I'd recommend just ascribing your type names. Sorry!
Footnotes
Firstly, this would break if another blanket implementation for TryFuture were ever added. ↩
Secondly, the combinator shouldn't really be using FnMut bounds either -- it should be using async FnMut bounds. ↩
Alternatively, rust-lang/futures-rs#2763 would fix this when paired with a slight adjustment to the closure signature inference that I'd be happy to make, but it seems like that PR is blocked.
changed the title [-]Async closure argument inference is broken with closures returning Result[/-][+]Async closure argument inference is broken with functions taking `futures::TryFuture`[/+]on Jul 16, 2024
Activity
compiler-errors commentedon Jul 15, 2024
This is because the signature of
try_for_eachis:We only extract an async closure bound from cases where there is
Future<Output = ???>bound on the trait, whichTryFutureis not. There isn't even a supertrait bound that constrains the output type.I also do not expect this to ever be fixed, because that would require overly complicating the closure signature inference algorithm in ways that make me quite uncomfortable12. The correct solution is to rework the futures crate to have first-class async closure support, so for now I'd recommend just ascribing your type names. Sorry!
Footnotes
Firstly, this would break if another blanket implementation for
TryFuturewere ever added. ↩Secondly, the combinator shouldn't really be using
FnMutbounds either -- it should be usingasync FnMutbounds. ↩compiler-errors commentedon Jul 15, 2024
Alternatively, rust-lang/futures-rs#2763 would fix this when paired with a slight adjustment to the closure signature inference that I'd be happy to make, but it seems like that PR is blocked.
[-]Async closure argument inference is broken with closures returning Result[/-][+]Async closure argument inference is broken with functions taking `futures::TryFuture`[/+]10 remaining items
[unstable-rust] Use `async_closure` syntax.
[unstable-rust] Use `async_closure` syntax.
[unstable-rust] Use `async_closure` syntax.
[unstable-rust] Use `feature(async_closure)`.
[unstable-rust] Use `feature(async_closure)`.
[unstable-rust] Use `feature(async_closure)`.
[unstable-rust] Use `feature(async_closure)`.
[unstable-rust] Use `feature(async_closure)`.
Rust 1.85: Use async closure syntax.
Rust 1.85: Use async closure syntax.
Rust 1.85: Use async closure syntax.
Rust 1.85: Use async closure syntax.
Rust 1.85: Use async closure syntax.
Rust 1.85: Use async closure syntax.
Rust 1.85: Use async closure syntax.