Async closure argument inference is broken with functions taking FnOnce() -> futures::TryFuture · Issue #127781 · rust-lang/rust (original) (raw)

I'm writing this as a continuation of #127425

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};

fn accept_str(: &str) {} fn accept_string(: &String) {}

#[tokio::main(flavor = "current_thread")] async fn main() -> Result<(), Error> {

repeat_with(|| "foo".to_owned())
    .take(1)
    .map(Result::<_, Error>::Ok)
    .try_for_each(async move |value| {
        // error on whole closure, rust thinks that type of value is `str`
        accept_str(&value);

        // type annotations needed, cannot infer type
        accept_str(value.as_str());

        // this works
        accept_string(&value); // ok
        
        Ok(())
    })
    .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

Version with regression

rustc 1.81.0-nightly (d9284afea 2024-07-14)
binary: rustc
commit-hash: d9284afea99e0969a0e692b9e9fd61ea4ba21366
commit-date: 2024-07-14
host: x86_64-pc-windows-msvc
release: 1.81.0-nightly
LLVM version: 18.1.7