summaryrefslogtreecommitdiffstats
path: root/tests/ui/const-generics/generic_const_exprs/issue-82268.rs
blob: d08fc5beb75f616fed697e31f2eb6e843e0a37f3 (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
66
67
68
69
70
71
72
73
// build-pass

#![allow(incomplete_features)]
#![feature(generic_const_exprs)]

trait Collate<Op> {
    type Pass;
    type Fail;

    fn collate(self) -> (Self::Pass, Self::Fail);
}

impl<Op> Collate<Op> for () {
    type Pass = ();
    type Fail = ();

    fn collate(self) -> ((), ()) {
        ((), ())
    }
}

trait CollateStep<X, Prev> {
    type Pass;
    type Fail;
    fn collate_step(x: X, prev: Prev) -> (Self::Pass, Self::Fail);
}

impl<X, P, F> CollateStep<X, (P, F)> for () {
    type Pass = (X, P);
    type Fail = F;

    fn collate_step(x: X, (p, f): (P, F)) -> ((X, P), F) {
        ((x, p), f)
    }
}

struct CollateOpImpl<const MASK: u32>;
trait CollateOpStep {
    type NextOp;
    type Apply;
}

impl<const MASK: u32> CollateOpStep for CollateOpImpl<MASK>
where
    CollateOpImpl<{ MASK >> 1 }>: Sized,
{
    type NextOp = CollateOpImpl<{ MASK >> 1 }>;
    type Apply = ();
}

impl<H, T, Op: CollateOpStep> Collate<Op> for (H, T)
where
    T: Collate<Op::NextOp>,
    Op::Apply: CollateStep<H, (T::Pass, T::Fail)>,
{
    type Pass = <Op::Apply as CollateStep<H, (T::Pass, T::Fail)>>::Pass;
    type Fail = <Op::Apply as CollateStep<H, (T::Pass, T::Fail)>>::Fail;

    fn collate(self) -> (Self::Pass, Self::Fail) {
        <Op::Apply as CollateStep<H, (T::Pass, T::Fail)>>::collate_step(self.0, self.1.collate())
    }
}

fn collate<X, const MASK: u32>(x: X) -> (X::Pass, X::Fail)
where
    X: Collate<CollateOpImpl<MASK>>,
{
    x.collate()
}

fn main() {
    dbg!(collate::<_, 5>(("Hello", (42, ('!', ())))));
}