From b7667d813edf29657e2e0378def73b137cd3f463 Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Mon, 26 Oct 2026 11:37:59 +0000 Subject: [PATCH] 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. --- diff --git a/CLAUDE.md b/CLAUDE.md index 3960455..be7e1b3 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -306,9 +306,11 @@ an array literal (report 2026-10.36), an array constant of its own in a chunk with no name, nested to any depth. An `ERROR` constant given another takes its chunk: the same error. -- An array a call returns is a temporary of the statement (report - 2026-10.28, 8): a slice of it kept past the statement is undefined - (10), and isn't caught. +- An array a call returns, or a record or variant holding one, and a + `const` parameter's temporary holding one, is a slot of the + procedure (report 2026-10.37, 8: `Sym.holdsArray`, `allocPerm`): a + slice of it may be kept to the end of the procedure, and returning + it is caught only as 7.3 catches a slice of the frame. - `Buf.add`, `str` and `spare`, and `Vec`'s growing, stop the program when the allocator has no room; `room` asks first. - `Sys.handle`'s handler only notes the signal, with `XCHG`; it is diff --git a/conform/run/CallArrayKept.expect b/conform/run/CallArrayKept.expect new file mode 100644 index 0000000..0f9b7ba --- /dev/null +++ b/conform/run/CallArrayKept.expect @@ -0,0 +1 @@ +name-00x name-00x name-00a name-00b name-00c name-00d diff --git a/conform/run/CallArrayKept.raz b/conform/run/CallArrayKept.raz new file mode 100644 index 0000000..6d944aa --- /dev/null +++ b/conform/run/CallArrayKept.raz @@ -0,0 +1,52 @@ +(* An array a call returns, or a record holding one, lives to the end of + the procedure the call is in (report section 8): a slice of it kept in + a variable still reads it after later statements, each call has its + own, and a const parameter's temporary holding one lives as long. *) +module CallArrayKept; +import Out; + +type R = record n: INT; a: [8]U8; end; + +(* "name-00" and c *) +proc name(c: U8): [8]U8; +var a: [8]U8; +begin + a := "name-000"; + a[7] := c; + return a; +end; + +(* A record holding name(c) *) +proc rec(c: U8): R; +begin + return {n: 1, a: name(c)}; +end; + +(* The array in r, as a slice *) +proc bytes(const r: R): []U8; +begin + return r.a[0..8]; +end; + +proc main*; +var s, t, u, v: []U8; i: INT; +begin + s := name('a')[0..8]; + t := name('b')[0..8]; + u := rec('c').a[0..8]; + v := bytes(rec('d')); + for i := 0 to 1 do + Out.str(name('x')[0..8]); + Out.char(' '); + end; + Out.str(s); + Out.char(' '); + Out.str(t); + Out.char(' '); + Out.str(u); + Out.char(' '); + Out.str(v); + Out.char(10); +end; + +end CallArrayKept. diff --git a/doc/compiler.md b/doc/compiler.md index 775c4e7..ab2c5e0 100644 --- a/doc/compiler.md +++ b/doc/compiler.md @@ -1503,9 +1503,13 @@ the epilogue and patches the frame's size in. Within a statement, scratch space comes from `allocTemp` and is given back by `releaseTemps`; `allocPerm` reserves space for the whole procedure. -An array a call returns is such a temporary, so it lives to the end of -its statement, as the report says (section 8), and a slice of it kept -past that reads what the next statement puts there. +A result that holds an array (`Sym.holdsArray`), and a `const` +parameter's temporary that holds one (`constTemp`), is given an +`allocPerm` slot instead, so that it lives to the end of the procedure, +as the report says (section 8): a slice of it may be kept in a local. +Before, it was an `allocTemp` slot, and the next statement wrote over +it. Each call has a slot of its own, so the frame grows by each such +result; in a loop, one call's slot holds its latest result. Register variables: the parser chooses them (`chooseRegs`, in Parse.raz), then gives the locals left in memory their slots diff --git a/doc/raz.7 b/doc/raz.7 index c038304..9298052 100644 --- a/doc/raz.7 +++ b/doc/raz.7 @@ -626,7 +626,8 @@ returns several results. Returning a slice of a local array, of a value parameter, or of an array literal is an error: the frame it points into is gone [7.3]. -An array a call returns lives to the end of its statement [8]. +An array a call returns lives to the end of the procedure the call +is in [8]. A procedure is a value of its type, .Cm "proc(a, b: INT): BOOL" , or nil. diff --git a/doc/raz.md b/doc/raz.md index fa50251..fdbfa73 100644 --- a/doc/raz.md +++ b/doc/raz.md @@ -13,7 +13,7 @@ This report defines the language. It is short on purpose. Whatever it doesn't allow, raz doesn't have. -This is version 2026-10.36 of the report; section 11 says where each +This is version 2026-10.37 of the report; section 11 says where each version's changes are told. 2026-10.11 and 2026-10.12 are a thaw the owner chose, for a language anyone can build again. They break programs written for 2026-10.10 and before: for 2026-10.9, say, the report of compiler @@ -62,6 +62,10 @@ parameter's slice returned (section 7.3); and a procedure type's parameter or result may name a type declared later in its `type` section (sections 4 and 5.9); it breaks no program. +2026-10.37 mends a defect: an array a call returns, or a record or +variant holding one, lives to the end of the procedure the call is in, +not of its statement, so a slice of it may be kept (section 8); it +breaks no program. The language is frozen, softly, from this version on: it changes only to mend a defect, and its library grows only by what breaks no program @@ -1314,7 +1318,7 @@ A slice of a `var` or `const` parameter, of a module variable, or of what a pointer points at may be returned: none of them is this procedure's frame. For a `const` parameter given a temporary, the slice -lives to the end of the caller's statement (section 8). A slice of a +lives to the end of the caller's procedure (section 8). A slice of a frame that leaves it some other way isn't caught, and is the exception of section 10: one held in a local slice variable, or in a record, that is returned; one stored in a module variable or through a pointer; and one that came back from a call, as @@ -1602,8 +1606,8 @@ written through. `const` keeps the procedure from changing the variable by its name, not every path to its bytes. Its argument is any expression of the parameter's type: a variable or a constant is passed in place, anything -else as a temporary of the statement, as an array a call returns is -(below). If the variable changes while the procedure runs, through a +else as a temporary, of the procedure if it holds an array, as an +array a call returns is (below). If the variable changes while the procedure runs, through a `var` parameter, a pointer or another thread, the procedure sees the change. @@ -1634,14 +1638,17 @@ procedure that can fail has an `ERROR` as its last result (section 5.8), which `try` passes on (section 7.8). -An array a call returns lives to the end of the statement the call is -in. A slice of it may be used there, indexed, passed or compared; one -kept past the statement, in a variable or anywhere else, points at -what is gone, and is the exception of section 10: +An array a call returns, or a record or variant holding one, is a +hidden variable of the procedure the call is in, as an array literal +used as a slice is (section 6.5), and lives to the end of that +procedure's call. A slice of it may be kept in a variable and used for +the rest of the procedure; in a loop, the hidden variable holds what +the latest call returned. Like any slice of the frame, it may not be +returned (section 7.3): ``` -s := f('a')[0..4]; (* f returns a [8]U8: s is undefined after this *) -Out.str(f('a')[0..4]); (* used in its statement: defined *) +s := f('a')[0..4]; (* f returns a [8]U8: s is good to the end of the procedure *) +t := f('b')[0..4]; (* a hidden variable of its own: s is unchanged *) ``` ``` @@ -1798,9 +1805,7 @@ defined by this report, with one exception: memory reached other than through a checked variable. Dereferencing a stale or made-up pointer, indexing through a pointer, a slice kept after the call whose frame -holds what it points at (section 7.3), a slice of an array a call -returned, or of a temporary given to a `const` parameter, kept past -its statement (section 8), an index out of range in a +holds what it points at (section 7.3), an index out of range in a module that says `check off;`, writing a string's bytes, a code procedure that breaks its rules, and C code can read or write any memory, and what follows isn't defined. Dereferencing `nil`, dividing diff --git a/doc/report-history.md b/doc/report-history.md index 6d3eee8..a06a35c 100644 --- a/doc/report-history.md +++ b/doc/report-history.md @@ -259,3 +259,9 @@ later in the same `type` section, so that a record's callback can take the record (sections 4 and 5.9). Appendix A says what a wipe can't reach. It breaks no program. +- **2026-10.37.** A defect mended: an array a call returns, or a + record or variant holding one, lives to the end of the procedure the + call is in, not of its statement, and so does a `const` parameter's + temporary holding one; a slice of it may be kept in a variable for + the rest of the procedure (sections 7.3, 8 and 10). It breaks no + program. diff --git a/src/Gen.raz b/src/Gen.raz index 27707f4..fe65534 100644 --- a/src/Gen.raz +++ b/src/Gen.raz @@ -36,7 +36,8 @@ (behindPointer), where C would send a small struct back in registers. A procedure's frame holds its parameters, its variables, and the - temporaries of the statement running. The variables are zeroed on + temporaries of the statement running, and the results of its calls + that hold an array (report 8). The variables are zeroed on entry. Depends on: Scan and Diag for errors, Sym for types, Chunk for code @@ -311,6 +312,7 @@ end; proc loaded(i: INT): INT; forward; +proc allocPerm*(size, align: I64): I64; forward; proc allocTemp*(size, align: I64): I64; forward; (* The value i, in a register, goes to a frame slot; one noted that @@ -2091,7 +2093,12 @@ res := c.sig^.base; c.ret := 0; ni := 0; nf := 0; - if (res # nil) and (Sym.behindPointer(res) or (res^.form = Sym.fSlice)) then + if (res # nil) and Sym.holdsArray(res) then + (* a slice of it may be kept for the rest of the procedure + (report 8), so its slot lasts as long *) + c.ret := allocPerm(res^.size, res^.align); + ni := 1; + elsif (res # nil) and (Sym.behindPointer(res) or (res^.form = Sym.fSlice)) then c.ret := allocTemp(res^.size, res^.align); if Sym.behindPointer(res) then ni := 1; end; end; diff --git a/src/Item.raz b/src/Item.raz index 96b2127..21723f3 100644 --- a/src/Item.raz +++ b/src/Item.raz @@ -1896,8 +1896,8 @@ (* A const parameter's argument that isn't a variable, or is one with no address of its own (a packed record's field, a view, through ^VOLATILE), - in a temporary of the statement, whose address then goes (report - section 8): a slice's two words, a record or an array copied, or a + in a temporary of the statement, or of the procedure if it holds an + array, whose address then goes (report section 8): a slice's two words, a record or an array copied, or a single value stored. *) proc constTemp(var x: Item): ERROR; var t: I64; @@ -1909,7 +1909,13 @@ Gen.popVar(frameAdr(t), 8); elsif Sym.isAggregate(x.typ) then if x.view then return noAddress(x, "a view of a record or an array can't be a const parameter"); end; - return copyToTemp(x); + if not Sym.holdsArray(x.typ) then return copyToTemp(x); end; + (* a slice of it may come back and be kept to the end of the + procedure (report 8), so its slot lasts as long *) + if x.vol then return Scan.markAt(x.pos, "can't copy through a volatile pointer"); end; + try address(x); + t := Gen.allocPerm(x.typ^.size, x.typ^.align); + Gen.copyTo(frameAdr(t), x.typ^.size); else try value(x); t := Gen.allocTemp(x.typ^.size, x.typ^.align); diff --git a/src/Raz.raz b/src/Raz.raz index ae05e0c..3c2c9be 100644 --- a/src/Raz.raz +++ b/src/Raz.raz @@ -32,7 +32,7 @@ import Sys, Out, Version, Files, Fmt, Diag, Scan, Sym, Chunk, X64, Gen, Parse, Link, Elf; const - Report = "2026-10.36"; (* the version of doc/raz.md it follows *) + Report = "2026-10.37"; (* the version of doc/raz.md it follows *) MemfdCreate = 319; (* the system call *) MfdCloexec = 1; (* an ELF still executes: the kernel opens it first *) Execve = 59; diff --git a/src/Sym.raz b/src/Sym.raz index e53dd25..8fb71ea 100644 --- a/src/Sym.raz +++ b/src/Sym.raz @@ -431,6 +431,33 @@ return t^.form in {fArray, fSlice, fRecord, fVariant}; end; +(* Whether t is an array, or a record or variant with one among its + fields, at any depth: a value a slice may be cut from (report 8, an + array a call returns) *) +proc holdsArray*(t: ^Type): BOOL; +var f, c: ^Obj; +begin + if t^.form = fArray then return true; end; + if t^.form = fRecord then + f := t^.fields; + while f # nil do + if holdsArray(f^.typ) then return true; end; + f := f^.next; + end; + elsif t^.form = fVariant then + c := t^.fields; + while c # nil do + f := c^.fields; + while f # nil do + if holdsArray(f^.typ) then return true; end; + f := f^.next; + end; + c := c^.next; + end; + end; + return false; +end; + (* Whether a result field f is a single value of at most a word, not a real *) proc oneWord(f: ^Obj): BOOL; begin