From ae6fc91dbe814b74e642624ea2d00ac3b10c776d Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Fri, 16 Oct 2026 15:18:57 +0100 Subject: [PATCH] Report 2026-10.32, the eighth thaw: the empty slice constant A constant typed []T whose literal is [] is the empty slice, 16 zero bytes in rodata, not an array; its LEN is no constant. A slice part of a constant may be [] or such a constant. literalLength moves above constPart, which now uses it. --- diff --git a/PROPOSALS.md b/PROPOSALS.md index b4c48f2..6ad0db8 100644 --- a/PROPOSALS.md +++ b/PROPOSALS.md @@ -628,24 +628,3 @@ thread, which can only set a flag that the turn reads after each read of the reply; a server that says nothing for a minute, as a model does while it thinks, keeps the turn going for that minute. - -## An empty array constant for a slice - -`const Runs: []Run = [];` is an error: "an array's length is from 1 -to 2^40, found 0". A constant typed `[]T` is an array constant whose -length is its literal's (4), and an array has at least one element -(5.2), so the empty literal has no array to be. Yet `[]` is the empty -slice everywhere else (6.5), and a slice of no elements is what such a -table may well be. - -The proposal: a constant typed `[]T` whose literal is `[]` is the empty -slice, as a slice variable starts, and no array at all; `LEN` of it is -0. Arrays stay at least one long; only the slice constant changes. A -program that compiles today means the same, so it breaks none. - -It costs a sentence of 4, and in the compiler, `literalArray` giving -the slice type back for a length of 0 instead of the error. - -Met in rrem's `RremUcd`, whose tables `tools/Ucd.raz` writes: before -the tool's first run, each table needs a placeholder of one element, -which the code reading it must then not take for a real one. diff --git a/conform/err/ArrayConstLenEmpty.raz b/conform/err/ArrayConstLenEmpty.raz deleted file mode 100644 index d615ec4..0000000 --- a/conform/err/ArrayConstLenEmpty.raz +++ /dev/null @@ -1,3 +0,0 @@ -module ArrayConstLenEmpty; -const P: []I64 = []; -end ArrayConstLenEmpty. diff --git a/conform/err/EmptySliceConstLen.raz b/conform/err/EmptySliceConstLen.raz new file mode 100644 index 0000000..aa25fe6 --- /dev/null +++ b/conform/err/EmptySliceConstLen.raz @@ -0,0 +1,4 @@ +module EmptySliceConstLen; +const P: []I64 = []; + N = LEN(P); (* P is a slice, so its LEN isn't a constant *) +end EmptySliceConstLen. diff --git a/conform/run/EmptySliceConst.expect b/conform/run/EmptySliceConst.expect new file mode 100644 index 0000000..458d821 --- /dev/null +++ b/conform/run/EmptySliceConst.expect @@ -0,0 +1,5 @@ +0 0 0 0 0 +same +wide 1 +narrow 0 +none 0 diff --git a/conform/run/EmptySliceConst.raz b/conform/run/EmptySliceConst.raz new file mode 100644 index 0000000..c2f89fe --- /dev/null +++ b/conform/run/EmptySliceConst.raz @@ -0,0 +1,38 @@ +module EmptySliceConst; +import Out, Widths; + +(* A constant typed []T whose literal is [] is the empty slice, used as + a slice is: as a table with nothing in it, or a part of a constant *) + +type Prop = record name: []U8; runs: [][2]I32; end; + +const + None: []I64 = []; + Wide: [][2]I32 = [[1100H, 115FH]]; + Narrow: [][2]I32 = []; + Props: []Prop = [{name: "wide", runs: Wide}, {name: "narrow", runs: Narrow}, {name: "none", runs: []}]; + +proc sum(a: []I64): I64; +var s: I64; i: INT; +begin + for i := 0 to LEN(a) - 1 do s := s + a[i]; end; + return s; +end; + +proc main*; +var s: []I64; i: INT; +begin + s := None; + Out.int(LEN(None)); Out.str(" "); Out.int(sum(None)); Out.str(" "); Out.int(LEN(s)); Out.str(" "); + Out.int(LEN(None[0..0])); Out.str(" "); Out.int(LEN(Widths.Narrow)); Out.ln; + if s = None then Out.str("same"); end; + Out.ln; + for i := 0 to LEN(Props) - 1 do + Out.str(Props[i].name); Out.str(" "); Out.int(LEN(Props[i].runs)); Out.ln; + end; + i := 0; + Out.int(None[i]); (* none to index: a failed check *) + Out.ln; +end; + +end EmptySliceConst. diff --git a/conform/run/EmptySliceConst.status b/conform/run/EmptySliceConst.status new file mode 100644 index 0000000..0cfbf08 --- /dev/null +++ b/conform/run/EmptySliceConst.status @@ -0,0 +1 @@ +2 diff --git a/conform/run/mods/Widths.raz b/conform/run/mods/Widths.raz index fb881a3..327fc01 100644 --- a/conform/run/mods/Widths.raz +++ b/conform/run/mods/Widths.raz @@ -2,5 +2,6 @@ (* ranges of wide characters, first and last, as a table made by a program would be *) const Wide*: [][2]I32 = [[1100H, 115FH], [2E80H, 303EH], [0AC00H, 0D7A3H]]; + Narrow*: [][2]I32 = []; (* and a table with nothing in it *) end Widths. diff --git a/doc/compiler.md b/doc/compiler.md index c48d28e..e26fc0f 100644 --- a/doc/compiler.md +++ b/doc/compiler.md @@ -20,7 +20,7 @@ | `src/X64.raz` | 1,517 | 1,253 | The instruction encoder, jumps within a procedure, the assembler | | `src/Gen.raz` | 2,552 | 1,821 | The stack machine, for x86-64: values, operations, checks, calls, frames | | `src/Item.raz` | 2,037 | 1,651 | Items: types, constants, variables, operators, conversions, calls | -| `src/Parse.raz` | 4,123 | 3,578 | Declarations, statements, expressions, built-ins, recovery, warnings | +| `src/Parse.raz` | 4,175 | 3,616 | Declarations, statements, expressions, built-ins, recovery, warnings | | `src/Link.raz` | 381 | 298 | Tree shaking, layout, patching, the places | | `src/Elf.raz` | 431 | 344 | Static and dynamic ELF, symbol table, line numbers | | `src/Raz.raz` | 305 | 238 | The command line | @@ -1286,6 +1286,14 @@ `restoreScan`), so `constPart` reads it as it reads any array's: const P: []I64 = [2, 3, 5]; (* read as *) const P: [3]I64 = [2, 3, 5]; + +With no element, `[]`, the type stays `[]T`, and the constant is 16 +zero bytes in read-only data: the empty slice, `ptr` `nil` and `len` 0, +read as a slice variable is, so its `LEN` is no constant. `constPart` +takes `[]` for any slice part, and an empty slice constant named there, +by leaving the part's bytes zero: + + const E: []I64 = []; (* read as the bytes of *) var E: []I64; `makeItem` makes it an `mVar` there that isn't `isVar`, so it is read, indexed, sliced and diff --git a/doc/raz.md b/doc/raz.md index d8f7375..90da50e 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.31 of the report; section 11 says where each +This is version 2026-10.32 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 @@ -39,7 +39,8 @@ (sections 5.1, 5.10 and 6.1); an enumeration's name may be written with its type, and must be where another declaration shares it (section 5.7); and `continue` (section 7.2), which breaks a program that has a -name `continue`. +name `continue`. 2026-10.32 is an eighth thaw: a constant typed `[]T` +may be `[]`, the empty slice (section 4); 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 @@ -378,8 +379,9 @@ record, and the value of an array or record constant is a literal (section 6.5) of constants. Its elements and fields are integers, reals, `BOOL`s, enumerations, sets, `ERROR`s, procedures, `nil`, a -string for a `[]U8`, an array constant for a slice of its element type, -and arrays and records of these; no variant and no other pointer: +string for a `[]U8`, an array constant or the empty slice for a slice +of its element type, and arrays and records of these; no variant and +no other pointer: ``` const Primes: [5]I64 = [2, 3, 5, 7, 11]; @@ -398,12 +400,15 @@ An array constant may be typed `[]T`, with no length: its length is then its literal's, as a string constant's is the string's, and it is -an array constant of that length, `[n]T`, in all else. Its literal has -at least one element. +an array constant of that length, `[n]T`, in all else. If its literal +is `[]`, it is no array but the empty slice, as a slice variable starts: +a value of type `[]T`, used as a slice is, and `LEN` of it is 0 but not +a constant. ``` const Wide: [][2]I32 = [[1100H, 115FH], [2E80H, 303EH], [0AC00H, 0D7A3H]]; Count = LEN(Wide); (* 3, a constant *) + Narrow: [][2]I32 = []; (* the empty slice *) ``` **Variables** of a module live as long as the program; variables of a diff --git a/doc/report-history.md b/doc/report-history.md index c4d86df..e950c5a 100644 --- a/doc/report-history.md +++ b/doc/report-history.md @@ -191,3 +191,10 @@ 7.2 and 7.7). It breaks a program that has a name `continue`. The standard library gains `Width`, the columns of a terminal a character takes (volume 2). +- **2026-10.32.** A thaw, the eighth. A constant typed `[]T` whose + literal is `[]` is the empty slice, a value of type `[]T`, and no + array; `LEN` of it is 0, not a constant. A slice part of a constant + may be the empty slice, by `[]` or by such a constant's name (section + 4). Before, both were errors, since an array has at least one element. It breaks no + program. `Sys.Info` gains `links`, how many names a file has + (volume 2). diff --git a/src/Item.raz b/src/Item.raz index e4e92a3..befd3d5 100644 --- a/src/Item.raz +++ b/src/Item.raz @@ -258,9 +258,9 @@ | oConst: if o^.typ = Sym.tStr then x.mode := mStr; x.val := o^.val; x.chunk := o^.chunk; - elsif o^.typ^.form in {Sym.fArray, Sym.fRecord} then - (* an array or record constant: a variable in read-only data, not one of - the program's, so nothing can change it *) + elsif o^.typ^.form in {Sym.fArray, Sym.fRecord, Sym.fSlice} then + (* an array, record or empty slice constant: a variable in read-only + data, not one of the program's, so nothing can change it *) x.mode := mVar; x.base := Gen.bGlobal; x.chunk := o^.chunk; else x.mode := mConst; x.val := o^.val; diff --git a/src/Parse.raz b/src/Parse.raz index 32a49dc..8ef3307 100644 --- a/src/Parse.raz +++ b/src/Parse.raz @@ -3148,11 +3148,44 @@ COPY(constBytes[off..off + n], src[0..n]); end; +(* The number of elements of the array literal at the scanner, at its + "[": read ahead, counting the commas outside any inner brackets, + then read again from the "[" (saveScan, restoreScan). Its slips are + told when it is read again. *) +proc literalLength: INT; +var st: Scan.State; depth, n: INT; wasSkipping: BOOL; +begin + Scan.saveScan(st); + wasSkipping := Scan.skipping; + Scan.setSkipping(true); + Scan.next; + n := 0; + if Scan.tok # Scan.sRbrak then n := 1; end; + depth := 0; + while (Scan.tok # Scan.sEof) and ((depth > 0) or (Scan.tok # Scan.sRbrak)) do + match Scan.tok of + | sLparen, sLbrak, sLbrace: INC(depth); + | sRparen, sRbrak, sRbrace: DEC(depth); + | sComma: if depth = 0 then INC(n); end; + | else: + end; + Scan.next; + end; + Scan.setSkipping(wasSkipping); + Scan.restoreScan(st); + return n; +end; + (* The part of type t at offset off of the constant *) proc constPart(t: PType; off: INT): ERROR; var e: TItem; f: PObj; k: INT; r: F32; start: Diag.Pos; src: ^U8; st: INT; begin start := Scan.tokPos(); + if (t^.form = Sym.fSlice) and (Scan.tok = Scan.sLbrak) and (literalLength() = 0) then + (* [], the empty slice (report, 4): its bytes stay zero, ptr nil and len 0 *) + Scan.next; + return expect(Scan.sRbrak); + end; if (t^.form = Sym.fArray) and (Scan.tok = Scan.sLbrak) then Scan.next; k := 0; @@ -3201,12 +3234,15 @@ COPY(constBytes[off..off + INT(e.val)], Chunk.rodata[st..st + INT(e.val)]); end; elsif t^.form = Sym.fSlice then - if (e.mode # Item.mVar) or (e.obj = nil) or (e.obj^.cls # Sym.oConst) or (e.typ^.form # Sym.fArray) + if (e.mode # Item.mVar) or (e.obj = nil) or (e.obj^.cls # Sym.oConst) + or not (e.typ^.form in {Sym.fArray, Sym.fSlice}) or (e.pos.b - e.pos.a # LEN(nameOf(e.obj^.name))) or not Sym.same(e.typ^.base, t^.base) then - return Scan.markAt(e.pos, say1("a constant slice is a string or an array constant of its elements, found {}", Item.what(e))); + return Scan.markAt(e.pos, say1("a constant slice is a string, an array constant of its elements or [], found {}", Item.what(e))); end; - try constFix(off, e.chunk); - putBytes(off + 8, 8, e.typ^.len); + if e.typ^.form = Sym.fArray then (* else an empty slice constant, whose bytes stay zero *) + try constFix(off, e.chunk); + putBytes(off + 8, 8, e.typ^.len); + end; elsif t^.form = Sym.fVariant then return Scan.markAt(e.pos, "a constant holds no variant"); elsif (t^.form = Sym.fArray) or (t^.form = Sym.fRecord) then @@ -3247,47 +3283,18 @@ return c, nil; end; -(* The number of elements of the array literal at the scanner, at its - "[": read ahead, counting the commas outside any inner brackets, - then read again from the "[" (saveScan, restoreScan). Its slips are - told when it is read again. *) -proc literalLength: INT; -var st: Scan.State; depth, n: INT; wasSkipping: BOOL; -begin - Scan.saveScan(st); - wasSkipping := Scan.skipping; - Scan.setSkipping(true); - Scan.next; - n := 0; - if Scan.tok # Scan.sRbrak then n := 1; end; - depth := 0; - while (Scan.tok # Scan.sEof) and ((depth > 0) or (Scan.tok # Scan.sRbrak)) do - match Scan.tok of - | sLparen, sLbrak, sLbrace: INC(depth); - | sRparen, sRbrak, sRbrace: DEC(depth); - | sComma: if depth = 0 then INC(n); end; - | else: - end; - Scan.next; - end; - Scan.setSkipping(wasSkipping); - Scan.restoreScan(st); - return n; -end; - (* A constant typed []T is an array constant whose length is its literal's (report, 4): t, the slice type, becomes [n]T, as if n had - been written, with n counted ahead (literalLength). *) -proc literalArray(t: PType; start: Diag.Pos): (PType, ERROR); + been written, with n counted ahead (literalLength). With no element, + [], it stays []T: the empty slice, kept as 16 zero bytes. *) +proc literalArray(t: PType): (PType, ERROR); var a: PType; n: INT; begin if Scan.tok # Scan.sLbrak then return nil, Scan.markAt(Scan.tokPos(), say1("expected a literal of {}", Sym.typeName(t))); end; n := literalLength(); - if n = 0 then - return nil, Scan.markAt(Scan.spanFrom(start), "an array's length is from 1 to 2^40, found 0"); - end; + if n = 0 then return t, nil; end; a := Sym.newType(Sym.fArray, I64(n) * t^.base^.size, t^.base^.align); a^.len := I64(n); a^.base := t^.base; @@ -3310,8 +3317,8 @@ end; end; try expect(Scan.sEql); - if (t # nil) and (t^.form = Sym.fSlice) then t := try literalArray(t, start); end; - if (t # nil) and ((t^.form = Sym.fArray) or (t^.form = Sym.fRecord)) then + if (t # nil) and (t^.form = Sym.fSlice) then t := try literalArray(t); end; + if (t # nil) and (t^.form in {Sym.fArray, Sym.fRecord, Sym.fSlice}) then o := Sym.newObj(Sym.oConst, n, t); o^.chunk := try aggregateConst(t, n); o^.pos := at; diff --git a/src/Raz.raz b/src/Raz.raz index 14040d9..5f9cffa 100644 --- a/src/Raz.raz +++ b/src/Raz.raz @@ -29,7 +29,7 @@ import Sys, Out, Version, Files, Fmt, Diag, Scan, Sym, Chunk, X64, Gen, Parse, Link, Elf; const - Report = "2026-10.31"; (* the version of doc/raz.md it follows *) + Report = "2026-10.32"; (* 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/test/msg/ArrayConstLenEmpty.err b/test/msg/ArrayConstLenEmpty.err deleted file mode 100644 index 8439585..0000000 --- a/test/msg/ArrayConstLenEmpty.err +++ /dev/null @@ -1,4 +0,0 @@ -conform/err/ArrayConstLenEmpty.raz:2:10: error: an array's length is from 1 to 2^40, found 0 - 2 | const P: []I64 = []; - | ^^^^^^^ -raz: 1 error diff --git a/test/msg/EmptySliceConst.stderr b/test/msg/EmptySliceConst.stderr new file mode 100644 index 0000000..25f5ef2 --- /dev/null +++ b/test/msg/EmptySliceConst.stderr @@ -0,0 +1,2 @@ +conform/run/EmptySliceConst.raz:34:18: index 0 is out of range for a length of 0 + in EmptySliceConst.main at conform/run/EmptySliceConst.raz:34:18: i = 0 diff --git a/test/msg/EmptySliceConstLen.err b/test/msg/EmptySliceConstLen.err new file mode 100644 index 0000000..5343ae9 --- /dev/null +++ b/test/msg/EmptySliceConstLen.err @@ -0,0 +1,4 @@ +conform/err/EmptySliceConstLen.raz:3:9: error: expected a constant + 3 | N = LEN(P); (* P is a slice, so its LEN isn't a constant *) + | ^^^^^^ +raz: 1 error