Closed monotone arena worlds

A world entry creates one empty arena for each call. An arena grows by appending elements and issues a branded ID for each successful append. The brand belongs to the call’s world. The compiler checks that values from different calls cannot meet, so an ID always refers to an initialized element in its own arena, including after the buffer grows.

struct Node(I: type) {
    value: i64
    edges: Vec(I)
}

fn score(n: i64) -> i64 {
    world W (nodes: Node(WorldId(W))) {
        let x = nodes.add(Node(WorldId(W)) {
            value: n, edges: Vec(WorldId(W)).new()
        })
        let y = nodes.add(Node(WorldId(W)) {
            value: 7, edges: Vec(WorldId(W)).new()
        })
        nodes.get_mut(x).edges.push(y)
        let next: WorldId(W) = nodes.get(x).edges[0]
        nodes.get(next).value
    }
}

The slice or vector index edges[0] uses the usual bounds check. Looking up next in nodes needs no bounds check. The same rule applies to IDs stored in maps, arrays, and user data.

Entries and helpers

Write world W (arena: T) { ... } as the entire body of a named, safe, nongeneric free function. The binder W has its own namespace and exists only while compiling. The function can take integers, booleans, str, and shared slices of primitive integers. It returns a boolean, integer, float, or unit by value. Calls through ordinary jihoo code can recur, run on separate threads, or run at compile time with a scalar or unit result.

A world-local helper declares its world as its first compile-time parameter and may borrow the arena. Its caller can write the current binder explicitly or let the compiler supply it. Helpers can call each other or recurse with the same brand. Same-world IDs and ordinary receiver-derived loans can return to the caller.

#[no_panic]
#[no_alloc]
fn lookup(comptime W: world, nodes: WorldArena(i64, W), id: WorldId(W)) -> ref i64 {
    nodes.get(id)
}

fn seven() -> i64 {
    world W (nodes: i64) {
        let id = nodes.add(7)
        lookup(W, nodes, id)
    }
}

Use inout nodes: WorldArena(T, W) and pass &nodes to a helper that can change the arena. World-local helpers require an active world and are available only through direct calls.

Types and confinement

WorldId(W) is a plain four-byte value with alignment four. It implements Eq and Hash, so it can be a Map key. Its representation is opaque. Reflection reports no struct fields, and casts, literals, patterns, user implementations, and make cannot construct or expose its bits. A user struct containing IDs can still be built with make when the parts supply valid IDs.

WorldArena(T, W) is the unique owner place created by the binder. Its operations are add(sink value: T) -> WorldId(W), get(id) -> ref T, get_mut(id) -> inout T, and len() -> i64. The compiler permits borrows of the owner as helper arguments. The owner cannot be moved, copied, assigned, stored in another value, constructed explicitly, or inspected by reflection. There is no removal or reset operation.

The compiler checks instantiated types and resolved calls within worlds. Arena elements may contain scalars, same-world IDs, tuples, arrays, user structs and enums, and audited containers such as Vec, Map, String, Option, and Result. Owned elements exclude borrows, slices, str, pointers, function values, linear and interior-mutable types, and user destructors. Locals may hold short-lived borrows and read-only views.

World-local code may call other entries, same-world helpers, and audited standard operations. The compiler checks the resolved implementation for operations that dispatch through traits, such as a map key’s equality and hashing. Other generic calls, globals, FFI, threads, raw memory operations, unsafe blocks, dynamic dispatch, and closure values are outside the v1 boundary. Compile-time results cannot contain branded data, including through a phantom type argument.

Lowering and lifetime of the buffer

Only a successful add gives out an ID. It checks length against 2³² and checks capacity and byte-size arithmetic before growing the buffer. Once the slot is initialized, it commits the length and returns the previous length as the ID. This also applies to zero-sized elements, for which the last valid ID is 0xffffffff. The world drops every initialized element and frees the buffer on scope exit, including an early return.

MIR represents a lookup as Ref(owner.WorldElem(id)) or RefMut(owner.WorldElem(id)). The borrow checker treats these as loans of the whole arena. A shared element loan prevents append and mutation while it is live. An exclusive loan prevents other accesses. An ID copy holds no loan, and owned payloads stay in their slots.

LLVM loads the arena’s current buffer pointer, zero-extends the ID from 32 to 64 bits, and computes the element address. Lookup has no length load, bounds branch, panic call, or runtime brand. A helper that only returns a lookup or reads the length can satisfy #[no_panic] and #[no_alloc].

See the runnable graph example and the ordinary arenas and pools.

Esc