raz / commit b7667d81

commit
b7667d813edf29657e2e0378def73b137cd3f463
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
14276032
Report 2026-10.37: an array a call returns lives to the end of its procedure

A result that holds an array (Sym.holdsArray), and a const parameter's
temporary that holds one, takes an allocPerm slot, not a statement's
temporary, so a slice of it kept in a local reads it to the end of the
procedure; section 10 loses that case. conform/run/CallArrayKept holds
it. The safety thaw's first piece; it breaks no program.

patch · log

11 files changed, +137 -26

CLAUDE.md modified +5 -3

@@ -306,9 +306,11 @@
306306 an array literal (report 2026-10.36), an array constant of its own in
307307 a chunk with no name, nested to any depth. An `ERROR` constant given
308308 another takes its chunk: the same error.
309-- An array a call returns is a temporary of the statement (report
310- 2026-10.28, 8): a slice of it kept past the statement is undefined
311- (10), and isn't caught.
309+- An array a call returns, or a record or variant holding one, and a
310+ `const` parameter's temporary holding one, is a slot of the
311+ procedure (report 2026-10.37, 8: `Sym.holdsArray`, `allocPerm`): a
312+ slice of it may be kept to the end of the procedure, and returning
313+ it is caught only as 7.3 catches a slice of the frame.
312314 - `Buf.add`, `str` and `spare`, and `Vec`'s growing, stop the program
313315 when the allocator has no room; `room` asks first.
314316 - `Sys.handle`'s handler only notes the signal, with `XCHG`; it is

conform/run/CallArrayKept.expect (history) added +1 -0

@@ -0,0 +1 @@
1+name-00x name-00x name-00a name-00b name-00c name-00d

conform/run/CallArrayKept.raz (history) added +52 -0

@@ -0,0 +1,52 @@
1+(* An array a call returns, or a record holding one, lives to the end of
2+ the procedure the call is in (report section 8): a slice of it kept in
3+ a variable still reads it after later statements, each call has its
4+ own, and a const parameter's temporary holding one lives as long. *)
5+module CallArrayKept;
6+import Out;
7+
8+type R = record n: INT; a: [8]U8; end;
9+
10+(* "name-00" and c *)
11+proc name(c: U8): [8]U8;
12+var a: [8]U8;
13+begin
14+ a := "name-000";
15+ a[7] := c;
16+ return a;
17+end;
18+
19+(* A record holding name(c) *)
20+proc rec(c: U8): R;
21+begin
22+ return {n: 1, a: name(c)};
23+end;
24+
25+(* The array in r, as a slice *)
26+proc bytes(const r: R): []U8;
27+begin
28+ return r.a[0..8];
29+end;
30+
31+proc main*;
32+var s, t, u, v: []U8; i: INT;
33+begin
34+ s := name('a')[0..8];
35+ t := name('b')[0..8];
36+ u := rec('c').a[0..8];
37+ v := bytes(rec('d'));
38+ for i := 0 to 1 do
39+ Out.str(name('x')[0..8]);
40+ Out.char(' ');
41+ end;
42+ Out.str(s);
43+ Out.char(' ');
44+ Out.str(t);
45+ Out.char(' ');
46+ Out.str(u);
47+ Out.char(' ');
48+ Out.str(v);
49+ Out.char(10);
50+end;
51+
52+end CallArrayKept.

doc/compiler.md (history) modified +7 -3

@@ -1503,9 +1503,13 @@
15031503 the epilogue and patches the frame's size in. Within a statement,
15041504 scratch space comes from `allocTemp` and is given back by
15051505 `releaseTemps`; `allocPerm` reserves space for the whole procedure.
1506-An array a call returns is such a temporary, so it lives to the end of
1507-its statement, as the report says (section 8), and a slice of it kept
1508-past that reads what the next statement puts there.
1506+A result that holds an array (`Sym.holdsArray`), and a `const`
1507+parameter's temporary that holds one (`constTemp`), is given an
1508+`allocPerm` slot instead, so that it lives to the end of the procedure,
1509+as the report says (section 8): a slice of it may be kept in a local.
1510+Before, it was an `allocTemp` slot, and the next statement wrote over
1511+it. Each call has a slot of its own, so the frame grows by each such
1512+result; in a loop, one call's slot holds its latest result.
15091513
15101514 Register variables: the parser chooses them (`chooseRegs`, in
15111515 Parse.raz), then gives the locals left in memory their slots

doc/raz.7 (history) modified +2 -1

@@ -626,7 +626,8 @@
626626 returns several results.
627627 Returning a slice of a local array, of a value parameter, or of an
628628 array literal is an error: the frame it points into is gone [7.3].
629-An array a call returns lives to the end of its statement [8].
629+An array a call returns lives to the end of the procedure the call
630+is in [8].
630631 A procedure is a value of its type,
631632 .Cm "proc(a, b: INT): BOOL" ,
632633 or nil.

doc/raz.md (history) modified +18 -13

@@ -13,7 +13,7 @@
1313 This report defines the language. It is short on purpose. Whatever it
1414 doesn't allow, raz doesn't have.
1515
16-This is version 2026-10.36 of the report; section 11 says where each
16+This is version 2026-10.37 of the report; section 11 says where each
1717 version's changes are told. 2026-10.11 and 2026-10.12 are a thaw the owner chose,
1818 for a language anyone can build again. They break programs written
1919 for 2026-10.10 and before: for 2026-10.9, say, the report of compiler
@@ -62,6 +62,10 @@
6262 parameter's slice returned (section 7.3); and a procedure type's
6363 parameter or result may name a type declared later in its `type`
6464 section (sections 4 and 5.9); it breaks no program.
65+2026-10.37 mends a defect: an array a call returns, or a record or
66+variant holding one, lives to the end of the procedure the call is in,
67+not of its statement, so a slice of it may be kept (section 8); it
68+breaks no program.
6569
6670 The language is frozen, softly, from this version on: it changes only
6771 to mend a defect, and its library grows only by what breaks no program
@@ -1314,7 +1318,7 @@
13141318 A slice of a `var` or `const` parameter, of a module variable, or of
13151319 what a pointer points at may be returned: none of them is this
13161320 procedure's frame. For a `const` parameter given a temporary, the slice
1317-lives to the end of the caller's statement (section 8). A slice of a
1321+lives to the end of the caller's procedure (section 8). A slice of a
13181322 frame that leaves it some other way isn't caught, and is the
13191323 exception of section 10: one held in a local slice variable, or in a record, that is returned; one stored in a module
13201324 variable or through a pointer; and one that came back from a call, as
@@ -1602,8 +1606,8 @@
16021606 written through. `const` keeps the procedure from changing the
16031607 variable by its name, not every path to its bytes. Its argument is any expression of the
16041608 parameter's type: a variable or a constant is passed in place, anything
1605-else as a temporary of the statement, as an array a call returns is
1606-(below). If the variable changes while the procedure runs, through a
1609+else as a temporary, of the procedure if it holds an array, as an
1610+array a call returns is (below). If the variable changes while the procedure runs, through a
16071611 `var` parameter, a pointer or another thread, the procedure sees the
16081612 change.
16091613
@@ -1634,14 +1638,17 @@
16341638 procedure that can fail has an `ERROR` as its last result (section
16351639 5.8), which `try` passes on (section 7.8).
16361640
1637-An array a call returns lives to the end of the statement the call is
1638-in. A slice of it may be used there, indexed, passed or compared; one
1639-kept past the statement, in a variable or anywhere else, points at
1640-what is gone, and is the exception of section 10:
1641+An array a call returns, or a record or variant holding one, is a
1642+hidden variable of the procedure the call is in, as an array literal
1643+used as a slice is (section 6.5), and lives to the end of that
1644+procedure's call. A slice of it may be kept in a variable and used for
1645+the rest of the procedure; in a loop, the hidden variable holds what
1646+the latest call returned. Like any slice of the frame, it may not be
1647+returned (section 7.3):
16411648
16421649 ```
1643-s := f('a')[0..4]; (* f returns a [8]U8: s is undefined after this *)
1644-Out.str(f('a')[0..4]); (* used in its statement: defined *)
1650+s := f('a')[0..4]; (* f returns a [8]U8: s is good to the end of the procedure *)
1651+t := f('b')[0..4]; (* a hidden variable of its own: s is unchanged *)
16451652 ```
16461653
16471654 ```
@@ -1798,9 +1805,7 @@
17981805 defined by this report, with one exception: memory reached other than
17991806 through a checked variable. Dereferencing a stale or made-up pointer,
18001807 indexing through a pointer, a slice kept after the call whose frame
1801-holds what it points at (section 7.3), a slice of an array a call
1802-returned, or of a temporary given to a `const` parameter, kept past
1803-its statement (section 8), an index out of range in a
1808+holds what it points at (section 7.3), an index out of range in a
18041809 module that says `check off;`, writing a string's bytes, a code procedure that
18051810 breaks its rules, and C code can read or write any memory, and what
18061811 follows isn't defined. Dereferencing `nil`, dividing

doc/report-history.md (history) modified +6 -0

@@ -259,3 +259,9 @@
259259 later in the same `type` section, so that a record's callback can
260260 take the record (sections 4 and 5.9). Appendix A says what a wipe
261261 can't reach. It breaks no program.
262+- **2026-10.37.** A defect mended: an array a call returns, or a
263+ record or variant holding one, lives to the end of the procedure the
264+ call is in, not of its statement, and so does a `const` parameter's
265+ temporary holding one; a slice of it may be kept in a variable for
266+ the rest of the procedure (sections 7.3, 8 and 10). It breaks no
267+ program.

src/Gen.raz modified +9 -2

@@ -36,7 +36,8 @@
3636 (behindPointer), where C would send a small struct back in registers.
3737
3838 A procedure's frame holds its parameters, its variables, and the
39- temporaries of the statement running. The variables are zeroed on
39+ temporaries of the statement running, and the results of its calls
40+ that hold an array (report 8). The variables are zeroed on
4041 entry.
4142
4243 Depends on: Scan and Diag for errors, Sym for types, Chunk for code
@@ -311,6 +312,7 @@
311312 end;
312313
313314 proc loaded(i: INT): INT; forward;
315+proc allocPerm*(size, align: I64): I64; forward;
314316 proc allocTemp*(size, align: I64): I64; forward;
315317
316318 (* The value i, in a register, goes to a frame slot; one noted that
@@ -2091,7 +2093,12 @@
20912093 res := c.sig^.base;
20922094 c.ret := 0;
20932095 ni := 0; nf := 0;
2094- if (res # nil) and (Sym.behindPointer(res) or (res^.form = Sym.fSlice)) then
2096+ if (res # nil) and Sym.holdsArray(res) then
2097+ (* a slice of it may be kept for the rest of the procedure
2098+ (report 8), so its slot lasts as long *)
2099+ c.ret := allocPerm(res^.size, res^.align);
2100+ ni := 1;
2101+ elsif (res # nil) and (Sym.behindPointer(res) or (res^.form = Sym.fSlice)) then
20952102 c.ret := allocTemp(res^.size, res^.align);
20962103 if Sym.behindPointer(res) then ni := 1; end;
20972104 end;

src/Item.raz (history) modified +9 -3

@@ -1896,8 +1896,8 @@
18961896
18971897 (* A const parameter's argument that isn't a variable, or is one with no
18981898 address of its own (a packed record's field, a view, through ^VOLATILE),
1899- in a temporary of the statement, whose address then goes (report
1900- section 8): a slice's two words, a record or an array copied, or a
1899+ in a temporary of the statement, or of the procedure if it holds an
1900+ array, whose address then goes (report section 8): a slice's two words, a record or an array copied, or a
19011901 single value stored. *)
19021902 proc constTemp(var x: Item): ERROR;
19031903 var t: I64;
@@ -1909,7 +1909,13 @@
19091909 Gen.popVar(frameAdr(t), 8);
19101910 elsif Sym.isAggregate(x.typ) then
19111911 if x.view then return noAddress(x, "a view of a record or an array can't be a const parameter"); end;
1912- return copyToTemp(x);
1912+ if not Sym.holdsArray(x.typ) then return copyToTemp(x); end;
1913+ (* a slice of it may come back and be kept to the end of the
1914+ procedure (report 8), so its slot lasts as long *)
1915+ if x.vol then return Scan.markAt(x.pos, "can't copy through a volatile pointer"); end;
1916+ try address(x);
1917+ t := Gen.allocPerm(x.typ^.size, x.typ^.align);
1918+ Gen.copyTo(frameAdr(t), x.typ^.size);
19131919 else
19141920 try value(x);
19151921 t := Gen.allocTemp(x.typ^.size, x.typ^.align);

src/Raz.raz (history) modified +1 -1

@@ -32,7 +32,7 @@
3232 import Sys, Out, Version, Files, Fmt, Diag, Scan, Sym, Chunk, X64, Gen, Parse, Link, Elf;
3333
3434 const
35- Report = "2026-10.36"; (* the version of doc/raz.md it follows *)
35+ Report = "2026-10.37"; (* the version of doc/raz.md it follows *)
3636 MemfdCreate = 319; (* the system call *)
3737 MfdCloexec = 1; (* an ELF still executes: the kernel opens it first *)
3838 Execve = 59;

src/Sym.raz (history) modified +27 -0

@@ -431,6 +431,33 @@
431431 return t^.form in {fArray, fSlice, fRecord, fVariant};
432432 end;
433433
434+(* Whether t is an array, or a record or variant with one among its
435+ fields, at any depth: a value a slice may be cut from (report 8, an
436+ array a call returns) *)
437+proc holdsArray*(t: ^Type): BOOL;
438+var f, c: ^Obj;
439+begin
440+ if t^.form = fArray then return true; end;
441+ if t^.form = fRecord then
442+ f := t^.fields;
443+ while f # nil do
444+ if holdsArray(f^.typ) then return true; end;
445+ f := f^.next;
446+ end;
447+ elsif t^.form = fVariant then
448+ c := t^.fields;
449+ while c # nil do
450+ f := c^.fields;
451+ while f # nil do
452+ if holdsArray(f^.typ) then return true; end;
453+ f := f^.next;
454+ end;
455+ c := c^.next;
456+ end;
457+ end;
458+ return false;
459+end;
460+
434461 (* Whether a result field f is a single value of at most a word, not a real *)
435462 proc oneWord(f: ^Obj): BOOL;
436463 begin