aboutsummaryrefslogtreecommitdiff
path: root/tests/programs/temporary_borrows.zc
blob: 40e00baab662b34acf7ed74f44212f6ade3a7e58 (plain) (blame)
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
namespace;
include rt;

// original allocations are released after their aliases' last expression use, across branches and loops.
word latest; int made;
unchecked char*.u message(int code) {
 char* p = rt.new(2); p[0] = code; p[1] = 0; latest = p; made++; return p;
}
char* alias(char* p) { return p; }
int read(char* p) { return p[0]; }
int output(char* p) { rt.put_s(p); return 0; }
int recursive(char* p, int n) { if (n) return recursive(p, n - 1); return read(p); }
int returning() { return read(alias(message('r'))); }
fn main() {
 rt.put_s(message('a')); rt.put_n(rt.isobj(latest)); rt.put_c(32);
 output(message('b')); rt.put_n(rt.isobj(latest)); rt.put_c(32);
 rt.put_n(read(alias(message('c')))); rt.put_c(32); rt.put_n(rt.isobj(latest)); rt.put_c(32);
 rt.put_n(alias(message('d'))[0]); rt.put_c(32); rt.put_n(rt.isobj(latest)); rt.put_c(32);
 rt.put_n(rt.eq_w(message('e'), message('e'))); rt.put_c(32); rt.put_n(rt.isobj(latest)); rt.put_c(32);
 rt.put_n(recursive(message('f'), 3)); rt.put_c(32); rt.put_n(rt.isobj(latest)); rt.put_c(32);
 int n = 0; while (n < 3 && read(message('g')) == 'g') { n++; }
 rt.put_n(n); rt.put_c(32); rt.put_n(rt.isobj(latest)); rt.put_c(32);
 int skipped = 0 && read(message('h')); int yes = 1 || read(message('i'));
 rt.put_n(skipped + yes); rt.put_c(32);
 int choice = alias(message(0)) ? read(message('j')) : 2;
 rt.put_n(choice); rt.put_c(32); rt.put_n(rt.isobj(latest)); rt.put_c(32);
 rt.put_n(returning()); rt.put_c(32); rt.put_n(rt.isobj(latest)); rt.put_c(32);
 rt.put_n(made); rt.put_c(10);
}