- 
                Notifications
    You must be signed in to change notification settings 
- Fork 13.9k
Closed
Labels
A-async-closures`async || {}``async || {}`A-closuresArea: Closures (`|…| { … }`)Area: Closures (`|…| { … }`)A-inferenceArea: Type inferenceArea: Type inferenceC-bugCategory: This is a bug.Category: This is a bug.T-typesRelevant to the types team, which will review and decide on the PR/issue.Relevant to the types team, which will review and decide on the PR/issue.WG-asyncWorking group: Async & awaitWorking group: Async & await
Description
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
Metadata
Metadata
Assignees
Labels
A-async-closures`async || {}``async || {}`A-closuresArea: Closures (`|…| { … }`)Area: Closures (`|…| { … }`)A-inferenceArea: Type inferenceArea: Type inferenceC-bugCategory: This is a bug.Category: This is a bug.T-typesRelevant to the types team, which will review and decide on the PR/issue.Relevant to the types team, which will review and decide on the PR/issue.WG-asyncWorking group: Async & awaitWorking group: Async & await