Contents

Haze manages memory automatically. The compiler identifies the values that need tracing, emits root maps at collection points, and lets the runtime reclaim objects that are no longer reachable. The language has no manual free, no reference-count annotations, and no lifetime parameters.

Managed and Unmanaged Values#

Category Types Management
Managed List[T], bytes, records, sum types, Option, Result, closures, ptr Traced by the collector; ptr is traced as a root but its pointee is not
Runtime-owned text str Immutable; storage is owned by the runtime; not reclaimed by the collector
Unmanaged scalar Integers, f64, bool, rune, enums Copied by value
Foreign cstr, extern records C representation; not traced; lifetime is the program's responsibility

The distinction matters when a value crosses an FFI boundary. A managed record cannot be stored in an extern record or passed as an opaque pointer and expected to remain valid. Convert it to a C representation at the boundary, and convert the result back immediately.

Reference Semantics#

A managed value is represented by a reference. Assigning it to another binding copies the reference, not the object. Field and element mutation is visible through every binding that refers to the same object.

type Point {
    x i32
    y i32
}

var first = Point { x = 1, y = 2 }
var second = first
second.x = 9
print(first.x)       // 9

Reassigning a binding does not affect another binding. The distinction between mutating an object and rebinding a name is the key to reasoning about aliasing.

var values = [1, 2]
var alias = values
alias[0] = 9
print(values[0])     // 9

alias = [7, 8]
print(values[0])     // 9: only the binding changed

Lists and byte buffers may reuse capacity when they grow. An append through one binding can therefore be visible through another, or the two can diverge after a reallocation. Treat a mutable collection as shared state unless the program is written so that only one binding reaches it.

Allocation#

A list literal allocates a list object with its length and capacity set to the number of elements. Record construction allocates a record object with its fields initialized before publication. A sum constructor allocates a tagged object, and an Option or Result constructor allocates a small tagged object. A closure allocates a closure object containing a function pointer and its captured values. Managed fields are described to the collector, so a record or list reached only through a root keeps its nested heap values alive across collection.

Lists grow by allocating a larger buffer or by using spare capacity. Bytes buffers grow in the same way. A list concatenation with + always produces a new list; an append with += may reuse the existing storage. Element assignment mutates the existing storage.

Roots#

The collector needs to know which managed values are reachable from the running program. The compiler emits a root frame for every function that has managed locals, parameters, a managed return value, or captures. The frame contains a descriptor and the addresses of the managed slots. The runtime walks the frames of every attached thread when it marks.

A closure keeps its captured managed values reachable through the closure object. A local variable that holds a managed value keeps that value reachable until the variable's scope ends. A module constant may be a managed value only when its initializer is a form the compiler can publish safely; managed module constants should be treated as immutable and few.

Collection#

The runtime is a precise tracing collector. Collection is triggered by allocation pressure and by explicit polls at function entry and loop back edges. A collection marks reachable objects from the roots, then sweeps unreachable storage. The design specification in the repository describes the intended Immix mark-region collector; the current runtime implements a parallel mark-and-sweep subset. The language-level model is the same either way: a managed value remains valid while it is reachable, and the program does not control when reclamation happens.

The collector does not provide finalizers, weak references, or prompt reclamation. An object may remain allocated after it becomes unreachable until the next collection. Do not use memory pressure or destructor timing as part of a program's semantics.

Cycles#

Reclamation is tracing, not reference counting. A cycle of managed values is not a special case at the language level; whether it is reclaimed depends on the collector's tracing of the object graph. The language does not expose a way to break a cycle manually, and it does not provide weak references. Avoid designing long-lived cyclic structures unless the domain requires them.

The FFI Boundary#

String concatenation and slicing create immutable runtime-owned storage. That storage is not part of the traced heap, so the collector does not reclaim it; keep large or unbounded string construction in mind when designing a long-running program.

A cstr points to unmanaged memory. A ptr is an opaque address. An extern record has C layout and is copied by value. None of these values carries a Haze lifetime, and the collector does not trace through them.

A managed value should not be passed to C as an opaque pointer unless the runtime explicitly supports that operation. The supported bridge types are cstr, bytes::ptr, and extern records. Even then, the foreign code must not retain the pointer beyond the call unless the program has arranged for the pointee to remain valid. The Extern and C FFI chapter lists the exact restrictions.

Errors and Unwinding#

A failed checked construction returns an Error from the enclosing function. The scopes between the construction and the return are exited, and their defers run. The values that were live in those scopes become unreachable according to ordinary scope rules; the collector reclaims them later.

A runtime contract failure or assertion failure panics. The runtime does not run Haze defers during a panic, because a panic is not a structured exit from the function. Do not rely on cleanup running after a contract violation; the program is already in a state its author said was impossible.

Release Mode#

-release removes runtime contract and invariant checks. It does not remove index bounds checks, and it does not disable allocation, root tracking, or collection. A release build still has the same managed-value semantics; it simply has fewer contract checks.

Index bounds checks remain a runtime safety property. An out-of-range index is a panic even in a release build. Use -release only after the program has been verified by testing and reasoning.

Concurrency#

The runtime supports multiple mutators and parallel collection for embedding and runtime tests. The Haze language in this tree has no thread, async, or synchronization syntax. A Haze program can use operating-system processes through the standard library, but it cannot spawn a Haze thread from the language. Do not assume that a managed value is safe to share across threads unless the embedding layer provides a synchronization protocol.

Practical Rules#

Rule Reason
Prefer new values to shared mutation Contracts describe arguments and results, not hidden aliasing
Do not mutate a parameter unless it is the API's purpose A postcondition sees the final parameter value
Do not retain a cstr or raw ptr without a documented owner The collector does not know the foreign lifetime
Do not store managed values in extern records C layout cannot describe a traced reference
Avoid long-lived cycles unless the domain requires them Reclamation depends on tracing the graph
Do not depend on collection timing Reclamation is not prompt and has no finalizer guarantee
Keep release builds verified -release removes contract and invariant checks

The next chapter, Extern and C FFI, describes the foreign boundary in detail.