Memory management / Ownership and drops

Ownership is an optimisation state layered over GC-safe semantics. The compiler transfers destruction responsibility only where static analysis proves it's safe.

Ownership and drops

Design contract

Moth ownership is an optimisation state layered over GC-safe source semantics. It isn't a separate source type system.

Borrow and lifetime analysis prove optional transfer eligibility and topology legality. Ownership lowering only realises already-validated facts.

The compiler may transfer destruction responsibility only where static analysis proves that doing so can't invalidate a later source use. If that proof is unavailable, the operation remains a borrow and the value may remain GC-managed.

Ownership is an optimisation state

Ownership bit

The ownership bit records destruction responsibility. It does not prove uniqueness, identify the final observer, establish lifetime legality or create dynamic shared ownership.


        borrowed
            The runtime path must not destroy the value.

        owned
            The runtime path carries release or safe-transfer responsibility.
    

Ownership responsibility versus lifetime ownership

Contrast these distinct concepts:

The bit does not imply individual releasability when the value belongs to a region-owned allocation family.

Inferred transfer

Assignments and calls can be optional transfer points. Analysis proves eligibility. Lowering realises validated transfer facts. There is no move keyword.

When transfer is realised:

Immutable and mutable parameters may both receive inferred destruction responsibility at a proven final-use call site. Parameter mutability controls access; ownership metadata controls destruction responsibility. An immutable parameter receiving ownership remains read-only. A mutable parameter receiving ownership may mutate according to the source contract.

Closed external boundary profiles override this general rule. WIT value-only calls are non-consuming. Restricted host-value crossings are non-consuming, and mutable opaque-handle access does not transfer Moth storage through the ordinary Moth ownership ABI.

Single ownership transfer

Group-owned values

Unified ownership ABI

Ownership-aware lowering uses one calling convention rather than separate source-visible borrowed and owned function variants.

Heap-managed or handle-based values that participate in ownership lowering carry ownership metadata with the runtime value. Scalar values may continue to use target-native representations.

ABI behaviour:

The unified ABI keeps dispatch static without generating separate borrowed and owned function families. It also limits ownership-driven monomorphisation and binary-size growth, especially for Wasm, while still allowing static specialisation to remove redundant ownership checks.

Conditions for release

Deterministic release is legal only when:

Conditional destruction

drop_if_owned is inserted at possible destruction points. It checks ownership metadata and releases only when responsibility is owned.

Control-flow exits

Possible destruction points include:

A non-exiting loop needs no exit destruction point. A loop that can exit must account for ownership on each reachable break, return, error-return or region-exit path.

A control-flow join determines whether a later exit may own. The join itself is not a destruction point and is not automatically a release site.

Deterministic release is enabled where analysis supports it. Where it does not, GC-backed or otherwise conservative representation remains legal for already-valid topology.

Aggregates and stored values

Aggregates may contain shared and independently owned children.

Static specialisation

Accepted effect categories:


        MayConsume
        NeverConsumes
        AlwaysConsumes
    

These are analysis/lowering facts only, not required exact Rust enum names and not source-visible signature categories.

Physical representations

Internal reference counting is permitted as a backend representation for a statically legal topology. Source-visible RC, retain/release, weak ownership or dynamic shared-ownership semantics remain outside the language design.

Compatible extension points within the accepted memory model:

These must preserve source-language semantics, the unified ownership contract and GC as a legal representation for valid topology.

Common mistakes

Related reading