summaryrefslogtreecommitdiffstats
path: root/tests/ui/closures/supertrait-hint-cycle.rs
blob: dbb06b2ef7a7a345861b9f040faa759a517f5f45 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// edition:2021
// check-pass

#![feature(type_alias_impl_trait)]
#![feature(closure_lifetime_binder)]

use std::future::Future;

trait AsyncFn<I, R>: FnMut(I) -> Self::Fut {
    type Fut: Future<Output = R>;
}

impl<F, I, R, Fut> AsyncFn<I, R> for F
where
    Fut: Future<Output = R>,
    F: FnMut(I) -> Fut,
{
    type Fut = Fut;
}

async fn call<C, R, F>(mut ctx: C, mut f: F) -> Result<R, ()>
where
    F: for<'a> AsyncFn<&'a mut C, Result<R, ()>>,
{
    loop {
        match f(&mut ctx).await {
            Ok(val) => return Ok(val),
            Err(_) => continue,
        }
    }
}

trait Cap<'a> {}
impl<T> Cap<'_> for T {}

fn works(ctx: &mut usize) {
    let mut inner = 0;

    type Ret<'a, 'b: 'a> = impl Future<Output = Result<usize, ()>> + 'a + Cap<'b>;

    let callback = for<'a, 'b> |c: &'a mut &'b mut usize| -> Ret<'a, 'b> {
        inner += 1;
        async move {
            let _c = c;
            Ok(1usize)
        }
    };
    call(ctx, callback);
}

fn doesnt_work_but_should(ctx: &mut usize) {
    let mut inner = 0;

    type Ret<'a, 'b: 'a> = impl Future<Output = Result<usize, ()>> + 'a + Cap<'b>;

    call(ctx, for<'a, 'b> |c: &'a mut &'b mut usize| -> Ret<'a, 'b> {
        inner += 1;
        async move {
            let _c = c;
            Ok(1usize)
        }
    });
}

fn main() {}