tobogganSe connecter

louise

Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.

56 lignes · 6122 octetsBrut · Historique

ADR 0037: Early return

Status: Proposed (2026-10-13, issue #149); needs engineering review (#44). Builds on ADR 0010 (lexical handlers), ADR 0016 (defer) and ADR 0002 (Perceus).

Context

A function that checks a few things before its main work had to nest them: each guard became an if around the rest, or a match. With use (Gleam's callback sugar), even a plain loop body can't stop the function: the callback is another function. Issue #149 asked for return, in two steps:

  1. return from the function's own blocks;
  2. return from inside a use callback, and from defer, handle and region bodies.

Decision

Syntax and types

  • return becomes a keyword; a handler's return clause still reads return(x) -> ....
  • return e returns e: the rest of the line's expression, the loosest there is (return a |> f returns the pipe). return followed by a line break, }, , or ) returns ().
  • e is checked against the enclosing named function's result type. return e has type Never, which fits any expected type (as panic's does).
  • Refused (E0119): inside a lambda, a capture (f(_)), a handler clause or a defer cleanup. Each may run after the function returned, or in another actor; "return from the lambda" would be a different feature, with a different meaning.

Step 1: Exit in Core

A new Core expression, Exit(a), leaves the function from any block depth (Return(a) is a block's value).

  • Lowering: return e is a block that exits. What follows a block that never ends normally is dead code; wrap cuts it and ends the chain with the block, so return f(x) becomes let r = f(x); exit r.
  • Perceus: variables an enclosing block still needs are lent to a nested block (borrowed there). An exit never gives them back, so it releases them. A chain that ends in an exit takes what it was lent as owned from its start. That way the dead ones are dropped before a final call, and let r = f(x); exit r stays a tail call. A projection the chain borrows from a lent value first takes its own reference, since that value may now be dropped before the projection's last use. verify checks that every count is zero at an exit, and a reuse token placed past a block is dropped before that block's exits.
  • Back ends: C emits return a; from wherever it is; let r = call; exit r is a tail call (TL_MUSTTAIL, or a jump for self-calls) at any depth. The interpreter pops the activation's Bind frames, then delivers the value (a call there replaces the activation).

Step 2: across callbacks and bodies, with an effect

A use callback, and the bodies of defer, handle and region, are closures in Core. Leaving the function from inside them means unwinding through the code in between, as a final ctl operation does.

  • The prelude declares a private effect, effect early_return<a> { final ctl early_return(value: a) -> Never }. Programs can't name it.
  • The outermost such construct a return is inside of is its return scope. The function handles early_return around each scope:
    • the clause wraps the value in Ret(x), the return clause the scope's value in Ok(v);
    • a switch then exits with x, or goes on with v.
  • A return inside performs the operation, which goes to that handler like any operation goes to the nearest handler (ADR 0010). So:
    • defer cleanups in between run once (ADR 0016);
    • a handle in between is left without its return clause;
    • a region in between is closed.
  • The handler's only operation is a final ctl, so it needs no fiber.

Safety: each scope is a new instance, a scoped rigid variable, exactly like a handler's.

  • A callback that performs the return has it in its type. The scope's value can't mention it (E0703), so the callback can't be returned out of the use.
  • Level checks in unification stop it from being stored in a variable from outside.
  • A callee whose callback type fixes its effects (fn() -> Int, process.spawn's body) can't accept it (E0502, with a note naming the early return).
  • The label is discharged at the scope and never appears in the function's signature.

Tests with a small stack

ulimit -s can't show that recursion runs in constant stack: programs run on their own fiber stacks, the root's 1 GiB. //@ small-stack in a tests/run program runs it with LOUISE_STACK_KIB=256 natively and a 10,000-frame limit in the interpreter (LOUISE_INTERP_MAX_FRAMES, or run_limited). tests/run/return.lse makes a million return count(n - 1) calls under it.

Consequences

  • Guards read as guards: if bad { return Error(...) }, then the main path, unindented.
  • return inside a use callback costs a handler (a closure and a marker) per execution of the scope, and nothing when the scope has no return.
  • Code without return compiles as before: Perceus treats a chain differently only when it ends in an exit, and wrap cuts only after a block that never ends normally.
  • Fixed along the way: natively, a handler's return clause leaked when a final ctl clause took over (now released; tests/run/final_ctl_return_clause.lse).
  • The tree-sitter grammar can't see line breaks, so a bare return followed by an expression on the next line highlights as one return e.

Alternatives considered

  • Only step 1: simpler, but use is the common way to write loops and Result chains here, and a guard inside one is exactly where return helps.
  • return from lambdas returning from the lambda (as in JavaScript): a different feature, and confusing next to the named-function meaning.
  • Exceptions-style unwinding in the runtime (a dedicated yield state): the final ctl machinery already unwinds correctly through defer, handlers, regions and multi-shot continuations; a second mechanism would need all of that again.
  • A synthetic definition outside every module for the effect: the same, with more special cases in name resolution; a private prelude item is invisible to programs anyway.