raz / commit 80996a6b

commit
80996a6bf02c7ec97efab5619824e5590c0efc07
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
b4f39282
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.

patch · log

1 file changed, +274 -0

PROPOSALS.md modified +274 -0

@@ -128,6 +128,280 @@
128128
129129 Met in sauce's `Ssha.short`, which takes `var h: Hex` now.
130130
131+## Marking what a procedure does with what it is given: `ret` and `keep`
132+
133+### The hole
134+
135+A procedure's locals, its value parameters and the array literals it
136+passes as slices live in its frame, which is gone once it returns. A
137+slice that still points there afterwards reads whatever the next call
138+put in its place, and writes into it. Section 7.3 catches the plain
139+case, a slice of a local returned at once:
140+
141+```
142+proc short(h: Hex): []U8; (* Hex = [64]U8 *)
143+begin
144+ return h[0..8]; (* an error: h is the procedure's own copy *)
145+end;
146+```
147+
148+It can't catch the same slice once it has passed through a call,
149+since the caller can't see what the callee returns a slice of:
150+
151+```
152+proc head(var h: Hex): []U8;
153+begin
154+ return h[0..8]; (* fine: h is the caller's *)
155+end;
156+
157+proc short: []U8;
158+var h: Hex;
159+begin
160+ return head(h); (* a slice of short's frame; not caught *)
161+end;
162+```
163+
164+Nor the other way, a slice given to a procedure that keeps it:
165+
166+```
167+proc remember(name: []U8);
168+var buf: [16]U8;
169+begin
170+ ...
171+ _ := Names.put(ADR(table), buf[0..n], 1); (* the map, on the heap, keeps a slice of buf *)
172+end;
173+```
174+
175+Section 10 leaves both undefined. The marks close them: each parameter
176+says what the procedure may do with what it is given, the compiler
177+holds the procedure's body to its heading, and a caller trusts the
178+heading without seeing the body. Each check needs only the procedure
179+it is in, so a one-pass compiler can make it.
180+
181+### Words
182+
183+**Carrying a slice.** A type carries a slice if it is a slice, or an
184+array, record or variant with one in it: `[]U8`, `[4][]U8`,
185+`Sys.Program`, `Fmt.Arg`. `INT`, `[64]U8` and every pointer type don't.
186+What a pointer points at is not followed: a slice reached through a
187+pointer, `p^.s`, is the heap's or a module's, as far as the marks
188+know.
189+
190+**Where a value points.** Every value that carries a slice has, for
191+the compiler, a set of places its slices may point into:
192+
193+- **the frame**, this call's own: a slice of a local array, of a
194+ value parameter's array, or an array literal used as a slice;
195+- **a parameter `p`**: what `p` lends. For a value parameter, that is
196+ what its slices point at, which is the caller's business, not this
197+ frame. For a `var` parameter, it is also the caller's variable
198+ itself: `h[0..8]` of `var h: Hex` points into `h`.
199+- **nowhere that ends**: module variables, constants, string literals,
200+ and anything reached through a pointer.
201+
202+A value made from others points wherever any of them does: a slice, a
203+field, an element or a view of `x` points where `x` does; a record, a
204+variant or an array literal built from parts, wherever the parts do; a
205+call's result, wherever its arguments for `ret` parameters do (below).
206+A local variable that carries a slice points wherever anything
207+assigned to it anywhere in the procedure does, wherever the assignment
208+stands, since a loop can run a `return` after an assignment written
209+below it. **Frame-bound** means pointing into the frame.
210+
211+**Outliving the call.** A place outlives the call if it is a module
212+variable, anything reached through a pointer (`p^.f`, `p^[i]`), or a
213+`var` parameter of this procedure. A value stored there, or returned,
214+leaves the frame.
215+
216+### No mark
217+
218+A parameter without a mark is only used while the call runs. The
219+procedure may read it, copy its bytes (`COPY`, `Buf.str`), keep it in
220+locals and pass it to other calls' unmarked or `ret` parameters. It
221+may not return what it points at, nor store it anywhere that outlives
222+the call. This is what most procedures do: `Str.eq`, `Str.find`,
223+`Buf.str`, `Sys.write`, `Fmt.print`, `Map.get`'s key.
224+
225+### `ret`
226+
227+```
228+proc trim(ret s: []U8): []U8;
229+```
230+
231+`ret s` says: the results may point where `s` points. In the body,
232+returning a slice of `s` is allowed; in the caller, the result points
233+wherever the argument given for `s` did.
234+
235+```
236+proc greet: []U8;
237+var line: [80]U8; n: INT;
238+begin
239+ n := Sys.read(Sys.stdin, line);
240+ Out.str(Str.trim(line[0..n])); (* fine: used here, in the frame *)
241+ return Str.trim(line[0..n]); (* an error: frame-bound through trim's ret *)
242+end;
243+
244+proc word(ret s: []U8): []U8;
245+begin
246+ return Str.trim(s); (* fine: points where s does, and s is ret *)
247+end;
248+
249+proc word2(s: []U8): []U8;
250+begin
251+ return Str.trim(s); (* an error: s isn't ret here *)
252+end;
253+```
254+
255+`ret` is allowed on a parameter that carries a slice, or on a `var`
256+parameter whose type holds an array (it lends the caller's variable),
257+of a procedure with a result that carries a slice. It is an error
258+elsewhere:
259+
260+- `ret n: INT`, `ret a: [4]U8`: nothing to lend. A value array is the
261+ procedure's own copy, and a slice of it can never be returned.
262+- `ret` on a procedure whose results carry no slice: nothing to lend
263+ it to.
264+
265+With several results, `ret` covers them all: in `cut(ret s: []U8;
266+sep: []U8): ([]U8, []U8, BOOL)` both slices may point into `s`. With
267+several `ret` parameters, a result may point where any of them does,
268+and the caller takes the result as pointing where all of their
269+arguments do.
270+
271+### `keep`
272+
273+```
274+proc put(m: ^Map; keep k: K; keep v: V): BOOL;
275+```
276+
277+`keep k` says: what `k` points at may outlive the call; the procedure
278+may store it in a place that does. In the caller, an argument for a
279+`keep` parameter leaves the frame as a store would: it must not be
280+frame-bound, and if it points where one of the caller's own
281+parameters does, that parameter must be `keep` too.
282+
283+```
284+(* import Names := Map([]U8, INT, Str.hash, Str.eq);
285+ table: Names.Map and heap: Heap's allocator, module variables *)
286+proc count(name: []U8);
287+var key: [32]U8;
288+begin
289+ _ := Names.put(ADR(table), name, 1); (* an error: name isn't keep here *)
290+ _ := Names.put(ADR(table), key[0..4], 1); (* an error: frame-bound to a keep *)
291+ _ := Names.put(ADR(table), "main", 1); (* fine: a literal never ends *)
292+ _ := Names.put(ADR(table), Str.copy(ADR(heap), name), 1); (* fine: copied to the heap *)
293+end;
294+```
295+
296+`keep` is allowed on a parameter that carries a slice or is a `var`
297+parameter whose type holds an array; on another, `keep n: INT`, it
298+is an error, as `ret` is.
299+
300+One exception keeps the common loop free of marks: a `var` parameter
301+may be given a value that points only where it already pointed, so
302+`s := s[1..LEN(s)]` with `var s: []U8` needs no `keep`. The caller's
303+variable is no worse off.
304+
305+### Both, and the order
306+
307+`ret keep v: T` is both. The marks come before `var`, and each
308+mark covers the group it starts, so `(ret s, t: []U8)` marks both;
309+`(ret s: []U8; sep: []U8)` marks only `s`. `ret` and `keep` are not
310+reserved words: they are marks only before a parameter's name, as
311+`static` is a mark only in `link static;`. `proc f(ret: INT)` is a
312+parameter named `ret`.
313+
314+### What the compiler checks, all of it
315+
316+In each procedure:
317+
318+1. `return e`, each result: `e` must not be frame-bound, and every
319+ parameter it points at must be `ret`.
320+2. A store into a place that outlives the call, `M.x := e`,
321+ `p^.f := e`, or `v := e` for a `var` parameter `v`: `e` must not be
322+ frame-bound, and every parameter it points at must be `keep`, but
323+ for the exception above. `COPY` into such a place is no such store:
324+ it copies bytes, not where they point.
325+3. An argument for a call's `keep` parameter, as 2.
326+4. A heading's marks are allowed where it says so above.
327+
328+Checks 1 to 3 on a local variable's value wait for the procedure's
329+`end`, when everything assigned to it is known.
330+
331+### Procedure values, generic modules, and the rest
332+
333+**Procedure types** carry the marks, `proc(ret s: []U8): []U8`. A
334+procedure may be given where its type has the same marks or more,
335+since a mark promises the caller less: a procedure without `ret` can
336+stand where `ret` was allowed, not the other way.
337+
338+**Generic modules.** A mark is allowed on a parameter whose type is a
339+module parameter, `keep v: T`, whatever `T` will be. The body is
340+checked in each instance: `Map(INT, INT)`'s `put` would pass without
341+`keep`, `Map([]U8, INT)`'s wouldn't, so the library marks its generic
342+modules for the instance that needs it.
343+
344+**`extern` procedures** take no marks: C is outside the promise of
345+section 10 already. **Code procedures** take them, and are trusted:
346+there is no body to check.
347+
348+**`ADR`** gives a pointer, which the marks don't follow; a pointer to
349+a local that outlives it stays undefined, as section 10 says. So does
350+a slice reached through such a pointer, and a thread given a pointer
351+to its spawner's locals.
352+
353+### False alarms
354+
355+The rules are sure, not exact: they reject some programs that were
356+right.
357+
358+- **A record that lives in the frame, filled through a pointer.**
359+ `examples/Sh.raz` does
360+
361+ ```
362+ Opt.init(ADR(p), "Sh", [Opt.usage(...), Opt.option(...)]);
363+ ```
364+
365+ `Opt.init` keeps its table in `p^`, so it needs `keep table`, and
366+ the array literal is frame-bound: an error, though `p` is a local
367+ and dies with the literal. `keep` can't say "as long as `p^`";
368+ saying that needs lifetimes on records, Rust's whole apparatus, which
369+ is not proposed. The way out: the table in a module variable.
370+- **A local variable used twice.** One slice variable that holds a
371+ slice of a local buffer early on and a result to return later is
372+ frame-bound throughout. Two variables cure it.
373+- **Several `ret` parameters.** A result is taken to point at all of
374+ them, though it may point at one.
375+
376+### What it breaks
377+
378+Every procedure, in the library or a program, that returns or keeps
379+what a parameter points at stops compiling until it is marked: a
380+thaw. In the library, by a search of the headings: `Str.trim`,
381+`trimLeft`, `trimRight`, `cut` and `field` (`ret`); `Map.put` and
382+`List.push` (`keep`); `Opt.init` (`keep`), and `Sh`'s call of it, as
383+above. Not marked: `Str.copy` (its result is the allocator's),
384+`Sys.env` and `readLink` (their own buffer), `Buf.str` and
385+`Fmt.write` (they copy), `Map.get`. In sauce, `Svec.push` and `add`
386+(`keep`), and what returns a slice of its argument.
387+
388+### What it costs
389+
390+Reports: 5.9 (procedure types), 7.3, 8 (parameters), 10 (the promise,
391+whose exceptions shrink to `ADR`, C, code procedures and `check
392+off`), appendix B. The compiler: a set of the places a value points
393+into, carried with each value as its type is, a 64-bit set (the frame
394+and up to 63 parameters); the same on each local; the checks above,
395+and a list per procedure of the checks that wait for its `end`.
396+Perhaps 250 to 400 lines; no cost at run time, and little at compile
397+time.
398+
399+Precedent: D's `scope` and `return scope` (DIP1000) are `keep`'s
400+opposite and `ret`; Rust's lifetime elision reads a call's result as
401+`ret` does. D's experience is that the corners (fields, generics,
402+procedure values) take years; the narrow form here leaves record
403+lifetimes out, and so the first false alarm above, on purpose.
404+
131405 ## `Vec(T)`, a growable array
132406
133407 The library has `List`, linked, and `Map`, a hash; nothing that grows