
louise
Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.
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:
returnfrom the function's own blocks;returnfrom inside ausecallback, and fromdefer,handleandregionbodies.
Decision
Syntax and types
returnbecomes a keyword; a handler's return clause still readsreturn(x) -> ....return ereturnse: the rest of the line's expression, the loosest there is (return a |> freturns the pipe).returnfollowed by a line break,},,or)returns().eis checked against the enclosing named function's result type.return ehas typeNever, which fits any expected type (aspanic's does).- Refused (E0119): inside a lambda, a capture (
f(_)), a handler clause or adefercleanup. 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 eis a block that exits. What follows a block that never ends normally is dead code;wrapcuts it and ends the chain with the block, soreturn f(x)becomeslet r = f(x); exit r. - Perceus: variables an enclosing block still needs are lent to a nested block (borrowed there). An
exitnever gives them back, so it releases them. A chain that ends in anexittakes what it was lent as owned from its start. That way the dead ones are dropped before a final call, andlet r = f(x); exit rstays 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.verifychecks that every count is zero at anexit, 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 ris a tail call (TL_MUSTTAIL, or a jump for self-calls) at any depth. The interpreter pops the activation'sBindframes, 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
returnis inside of is its return scope. The function handlesearly_returnaround each scope:- the clause wraps the value in
Ret(x), the return clause the scope's value inOk(v); - a
switchthen exits withx, or goes on withv.
- the clause wraps the value in
- A
returninside performs the operation, which goes to that handler like any operation goes to the nearest handler (ADR 0010). So:defercleanups in between run once (ADR 0016);- a
handlein between is left without itsreturnclause; - 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. returninside ausecallback costs a handler (a closure and a marker) per execution of the scope, and nothing when the scope has noreturn.- Code without
returncompiles as before: Perceus treats a chain differently only when it ends in anexit, andwrapcuts only after a block that never ends normally. - Fixed along the way: natively, a handler's
returnclause leaked when afinal ctlclause took over (now released;tests/run/final_ctl_return_clause.lse). - The tree-sitter grammar can't see line breaks, so a bare
returnfollowed by an expression on the next line highlights as onereturn e.
Alternatives considered
- Only step 1: simpler, but
useis the common way to write loops andResultchains here, and a guard inside one is exactly wherereturnhelps. returnfrom 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 ctlmachinery already unwinds correctly throughdefer, 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.