omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / tests / compiler / progs / structs_owner.osc
| 1 | // entry: moves borrow_calls |
| 2 | // OSC-2 structs: moves (let and own parameters), shared and mutable borrows, |
| 3 | // passing structs to functions as own, & and &mut parameters. |
| 4 | struct Acc { sum: i64, hits: [u32; 3] } |
| 5 | |
| 6 | fn total(a: &Acc) -> i64 { |
| 7 | return a.sum + (a.hits[0] as i64) + (a.hits[1] as i64) + (a.hits[2] as i64); |
| 8 | } |
| 9 | |
| 10 | fn add(a: &mut Acc, k: i64, slot: u8) { |
| 11 | a.sum = a.sum + k; |
| 12 | a.hits[slot] = a.hits[slot] + 1; |
| 13 | } |
| 14 | |
| 15 | fn consume(a: own Acc) -> i64 { |
| 16 | return total(&a) * 2; |
| 17 | } |
| 18 | |
| 19 | fn moves(k: i32) -> i64 { |
| 20 | let a: own Acc = Acc { sum: k as i64, hits: [0; 3] }; |
| 21 | let b: own Acc = a; |
| 22 | b.hits[2] = 5; |
| 23 | let c: own Acc = b; |
| 24 | return consume(c) + 1; |
| 25 | } |
| 26 | |
| 27 | fn borrow_calls(k: i16, slot: u8) -> i64 { |
| 28 | let a: own Acc = Acc { hits: [1; 3], sum: 0 }; |
| 29 | add(&mut a, k as i64, slot); |
| 30 | { |
| 31 | let m: &mut Acc = &mut a; |
| 32 | add(&mut m, 1, 0); |
| 33 | m.sum = m.sum * 2; |
| 34 | } |
| 35 | let r: &Acc = &a; |
| 36 | let t: i64 = total(&r); |
| 37 | return t + (r.hits[0] as i64); |
| 38 | } |
| 39 | // expect-run: moves 10 -> 31 |
| 40 | // expect-run: borrow_calls 3 1 -> 15 |
| 41 | // expect-run: borrow_calls 3 3 -> trap=BOUNDS |