summaryrefslogtreecommitdiff
path: root/tests/src/loops.rs
blob: 8a558b9dc2c601c3070336fe727870af49f806d5 (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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
//@ [coq] aeneas-args=-use-fuel
//@ [fstar] aeneas-args=-decreases-clauses
//@ [fstar] aeneas-args=-split-files
//@ [coq,fstar] subdir=misc
use std::vec::Vec;

/// No borrows
pub fn sum(max: u32) -> u32 {
    let mut i = 0;
    let mut s = 0;
    while i < max {
        s += i;
        i += 1;
    }

    s *= 2;
    s
}

/// Same as [sum], but we use borrows in order tocreate loans inside a loop
/// iteration, and those borrows will have to be ended by the end of the
/// iteration.
pub fn sum_with_mut_borrows(max: u32) -> u32 {
    let mut i = 0;
    let mut s = 0;
    while i < max {
        let ms = &mut s;
        *ms += i;
        let mi = &mut i;
        *mi += 1;
    }

    s *= 2;
    s
}

/// Similar to [sum_with_mut_borrows].
pub fn sum_with_shared_borrows(max: u32) -> u32 {
    let mut i = 0;
    let mut s = 0;
    while i < max {
        i += 1;
        // We changed the order compared to [sum_with_mut_borrows]:
        // we want to have a shared borrow surviving until the end
        // of the iteration.
        let mi = &i;
        s += *mi;
    }

    s *= 2;
    s
}

pub fn sum_array<const N: usize>(a: [u32; N]) -> u32 {
    let mut i = 0;
    let mut s = 0;
    while i < N {
        s += a[i];
        i += 1;
    }
    s
}

/// This case is interesting, because the fixed point for the loop doesn't
/// introduce new abstractions.
pub fn clear(v: &mut Vec<u32>) {
    let mut i = 0;
    while i < v.len() {
        v[i] = 0;
        i += 1;
    }
}

pub enum List<T> {
    Cons(T, Box<List<T>>),
    Nil,
}

/// The parameter `x` is a borrow on purpose
pub fn list_mem(x: &u32, mut ls: &List<u32>) -> bool {
    while let List::Cons(y, tl) = ls {
        if *y == *x {
            return true;
        } else {
            ls = tl;
        }
    }
    false
}

/// Same as [list_nth_mut] but with a loop
pub fn list_nth_mut_loop<T>(mut ls: &mut List<T>, mut i: u32) -> &mut T {
    while let List::Cons(x, tl) = ls {
        if i == 0 {
            return x;
        } else {
            ls = tl;
            i -= 1;
        }
    }
    panic!()
}

/// Same as [list_nth_mut_loop] but with shared borrows
pub fn list_nth_shared_loop<T>(mut ls: &List<T>, mut i: u32) -> &T {
    while let List::Cons(x, tl) = ls {
        if i == 0 {
            return x;
        } else {
            ls = tl;
            i -= 1;
        }
    }
    panic!()
}

pub fn get_elem_mut(slots: &mut Vec<List<usize>>, x: usize) -> &mut usize {
    let mut ls = &mut slots[0];
    loop {
        match ls {
            List::Nil => panic!(),
            List::Cons(y, tl) => {
                if *y == x {
                    return y;
                } else {
                    ls = tl;
                }
            }
        }
    }
}

pub fn get_elem_shared(slots: &Vec<List<usize>>, x: usize) -> &usize {
    let mut ls = &slots[0];
    loop {
        match ls {
            List::Nil => panic!(),
            List::Cons(y, tl) => {
                if *y == x {
                    return y;
                } else {
                    ls = tl;
                }
            }
        }
    }
}

pub fn id_mut<T>(ls: &mut List<T>) -> &mut List<T> {
    ls
}

pub fn id_shared<T>(ls: &List<T>) -> &List<T> {
    ls
}

/// Small variation of [list_nth_mut_loop]
pub fn list_nth_mut_loop_with_id<T>(ls: &mut List<T>, mut i: u32) -> &mut T {
    let mut ls = id_mut(ls);
    while let List::Cons(x, tl) = ls {
        if i == 0 {
            return x;
        } else {
            ls = tl;
            i -= 1;
        }
    }
    panic!()
}

/// Small variation of [list_nth_shared_loop]
pub fn list_nth_shared_loop_with_id<T>(ls: &List<T>, mut i: u32) -> &T {
    let mut ls = id_shared(ls);
    while let List::Cons(x, tl) = ls {
        if i == 0 {
            return x;
        } else {
            ls = tl;
            i -= 1;
        }
    }
    panic!()
}

/// Same as [list_nth_mut] but on a pair of lists.
///
/// This test checks that we manage to decompose a loop into disjoint regions.
pub fn list_nth_mut_loop_pair<'a, 'b, T>(
    mut ls0: &'a mut List<T>,
    mut ls1: &'b mut List<T>,
    mut i: u32,
) -> (&'a mut T, &'b mut T) {
    loop {
        match (ls0, ls1) {
            (List::Nil, _) | (_, List::Nil) => {
                panic!()
            }
            (List::Cons(x0, tl0), List::Cons(x1, tl1)) => {
                if i == 0 {
                    return (x0, x1);
                } else {
                    ls0 = tl0;
                    ls1 = tl1;
                    i -= 1;
                }
            }
        }
    }
}

/// Same as [list_nth_mut_loop_pair] but with shared borrows.
pub fn list_nth_shared_loop_pair<'a, 'b, T>(
    mut ls0: &'a List<T>,
    mut ls1: &'b List<T>,
    mut i: u32,
) -> (&'a T, &'b T) {
    loop {
        match (ls0, ls1) {
            (List::Nil, _) | (_, List::Nil) => {
                panic!()
            }
            (List::Cons(x0, tl0), List::Cons(x1, tl1)) => {
                if i == 0 {
                    return (x0, x1);
                } else {
                    ls0 = tl0;
                    ls1 = tl1;
                    i -= 1;
                }
            }
        }
    }
}

/// Same as [list_nth_mut_loop_pair] but this time we force the two loop
/// regions to be merged.
pub fn list_nth_mut_loop_pair_merge<'a, T>(
    mut ls0: &'a mut List<T>,
    mut ls1: &'a mut List<T>,
    mut i: u32,
) -> (&'a mut T, &'a mut T) {
    while let (List::Cons(x0, tl0), List::Cons(x1, tl1)) = (ls0, ls1) {
        if i == 0 {
            return (x0, x1);
        } else {
            ls0 = tl0;
            ls1 = tl1;
            i -= 1;
        }
    }
    panic!()
}

/// Same as [list_nth_mut_loop_pair_merge] but with shared borrows.
pub fn list_nth_shared_loop_pair_merge<'a, T>(
    mut ls0: &'a List<T>,
    mut ls1: &'a List<T>,
    mut i: u32,
) -> (&'a T, &'a T) {
    while let (List::Cons(x0, tl0), List::Cons(x1, tl1)) = (ls0, ls1) {
        if i == 0 {
            return (x0, x1);
        } else {
            ls0 = tl0;
            ls1 = tl1;
            i -= 1;
        }
    }
    panic!()
}

/// Mixing mutable and shared borrows.
pub fn list_nth_mut_shared_loop_pair<'a, 'b, T>(
    mut ls0: &'a mut List<T>,
    mut ls1: &'b List<T>,
    mut i: u32,
) -> (&'a mut T, &'b T) {
    while let (List::Cons(x0, tl0), List::Cons(x1, tl1)) = (ls0, ls1) {
        if i == 0 {
            return (x0, x1);
        } else {
            ls0 = tl0;
            ls1 = tl1;
            i -= 1;
        }
    }
    panic!()
}

/// Same as [list_nth_mut_shared_loop_pair] but this time we force the two loop
/// regions to be merged.
pub fn list_nth_mut_shared_loop_pair_merge<'a, T>(
    mut ls0: &'a mut List<T>,
    mut ls1: &'a List<T>,
    mut i: u32,
) -> (&'a mut T, &'a T) {
    while let (List::Cons(x0, tl0), List::Cons(x1, tl1)) = (ls0, ls1) {
        if i == 0 {
            return (x0, x1);
        } else {
            ls0 = tl0;
            ls1 = tl1;
            i -= 1;
        }
    }
    panic!()
}

/// Same as [list_nth_mut_shared_loop_pair], but we switched the positions of
/// the mutable and shared borrows.
pub fn list_nth_shared_mut_loop_pair<'a, 'b, T>(
    mut ls0: &'a List<T>,
    mut ls1: &'b mut List<T>,
    mut i: u32,
) -> (&'a T, &'b mut T) {
    while let (List::Cons(x0, tl0), List::Cons(x1, tl1)) = (ls0, ls1) {
        if i == 0 {
            return (x0, x1);
        } else {
            ls0 = tl0;
            ls1 = tl1;
            i -= 1;
        }
    }
    panic!()
}

/// Same as [list_nth_mut_shared_loop_pair_merge], but we switch the positions of
/// the mutable and shared borrows.
pub fn list_nth_shared_mut_loop_pair_merge<'a, T>(
    mut ls0: &'a List<T>,
    mut ls1: &'a mut List<T>,
    mut i: u32,
) -> (&'a T, &'a mut T) {
    while let (List::Cons(x0, tl0), List::Cons(x1, tl1)) = (ls0, ls1) {
        if i == 0 {
            return (x0, x1);
        } else {
            ls0 = tl0;
            ls1 = tl1;
            i -= 1;
        }
    }
    panic!()
}

// We do not use the input borrow inside the loop
#[allow(clippy::empty_loop)]
pub fn ignore_input_mut_borrow(_a: &mut u32, mut i: u32) {
    while i > 0 {
        i -= 1;
    }
}

// We do not use the input borrow inside the loop
#[allow(clippy::empty_loop)]
pub fn incr_ignore_input_mut_borrow(a: &mut u32, mut i: u32) {
    *a += 1;
    while i > 0 {
        i -= 1;
    }
}

// We do not use the input borrow inside the loop
#[allow(clippy::empty_loop)]
pub fn ignore_input_shared_borrow(_a: &mut u32, mut i: u32) {
    while i > 0 {
        i -= 1;
    }
}