raz / commit ae6fc91d

commit
ae6fc91dbe814b74e642624ea2d00ac3b10c776d
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
261c3a07
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.

patch · log

16 files changed, +132 -78

PROPOSALS.md modified +0 -21

@@ -628,24 +628,3 @@
628628 thread, which can only set a flag that the turn reads after each read
629629 of the reply; a server that says nothing for a minute, as a model does
630630 while it thinks, keeps the turn going for that minute.
631-
632-## An empty array constant for a slice
633-
634-`const Runs: []Run = [];` is an error: "an array's length is from 1
635-to 2^40, found 0". A constant typed `[]T` is an array constant whose
636-length is its literal's (4), and an array has at least one element
637-(5.2), so the empty literal has no array to be. Yet `[]` is the empty
638-slice everywhere else (6.5), and a slice of no elements is what such a
639-table may well be.
640-
641-The proposal: a constant typed `[]T` whose literal is `[]` is the empty
642-slice, as a slice variable starts, and no array at all; `LEN` of it is
643-0. Arrays stay at least one long; only the slice constant changes. A
644-program that compiles today means the same, so it breaks none.
645-
646-It costs a sentence of 4, and in the compiler, `literalArray` giving
647-the slice type back for a length of 0 instead of the error.
648-
649-Met in rrem's `RremUcd`, whose tables `tools/Ucd.raz` writes: before
650-the tool's first run, each table needs a placeholder of one element,
651-which the code reading it must then not take for a real one.

conform/err/ArrayConstLenEmpty.raz deleted +0 -3

@@ -1,3 +0,0 @@
1-module ArrayConstLenEmpty;
2-const P: []I64 = [];
3-end ArrayConstLenEmpty.

conform/err/EmptySliceConstLen.raz (history) added +4 -0

@@ -0,0 +1,4 @@
1+module EmptySliceConstLen;
2+const P: []I64 = [];
3+ N = LEN(P); (* P is a slice, so its LEN isn't a constant *)
4+end EmptySliceConstLen.

conform/run/EmptySliceConst.expect (history) added +5 -0

@@ -0,0 +1,5 @@
1+0 0 0 0 0
2+same
3+wide 1
4+narrow 0
5+none 0

conform/run/EmptySliceConst.raz (history) added +38 -0

@@ -0,0 +1,38 @@
1+module EmptySliceConst;
2+import Out, Widths;
3+
4+(* A constant typed []T whose literal is [] is the empty slice, used as
5+ a slice is: as a table with nothing in it, or a part of a constant *)
6+
7+type Prop = record name: []U8; runs: [][2]I32; end;
8+
9+const
10+ None: []I64 = [];
11+ Wide: [][2]I32 = [[1100H, 115FH]];
12+ Narrow: [][2]I32 = [];
13+ Props: []Prop = [{name: "wide", runs: Wide}, {name: "narrow", runs: Narrow}, {name: "none", runs: []}];
14+
15+proc sum(a: []I64): I64;
16+var s: I64; i: INT;
17+begin
18+ for i := 0 to LEN(a) - 1 do s := s + a[i]; end;
19+ return s;
20+end;
21+
22+proc main*;
23+var s: []I64; i: INT;
24+begin
25+ s := None;
26+ Out.int(LEN(None)); Out.str(" "); Out.int(sum(None)); Out.str(" "); Out.int(LEN(s)); Out.str(" ");
27+ Out.int(LEN(None[0..0])); Out.str(" "); Out.int(LEN(Widths.Narrow)); Out.ln;
28+ if s = None then Out.str("same"); end;
29+ Out.ln;
30+ for i := 0 to LEN(Props) - 1 do
31+ Out.str(Props[i].name); Out.str(" "); Out.int(LEN(Props[i].runs)); Out.ln;
32+ end;
33+ i := 0;
34+ Out.int(None[i]); (* none to index: a failed check *)
35+ Out.ln;
36+end;
37+
38+end EmptySliceConst.

conform/run/EmptySliceConst.status (history) added +1 -0

@@ -0,0 +1 @@
1+2

conform/run/mods/Widths.raz (history) modified +1 -0

@@ -2,5 +2,6 @@
22
33 (* ranges of wide characters, first and last, as a table made by a program would be *)
44 const Wide*: [][2]I32 = [[1100H, 115FH], [2E80H, 303EH], [0AC00H, 0D7A3H]];
5+ Narrow*: [][2]I32 = []; (* and a table with nothing in it *)
56
67 end Widths.

doc/compiler.md (history) modified +9 -1

@@ -20,7 +20,7 @@
2020 | `src/X64.raz` | 1,517 | 1,253 | The instruction encoder, jumps within a procedure, the assembler |
2121 | `src/Gen.raz` | 2,552 | 1,821 | The stack machine, for x86-64: values, operations, checks, calls, frames |
2222 | `src/Item.raz` | 2,037 | 1,651 | Items: types, constants, variables, operators, conversions, calls |
23-| `src/Parse.raz` | 4,123 | 3,578 | Declarations, statements, expressions, built-ins, recovery, warnings |
23+| `src/Parse.raz` | 4,175 | 3,616 | Declarations, statements, expressions, built-ins, recovery, warnings |
2424 | `src/Link.raz` | 381 | 298 | Tree shaking, layout, patching, the places |
2525 | `src/Elf.raz` | 431 | 344 | Static and dynamic ELF, symbol table, line numbers |
2626 | `src/Raz.raz` | 305 | 238 | The command line |
@@ -1286,6 +1286,14 @@
12861286 `restoreScan`), so `constPart` reads it as it reads any array's:
12871287
12881288 const P: []I64 = [2, 3, 5]; (* read as *) const P: [3]I64 = [2, 3, 5];
1289+
1290+With no element, `[]`, the type stays `[]T`, and the constant is 16
1291+zero bytes in read-only data: the empty slice, `ptr` `nil` and `len` 0,
1292+read as a slice variable is, so its `LEN` is no constant. `constPart`
1293+takes `[]` for any slice part, and an empty slice constant named there,
1294+by leaving the part's bytes zero:
1295+
1296+ const E: []I64 = []; (* read as the bytes of *) var E: []I64;
12891297
12901298 `makeItem` makes it an
12911299 `mVar` there that isn't `isVar`, so it is read, indexed, sliced and

doc/raz.md (history) modified +11 -6

@@ -13,7 +13,7 @@
1313 This report defines the language. It is short on purpose. Whatever it
1414 doesn't allow, raz doesn't have.
1515
16-This is version 2026-10.31 of the report; section 11 says where each
16+This is version 2026-10.32 of the report; section 11 says where each
1717 version's changes are told. 2026-10.11 and 2026-10.12 are a thaw the owner chose,
1818 for a language anyone can build again. They break programs written
1919 for 2026-10.10 and before: for 2026-10.9, say, the report of compiler
@@ -39,7 +39,8 @@
3939 (sections 5.1, 5.10 and 6.1); an enumeration's name may be written with
4040 its type, and must be where another declaration shares it (section
4141 5.7); and `continue` (section 7.2), which breaks a program that has a
42-name `continue`.
42+name `continue`. 2026-10.32 is an eighth thaw: a constant typed `[]T`
43+may be `[]`, the empty slice (section 4); it breaks no program.
4344
4445 The language is frozen, softly, from this version on: it changes only
4546 to mend a defect, and its library grows only by what breaks no program
@@ -378,8 +379,9 @@
378379 record, and the value of an array or record constant is a literal
379380 (section 6.5) of constants. Its elements and fields are integers,
380381 reals, `BOOL`s, enumerations, sets, `ERROR`s, procedures, `nil`, a
381-string for a `[]U8`, an array constant for a slice of its element type,
382-and arrays and records of these; no variant and no other pointer:
382+string for a `[]U8`, an array constant or the empty slice for a slice
383+of its element type, and arrays and records of these; no variant and
384+no other pointer:
383385
384386 ```
385387 const Primes: [5]I64 = [2, 3, 5, 7, 11];
@@ -398,12 +400,15 @@
398400
399401 An array constant may be typed `[]T`, with no length: its length is
400402 then its literal's, as a string constant's is the string's, and it is
401-an array constant of that length, `[n]T`, in all else. Its literal has
402-at least one element.
403+an array constant of that length, `[n]T`, in all else. If its literal
404+is `[]`, it is no array but the empty slice, as a slice variable starts:
405+a value of type `[]T`, used as a slice is, and `LEN` of it is 0 but not
406+a constant.
403407
404408 ```
405409 const Wide: [][2]I32 = [[1100H, 115FH], [2E80H, 303EH], [0AC00H, 0D7A3H]];
406410 Count = LEN(Wide); (* 3, a constant *)
411+ Narrow: [][2]I32 = []; (* the empty slice *)
407412 ```
408413
409414 **Variables** of a module live as long as the program; variables of a

doc/report-history.md (history) modified +7 -0

@@ -191,3 +191,10 @@
191191 7.2 and 7.7). It breaks a program that has a name `continue`. The
192192 standard library gains `Width`, the columns of a terminal a character
193193 takes (volume 2).
194+- **2026-10.32.** A thaw, the eighth. A constant typed `[]T` whose
195+ literal is `[]` is the empty slice, a value of type `[]T`, and no
196+ array; `LEN` of it is 0, not a constant. A slice part of a constant
197+ may be the empty slice, by `[]` or by such a constant's name (section
198+ 4). Before, both were errors, since an array has at least one element. It breaks no
199+ program. `Sys.Info` gains `links`, how many names a file has
200+ (volume 2).

src/Item.raz (history) modified +3 -3

@@ -258,9 +258,9 @@
258258 | oConst:
259259 if o^.typ = Sym.tStr then
260260 x.mode := mStr; x.val := o^.val; x.chunk := o^.chunk;
261- elsif o^.typ^.form in {Sym.fArray, Sym.fRecord} then
262- (* an array or record constant: a variable in read-only data, not one of
263- the program's, so nothing can change it *)
261+ elsif o^.typ^.form in {Sym.fArray, Sym.fRecord, Sym.fSlice} then
262+ (* an array, record or empty slice constant: a variable in read-only
263+ data, not one of the program's, so nothing can change it *)
264264 x.mode := mVar; x.base := Gen.bGlobal; x.chunk := o^.chunk;
265265 else
266266 x.mode := mConst; x.val := o^.val;

src/Parse.raz (history) modified +46 -39

@@ -3148,11 +3148,44 @@
31483148 COPY(constBytes[off..off + n], src[0..n]);
31493149 end;
31503150
3151+(* The number of elements of the array literal at the scanner, at its
3152+ "[": read ahead, counting the commas outside any inner brackets,
3153+ then read again from the "[" (saveScan, restoreScan). Its slips are
3154+ told when it is read again. *)
3155+proc literalLength: INT;
3156+var st: Scan.State; depth, n: INT; wasSkipping: BOOL;
3157+begin
3158+ Scan.saveScan(st);
3159+ wasSkipping := Scan.skipping;
3160+ Scan.setSkipping(true);
3161+ Scan.next;
3162+ n := 0;
3163+ if Scan.tok # Scan.sRbrak then n := 1; end;
3164+ depth := 0;
3165+ while (Scan.tok # Scan.sEof) and ((depth > 0) or (Scan.tok # Scan.sRbrak)) do
3166+ match Scan.tok of
3167+ | sLparen, sLbrak, sLbrace: INC(depth);
3168+ | sRparen, sRbrak, sRbrace: DEC(depth);
3169+ | sComma: if depth = 0 then INC(n); end;
3170+ | else:
3171+ end;
3172+ Scan.next;
3173+ end;
3174+ Scan.setSkipping(wasSkipping);
3175+ Scan.restoreScan(st);
3176+ return n;
3177+end;
3178+
31513179 (* The part of type t at offset off of the constant *)
31523180 proc constPart(t: PType; off: INT): ERROR;
31533181 var e: TItem; f: PObj; k: INT; r: F32; start: Diag.Pos; src: ^U8; st: INT;
31543182 begin
31553183 start := Scan.tokPos();
3184+ if (t^.form = Sym.fSlice) and (Scan.tok = Scan.sLbrak) and (literalLength() = 0) then
3185+ (* [], the empty slice (report, 4): its bytes stay zero, ptr nil and len 0 *)
3186+ Scan.next;
3187+ return expect(Scan.sRbrak);
3188+ end;
31563189 if (t^.form = Sym.fArray) and (Scan.tok = Scan.sLbrak) then
31573190 Scan.next;
31583191 k := 0;
@@ -3201,12 +3234,15 @@
32013234 COPY(constBytes[off..off + INT(e.val)], Chunk.rodata[st..st + INT(e.val)]);
32023235 end;
32033236 elsif t^.form = Sym.fSlice then
3204- if (e.mode # Item.mVar) or (e.obj = nil) or (e.obj^.cls # Sym.oConst) or (e.typ^.form # Sym.fArray)
3237+ if (e.mode # Item.mVar) or (e.obj = nil) or (e.obj^.cls # Sym.oConst)
3238+ or not (e.typ^.form in {Sym.fArray, Sym.fSlice})
32053239 or (e.pos.b - e.pos.a # LEN(nameOf(e.obj^.name))) or not Sym.same(e.typ^.base, t^.base) then
3206- return Scan.markAt(e.pos, say1("a constant slice is a string or an array constant of its elements, found {}", Item.what(e)));
3240+ return Scan.markAt(e.pos, say1("a constant slice is a string, an array constant of its elements or [], found {}", Item.what(e)));
32073241 end;
3208- try constFix(off, e.chunk);
3209- putBytes(off + 8, 8, e.typ^.len);
3242+ if e.typ^.form = Sym.fArray then (* else an empty slice constant, whose bytes stay zero *)
3243+ try constFix(off, e.chunk);
3244+ putBytes(off + 8, 8, e.typ^.len);
3245+ end;
32103246 elsif t^.form = Sym.fVariant then
32113247 return Scan.markAt(e.pos, "a constant holds no variant");
32123248 elsif (t^.form = Sym.fArray) or (t^.form = Sym.fRecord) then
@@ -3247,47 +3283,18 @@
32473283 return c, nil;
32483284 end;
32493285
3250-(* The number of elements of the array literal at the scanner, at its
3251- "[": read ahead, counting the commas outside any inner brackets,
3252- then read again from the "[" (saveScan, restoreScan). Its slips are
3253- told when it is read again. *)
3254-proc literalLength: INT;
3255-var st: Scan.State; depth, n: INT; wasSkipping: BOOL;
3256-begin
3257- Scan.saveScan(st);
3258- wasSkipping := Scan.skipping;
3259- Scan.setSkipping(true);
3260- Scan.next;
3261- n := 0;
3262- if Scan.tok # Scan.sRbrak then n := 1; end;
3263- depth := 0;
3264- while (Scan.tok # Scan.sEof) and ((depth > 0) or (Scan.tok # Scan.sRbrak)) do
3265- match Scan.tok of
3266- | sLparen, sLbrak, sLbrace: INC(depth);
3267- | sRparen, sRbrak, sRbrace: DEC(depth);
3268- | sComma: if depth = 0 then INC(n); end;
3269- | else:
3270- end;
3271- Scan.next;
3272- end;
3273- Scan.setSkipping(wasSkipping);
3274- Scan.restoreScan(st);
3275- return n;
3276-end;
3277-
32783286 (* A constant typed []T is an array constant whose length is its
32793287 literal's (report, 4): t, the slice type, becomes [n]T, as if n had
3280- been written, with n counted ahead (literalLength). *)
3281-proc literalArray(t: PType; start: Diag.Pos): (PType, ERROR);
3288+ been written, with n counted ahead (literalLength). With no element,
3289+ [], it stays []T: the empty slice, kept as 16 zero bytes. *)
3290+proc literalArray(t: PType): (PType, ERROR);
32823291 var a: PType; n: INT;
32833292 begin
32843293 if Scan.tok # Scan.sLbrak then
32853294 return nil, Scan.markAt(Scan.tokPos(), say1("expected a literal of {}", Sym.typeName(t)));
32863295 end;
32873296 n := literalLength();
3288- if n = 0 then
3289- return nil, Scan.markAt(Scan.spanFrom(start), "an array's length is from 1 to 2^40, found 0");
3290- end;
3297+ if n = 0 then return t, nil; end;
32913298 a := Sym.newType(Sym.fArray, I64(n) * t^.base^.size, t^.base^.align);
32923299 a^.len := I64(n);
32933300 a^.base := t^.base;
@@ -3310,8 +3317,8 @@
33103317 end;
33113318 end;
33123319 try expect(Scan.sEql);
3313- if (t # nil) and (t^.form = Sym.fSlice) then t := try literalArray(t, start); end;
3314- if (t # nil) and ((t^.form = Sym.fArray) or (t^.form = Sym.fRecord)) then
3320+ if (t # nil) and (t^.form = Sym.fSlice) then t := try literalArray(t); end;
3321+ if (t # nil) and (t^.form in {Sym.fArray, Sym.fRecord, Sym.fSlice}) then
33153322 o := Sym.newObj(Sym.oConst, n, t);
33163323 o^.chunk := try aggregateConst(t, n);
33173324 o^.pos := at;

src/Raz.raz (history) modified +1 -1

@@ -29,7 +29,7 @@
2929 import Sys, Out, Version, Files, Fmt, Diag, Scan, Sym, Chunk, X64, Gen, Parse, Link, Elf;
3030
3131 const
32- Report = "2026-10.31"; (* the version of doc/raz.md it follows *)
32+ Report = "2026-10.32"; (* the version of doc/raz.md it follows *)
3333 MemfdCreate = 319; (* the system call *)
3434 MfdCloexec = 1; (* an ELF still executes: the kernel opens it first *)
3535 Execve = 59;

test/msg/ArrayConstLenEmpty.err deleted +0 -4

@@ -1,4 +0,0 @@
1-conform/err/ArrayConstLenEmpty.raz:2:10: error: an array's length is from 1 to 2^40, found 0
2- 2 | const P: []I64 = [];
3- | ^^^^^^^
4-raz: 1 error

test/msg/EmptySliceConst.stderr (history) added +2 -0

@@ -0,0 +1,2 @@
1+conform/run/EmptySliceConst.raz:34:18: index 0 is out of range for a length of 0
2+ in EmptySliceConst.main at conform/run/EmptySliceConst.raz:34:18: i = 0

test/msg/EmptySliceConstLen.err (history) added +4 -0

@@ -0,0 +1,4 @@
1+conform/err/EmptySliceConstLen.raz:3:9: error: expected a constant
2+ 3 | N = LEN(P); (* P is a slice, so its LEN isn't a constant *)
3+ | ^^^^^^
4+raz: 1 error