0040. Vectors and for loops
- Status: Accepted; E0410 participation amended by 0270
- Date: 2026-10-30
- Plan: M5.4
- Review: Memory and representation
Context
Review found quadratic set memory, repeated lazy searches and costly gcc -O2 str literals (bench/vec-resources).
Decision
vec<T> is seeded, structurally equal,
retaining elements (ADRs 0037/0038). [a, b,]
evaluates left to right; [] needs context (E0602).
Type arguments participate in E0410 cycle checks. C uses
type-erased hrt_vec, interp SeqBuf::Items; slices are same-type
views. Constant scalar/str literals use static const arrays copied
into writable arena storage by hrt_vec_new. Array names use a
per-function counter, not possibly repeated IR source
positions.
for is hard, in contextual. Heads
accept vec/bytes/half-open i64 ranges, evaluated once, left to
right; binders are immutable without shadowing. Advance before the
body; break/continue target the innermost loop. Ranges aren't
values; str iteration is E0716, malformed heads E0717. Amend
ADR-0017: each for head (including its final test) charges fuel at
the keyword; generated work charges none. Lazy lines/split compute
one record step per iteration on both backends, then project
valid/value/next, sharing materialized semantics without a
vector.
Literals inline at 100 columns; comments/overflow force
trailing-comma lines. Loop heads use for x in v { and
for i in a..b {.
Set normally copies O(n). A narrow whole-function IR proof permits O(1) direct self-set: a named local starts with VecNew, every assignment is VecNew, VecConcat (fresh copy), or self-set/push. Every use must read elements/scalars or perform those self-updates/copies. Any container snapshot, slice, aggregate capture, call argument or incoming alias rejects it everywhere, including across branches/loops. Element reads cannot alias their acyclic container. Placement still promotes stored elements. The append tip rule is insufficient: it protects only the appended suffix, not existing slots. A direct return is terminal and allowed: earlier aliases still reject the whole local; no subsequent update in that frame can affect the returned value. Return placement/promotion remains unchanged, including early returns from loops.
Amend ADR-0036: default pages to 8 GiB (20M pushes exceeded the old 256 MiB cap). Read HRT_MEMORY_LIMIT once before first commitment: decimal bytes, optional K/M/G (case-insensitive, ×1024); invalid/overflowing settings use the C default. Env overrides C for run/built binaries. Check before mprotect; reset returns budget. OOM traps/exit 101 at allocation (push/set receiver); resource-dependent OOM is excluded from parity. Interp has no arena budget; OS pressure can still kill.
Alternatives considered
- Range values and an iterator protocol — adds types and dispatch before need.
- Materialize every text loop — lazy lines/split heads can yield views.
- Tip check for set — unsound for equal-length snapshots; use static proof.
- Adaptive OS/cgroup policing — platform-specific/racy; generous explicit cap.
Consequences
Get/first/last return option