]> git.lizzy.rs Git - rust.git/blob - tests/ui/closures/supertrait-hint-cycle.rs
Rollup merge of #107700 - jyn514:tools-builder, r=Mark-Simulacrum
[rust.git] / tests / ui / closures / supertrait-hint-cycle.rs
1 // edition:2021
2 // check-pass
3
4 #![feature(type_alias_impl_trait)]
5 #![feature(closure_lifetime_binder)]
6
7 use std::future::Future;
8
9 trait AsyncFn<I, R>: FnMut(I) -> Self::Fut {
10     type Fut: Future<Output = R>;
11 }
12
13 impl<F, I, R, Fut> AsyncFn<I, R> for F
14 where
15     Fut: Future<Output = R>,
16     F: FnMut(I) -> Fut,
17 {
18     type Fut = Fut;
19 }
20
21 async fn call<C, R, F>(mut ctx: C, mut f: F) -> Result<R, ()>
22 where
23     F: for<'a> AsyncFn<&'a mut C, Result<R, ()>>,
24 {
25     loop {
26         match f(&mut ctx).await {
27             Ok(val) => return Ok(val),
28             Err(_) => continue,
29         }
30     }
31 }
32
33 trait Cap<'a> {}
34 impl<T> Cap<'_> for T {}
35
36 fn works(ctx: &mut usize) {
37     let mut inner = 0;
38
39     type Ret<'a, 'b: 'a> = impl Future<Output = Result<usize, ()>> + 'a + Cap<'b>;
40
41     let callback = for<'a, 'b> |c: &'a mut &'b mut usize| -> Ret<'a, 'b> {
42         inner += 1;
43         async move {
44             let _c = c;
45             Ok(1usize)
46         }
47     };
48     call(ctx, callback);
49 }
50
51 fn doesnt_work_but_should(ctx: &mut usize) {
52     let mut inner = 0;
53
54     type Ret<'a, 'b: 'a> = impl Future<Output = Result<usize, ()>> + 'a + Cap<'b>;
55
56     call(ctx, for<'a, 'b> |c: &'a mut &'b mut usize| -> Ret<'a, 'b> {
57         inner += 1;
58         async move {
59             let _c = c;
60             Ok(1usize)
61         }
62     });
63 }
64
65 fn main() {}