Owner rules: addressability is decided by lowering (a variable that is borrowed, indexed or written through a place gets frame storage in the entry block; otherwise SSA; by name, deterministic); borrows are bound only by 'ref r = place;' / 'mut r = place;' and live to the end of the block (borrow.end at every normal exit, including break/continue/return); no shadowing of an active borrow in the same scope; ref/mut as call arguments are temporaries ended after the call; *r only for scalar and struct referents, slices via s[i], s[a..b], s.len; writes *r = v, r.f = v, s[i] = v with the value evaluated first; split_at as the only multi-result binding; slice.copy(mut dst, ref src) as the only copy; [e; N] evaluates e once; byte strings as [u8; N] in frame storage with a fixed escape set; borrow.index.proven only for the verifier's own fact form (if i < s.len, same SSA values), otherwise checked. Diagnostics on lowered AIR are deduplicated per code and source location. aji lower --stats counts code only (no comment lines). Tests: 10 run (architecture goldens: value-only has no local.alloc, borrowed has local.alloc, proven indexing), 6 rejects incl. verifier-caught E2101 (aliasing; parent used after split_at). Expansion on these programs: 4-9x bytes, 2.2-6.6x lines.
28 lines
718 B
Rust
28 lines
718 B
Rust
// mut binding, proven indexing under `if i < s.len`, split_at into disjoint halves, ranges and slice.copy.
|
|
// expect: run
|
|
// result: 4321
|
|
// phase: 6
|
|
// spec: docs/spec/15-source-language.md
|
|
module demo.slices
|
|
|
|
fn main() -> u64
|
|
effects []
|
|
{
|
|
var buf: [u64; 4] = zero;
|
|
{
|
|
mut s = buf;
|
|
for i in 0..4usize {
|
|
if i < s.len {
|
|
s[i] = i as! u64 + 1;
|
|
}
|
|
}
|
|
let (left, right) = s.split_at(2);
|
|
left[0] = left[0] * 1;
|
|
right[1] = right[1] * 1;
|
|
}
|
|
var out: [u64; 4] = zero;
|
|
slice.copy(mut out[0..2], ref buf[2..4]);
|
|
slice.copy(mut out[2..4], ref buf[0..2]);
|
|
return out[1] * 1000 + out[0] * 100 + out[3] * 10 + out[2];
|
|
}
|