Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Returning values from calls

A method can return ownership directly with → given Data, including by giving one of its owned parameters. Its return permission can also name its receiver or parameters. For example, given[x] describes a value given from parameter x, while shared[x] describes sharing from it. At a call site, the checker resolves these permissions before removing the temporary bindings used for the arguments.

The following properties describe separate cases of that process. Each property has its own test module; call_returns groups them without asserting a broader soundness theorem. Here, “remains borrowed” means that a live returned value continues to restrict conflicting accesses to its referents in the caller.

Ownership can escape a call

A method declaring → given Data can return x→ from a parameter x: given Data, or self→ from a given self receiver. The caller can then pass the returned value to a method requiring given Data: ownership survives the disappearance of the parameter binding. With two given parameters, either can supply the owned result.

Ownership can also escape when the declared return permission names a parameter: given[x] resolves to given when x receives a given argument. An explicit given return promises ownership directly; it need not name which parameter supplied the value.

The same applies to given[self] and to given[x, y] when both arguments are given. A receiver named self in the caller does not change which binding the callee’s return permission denotes, even when the caller’s receiver is borrowed.

Evidence and scope

Complete Dada programs with explicit given returns move a named argument, the receiver itself, or the first or second of two given arguments into the result. The two-argument cases use separate methods with fixed choices; they do not test branch-dependent moves. Consumer calls check that each returned value is usable as given after the call, then execute that transfer.

The given examples cover a receiver, a named argument, and two possible given origins. Their receiver examples create a fresh result with the receiver’s permission; their named-argument examples return the argument itself. Field reads print 42 after the call; separate consumer calls check that the result satisfies given.

Every program is type-checked and interpreted from Main.main. Snapshots check the executed calls, printed field values where present, successful completion, and an empty final heap. The borrowed-caller example invokes a generic shared receiver with shared[caller], so it exercises the call from an actual borrowed receiver.

These tests cover ordinary non-shared structs and given arguments. They provide static and runtime evidence for these programs, not a preservation theorem or coverage of every owned permission, type, or cleanup path.

Property source and tests

Borrows of consumed arguments cannot escape a call

A call cannot return a borrow whose referent is a uniquely owned argument destroyed when the call returns. For an ordinary non-shared Data, passing d→ to a parameter x: given Data does not permit the call to return shared[x] Data. The caller’s former ownership of d does not keep that consumed argument alive.

Every possible referent in the return permission must survive. A return type shared[x, y] Data is rejected when either argument is consumed in this way, even when the method body returns only a borrow of the other argument.

The contrasting case passes d.share to the same generic method. The returned borrow can then refer to d, which remains in the caller, instead of depending on the temporary parameter binding. Merely naming a parameter in the return permission does not make its borrow invalid.

Evidence and scope

Complete Dada programs check a consumed receiver, a named argument, two consumed arguments, and one borrowed argument paired with a consumed one. A positive/negative pair uses the identical generic method returning shared[x] and changes its argument from d.share to d→ (and the corresponding permission argument). Both programs attempt to read the result’s integer field with print(result.n→); the borrowed-argument program type-checks and executes, printing 42, while the given-argument program is rejected at the call. The other rejected calls report dangling borrows even when their results are unused.

This property concerns static rejection of borrows of destroyed given arguments. It does not claim that references to all owned values must be rejected: shared ownership can survive independently. The evidence uses ordinary non-shared structs. Execution of the valid control does not prove general runtime destruction behavior or cover every permission composition.

Property source and tests

Forwarded borrows keep their referents borrowed

When a method returns a borrow through one of its borrowed parameters, the caller’s referent remains borrowed while the returned value is live. The method parameter need not remain in scope: resolving the return permission carries its borrowing dependency back to the caller’s referent.

A shared borrow returned through a shared-borrowed argument permits another shared borrow of the referent but prevents mutable access. A mutable borrow returned through a mutably borrowed argument prevents both shared and mutable access to the referent. Disjoint fields remain accessible, and the restrictions cease after the returned value’s last use. Returning a shared view through a mutable argument can retain the underlying mutable borrowing restrictions; that composition is covered in the alternative-referent property.

Evidence and scope

Complete Dada programs forward shared borrows through a named parameter and a receiver, then read an integer field through the returned value with print(result.n→). Both programs type-check, execute, and print 42. Paired accepting and rejecting programs check access to a single caller-owned referent while the returned borrow is live and after its last use. A projected mutable-borrow example checks that a disjoint sibling field remains accessible. All accepting programs execute successfully, return unit, and leave no live allocations; rejecting programs assert the static borrowing conflict.

These tests use ordinary non-shared structs and single-origin return permissions. They demonstrate static access restrictions and successful execution for these examples, not a proved preservation theorem or all runtime pointer behavior. Alternative return origins are covered by all_possible_return_referents_remain_borrowed.

Property source and tests

All possible return referents remain borrowed

When a method’s return signature names several possible borrowed origins, each corresponding caller referent remains borrowed while the returned value is live. This restriction follows the signature even when the method body always returns a borrow of its first argument.

Here x and y are the callee’s parameter bindings; d1 and d2 are the caller’s referents. Returning shared[x, y] or ![x, y] through borrowed arguments must retain the dependencies on both d1 and d2 after the call’s temporary bindings disappear. The callee’s use of x alone does not let the caller disregard y’s declared contribution to the result.

Evidence and scope

Complete Dada programs check each referent independently for shared returns, mutable returns, and shared returns through mutable arguments. A live shared result prevents discarding either referent; a live result returned through mutable arguments prevents mutably reborrowing either referent. Each rejection has a control performing the same access after the result’s last use. Another control consumes both referents after the result’s last use.

The standalone d1→; and d2→; statements discard their values. Their rejection does not say that every move is forbidden: a move into a new binding can relocate a referent and its borrowing dependencies, as described by moves_preserve_dependencies.

All accepting programs type-check and execute. Shared field reads print 42 from the first argument, and the mutable result prints 43 after assignment. Every accepting program returns unit and leaves no live allocations. These snapshots observe the referent actually returned; the rejection tests separately establish static borrowing restrictions on both possible referents.

These examples use ordinary non-shared structs and two direct borrowed arguments. They provide static and runtime evidence for these programs, not a proof for arbitrary signatures, all runtime choices, or pointer relocation.

Property source and tests

Static and runtime evidence

These properties use complete programs to check acceptance and rejection. Accepted programs also run through the interpreter with output, result, and heap snapshots. Rejected programs use checker-error snapshots; they are not executed. The separate runtime normalization module contains direct frame tests that inspect the returned value’s type and verify that method bindings do not leak into the caller. Those tests check their type declarations but evaluate the individual calls directly, without type-checking them. The same module also has two end-to-end examples that type-check and run complete programs, covering an owned field returned to a borrowed caller and nested calls forwarding a borrow. The direct frame assertions and the end-to-end examples provide different kinds of evidence.

The static result accounts for all possibilities in a return signature. The interpreter’s result type describes the value actually returned. Keeping these observations separate helps distinguish conservative borrowing restrictions from the runtime choice of referent.