From 80996a6bf02c7ec97efab5619824e5590c0efc07 Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Thu, 15 Oct 2026 17:33:50 +0100 Subject: [PATCH] PROPOSALS: ret and keep, what a procedure does with what it is given Marks on parameters that would close the hole section 10 leaves for slices of a frame: ret, the results may point where it does; keep, it may outlive the call. Each check needs only the procedure it is in. Written out in full: the words, each mark and where it may stand, every check, procedure types, generics, extern and code procedures, the false alarms (Sh's Opt.init), what it breaks (a thaw) and costs. Nothing built; the owner keeps the undefined behaviour for now. --- diff --git a/PROPOSALS.md b/PROPOSALS.md index 072277c..2aed653 100644 --- a/PROPOSALS.md +++ b/PROPOSALS.md @@ -128,6 +128,280 @@ Met in sauce's `Ssha.short`, which takes `var h: Hex` now. +## Marking what a procedure does with what it is given: `ret` and `keep` + +### The hole + +A procedure's locals, its value parameters and the array literals it +passes as slices live in its frame, which is gone once it returns. A +slice that still points there afterwards reads whatever the next call +put in its place, and writes into it. Section 7.3 catches the plain +case, a slice of a local returned at once: + +``` +proc short(h: Hex): []U8; (* Hex = [64]U8 *) +begin + return h[0..8]; (* an error: h is the procedure's own copy *) +end; +``` + +It can't catch the same slice once it has passed through a call, +since the caller can't see what the callee returns a slice of: + +``` +proc head(var h: Hex): []U8; +begin + return h[0..8]; (* fine: h is the caller's *) +end; + +proc short: []U8; +var h: Hex; +begin + return head(h); (* a slice of short's frame; not caught *) +end; +``` + +Nor the other way, a slice given to a procedure that keeps it: + +``` +proc remember(name: []U8); +var buf: [16]U8; +begin + ... + _ := Names.put(ADR(table), buf[0..n], 1); (* the map, on the heap, keeps a slice of buf *) +end; +``` + +Section 10 leaves both undefined. The marks close them: each parameter +says what the procedure may do with what it is given, the compiler +holds the procedure's body to its heading, and a caller trusts the +heading without seeing the body. Each check needs only the procedure +it is in, so a one-pass compiler can make it. + +### Words + +**Carrying a slice.** A type carries a slice if it is a slice, or an +array, record or variant with one in it: `[]U8`, `[4][]U8`, +`Sys.Program`, `Fmt.Arg`. `INT`, `[64]U8` and every pointer type don't. +What a pointer points at is not followed: a slice reached through a +pointer, `p^.s`, is the heap's or a module's, as far as the marks +know. + +**Where a value points.** Every value that carries a slice has, for +the compiler, a set of places its slices may point into: + +- **the frame**, this call's own: a slice of a local array, of a + value parameter's array, or an array literal used as a slice; +- **a parameter `p`**: what `p` lends. For a value parameter, that is + what its slices point at, which is the caller's business, not this + frame. For a `var` parameter, it is also the caller's variable + itself: `h[0..8]` of `var h: Hex` points into `h`. +- **nowhere that ends**: module variables, constants, string literals, + and anything reached through a pointer. + +A value made from others points wherever any of them does: a slice, a +field, an element or a view of `x` points where `x` does; a record, a +variant or an array literal built from parts, wherever the parts do; a +call's result, wherever its arguments for `ret` parameters do (below). +A local variable that carries a slice points wherever anything +assigned to it anywhere in the procedure does, wherever the assignment +stands, since a loop can run a `return` after an assignment written +below it. **Frame-bound** means pointing into the frame. + +**Outliving the call.** A place outlives the call if it is a module +variable, anything reached through a pointer (`p^.f`, `p^[i]`), or a +`var` parameter of this procedure. A value stored there, or returned, +leaves the frame. + +### No mark + +A parameter without a mark is only used while the call runs. The +procedure may read it, copy its bytes (`COPY`, `Buf.str`), keep it in +locals and pass it to other calls' unmarked or `ret` parameters. It +may not return what it points at, nor store it anywhere that outlives +the call. This is what most procedures do: `Str.eq`, `Str.find`, +`Buf.str`, `Sys.write`, `Fmt.print`, `Map.get`'s key. + +### `ret` + +``` +proc trim(ret s: []U8): []U8; +``` + +`ret s` says: the results may point where `s` points. In the body, +returning a slice of `s` is allowed; in the caller, the result points +wherever the argument given for `s` did. + +``` +proc greet: []U8; +var line: [80]U8; n: INT; +begin + n := Sys.read(Sys.stdin, line); + Out.str(Str.trim(line[0..n])); (* fine: used here, in the frame *) + return Str.trim(line[0..n]); (* an error: frame-bound through trim's ret *) +end; + +proc word(ret s: []U8): []U8; +begin + return Str.trim(s); (* fine: points where s does, and s is ret *) +end; + +proc word2(s: []U8): []U8; +begin + return Str.trim(s); (* an error: s isn't ret here *) +end; +``` + +`ret` is allowed on a parameter that carries a slice, or on a `var` +parameter whose type holds an array (it lends the caller's variable), +of a procedure with a result that carries a slice. It is an error +elsewhere: + +- `ret n: INT`, `ret a: [4]U8`: nothing to lend. A value array is the + procedure's own copy, and a slice of it can never be returned. +- `ret` on a procedure whose results carry no slice: nothing to lend + it to. + +With several results, `ret` covers them all: in `cut(ret s: []U8; +sep: []U8): ([]U8, []U8, BOOL)` both slices may point into `s`. With +several `ret` parameters, a result may point where any of them does, +and the caller takes the result as pointing where all of their +arguments do. + +### `keep` + +``` +proc put(m: ^Map; keep k: K; keep v: V): BOOL; +``` + +`keep k` says: what `k` points at may outlive the call; the procedure +may store it in a place that does. In the caller, an argument for a +`keep` parameter leaves the frame as a store would: it must not be +frame-bound, and if it points where one of the caller's own +parameters does, that parameter must be `keep` too. + +``` +(* import Names := Map([]U8, INT, Str.hash, Str.eq); + table: Names.Map and heap: Heap's allocator, module variables *) +proc count(name: []U8); +var key: [32]U8; +begin + _ := Names.put(ADR(table), name, 1); (* an error: name isn't keep here *) + _ := Names.put(ADR(table), key[0..4], 1); (* an error: frame-bound to a keep *) + _ := Names.put(ADR(table), "main", 1); (* fine: a literal never ends *) + _ := Names.put(ADR(table), Str.copy(ADR(heap), name), 1); (* fine: copied to the heap *) +end; +``` + +`keep` is allowed on a parameter that carries a slice or is a `var` +parameter whose type holds an array; on another, `keep n: INT`, it +is an error, as `ret` is. + +One exception keeps the common loop free of marks: a `var` parameter +may be given a value that points only where it already pointed, so +`s := s[1..LEN(s)]` with `var s: []U8` needs no `keep`. The caller's +variable is no worse off. + +### Both, and the order + +`ret keep v: T` is both. The marks come before `var`, and each +mark covers the group it starts, so `(ret s, t: []U8)` marks both; +`(ret s: []U8; sep: []U8)` marks only `s`. `ret` and `keep` are not +reserved words: they are marks only before a parameter's name, as +`static` is a mark only in `link static;`. `proc f(ret: INT)` is a +parameter named `ret`. + +### What the compiler checks, all of it + +In each procedure: + +1. `return e`, each result: `e` must not be frame-bound, and every + parameter it points at must be `ret`. +2. A store into a place that outlives the call, `M.x := e`, + `p^.f := e`, or `v := e` for a `var` parameter `v`: `e` must not be + frame-bound, and every parameter it points at must be `keep`, but + for the exception above. `COPY` into such a place is no such store: + it copies bytes, not where they point. +3. An argument for a call's `keep` parameter, as 2. +4. A heading's marks are allowed where it says so above. + +Checks 1 to 3 on a local variable's value wait for the procedure's +`end`, when everything assigned to it is known. + +### Procedure values, generic modules, and the rest + +**Procedure types** carry the marks, `proc(ret s: []U8): []U8`. A +procedure may be given where its type has the same marks or more, +since a mark promises the caller less: a procedure without `ret` can +stand where `ret` was allowed, not the other way. + +**Generic modules.** A mark is allowed on a parameter whose type is a +module parameter, `keep v: T`, whatever `T` will be. The body is +checked in each instance: `Map(INT, INT)`'s `put` would pass without +`keep`, `Map([]U8, INT)`'s wouldn't, so the library marks its generic +modules for the instance that needs it. + +**`extern` procedures** take no marks: C is outside the promise of +section 10 already. **Code procedures** take them, and are trusted: +there is no body to check. + +**`ADR`** gives a pointer, which the marks don't follow; a pointer to +a local that outlives it stays undefined, as section 10 says. So does +a slice reached through such a pointer, and a thread given a pointer +to its spawner's locals. + +### False alarms + +The rules are sure, not exact: they reject some programs that were +right. + +- **A record that lives in the frame, filled through a pointer.** + `examples/Sh.raz` does + + ``` + Opt.init(ADR(p), "Sh", [Opt.usage(...), Opt.option(...)]); + ``` + + `Opt.init` keeps its table in `p^`, so it needs `keep table`, and + the array literal is frame-bound: an error, though `p` is a local + and dies with the literal. `keep` can't say "as long as `p^`"; + saying that needs lifetimes on records, Rust's whole apparatus, which + is not proposed. The way out: the table in a module variable. +- **A local variable used twice.** One slice variable that holds a + slice of a local buffer early on and a result to return later is + frame-bound throughout. Two variables cure it. +- **Several `ret` parameters.** A result is taken to point at all of + them, though it may point at one. + +### What it breaks + +Every procedure, in the library or a program, that returns or keeps +what a parameter points at stops compiling until it is marked: a +thaw. In the library, by a search of the headings: `Str.trim`, +`trimLeft`, `trimRight`, `cut` and `field` (`ret`); `Map.put` and +`List.push` (`keep`); `Opt.init` (`keep`), and `Sh`'s call of it, as +above. Not marked: `Str.copy` (its result is the allocator's), +`Sys.env` and `readLink` (their own buffer), `Buf.str` and +`Fmt.write` (they copy), `Map.get`. In sauce, `Svec.push` and `add` +(`keep`), and what returns a slice of its argument. + +### What it costs + +Reports: 5.9 (procedure types), 7.3, 8 (parameters), 10 (the promise, +whose exceptions shrink to `ADR`, C, code procedures and `check +off`), appendix B. The compiler: a set of the places a value points +into, carried with each value as its type is, a 64-bit set (the frame +and up to 63 parameters); the same on each local; the checks above, +and a list per procedure of the checks that wait for its `end`. +Perhaps 250 to 400 lines; no cost at run time, and little at compile +time. + +Precedent: D's `scope` and `return scope` (DIP1000) are `keep`'s +opposite and `ret`; Rust's lifetime elision reads a call's result as +`ret` does. D's experience is that the corners (fields, generics, +procedure values) take years; the narrow form here leaves record +lifetimes out, and so the first false alarm above, on purpose. + ## `Vec(T)`, a growable array The library has `List`, linked, and `Map`, a hash; nothing that grows