A slice of the frame returned is an error (report 2026-10.27) Item.frame: an array or record in the procedure's frame (a local, a value parameter, a temporary), or a slice that points there. makeItem and tempItem set it; field and index keep it for part of an array and drop it for a slice's element or a slice kept in a field; deref drops it; slice and coerce's array-to-slice carry it. notOfFrame, from returnValue and returnField, refuses a slice that has it. The report gains the array a call returns among what is caught. conform/err/FrameSlice, with its message in test/msg; newer than the seed in src/check.sh. 19 lines of code; make check finds no program of ours that did it.
| @@ -0,0 +1,50 @@ | ||
| 1 | +module FrameSlice; | |
| 2 | +(* a slice of the procedure's own frame can't be returned (report | |
| 3 | + section 7.3); one of a var parameter, a module variable or what a | |
| 4 | + pointer points at can *) | |
| 5 | + | |
| 6 | +type | |
| 7 | + Hex = [64]U8; | |
| 8 | + Pair = record a: [4]U8; s: []U8; end; | |
| 9 | + | |
| 10 | +var g: [8]U8; | |
| 11 | + | |
| 12 | +proc param(h: Hex): []U8; | |
| 13 | +begin | |
| 14 | + return h[0..8]; (* the procedure's copy *) | |
| 15 | +end; | |
| 16 | + | |
| 17 | +proc local: []U8; | |
| 18 | +var a: [4]U8; | |
| 19 | +begin | |
| 20 | + return a; (* the array, silently a slice *) | |
| 21 | +end; | |
| 22 | + | |
| 23 | +proc field(n: INT): ([]U8, INT); | |
| 24 | +var p: Pair; | |
| 25 | +begin | |
| 26 | + return p.a[0..n], n; (* one of several results *) | |
| 27 | +end; | |
| 28 | + | |
| 29 | +proc literal: []INT; | |
| 30 | +begin | |
| 31 | + return [1, 2, 3]; (* a hidden variable of the call *) | |
| 32 | +end; | |
| 33 | + | |
| 34 | +proc whole: Hex; | |
| 35 | +var h: Hex; | |
| 36 | +begin | |
| 37 | + return h; (* an array, copied out: fine *) | |
| 38 | +end; | |
| 39 | + | |
| 40 | +proc result: []U8; | |
| 41 | +begin | |
| 42 | + return whole()[0..2]; (* the array whole returns, a temporary *) | |
| 43 | +end; | |
| 44 | + | |
| 45 | +proc ok(var h: Hex; s: []U8; p: ^Hex; q: Pair): ([]U8, []U8, []U8, []U8, []U8); | |
| 46 | +begin | |
| 47 | + return h[0..8], s[1..2], p^[0..2], g, q.s; | |
| 48 | +end; | |
| 49 | + | |
| 50 | +end FrameSlice. |
| @@ -19,7 +19,7 @@ | ||
| 19 | 19 | | `src/Chunk.raz` | 363 | 279 | The compiled program: chunks, fixups, line notes | |
| 20 | 20 | | `src/X64.raz` | 1,517 | 1,253 | The instruction encoder, jumps within a procedure, the assembler | |
| 21 | 21 | | `src/Gen.raz` | 2,552 | 1,821 | The stack machine, for x86-64: values, operations, checks, calls, frames | |
| 22 | -| `src/Item.raz` | 1,954 | 1,598 | Items: types, constants, variables, operators, conversions, calls | | |
| 22 | +| `src/Item.raz` | 1,986 | 1,617 | Items: types, constants, variables, operators, conversions, calls | | |
| 23 | 23 | | `src/Parse.raz` | 4,000 | 3,478 | Declarations, statements, expressions, built-ins, recovery, warnings | |
| 24 | 24 | | `src/Link.raz` | 381 | 298 | Tree shaking, layout, patching, the places | |
| 25 | 25 | | `src/Elf.raz` | 431 | 344 | Static and dynamic ELF, symbol table, line numbers | |
| @@ -1270,6 +1270,18 @@ | ||
| 1270 | 1270 | checks, unless the module says `check off;` (`checks`, set by |
| 1271 | 1271 | `setChecks`, which `compileFile` sets for each module), and `convert` |
| 1272 | 1272 | the check of an enumeration's position. |
| 1273 | + | |
| 1274 | +The frame: an item's `frame` says, for an array or record, that it lies | |
| 1275 | +in the procedure's own frame (a local, a value parameter, which is a | |
| 1276 | +copy even when passed by address, or a temporary), and for a slice, | |
| 1277 | +that it points there. `makeItem` and `tempItem` set it; `field` and | |
| 1278 | +`index` keep it for a part of the array, and drop it for an element of | |
| 1279 | +a slice or a slice kept in a field, which may point anywhere; `deref` | |
| 1280 | +drops it; `slice`, and `coerce` where an array becomes a slice, carry | |
| 1281 | +it to the slice they make. `notOfFrame`, from `returnValue` and | |
| 1282 | +`returnField`, refuses a returned slice that has it (report 7.3). It | |
| 1283 | +sees only the expression: a slice put in a local slice variable first | |
| 1284 | +has lost it, which the report leaves undefined (section 10). | |
| 1273 | 1285 | |
| 1274 | 1286 | Assignment and conversion: `coerce` makes a value fit the type it's |
| 1275 | 1287 | going to, or reports that it can't; it's where a string literal becomes |
| @@ -1038,8 +1038,9 @@ | ||
| 1038 | 1038 | |
| 1039 | 1039 | A slice returned must not point into the procedure's own frame, which |
| 1040 | 1040 | is gone once it returns. It is an error to return, as a slice, a local |
| 1041 | -array, a value parameter that is an array, or an array literal, or a | |
| 1042 | -slice of one, a field or an element of one included: | |
| 1041 | +array, a value parameter that is an array, an array literal, or an | |
| 1042 | +array a call returns, or a slice of one, a field or an element of one | |
| 1043 | +included: | |
| 1043 | 1044 | |
| 1044 | 1045 | ``` |
| 1045 | 1046 | proc short(h: Hex): []U8; (* Hex = [64]U8 *) |
| @@ -55,6 +55,10 @@ | ||
| 55 | 55 | deref*: BOOL; (* mVar: at an address from a pointer, which may be |
| 56 | 56 | nil and isn't tested yet (nilTest) *) |
| 57 | 57 | obj*: ^Sym.Obj; (* mProc, mBuiltin, mCase; mVar: the variable named *) |
| 58 | + frame*: BOOL; (* a slice: points into the procedure's own frame, | |
| 59 | + which returnValue refuses (report section 7.3); | |
| 60 | + anything else: lies in that frame, a local, a | |
| 61 | + value parameter or a temporary, or part of one *) | |
| 58 | 62 | pos*: Diag.Pos; (* where it is in the source, for messages *) |
| 59 | 63 | end; |
| 60 | 64 | |
| @@ -276,6 +280,8 @@ | ||
| 276 | 280 | else |
| 277 | 281 | x.base := Gen.bFrame; x.off := o^.val; |
| 278 | 282 | end; |
| 283 | + (* a value parameter passed by address (ind) is a copy all the same *) | |
| 284 | + x.frame := (o^.lev > 0) and not o^.isVar and (o^.typ^.form # Sym.fSlice); | |
| 279 | 285 | | oProc: x.mode := mProc; |
| 280 | 286 | | oType: x.mode := mType; |
| 281 | 287 | | oBuiltin: x.mode := mBuiltin; |
| @@ -422,6 +428,7 @@ | ||
| 422 | 428 | x := {}; |
| 423 | 429 | x.pos := p; |
| 424 | 430 | x.mode := mVar; x.typ := t; x.base := Gen.bFrame; x.off := off; |
| 431 | + x.frame := t^.form # Sym.fSlice; (* a slice made here says where it points *) | |
| 425 | 432 | end; |
| 426 | 433 | |
| 427 | 434 | proc toSlot*(var x: Item): (I64, ERROR); |
| @@ -531,7 +538,7 @@ | ||
| 531 | 538 | end; |
| 532 | 539 | |
| 533 | 540 | proc coerce*(var x: Item; t: ^Sym.Type): ERROR; |
| 534 | -var s: ^Sym.Type; tmp: I64; | |
| 541 | +var s: ^Sym.Type; tmp: I64; inFrame: BOOL; | |
| 535 | 542 | begin |
| 536 | 543 | if x.mode = mCond then try value(x); end; |
| 537 | 544 | s := x.typ; |
| @@ -568,8 +575,10 @@ | ||
| 568 | 575 | if x.vol then return Scan.markAt(x.pos, "can't make a slice through a volatile pointer"); end; |
| 569 | 576 | if x.odd then return Scan.markAt(x.pos, "can't make a slice of a field of a packed record"); end; |
| 570 | 577 | if x.view then return noAddress(x, "can't make a slice of a view"); end; |
| 578 | + inFrame := x.frame; | |
| 571 | 579 | try address(x); |
| 572 | 580 | sliceOf(x, t, s^.len); |
| 581 | + x.frame := inFrame; | |
| 573 | 582 | elsif (x.mode = mProc) and (t^.form # Sym.fProc) and (s^.base = nil) then |
| 574 | 583 | return Scan.markAt(x.pos, say1("{} is a procedure, and returns nothing", Diag.source(x.pos))); |
| 575 | 584 | elsif (x.mode = mProc) and (t^.form # Sym.fProc) then |
| @@ -634,7 +643,7 @@ | ||
| 634 | 643 | try value(x); |
| 635 | 644 | x.mode := mVar; x.base := Gen.bStack; x.off := 0; x.deref := true; |
| 636 | 645 | x.typ := t^.base; x.vol := t^.vol; x.isVar := true; x.odd := false; x.ro := false; |
| 637 | - x.view := false; | |
| 646 | + x.view := false; x.frame := false; | |
| 638 | 647 | return nil; |
| 639 | 648 | end; |
| 640 | 649 | |
| @@ -650,6 +659,7 @@ | ||
| 650 | 659 | end; |
| 651 | 660 | x.off := x.off + f^.val; |
| 652 | 661 | x.typ := f^.typ; |
| 662 | + if x.typ^.form = Sym.fSlice then x.frame := false; end; (* a slice kept here points anywhere *) | |
| 653 | 663 | return nil; |
| 654 | 664 | end; |
| 655 | 665 | |
| @@ -710,6 +720,9 @@ | ||
| 710 | 720 | x.base := Gen.bStack; x.off := 0; x.isVar := true; x.vol := false; x.ro := false; |
| 711 | 721 | end; |
| 712 | 722 | x.typ := t^.base; |
| 723 | + (* an element of an array lies where the array does; one of a slice or | |
| 724 | + through a pointer, and a slice kept in an element, anywhere *) | |
| 725 | + if (t^.form # Sym.fArray) or (x.typ^.form = Sym.fSlice) then x.frame := false; end; | |
| 713 | 726 | return nil; |
| 714 | 727 | end; |
| 715 | 728 | |
| @@ -722,7 +735,7 @@ | ||
| 722 | 735 | end; |
| 723 | 736 | |
| 724 | 737 | proc slice*(var x, lo, hi: Item): ERROR; |
| 725 | -var t, st: ^Sym.Type; tmp: I64; | |
| 738 | +var t, st: ^Sym.Type; tmp: I64; inFrame: BOOL; | |
| 726 | 739 | begin |
| 727 | 740 | t := x.typ; |
| 728 | 741 | if not Sym.isInt(hi.typ) then |
| @@ -733,12 +746,14 @@ | ||
| 733 | 746 | if x.view then return noAddress(x, "can't make a slice of a view"); end; |
| 734 | 747 | if t^.form = Sym.fArray then try notReadOnly(x); end; |
| 735 | 748 | try value(hi); |
| 749 | + inFrame := x.frame and (t^.form # Sym.fPtr); | |
| 736 | 750 | tmp := Gen.allocTemp(16, 8); |
| 737 | 751 | if t^.form = Sym.fPtr then Gen.slicePtr(t^.base^.size, tmp, checks, lo.pos); |
| 738 | 752 | else Gen.slice(adrOf(x), t^.form = Sym.fArray, t^.len, t^.base^.size, tmp, checks, lo.pos); end; |
| 739 | 753 | st := Sym.newType(Sym.fSlice, 16, 8); |
| 740 | 754 | st^.base := t^.base; |
| 741 | 755 | tempItem(x, st, tmp); |
| 756 | + x.frame := inFrame; | |
| 742 | 757 | return nil; |
| 743 | 758 | end; |
| 744 | 759 | |
| @@ -1824,10 +1839,26 @@ | ||
| 1824 | 1839 | |
| 1825 | 1840 | (* The result of return x: a value on top, a slice's two words on top, |
| 1826 | 1841 | or one copied behind the hidden pointer *) |
| 1842 | +(* A slice returned must not point into the procedure's own frame, | |
| 1843 | + which is gone once it returns (report section 7.3). Only what can be | |
| 1844 | + seen in the expression is caught: a local array, a value parameter | |
| 1845 | + that is one, an array literal or a temporary, or a slice of one, a | |
| 1846 | + field or an element of one included; x.frame says so, set where the | |
| 1847 | + variable is named and kept through fields, elements and slices. *) | |
| 1848 | +proc notOfFrame(x: Item; t: ^Sym.Type): ERROR; | |
| 1849 | +begin | |
| 1850 | + if (t^.form = Sym.fSlice) and x.frame then | |
| 1851 | + return Scan.markHelp(x.pos, "can't return a slice of the procedure's own frame", | |
| 1852 | + "it is gone once the procedure returns: take the array as a var parameter, or fill a buffer the caller gives"); | |
| 1853 | + end; | |
| 1854 | + return nil; | |
| 1855 | +end; | |
| 1856 | + | |
| 1827 | 1857 | proc returnValue*(var x: Item): ERROR; |
| 1828 | 1858 | begin |
| 1829 | 1859 | if result = nil then return Scan.markAt(x.pos, "this procedure returns nothing"); end; |
| 1830 | 1860 | try coerce(x, result); |
| 1861 | + try notOfFrame(x, result); | |
| 1831 | 1862 | if Sym.inTwoRegs(result) then try pushWords(x); |
| 1832 | 1863 | elsif Sym.isAggregate(result) then |
| 1833 | 1864 | (* a record literal made behind the hidden pointer is there already *) |
| @@ -1845,6 +1876,7 @@ | ||
| 1845 | 1876 | proc returnField*(f: ^Sym.Obj; var x: Item): ERROR; |
| 1846 | 1877 | begin |
| 1847 | 1878 | try coerce(x, f^.typ); |
| 1879 | + try notOfFrame(x, f^.typ); | |
| 1848 | 1880 | if Sym.inTwoRegs(result) then try value(x); |
| 1849 | 1881 | elsif Sym.isScalar(f^.typ) then |
| 1850 | 1882 | try value(x); |
| @@ -29,7 +29,7 @@ | ||
| 29 | 29 | import Sys, Out, Version, Files, Fmt, Diag, Scan, Sym, Chunk, X64, Gen, Parse, Link, Elf; |
| 30 | 30 | |
| 31 | 31 | const |
| 32 | - Report = "2026-10.26"; (* the version of doc/raz.md it follows *) | |
| 32 | + Report = "2026-10.27"; (* the version of doc/raz.md it follows *) | |
| 33 | 33 | MemfdCreate = 319; (* the system call *) |
| 34 | 34 | MfdCloexec = 1; (* an ELF still executes: the kernel opens it first *) |
| 35 | 35 | Execve = 59; |
| @@ -12,14 +12,16 @@ | ||
| 12 | 12 | # the messages for an I64 that goes to an INT, and a help that |
| 13 | 13 | # converts the narrower of two integers; and views, v.(T), |
| 14 | 14 | # whose tests are conform/*/View*.raz, and which Math, examples/Elf.raz |
| 15 | -# and, through Math, test/bench/NBody.raz use. | |
| 15 | +# and, through Math, test/bench/NBody.raz use; and returning a slice of | |
| 16 | +# the frame as an error (report 2026-10.27). | |
| 16 | 17 | newer="conform/run/RawStrings.raz conform/err/ScanRawOpen.raz conform/err/ScanRawIndent.raz |
| 17 | 18 | conform/run/MatchStr.raz conform/err/MatchStrTwice.raz conform/err/MatchStrNoElse.raz |
| 18 | 19 | conform/err/MatchStrVar.raz conform/err/MatchReal.raz conform/err/AtMatch.raz |
| 19 | 20 | examples/Sh.raz examples/Settings.raz |
| 20 | 21 | conform/run/IntWiden.raz conform/err/IntIncNarrow.raz conform/err/IntNarrow.raz conform/err/IntNarrowSum.raz |
| 21 | 22 | conform/err/IntResultsNarrow.raz conform/err/GenIntTypes.raz |
| 22 | - lib/Math.raz examples/Elf.raz test/bench/NBody.raz" | |
| 23 | + lib/Math.raz examples/Elf.raz test/bench/NBody.raz | |
| 24 | + conform/err/FrameSlice.raz" | |
| 23 | 25 | newer() { |
| 24 | 26 | case $1 in conform/*/View*.raz) return 0 ;; esac |
| 25 | 27 | for g in $newer; do [ $g = $1 ] && return 0; done |
| @@ -0,0 +1,21 @@ | ||
| 1 | +conform/err/FrameSlice.raz:14:12: error: can't return a slice of the procedure's own frame | |
| 2 | + 14 | return h[0..8]; (* the procedure's copy *) | |
| 3 | + | ^^^^^^^ | |
| 4 | +help: it is gone once the procedure returns: take the array as a var parameter, or fill a buffer the caller gives | |
| 5 | +conform/err/FrameSlice.raz:20:12: error: can't return a slice of the procedure's own frame | |
| 6 | + 20 | return a; (* the array, silently a slice *) | |
| 7 | + | ^ | |
| 8 | +help: it is gone once the procedure returns: take the array as a var parameter, or fill a buffer the caller gives | |
| 9 | +conform/err/FrameSlice.raz:26:12: error: can't return a slice of the procedure's own frame | |
| 10 | + 26 | return p.a[0..n], n; (* one of several results *) | |
| 11 | + | ^^^^^^^^^ | |
| 12 | +help: it is gone once the procedure returns: take the array as a var parameter, or fill a buffer the caller gives | |
| 13 | +conform/err/FrameSlice.raz:31:12: error: can't return a slice of the procedure's own frame | |
| 14 | + 31 | return [1, 2, 3]; (* a hidden variable of the call *) | |
| 15 | + | ^^^^^^^^^ | |
| 16 | +help: it is gone once the procedure returns: take the array as a var parameter, or fill a buffer the caller gives | |
| 17 | +conform/err/FrameSlice.raz:42:12: error: can't return a slice of the procedure's own frame | |
| 18 | + 42 | return whole()[0..2]; (* the array whole returns, a temporary *) | |
| 19 | + | ^^^^^^^^^^^^^ | |
| 20 | +help: it is gone once the procedure returns: take the array as a var parameter, or fill a buffer the caller gives | |
| 21 | +raz: 5 errors |