Open Manual

Ownership

Ownership contracts say whether a callee borrows a value temporarily or takes responsibility for it. Analysis is opt-in and bounded; see the specification for supported rules.

Prerequisites: Start tour for bindings and function calls.

Borrow a value

The borrowed value remains available through its owner:

use core

def source = [1, 2, 3]
def view = borrow(source)
assert_eq(len(view), 3, "borrowed length")
assert_eq(len(source), 3, "owner remains available")

This behavior is also covered by ownership.nshape.

Choose the contract

SituationContract
The callee reads a value without keeping it@borrows(value)
A returned view refers to an argument@returns_borrow(value)
The callee takes responsibility for an argument@consumes(value)
The callee releases an owned resource@releases(value)
A fresh result belongs to the caller@returns_owned
The callee changes a supported argument@mutates(value)

Declare the contract on the function that owns the boundary. Keep foreign resource cleanup in a small wrapper, and use with when the resource must be released at scope exit. A borrow must not outlive its owner; clone or return an owned value when the lifetime cannot be tied to an argument.

Static checks

ny --borrow-check file.ny
ny --borrow-check --ownership-check=strict file.ny
ny --mode=safe file.ny

--borrow-check reports supported issues without selecting RAII cleanup. --ownership-check=strict promotes those diagnostics to errors. --heap=raii selects ownership-aware cleanup; --mode=safe enables the broader safety profile. These modes do not turn unsupported source shapes into proven-safe ones.

@allowance(parameter, [lower, upper]) can constrain how often a parameter is used on supported control-flow paths. Use it only when the count is part of the API contract; the checker rejects an allowance it cannot validate.

Verify the boundary

Test both valid and invalid use for the resource and control-flow shape you rely on. A pass on one backend does not prove every alias or aggregate form. See Testing for focused fixture commands and Implementation status for known limits.

Next: