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.
| @@ -306,9 +306,11 @@ | ||
| 306 | 306 | an array literal (report 2026-10.36), an array constant of its own in |
| 307 | 307 | a chunk with no name, nested to any depth. An `ERROR` constant given |
| 308 | 308 | 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. | |
| 312 | 314 | - `Buf.add`, `str` and `spare`, and `Vec`'s growing, stop the program |
| 313 | 315 | when the allocator has no room; `room` asks first. |
| 314 | 316 | - `Sys.handle`'s handler only notes the signal, with `XCHG`; it is |
| @@ -0,0 +1 @@ | ||
| 1 | +name-00x name-00x name-00a name-00b name-00c name-00d |
| @@ -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. |
| @@ -1503,9 +1503,13 @@ | ||
| 1503 | 1503 | the epilogue and patches the frame's size in. Within a statement, |
| 1504 | 1504 | scratch space comes from `allocTemp` and is given back by |
| 1505 | 1505 | `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. | |
| 1509 | 1513 | |
| 1510 | 1514 | Register variables: the parser chooses them (`chooseRegs`, in |
| 1511 | 1515 | Parse.raz), then gives the locals left in memory their slots |
| @@ -626,7 +626,8 @@ | ||
| 626 | 626 | returns several results. |
| 627 | 627 | Returning a slice of a local array, of a value parameter, or of an |
| 628 | 628 | 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]. | |
| 630 | 631 | A procedure is a value of its type, |
| 631 | 632 | .Cm "proc(a, b: INT): BOOL" , |
| 632 | 633 | or nil. |
| @@ -13,7 +13,7 @@ | ||
| 13 | 13 | This report defines the language. It is short on purpose. Whatever it |
| 14 | 14 | doesn't allow, raz doesn't have. |
| 15 | 15 | |
| 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 | |
| 17 | 17 | version's changes are told. 2026-10.11 and 2026-10.12 are a thaw the owner chose, |
| 18 | 18 | for a language anyone can build again. They break programs written |
| 19 | 19 | for 2026-10.10 and before: for 2026-10.9, say, the report of compiler |
| @@ -62,6 +62,10 @@ | ||
| 62 | 62 | parameter's slice returned (section 7.3); and a procedure type's |
| 63 | 63 | parameter or result may name a type declared later in its `type` |
| 64 | 64 | 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. | |
| 65 | 69 | |
| 66 | 70 | The language is frozen, softly, from this version on: it changes only |
| 67 | 71 | to mend a defect, and its library grows only by what breaks no program |
| @@ -1314,7 +1318,7 @@ | ||
| 1314 | 1318 | A slice of a `var` or `const` parameter, of a module variable, or of |
| 1315 | 1319 | what a pointer points at may be returned: none of them is this |
| 1316 | 1320 | 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 | |
| 1318 | 1322 | frame that leaves it some other way isn't caught, and is the |
| 1319 | 1323 | exception of section 10: one held in a local slice variable, or in a record, that is returned; one stored in a module |
| 1320 | 1324 | variable or through a pointer; and one that came back from a call, as |
| @@ -1602,8 +1606,8 @@ | ||
| 1602 | 1606 | written through. `const` keeps the procedure from changing the |
| 1603 | 1607 | variable by its name, not every path to its bytes. Its argument is any expression of the |
| 1604 | 1608 | 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 | |
| 1607 | 1611 | `var` parameter, a pointer or another thread, the procedure sees the |
| 1608 | 1612 | change. |
| 1609 | 1613 | |
| @@ -1634,14 +1638,17 @@ | ||
| 1634 | 1638 | procedure that can fail has an `ERROR` as its last result (section |
| 1635 | 1639 | 5.8), which `try` passes on (section 7.8). |
| 1636 | 1640 | |
| 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): | |
| 1641 | 1648 | |
| 1642 | 1649 | ``` |
| 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 *) | |
| 1645 | 1652 | ``` |
| 1646 | 1653 | |
| 1647 | 1654 | ``` |
| @@ -1798,9 +1805,7 @@ | ||
| 1798 | 1805 | defined by this report, with one exception: memory reached other than |
| 1799 | 1806 | through a checked variable. Dereferencing a stale or made-up pointer, |
| 1800 | 1807 | 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 | |
| 1804 | 1809 | module that says `check off;`, writing a string's bytes, a code procedure that |
| 1805 | 1810 | breaks its rules, and C code can read or write any memory, and what |
| 1806 | 1811 | follows isn't defined. Dereferencing `nil`, dividing |
| @@ -259,3 +259,9 @@ | ||
| 259 | 259 | later in the same `type` section, so that a record's callback can |
| 260 | 260 | take the record (sections 4 and 5.9). Appendix A says what a wipe |
| 261 | 261 | 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. |
| @@ -36,7 +36,8 @@ | ||
| 36 | 36 | (behindPointer), where C would send a small struct back in registers. |
| 37 | 37 | |
| 38 | 38 | 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 | |
| 40 | 41 | entry. |
| 41 | 42 | |
| 42 | 43 | Depends on: Scan and Diag for errors, Sym for types, Chunk for code |
| @@ -311,6 +312,7 @@ | ||
| 311 | 312 | end; |
| 312 | 313 | |
| 313 | 314 | proc loaded(i: INT): INT; forward; |
| 315 | +proc allocPerm*(size, align: I64): I64; forward; | |
| 314 | 316 | proc allocTemp*(size, align: I64): I64; forward; |
| 315 | 317 | |
| 316 | 318 | (* The value i, in a register, goes to a frame slot; one noted that |
| @@ -2091,7 +2093,12 @@ | ||
| 2091 | 2093 | res := c.sig^.base; |
| 2092 | 2094 | c.ret := 0; |
| 2093 | 2095 | 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 | |
| 2095 | 2102 | c.ret := allocTemp(res^.size, res^.align); |
| 2096 | 2103 | if Sym.behindPointer(res) then ni := 1; end; |
| 2097 | 2104 | end; |
| @@ -1896,8 +1896,8 @@ | ||
| 1896 | 1896 | |
| 1897 | 1897 | (* A const parameter's argument that isn't a variable, or is one with no |
| 1898 | 1898 | 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 | |
| 1901 | 1901 | single value stored. *) |
| 1902 | 1902 | proc constTemp(var x: Item): ERROR; |
| 1903 | 1903 | var t: I64; |
| @@ -1909,7 +1909,13 @@ | ||
| 1909 | 1909 | Gen.popVar(frameAdr(t), 8); |
| 1910 | 1910 | elsif Sym.isAggregate(x.typ) then |
| 1911 | 1911 | 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); | |
| 1913 | 1919 | else |
| 1914 | 1920 | try value(x); |
| 1915 | 1921 | t := Gen.allocTemp(x.typ^.size, x.typ^.align); |
| @@ -32,7 +32,7 @@ | ||
| 32 | 32 | import Sys, Out, Version, Files, Fmt, Diag, Scan, Sym, Chunk, X64, Gen, Parse, Link, Elf; |
| 33 | 33 | |
| 34 | 34 | 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 *) | |
| 36 | 36 | MemfdCreate = 319; (* the system call *) |
| 37 | 37 | MfdCloexec = 1; (* an ELF still executes: the kernel opens it first *) |
| 38 | 38 | Execve = 59; |
| @@ -431,6 +431,33 @@ | ||
| 431 | 431 | return t^.form in {fArray, fSlice, fRecord, fVariant}; |
| 432 | 432 | end; |
| 433 | 433 | |
| 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 | + | |
| 434 | 461 | (* Whether a result field f is a single value of at most a word, not a real *) |
| 435 | 462 | proc oneWord(f: ^Obj): BOOL; |
| 436 | 463 | begin |