DEVELOPMENT EDITION · 769442725406
Language Reference
On this page

Ownership, borrowing, copying, and reclamation

Ownership gives dynamically allocated objects a single responsible owner. It lets Verada check aliasing, use after move, and leaks without treating every pointer as an unrestricted reference. Storage exhaustion is still an environmental/resource possibility; ownership proof is not a promise of unlimited memory.

Owners and allocation

A writable pool-specific access value owns its designated object. new constructs a supported allocated value, and its initializer must establish every part required for use. Uninitialized allocation is rejected. An owner can be null unless its subtype excludes null. Dereferencing requires proof that the owner is not null; not null moves that requirement to construction and call boundaries.

This is a Vada fragment illustrating the declaration categories:

type Node;
type Link is access Node;
type Node is record
   Value : Integer;
   Next : Link := null;
end record;

The incomplete declaration makes a recursive structure expressible. The Next component can own another node; it is not a shared arbitrary graph edge. Varc's access forms and Vibe's Owned, move, and deref helpers express supported parts of the same model. For shared graph-like structures, use suitable index/arena APIs rather than escaping ownership through unchecked pointers.

Moves and partial moves

Assigning an owning value transfers responsibility instead of duplicating it. The source becomes moved and cannot be read as an owner until restored. The compiler follows this state through branches, loops, calls, returns, and exceptions. A source being represented by a machine address does not permit a second usable alias.

A move can affect a component of a larger object. Reading the whole enclosing value then also reads the moved part and is forbidden. Reassigning the moved part with a valid value restores it. Conservative tracking can treat a dynamic array index as affecting the relevant indexed region rather than proving two arbitrary index expressions independent.

At a normal return, writable parameters and global objects must have their required ownership restored. Moving out of an ordinary in parameter would remove an object still owned by the caller and is rejected. A loop must not return to its next iteration with required values left moved. B6002, B6003, B6004, and B6008 distinguish these cases.

An owner returned as a result transfers through the supported result construction; it does not remain simultaneously owned by the callee's dying local. The same reasoning applies to composites containing owners.

Borrowers and observers

A borrower is a permitted temporary exclusive writable view of an existing object or designated region. The original owner is unavailable for conflicting use while the borrower is live. An observer is a read-only view whose observed structure cannot be changed incompatibly during its lifetime. Anonymous access forms supply the relevant context for X'Access. A named general access type (access all T) is the other way to designate a declared object; see below.

View Permitted use Restriction on the source
Owner Transfer, update, or free its designated object under its type contract No second independent owner
Borrower Read/update the borrowed region Conflicting owner access is suspended until return of the borrow
Observer Read the permitted observed value Changes that invalidate the observed structure are forbidden

The anonymous access type, designated type, mutability, and expected context must match. Borrowing a constant as writable is illegal. A supported traversal result borrows/observes from its first actual, with lifetime and return-state restrictions. These are checked forms, not unrestricted escaping references or general user-controlled pointer arithmetic.

A procedure's in parameter of an access-to-variable type, or of a private type whose full view is one, borrows what it designates: the procedure may change the designated object, and a caller's proof knows only what the procedure's postcondition says of it afterwards. Verada treats the parameter the same with or without pragma Annotate (Verada, Mutable_In_Parameters, T) after such a procedure; it accepts and checks the pragma (B1004 when T is not such a type or no procedure comes just before), and accepts GNATprove in place of Verada in code written for GNATprove, which needs the pragma when T is private.

At scope exit or the supported return of a borrow, the owner becomes usable again. Reborrowing and traversal must preserve that relationship. B6009 reports using an object while borrowed; B6010 reports a conflicting observed change. A whole-object operation can conflict even when the source-level spelling seems to mention another field.

A ghost function with Annotate => (Verada, At_End_Borrow) (GNATprove is accepted in place of Verada), an identity function on an anonymous access type, names the value of a borrower or of a borrowed object at the end of the borrow. It is written in assertions and loop invariants while the borrow lasts, so that a loop changing a structure in place through a borrower can say what the whole structure will be:

function At_End (L : access constant Node) return access constant Node is (L)
with Ghost, Annotate => (Verada, At_End_Borrow);

function All_Zero (L : access constant Node) return Boolean is
  (L = null or else (L.Value = 0 and then All_Zero (L.Next)))
with Ghost, Global => null, Subprogram_Variant => (Structural => L);

procedure Zero_All (L : in out List) with Post => All_Zero (L);

--  the body
procedure Zero_All (L : in out List) is
   B : access Node := L;
begin
   while B /= null loop
      pragma Loop_Invariant (if All_Zero (At_End (B)) then All_Zero (At_End (L)));
      B.Value := 0;
      B := B.Next;
   end loop;
end Zero_All;

At_End (B) of a borrower and At_End (L) of the object it borrows from are the only arguments it takes (B2005 otherwise); such assertions are proved, never run. A traversal function, whose result borrows a part of its first parameter, may say what the parameter leaves in terms of what the result leaves, and a loop that moves on with it (B := Tail (B)) gets that fact:

function Tail (X : access Node) return access Node is (X.Next)
with Pre => X /= null,
     Post => At_End (X) /= null and then At_End (X).Value = X.Value
             and then All_Zero (At_End (X)) = (X.Value = 0 and then All_Zero (At_End (Tail'Result)));

A recursive function over an owned structure terminates by Subprogram_Variant => (Structural => L): each recursive call is given a strict part of L, a component reached through a dereference (B5109 otherwise). Give such functions Global => null, so that the prover treats two calls on one value as one. Vibe has no borrower that moves on (its views are not rebound), so At_End has no Vibe form; Varc writes both as aspects (@Annotate(Verada, At_End_Borrow), @Subprogram_Variant(Structural => L)).

General access types

A general access type, type Cell_Ref is access all Cell;, may designate an object that was declared rather than made by new. X'Access of such a type takes X for good: X must be a whole object declared aliased, declared no deeper than the access type (so that no value of the type outlives it), and from then on X is read and written only through the access value (B6002 otherwise). Nothing frees what a general access value designates: no instance of Ada.Unchecked_Deallocation takes such a type, new makes an object for one only at library level, and these values have no leak checks (B2028 reports each rule broken). What a general access-to-variable value designates is a variable even when the access value is a constant, so Ref below may be declared constant and still write Ref.Value.

type Counter is record
   Value : Integer := 0;
end record;
type Counter_Ref is access all Counter;

Shared : aliased Counter;
Ref : Counter_Ref := Shared'Access;   --  Shared is reached through Ref from now on
...
Ref.Value := Ref.Value + 1;

Deallocation and leaks

An instance of Ada.Unchecked_Deallocation frees the object owned by its access argument and makes the argument null. Despite the Ada library name, the Verada use is constrained by ownership. The object being freed must not leave owned children unfreed or lose other resources. Freeing an outer node does not recursively destroy a linked structure merely because its field is an access value.

An owning local must not silently die while still owning an unreclaimed object, and an assignment must not overwrite a live owner in a way that leaks its object. Leak conditions are proof obligations (B6005), including relevant composite parts and exit paths. Transferring ownership to another valid owner or a properly reclaimed abstraction is different from discarding the address.

Limited reclaimed types

A reclaimed abstraction registers a finalizer using the supported Finalizable aspect:

with Finalizable =>
  (Finalize => Release, Relaxed_Finalization => True)

This fragment belongs on a suitable type with a conforming Release(X : in out T) operation declared in its package. A reclaimed type without an adjuster must be limited so ordinary copies cannot duplicate its resource responsibility. Its finalizer must accept every valid implicit call: it cannot have a precondition and must terminate. Required owned resources must be discharged when it completes.

Applicable cleanup occurs as local scopes end, including return, loop exit, exception propagation, and supported temporary lifetimes. Nested reclaimed components are handled according to their live/moved state. A moved part is not finalized a second time as though it still owned its former object.

Finalization is deliberately relaxed. Relaxed_Finalization => True is required, and no program-termination finalization guarantee is supplied. Do not use a library-level finalizer as the sole mechanism for an application action that must happen at process exit. The REPL explicitly finalizes its session objects when reset/end applies; foreign faults or abrupt process termination are different circumstances.

Copyable reclaimed types

A nonlimited reclaimed type can be copyable when it supplies Adjust as well as Finalize. The adjuster makes a new logical copy's resource state valid. It has one in out parameter, no precondition, must terminate, and requires Global => null so copy placement cannot create observable unrelated state changes.

A copyable reclaimed type cannot directly contain ordinary owned access components: copying them would create two owners. A suitable runtime-managed handle can instead support immutable/reference-counted storage, as Verada.Strings.Texts.Text does. A limited type cannot supply Adjust, because it is not freely copied.

The implementation may choose different intermediate copies from another compiler. Programs cannot use adjustment/finalization counts as an ordinary semantic signal. Value equality and public contracts describe logical values, not incidental sharing of immutable buffers. Explicit Release calls must obey the abstraction's API rather than assuming an internal pointer is safe to free directly.

Vibe sharing and cross-syntax use

Vibe's local aliases, element views, explicit copies, and stores are restricted to preserve the meaning of accepted Python-shaped programs under Verada's value/ownership model. Updating a view can require delayed putback and can forbid concurrent container use or exceptional escape. A stored copy is not necessarily a persistent Python alias that later mutations can observe.

Use the complete Vibe sharing reference for accepted patterns. Do not summarize the whole language as either “everything copies” or “everything aliases”. Across syntax boundaries, pass values through their declared modes and resource contracts; translation does not grant access to an unavailable borrowing or finalizer declaration form.