diff options
| author | Matthias Krüger <matthias.krueger@famsik.de> | 2024-08-15 19:32:36 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-08-15 19:32:36 +0200 |
| commit | 53bf554de82d64daecf0cf2696cf4da367a96e69 (patch) | |
| tree | 7e9c4c5b2c9dcd61cc43a20d2ebd13644fd710df /tests | |
| parent | bb63d75ac1477eb361bfee764709f5d54e08cec8 (diff) | |
| parent | 4290943fb35b1e0472d6ee4888c5ab106abe1311 (diff) | |
| download | rust-53bf554de82d64daecf0cf2696cf4da367a96e69.tar.gz rust-53bf554de82d64daecf0cf2696cf4da367a96e69.zip | |
Rollup merge of #129072 - compiler-errors:more-powerful-async-closure-inference, r=lcnr
Infer async closure args from `Fn` bound even if there is no corresponding `Future` bound on return
In #127482, I implemented the functionality to infer an async closure signature when passed into a function that has `Fn` + `Future` where clauses that look like:
```
fn whatever(callback: F)
where
F: Fn(Arg) -> Fut,
Fut: Future<Output = Out>,
```
However, #127781 demonstrates that this is still incomplete to address the cases users care about. So let's not bail when we fail to find a `Future` bound, and try our best to just use the args from the `Fn` bound if we find it. This is *fine* since most users of closures only really care about the *argument* types for inference guidance, since we require the receiver of a `.` method call to be known in order to probe methods.
When I experimented with programmatically rewriting `|| async {}` to `async || {}` in #127827, this also seems to have fixed ~5000 regressions (probably all coming from usages `TryFuture`/`TryStream` from futures-rs): the [before](https://github.com/rust-lang/rust/pull/127827#issuecomment-2254061733) and [after](https://github.com/rust-lang/rust/pull/127827#issuecomment-2255470176) crater runs.
Fixes #127781.
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/ui/async-await/async-closures/sig-from-bare-fn.rs | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/tests/ui/async-await/async-closures/sig-from-bare-fn.rs b/tests/ui/async-await/async-closures/sig-from-bare-fn.rs new file mode 100644 index 00000000000..a679471a3b3 --- /dev/null +++ b/tests/ui/async-await/async-closures/sig-from-bare-fn.rs @@ -0,0 +1,49 @@ +//@ check-pass +//@ edition: 2021 + +// Make sure that we infer the args of an async closure even if it's passed to +// a function that requires the async closure implement `Fn*` but does *not* have +// a `Future` bound on the return type. + +#![feature(async_closure)] + +use std::future::Future; + +trait TryStream { + type Ok; + type Err; +} + +trait TryFuture { + type Ok; + type Err; +} + +impl<F, T, E> TryFuture for F where F: Future<Output = Result<T, E>> { + type Ok = T; + type Err = E; +} + +trait TryStreamExt: TryStream { + fn try_for_each<F, Fut>(&self, f: F) + where + F: FnMut(Self::Ok) -> Fut, + Fut: TryFuture<Ok = (), Err = Self::Err>; +} + +impl<S> TryStreamExt for S where S: TryStream { + fn try_for_each<F, Fut>(&self, f: F) + where + F: FnMut(Self::Ok) -> Fut, + Fut: TryFuture<Ok = (), Err = Self::Err>, + { } +} + +fn test(stream: impl TryStream<Ok = &'static str, Err = ()>) { + stream.try_for_each(async |s| { + s.trim(); // Make sure we know the type of `s` at this point. + Ok(()) + }); +} + +fn main() {} |
