summaryrefslogtreecommitdiffstats
path: root/src/test/ui/associated-type-bounds/auxiliary/fn-aux.rs
blob: 0ea23ad1dbffa7ed4903aabb680dca2c0d9f5008 (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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// Traits:

pub trait Alpha {
    fn alpha(self) -> usize;
}

pub trait Beta {
    type Gamma;
    fn gamma(self) -> Self::Gamma;
}

pub trait Delta {
    fn delta(self) -> usize;
}

pub trait Epsilon<'a> {
    type Zeta;
    fn zeta(&'a self) -> Self::Zeta;

    fn epsilon(&'a self) -> usize;
}

pub trait Eta {
    fn eta(self) -> usize;
}

// Assertions:

pub fn assert_alpha<T: Alpha>(x: T) -> usize { x.alpha() }
pub fn assert_static<T: 'static>(_: T) -> usize { 24 }
pub fn assert_delta<T: Delta>(x: T) -> usize { x.delta() }
pub fn assert_epsilon_specific<'a, T: 'a + Epsilon<'a>>(x: &'a T) -> usize { x.epsilon() }
pub fn assert_epsilon_forall<T: for<'a> Epsilon<'a>>() {}
pub fn assert_forall_epsilon_zeta_satisfies_eta<T>(x: T) -> usize
where
    T: for<'a> Epsilon<'a>,
    for<'a> <T as Epsilon<'a>>::Zeta: Eta,
{
    x.epsilon() + x.zeta().eta()
}

// Implementations and types:

#[derive(Copy, Clone)]
pub struct BetaType;

#[derive(Copy, Clone)]
pub struct GammaType;

#[derive(Copy, Clone)]
pub struct ZetaType;

impl Beta for BetaType {
    type Gamma = GammaType;
    fn gamma(self) -> Self::Gamma { GammaType }
}

impl<'a> Beta for &'a BetaType {
    type Gamma = GammaType;
    fn gamma(self) -> Self::Gamma { GammaType }
}

impl Beta for GammaType {
    type Gamma = Self;
    fn gamma(self) -> Self::Gamma { self }
}

impl Alpha for GammaType {
    fn alpha(self) -> usize { 42 }
}

impl Delta for GammaType {
    fn delta(self) -> usize { 1337 }
}

impl<'a> Epsilon<'a> for GammaType {
    type Zeta = ZetaType;
    fn zeta(&'a self) -> Self::Zeta { ZetaType }

    fn epsilon(&'a self) -> usize { 7331 }
}

impl Eta for ZetaType {
    fn eta(self) -> usize { 7 }
}

// Desugared forms to check against:

pub fn desugared_bound<B>(beta: B) -> usize
where
    B: Beta,
    B::Gamma: Alpha
{
    let gamma: B::Gamma = beta.gamma();
    assert_alpha::<B::Gamma>(gamma)
}

pub fn desugared_bound_region<B>(beta: B) -> usize
where
    B: Beta,
    B::Gamma: 'static,
{
    assert_static::<B::Gamma>(beta.gamma())
}

pub fn desugared_bound_multi<B>(beta: B) -> usize
where
    B: Copy + Beta,
    B::Gamma: Alpha + 'static + Delta,
{
    assert_alpha::<B::Gamma>(beta.gamma()) +
    assert_static::<B::Gamma>(beta.gamma()) +
    assert_delta::<B::Gamma>(beta.gamma())
}

pub fn desugared_bound_region_specific<'a, B>(gamma: &'a B::Gamma) -> usize
where
    B: Beta,
    B::Gamma: 'a + Epsilon<'a>,
{
    assert_epsilon_specific::<B::Gamma>(gamma)
}

pub fn desugared_bound_region_forall<B>(beta: B) -> usize
where
    B: Beta,
    B::Gamma: Copy + for<'a> Epsilon<'a>,
{
    assert_epsilon_forall::<B::Gamma>();
    let g1: B::Gamma = beta.gamma();
    let g2: B::Gamma = g1;
    assert_epsilon_specific::<B::Gamma>(&g1) +
    assert_epsilon_specific::<B::Gamma>(&g2)
}

pub fn desugared_bound_region_forall2<B>(beta: B) -> usize
where
    B: Beta,
    B::Gamma: Copy + for<'a> Epsilon<'a>,
    for<'a> <B::Gamma as Epsilon<'a>>::Zeta: Eta,
{
    let gamma = beta.gamma();
    assert_forall_epsilon_zeta_satisfies_eta::<B::Gamma>(gamma)
}

pub fn desugared_contraint_region_forall<B>(beta: B) -> usize
where
    for<'a> &'a B: Beta,
    for<'a> <&'a B as Beta>::Gamma: Alpha,
{
    let g1 = beta.gamma();
    let g2 = beta.gamma();
    assert_alpha(g1) + assert_alpha(g2)
}

pub fn desugared_bound_nested<B>(beta: B) -> usize
where
    B: Beta,
    B::Gamma: Copy + Alpha + Beta,
    <B::Gamma as Beta>::Gamma: Delta,
{
    let go = beta.gamma();
    let gi = go.gamma();
    go.alpha() + gi.delta()
}

pub fn desugared() {
    let beta = BetaType;
    let gamma = beta.gamma();

    assert_eq!(42, desugared_bound(beta));
    assert_eq!(24, desugared_bound_region(beta));
    assert_eq!(42 + 24 + 1337, desugared_bound_multi(beta));
    assert_eq!(7331, desugared_bound_region_specific::<BetaType>(&gamma));
    assert_eq!(7331 * 2, desugared_bound_region_forall(beta));
    assert_eq!(42 + 1337, desugared_bound_nested(beta));
}