From cd2f687b52eb16a89093224401206379d95b75de Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Thu, 15 Oct 2026 17:39:02 +0100 Subject: [PATCH] Report 2026-10.27: a slice of the frame returned is an error A defect mended (section 7.3): returning as a slice a local array, a value parameter that is an array, an array literal, or a slice, field or element of one. What the narrow rule doesn't catch is named among section 10's exceptions; ret and keep, in PROPOSALS.md, would close it at a thaw. The history also tells Sys's and Buf's gains, and Vec, for volume 2. --- diff --git a/PROPOSALS.md b/PROPOSALS.md index 2aed653..c528f3d 100644 --- a/PROPOSALS.md +++ b/PROPOSALS.md @@ -95,39 +95,6 @@ new files `sauce commit` takes 1.2 s against the Pascal sauce's 0.7, and `verify` 0.95 s against 0.48: the hashing. -## A slice that outlives what it points at - -``` -proc short(h: Hex): []U8; (* Hex = [64]U8 *) -begin - return h[0..8]; -end; - -proc word: []U8; -var a: [4]U8; -begin - ... - return a; (* the array, silently a slice *) -end; -``` - -Both compile without a word, under `-Werror`, and return a slice of -the procedure's own frame, which the next call overwrites. Section 10 -says a program that reaches memory only through checked variables is -defined; a slice is checked, so this is a hole in that promise, a -defect rather than a wish, and it can be mended without a thaw. - -The narrow mend, which needs no analysis of where values go: it is an -error to return, as a slice, a slice of a local array or of a value -parameter that is an array or holds one, the silent conversion of -such an array included. It doesn't catch a slice stored in a module -variable or through a pointer, which is the rarer mistake, nor one of -an `ADR`, which section 10 already gives no promise for. The report -says it in section 7.3, Return; the compiler checks it where it -checks the type of `return`'s expression. - -Met in sauce's `Ssha.short`, which takes `var h: Hex` now. - ## Marking what a procedure does with what it is given: `ret` and `keep` ### The hole diff --git a/doc/raz.md b/doc/raz.md index 4400890..b2689f2 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.26 of the report; section 11 says where each +This is version 2026-10.27 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 @@ -26,7 +26,9 @@ is a fifth: an `INT` widens to an `I64` wherever it goes to one, in an expression too (sections 5.10 and 6.1); and a view, `v.(T)`, reads and writes the bytes of a variable as a `T` (section 6.9); it -breaks no program. +breaks no program. 2026-10.27 mends a defect: returning a slice of the +procedure's own frame is an error (section 7.3), which breaks only a +program that read memory already gone. 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 @@ -1034,6 +1036,26 @@ procedure with results that runs off its end returns zeros, each result as a variable of its type starts (section 4). +A slice returned must not point into the procedure's own frame, which +is gone once it returns. It is an error to return, as a slice, a local +array, a value parameter that is an array, or an array literal, or a +slice of one, a field or an element of one included: + +``` +proc short(h: Hex): []U8; (* Hex = [64]U8 *) +begin + return h[0..8]; (* an error: h is the procedure's copy *) +end; +``` + +A slice of a `var` parameter, of a module variable, or of what a +pointer points at may be returned: none of them is this procedure's +frame. 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 +the slice of a `var` parameter does to the caller whose local it is. + ### 7.4 Match ``` @@ -1459,8 +1481,9 @@ and indexing arrays and slices is checked, so a program's behaviour is 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, an index out of range in a module that -says `check off;`, writing a string's bytes, a code procedure that +indexing through a pointer, a slice kept after the call whose frame +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 by zero, calling a `nil` procedure and running out of stack stop the @@ -1565,7 +1588,7 @@ language, reals aside, is the core of the library: `Rt`, `Out`, `Mem`, `Arena`, `Lock`, `Str` and `Buf`, and of `Sys` what they need. A program that keeps to the core runs on every target. The rest of the library -(`Heap`, `Fmt`, `List`, `Map`, `Sort`, `Math`, `Files`, `Opt` and the +(`Heap`, `Fmt`, `List`, `Map`, `Vec`, `Sort`, `Math`, `Files`, `Opt` and the rest of `Sys`) is the standard library: a target may leave a module out, and one that has it gives what `doc/raz-lib.md` says. `conform/` tests the core only. diff --git a/doc/report-history.md b/doc/report-history.md index f2baf8c..3061338 100644 --- a/doc/report-history.md +++ b/doc/report-history.md @@ -151,3 +151,12 @@ appendix B). It breaks no program. `Sys` gains `position`, `truncate`, `sync` and `infoOf`; `Opt` gains `at`, where the operands are (volume 2). +- **2026-10.27.** A defect mended: returning, as a slice, a local + array, a value parameter that is an array, an array literal, or a + slice, field or element of one, is an error, since the frame it + points into is gone once the procedure returns (section 7.3); what + isn't caught is named among the exceptions of section 10. `Sys` + gains `openMode`, `syncAll`, `lock`, `linkInfo` and `Info.ino`; + `Buf` gains `spare`, and its `add` and `str` stop the program when + memory runs out rather than dropping bytes; the standard library + gains `Vec`, a growable array (volume 2).