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.
| @@ -628,24 +628,3 @@ | ||
| 628 | 628 | thread, which can only set a flag that the turn reads after each read |
| 629 | 629 | of the reply; a server that says nothing for a minute, as a model does |
| 630 | 630 | 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. |
| @@ -1,3 +0,0 @@ | ||
| 1 | -module ArrayConstLenEmpty; | |
| 2 | -const P: []I64 = []; | |
| 3 | -end ArrayConstLenEmpty. |
| @@ -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. |
| @@ -0,0 +1,5 @@ | ||
| 1 | +0 0 0 0 0 | |
| 2 | +same | |
| 3 | +wide 1 | |
| 4 | +narrow 0 | |
| 5 | +none 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. |
| @@ -0,0 +1 @@ | ||
| 1 | +2 |
| @@ -2,5 +2,6 @@ | ||
| 2 | 2 | |
| 3 | 3 | (* ranges of wide characters, first and last, as a table made by a program would be *) |
| 4 | 4 | const Wide*: [][2]I32 = [[1100H, 115FH], [2E80H, 303EH], [0AC00H, 0D7A3H]]; |
| 5 | + Narrow*: [][2]I32 = []; (* and a table with nothing in it *) | |
| 5 | 6 | |
| 6 | 7 | end Widths. |
| @@ -20,7 +20,7 @@ | ||
| 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 | 22 | | `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 | | |
| 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 | |
| 26 | 26 | | `src/Raz.raz` | 305 | 238 | The command line | |
| @@ -1286,6 +1286,14 @@ | ||
| 1286 | 1286 | `restoreScan`), so `constPart` reads it as it reads any array's: |
| 1287 | 1287 | |
| 1288 | 1288 | 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; | |
| 1289 | 1297 | |
| 1290 | 1298 | `makeItem` makes it an |
| 1291 | 1299 | `mVar` there that isn't `isVar`, so it is read, indexed, sliced and |
| @@ -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.31 of the report; section 11 says where each | |
| 16 | +This is version 2026-10.32 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 |
| @@ -39,7 +39,8 @@ | ||
| 39 | 39 | (sections 5.1, 5.10 and 6.1); an enumeration's name may be written with |
| 40 | 40 | its type, and must be where another declaration shares it (section |
| 41 | 41 | 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. | |
| 43 | 44 | |
| 44 | 45 | The language is frozen, softly, from this version on: it changes only |
| 45 | 46 | to mend a defect, and its library grows only by what breaks no program |
| @@ -378,8 +379,9 @@ | ||
| 378 | 379 | record, and the value of an array or record constant is a literal |
| 379 | 380 | (section 6.5) of constants. Its elements and fields are integers, |
| 380 | 381 | 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: | |
| 383 | 385 | |
| 384 | 386 | ``` |
| 385 | 387 | const Primes: [5]I64 = [2, 3, 5, 7, 11]; |
| @@ -398,12 +400,15 @@ | ||
| 398 | 400 | |
| 399 | 401 | An array constant may be typed `[]T`, with no length: its length is |
| 400 | 402 | 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. | |
| 403 | 407 | |
| 404 | 408 | ``` |
| 405 | 409 | const Wide: [][2]I32 = [[1100H, 115FH], [2E80H, 303EH], [0AC00H, 0D7A3H]]; |
| 406 | 410 | Count = LEN(Wide); (* 3, a constant *) |
| 411 | + Narrow: [][2]I32 = []; (* the empty slice *) | |
| 407 | 412 | ``` |
| 408 | 413 | |
| 409 | 414 | **Variables** of a module live as long as the program; variables of a |
| @@ -191,3 +191,10 @@ | ||
| 191 | 191 | 7.2 and 7.7). It breaks a program that has a name `continue`. The |
| 192 | 192 | standard library gains `Width`, the columns of a terminal a character |
| 193 | 193 | 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). |
| @@ -258,9 +258,9 @@ | ||
| 258 | 258 | | oConst: |
| 259 | 259 | if o^.typ = Sym.tStr then |
| 260 | 260 | 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 *) | |
| 264 | 264 | x.mode := mVar; x.base := Gen.bGlobal; x.chunk := o^.chunk; |
| 265 | 265 | else |
| 266 | 266 | x.mode := mConst; x.val := o^.val; |
| @@ -3148,11 +3148,44 @@ | ||
| 3148 | 3148 | COPY(constBytes[off..off + n], src[0..n]); |
| 3149 | 3149 | end; |
| 3150 | 3150 | |
| 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 | + | |
| 3151 | 3179 | (* The part of type t at offset off of the constant *) |
| 3152 | 3180 | proc constPart(t: PType; off: INT): ERROR; |
| 3153 | 3181 | var e: TItem; f: PObj; k: INT; r: F32; start: Diag.Pos; src: ^U8; st: INT; |
| 3154 | 3182 | begin |
| 3155 | 3183 | 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; | |
| 3156 | 3189 | if (t^.form = Sym.fArray) and (Scan.tok = Scan.sLbrak) then |
| 3157 | 3190 | Scan.next; |
| 3158 | 3191 | k := 0; |
| @@ -3201,12 +3234,15 @@ | ||
| 3201 | 3234 | COPY(constBytes[off..off + INT(e.val)], Chunk.rodata[st..st + INT(e.val)]); |
| 3202 | 3235 | end; |
| 3203 | 3236 | 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}) | |
| 3205 | 3239 | 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))); | |
| 3207 | 3241 | 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; | |
| 3210 | 3246 | elsif t^.form = Sym.fVariant then |
| 3211 | 3247 | return Scan.markAt(e.pos, "a constant holds no variant"); |
| 3212 | 3248 | elsif (t^.form = Sym.fArray) or (t^.form = Sym.fRecord) then |
| @@ -3247,47 +3283,18 @@ | ||
| 3247 | 3283 | return c, nil; |
| 3248 | 3284 | end; |
| 3249 | 3285 | |
| 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 | - | |
| 3278 | 3286 | (* A constant typed []T is an array constant whose length is its |
| 3279 | 3287 | 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); | |
| 3282 | 3291 | var a: PType; n: INT; |
| 3283 | 3292 | begin |
| 3284 | 3293 | if Scan.tok # Scan.sLbrak then |
| 3285 | 3294 | return nil, Scan.markAt(Scan.tokPos(), say1("expected a literal of {}", Sym.typeName(t))); |
| 3286 | 3295 | end; |
| 3287 | 3296 | 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; | |
| 3291 | 3298 | a := Sym.newType(Sym.fArray, I64(n) * t^.base^.size, t^.base^.align); |
| 3292 | 3299 | a^.len := I64(n); |
| 3293 | 3300 | a^.base := t^.base; |
| @@ -3310,8 +3317,8 @@ | ||
| 3310 | 3317 | end; |
| 3311 | 3318 | end; |
| 3312 | 3319 | 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 | |
| 3315 | 3322 | o := Sym.newObj(Sym.oConst, n, t); |
| 3316 | 3323 | o^.chunk := try aggregateConst(t, n); |
| 3317 | 3324 | o^.pos := at; |
| @@ -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.31"; (* the version of doc/raz.md it follows *) | |
| 32 | + Report = "2026-10.32"; (* 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; |
| @@ -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 |
| @@ -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 |
| @@ -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 |