On this page

0042. Non-escaping blocks lower to IR loops

Context

Vector transformations need concise syntax with ordinary loop costs, without function types, closure storage or indirect calls. ADR-0020 already uses local lookahead to distinguish literals from control-flow braces.

Decision

After a method name/arguments, { Ident (, Ident)* -> (ignoring newlines) starts a BlockArg on MethodCall. Elsewhere a bare block is E0700; a function with a trailing block is E0701. map/filter/fold(init)/sort_by take respectively one/one/two/two immutable parameters. Parameters obey ADR-0013. Blocks yield their last expression; capture reads and captured var assignments are allowed. Return, ? (implicit early return), and transfers to loops outside the block are E0703. No block escapes. Map infers vec from body/context; filter and sort_by require bool; fold's accumulator and result use the initializer type. E0702/E0704/E0705 teach arity, missing blocks and result types. Builtins win over interface methods.

Lower each call site/monomorphized instance into structured IR Loops, charging fuel at the method name. Stable bottom-up merge sort compares right against left, taking right only when true; even inconsistent comparators terminate. There are two private alternating buffers, O(n) extra space, no qsort/callback. The main branch has no VecReserve/VecCopy/BufWrite from the draft plan: map and filter use VecNew/Push with the existing append guarantee; fold is a scalar or heap-bearing carry local. Sort first copies input into the private workspace, then VecConcat-copies that workspace into the private result buffer. This block-local dependency propagates the result's lifetime to both buffers before the loop, including when returned. They alternate without whole-vector promotion at the swap; retained heap elements are placed/promoted before sorting. There is no reserve/copy operation or indirect call.

The one new intrinsic, BufWrite, has VecSetUnique's checked in-place semantics (vec, i64 index, element -> same vec). It reuses the existing C runtime helper and the interpreter's checked write. Both check before touching storage; regions classify it as FreshItems, placing/promoting retained elements exactly as for set. It doesn't count as a copying set or retained replacement. VecSetUnique stays reserved for regions' whole-function no-alias proof, which rejects sort's alternating handles and final handoff. Ordinary VecSet would copy the entire buffer for every element. This minimal additive backend/IR extension replaces the draft's unavailable BufWrite operation rather than moving an algorithm into either backend.

BufWrite's construction proof belongs to lowering: for n>0 the two initial copies have disjoint storage, are never passed to source code, and alternate read/write roles after every complete pass. A comparator receives only elements; acyclic layouts mean an element cannot alias its containing vec. Captures can reach the original input but neither private copy. Swapping retains only hidden handles; no source-visible handle exists until the last pass completes. Empty buffers need no writes. Thus no observable snapshot is ever modified. The IR verifier checks vec layout, arity, i64 index, element type and live region. Bounds/widths saturate at n without overflowing i64; take right only when before(right, left) holds, so false ties retain input order. Every iteration consumes one element even with an inconsistent comparator.

Format one comment-free expression inline at <=100 columns; otherwise indent the body after the arrow. Canonical blocks are semicolon-free, including in calls.

Alternatives considered

  • Closures/function types — require escape/storage/call semantics beyond M5.
  • Pipe syntax — conflicts with existing diagnostics and adds another form.
  • Key sort or qsort — changes the agreed comparator API/order and stability.
  • Backend-specific algorithms — would duplicate semantics and comparator order.

Consequences

Capture effects and comparator traps have identical ordering on both backends. Inlining grows IR/C per site; the shared sort helper stays below 150 lines. The 4,096/65,536-element stats test uses 131,088/2,097,200 arena bytes including input construction, below 64n+65,536, and pins total allocation as well as peak: fresh buffers per pass cannot satisfy the test. Returned i64 sorts obey the same bound; returned heap-bearing records obey it scaled by their four-word layout. IR assertions forbid promotions in these merge passes, including the swap. The deferred ADR-0043 copies-return-vec-map fixture now pins zero copies and zero promotions.