raz / commit 3fb27192

commit
3fb271924622e8971d4a7b090aebc52db51baa51
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
16ccda37
A constant's slice part written as an array literal, an array constant of its own in a chunk with no name, nested to any depth, as report 2026-10.36 has it

patch · log

11 files changed, +162 -32

CLAUDE.md modified +4 -2

@@ -277,8 +277,10 @@
277277 PROPOSALS.md, would close it; the owner keeps it open for now.
278278 - An empty slice constant, `const E: []T = [];` (report 2026-10.32),
279279 is 16 zero bytes in read-only data, read as an array constant is;
280- `LEN` of it is no constant. A constant's slice part takes `[]`, and
281- names a constant whole, of any module, by its type, not its text.
280+ `LEN` of it is no constant. A constant's slice part takes `[]`, names
281+ a constant whole, of any module, by its type, not its text, and takes
282+ an array literal (report 2026-10.36), an array constant of its own in
283+ a chunk with no name, nested to any depth.
282284 - An array a call returns is a temporary of the statement (report
283285 2026-10.28, 8): a slice of it kept past the statement is undefined
284286 (10), and isn't caught.

conform/err/SliceLiteralElem.raz (history) added +3 -0

@@ -0,0 +1,3 @@
1+module SliceLiteralElem;
2+const Rows: [][]INT = [[1, 2], [3, true]];
3+end SliceLiteralElem.

conform/err/SliceLiteralPart.raz (history) added +3 -0

@@ -0,0 +1,3 @@
1+module SliceLiteralPart;
2+const Rows: [][]INT = [[1, 2], 3];
3+end SliceLiteralPart.

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

@@ -0,0 +1,4 @@
1+module SliceLiteralRecord;
2+type Rule = record name: []U8; keys: []INT; end;
3+const Rules: [1]Rule = [{name: "up", keys: [[1]]}];
4+end SliceLiteralRecord.

conform/run/SliceLiteralConst.expect (history) added +6 -0

@@ -0,0 +1,6 @@
1+2:3 1:3 0:0
2+[ab cd ][][e ]
3+up 2 172
4+none 0 0
5+6
6+hi there

conform/run/SliceLiteralConst.raz (history) added +50 -0

@@ -0,0 +1,50 @@
1+(* A constant's slice part written as an array literal (report section
2+ 4): each literal an array constant of its own, nested to any depth,
3+ inside records and variants too. *)
4+module SliceLiteralConst;
5+import Out;
6+
7+type
8+ Rule = record name: []U8; keys: []INT; end;
9+ Shape = variant dot; poly(xs: []INT); end;
10+
11+const
12+ Rows: [][]INT = [[1, 2], [3], []];
13+ Deep: [][][]U8 = [[['a', 'b'], "cd"], [], [['e']]];
14+ Rules: []Rule = [{name: "up", keys: [107, 65]}, {name: "none", keys: []}];
15+ Shapes: [2]Shape = [poly([1, 2, 3]), dot];
16+ Words: [][]U8 = [['h', 'i'], "there"];
17+
18+proc sum(xs: []INT): INT;
19+var i, s: INT;
20+begin
21+ s := 0;
22+ for i := 0 to LEN(xs) - 1 do s := s + xs[i]; end;
23+ return s;
24+end;
25+
26+proc main*;
27+var i, j: INT;
28+begin
29+ for i := 0 to LEN(Rows) - 1 do
30+ Out.int(LEN(Rows[i])); Out.str(":"); Out.int(sum(Rows[i])); Out.str(" ");
31+ end;
32+ Out.ln;
33+ for i := 0 to LEN(Deep) - 1 do
34+ Out.str("[");
35+ for j := 0 to LEN(Deep[i]) - 1 do Out.str(Deep[i][j]); Out.str(" "); end;
36+ Out.str("]");
37+ end;
38+ Out.ln;
39+ for i := 0 to LEN(Rules) - 1 do
40+ Out.str(Rules[i].name); Out.str(" "); Out.int(LEN(Rules[i].keys)); Out.str(" ");
41+ Out.int(sum(Rules[i].keys)); Out.ln;
42+ end;
43+ match Shapes[0] of
44+ | poly(xs): Out.int(sum(xs)); Out.ln;
45+ | dot:
46+ end;
47+ Out.str(Words[0]); Out.str(" "); Out.str(Words[1]); Out.ln;
48+end;
49+
50+end SliceLiteralConst.

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

@@ -23,7 +23,7 @@
2323 | `src/X64.raz` | 1,594 | 1,310 | The instruction encoder, jumps within a procedure, the assembler |
2424 | `src/Gen.raz` | 2,688 | 1,902 | The stack machine, for x86-64: values, operations, checks, calls, frames |
2525 | `src/Item.raz` | 2,378 | 1,909 | Items: types, constants, variables, operators, conversions, calls |
26-| `src/Parse.raz` | 4,476 | 3,864 | Declarations, statements, expressions, built-ins, recovery, warnings |
26+| `src/Parse.raz` | 4,513 | 3,887 | Declarations, statements, expressions, built-ins, recovery, warnings |
2727 | `src/Link.raz` | 407 | 315 | Tree shaking, layout, patching, the places |
2828 | `src/Elf.raz` | 449 | 355 | Static and dynamic ELF, symbol table, line numbers |
2929 | `src/Raz.raz` | 418 | 333 | The command line |
@@ -1394,6 +1394,19 @@
13941394 by leaving the part's bytes zero:
13951395
13961396 const E: []I64 = []; (* read as the bytes of *) var E: []I64;
1397+
1398+A slice part written as an array literal is an array constant of its
1399+own: `constPart` has `literalArray` make its type `[n]T`, and
1400+`constChunk` read it into a chunk of its own, with no name, before the
1401+constant around it is done; the part is then its address, a data fixup,
1402+and its length, as for a named array constant. `constChunk` keeps the
1403+outer constant's bytes aside while it reads the inner one, and puts the
1404+inner one's fixups in `fixes` after the outer one's, taking them off
1405+again when its chunk is made, so literals nest to any depth, in records
1406+and variants too:
1407+
1408+ const Rows: [][]INT = [[1, 2], [3]]; (* read as *)
1409+ const R0: [2]INT = [1, 2]; R1: [1]INT = [3]; Rows: [][]INT = [R0, R1];
13971410
13981411 `makeItem` makes it an
13991412 `mVar` there that isn't `isVar`, so it is read, indexed, sliced and

src/Parse.raz (history) modified +66 -29

@@ -3382,13 +3382,33 @@
33823382 return n;
33833383 end;
33843384
3385+(* A constant typed []T, or an array literal for a slice part of one,
3386+ is an array constant whose length is its literal's (report, 4): t,
3387+ the slice type, becomes [n]T, as if n had been written, with n
3388+ counted ahead (literalLength). With no element, [], it stays []T:
3389+ the empty slice, kept as 16 zero bytes. *)
3390+proc literalArray(t: PType): (PType, ERROR);
3391+var a: PType; n: INT;
3392+begin
3393+ if Scan.tok # Scan.sLbrak then
3394+ return nil, Scan.markAt(Scan.tokPos(), say1("expected a literal of {}", Sym.typeName(t)));
3395+ end;
3396+ n := literalLength();
3397+ if n = 0 then return t, nil; end;
3398+ a := Sym.newType(Sym.fArray, I64(n) * t^.base^.size, t^.base^.align);
3399+ a^.len := I64(n);
3400+ a^.base := t^.base;
3401+ return a, nil;
3402+end;
3403+
3404+proc constPart(t: PType; off: INT): ERROR; forward;
3405+proc constChunk(t: PType; n: INT): (INT, ERROR); forward;
3406+
33853407 (* A variant's case built from constants, at offset off of the constant
33863408 (report section 4): its number, then each field where the case lays
33873409 it out, as constructCase builds one in a program; the rest stays
33883410 zero. The case is read here, by its name, since expression would
33893411 make the code that builds one. *)
3390-proc constPart(t: PType; off: INT): ERROR; forward;
3391-
33923412 proc constCase(t: PType; off: INT): ERROR;
33933413 var c, f: PObj; at: Diag.Pos;
33943414 begin
@@ -3415,13 +3435,27 @@
34153435
34163436 (* The part of type t at offset off of the constant *)
34173437 proc constPart(t: PType; off: INT): ERROR;
3418-var e: TItem; f: PObj; k: INT; r: F32; start: Diag.Pos; src: ^U8; st, fx: INT;
3438+var e: TItem; f: PObj; k, c: INT; r: F32; start: Diag.Pos; src: ^U8; st, fx: INT; a: PType;
34193439 begin
34203440 start := Scan.tokPos();
3421- if (t^.form = Sym.fSlice) and (Scan.tok = Scan.sLbrak) and (literalLength() = 0) then
3422- (* [], the empty slice (report, 4): its bytes stay zero, ptr nil and len 0 *)
3423- Scan.next;
3424- return expect(Scan.sRbrak);
3441+ if (t^.form = Sym.fSlice) and (Scan.tok = Scan.sLbrak) then
3442+ a := try literalArray(t);
3443+ if a = t then
3444+ (* [], the empty slice (report, 4): its bytes stay zero, ptr nil and len 0 *)
3445+ Scan.next;
3446+ return expect(Scan.sRbrak);
3447+ end;
3448+ (* an array literal (report, 4) is an array constant of its own,
3449+ in a chunk of its own, made before this one is; the slice
3450+ points at it, as it would at a named one:
3451+ const Rows: [][]INT = [[1, 2], [3]];
3452+ is laid out as
3453+ const R0: [2]INT = [1, 2]; R1: [1]INT = [3];
3454+ const Rows: [][]INT = [R0, R1]; *)
3455+ c := try constChunk(a, -1);
3456+ try constFix(off, c);
3457+ putBytes(off + 8, 8, a^.len);
3458+ return nil;
34253459 end;
34263460 if (t^.form = Sym.fArray) and (Scan.tok = Scan.sLbrak) then
34273461 Scan.next;
@@ -3460,6 +3494,9 @@
34603494 return expect(Scan.sRbrace);
34613495 end;
34623496 if (t^.form = Sym.fVariant) and (Scan.tok = Scan.sIdent) then return constCase(t, off); end;
3497+ if Scan.tok = Scan.sLbrak then (* an array literal where neither an array nor a slice is *)
3498+ return Scan.mark(say1("expected {}, found an array literal", Sym.typeName(t)));
3499+ end;
34633500 try expression(e, t);
34643501 if (e.mode = Item.mStr) and ((t^.form = Sym.fSlice) or (t^.form = Sym.fArray)) and (t^.base = Sym.tU8) then
34653502 if t^.form = Sym.fSlice then
@@ -3476,7 +3513,7 @@
34763513 it, which has a type of its own: the element's, or a slice's *)
34773514 if (e.mode # Item.mVar) or (e.obj = nil) or (e.obj^.cls # Sym.oConst) or (e.typ # e.obj^.typ)
34783515 or not (e.typ^.form in {Sym.fArray, Sym.fSlice}) or not Sym.same(e.typ^.base, t^.base) then
3479- return Scan.markAt(e.pos, say1("a constant slice is a string, an array constant of its elements or [], found {}", Item.what(e)));
3516+ return Scan.markAt(e.pos, say1("a constant slice is a string, an array constant or literal of its elements, or [], found {}", Item.what(e)));
34803517 end;
34813518 if e.typ^.form = Sym.fArray then (* else an empty slice constant, whose bytes stay zero *)
34823519 try constFix(off, e.chunk);
@@ -3517,37 +3554,37 @@
35173554 return nil;
35183555 end;
35193556
3520-(* The chunk of n, a constant of type t whose literal is next *)
3521-proc aggregateConst(t: PType; n: INT): (INT, ERROR);
3522-var c, i, size: INT;
3557+(* The chunk of n, a constant of type t whose literal is next, or of
3558+ an array literal in a constant's slice part, with n -1. That one is
3559+ made while the constant around it is still being read: its bytes are
3560+ its own, and the constant's are put back after; its addresses go on
3561+ in fixes after the constant's, from base, and are taken off again. *)
3562+proc constChunk(t: PType; n: INT): (INT, ERROR);
3563+var c, i, size, base: INT; outer: ^U8;
35233564 begin
3565+ outer := constBytes;
3566+ base := numFixes;
35243567 size := INT(t^.size);
35253568 constBytes := Scan.grab(size + 8);
35263569 for i := 0 to size + 7 do constBytes[i] := 0; end;
3527- numFixes := 0;
35283570 try constPart(t, 0);
3529- c := Chunk.newChunk(Chunk.secRodata, Sym.curModule^.sname, n);
3571+ if n < 0 then
3572+ c := Chunk.newChunk(Chunk.secRodata, -1, -1);
3573+ else
3574+ c := Chunk.newChunk(Chunk.secRodata, Sym.curModule^.sname, n);
3575+ end;
35303576 Chunk.setData(c, constBytes, size, INT(t^.align));
3531- for i := 0 to numFixes - 1 do Chunk.addDataFixup(c, fixes[i].off, fixes[i].chunk, 0); end;
3577+ for i := base to numFixes - 1 do Chunk.addDataFixup(c, fixes[i].off, fixes[i].chunk, 0); end;
3578+ numFixes := base;
3579+ constBytes := outer;
35323580 return c, nil;
35333581 end;
35343582
3535-(* A constant typed []T is an array constant whose length is its
3536- literal's (report, 4): t, the slice type, becomes [n]T, as if n had
3537- been written, with n counted ahead (literalLength). With no element,
3538- [], it stays []T: the empty slice, kept as 16 zero bytes. *)
3539-proc literalArray(t: PType): (PType, ERROR);
3540-var a: PType; n: INT;
3583+(* The chunk of n, a constant of type t whose literal is next *)
3584+proc aggregateConst(t: PType; n: INT): (INT, ERROR);
35413585 begin
3542- if Scan.tok # Scan.sLbrak then
3543- return nil, Scan.markAt(Scan.tokPos(), say1("expected a literal of {}", Sym.typeName(t)));
3544- end;
3545- n := literalLength();
3546- if n = 0 then return t, nil; end;
3547- a := Sym.newType(Sym.fArray, I64(n) * t^.base^.size, t^.base^.align);
3548- a^.len := I64(n);
3549- a^.base := t^.base;
3550- return a, nil;
3586+ numFixes := 0; (* none left over from a constant an error broke off *)
3587+ return constChunk(t, n);
35513588 end;
35523589
35533590 proc constDecl(top: BOOL): ERROR;

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

@@ -0,0 +1,4 @@
1+conform/err/SliceLiteralElem.raz:2:36: error: can't use BOOL as INT
2+ 2 | const Rows: [][]INT = [[1, 2], [3, true]];
3+ | ^^^^
4+raz: 1 error

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

@@ -0,0 +1,4 @@
1+conform/err/SliceLiteralPart.raz:2:32: error: a constant slice is a string, an array constant or literal of its elements, or [], found 3
2+ 2 | const Rows: [][]INT = [[1, 2], 3];
3+ | ^
4+raz: 1 error

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

@@ -0,0 +1,4 @@
1+conform/err/SliceLiteralRecord.raz:3:45: error: expected INT, found an array literal
2+ 3 | const Rules: [1]Rule = [{name: "up", keys: [[1]]}];
3+ | ^
4+raz: 1 error