From 3fb271924622e8971d4a7b090aebc52db51baa51 Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Fri, 23 Oct 2026 13:15:26 +0100 Subject: [PATCH] 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 --- diff --git a/CLAUDE.md b/CLAUDE.md index fb37d8f..dbda932 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -277,8 +277,10 @@ PROPOSALS.md, would close it; the owner keeps it open for now. - An empty slice constant, `const E: []T = [];` (report 2026-10.32), is 16 zero bytes in read-only data, read as an array constant is; - `LEN` of it is no constant. A constant's slice part takes `[]`, and - names a constant whole, of any module, by its type, not its text. + `LEN` of it is no constant. A constant's slice part takes `[]`, names + a constant whole, of any module, by its type, not its text, and takes + an array literal (report 2026-10.36), an array constant of its own in + a chunk with no name, nested to any depth. - An array a call returns is a temporary of the statement (report 2026-10.28, 8): a slice of it kept past the statement is undefined (10), and isn't caught. diff --git a/conform/err/SliceLiteralElem.raz b/conform/err/SliceLiteralElem.raz new file mode 100644 index 0000000..c2023b9 --- /dev/null +++ b/conform/err/SliceLiteralElem.raz @@ -0,0 +1,3 @@ +module SliceLiteralElem; +const Rows: [][]INT = [[1, 2], [3, true]]; +end SliceLiteralElem. diff --git a/conform/err/SliceLiteralPart.raz b/conform/err/SliceLiteralPart.raz new file mode 100644 index 0000000..c93e567 --- /dev/null +++ b/conform/err/SliceLiteralPart.raz @@ -0,0 +1,3 @@ +module SliceLiteralPart; +const Rows: [][]INT = [[1, 2], 3]; +end SliceLiteralPart. diff --git a/conform/err/SliceLiteralRecord.raz b/conform/err/SliceLiteralRecord.raz new file mode 100644 index 0000000..dff9834 --- /dev/null +++ b/conform/err/SliceLiteralRecord.raz @@ -0,0 +1,4 @@ +module SliceLiteralRecord; +type Rule = record name: []U8; keys: []INT; end; +const Rules: [1]Rule = [{name: "up", keys: [[1]]}]; +end SliceLiteralRecord. diff --git a/conform/run/SliceLiteralConst.expect b/conform/run/SliceLiteralConst.expect new file mode 100644 index 0000000..0a51f66 --- /dev/null +++ b/conform/run/SliceLiteralConst.expect @@ -0,0 +1,6 @@ +2:3 1:3 0:0 +[ab cd ][][e ] +up 2 172 +none 0 0 +6 +hi there diff --git a/conform/run/SliceLiteralConst.raz b/conform/run/SliceLiteralConst.raz new file mode 100644 index 0000000..747772e --- /dev/null +++ b/conform/run/SliceLiteralConst.raz @@ -0,0 +1,50 @@ +(* A constant's slice part written as an array literal (report section + 4): each literal an array constant of its own, nested to any depth, + inside records and variants too. *) +module SliceLiteralConst; +import Out; + +type + Rule = record name: []U8; keys: []INT; end; + Shape = variant dot; poly(xs: []INT); end; + +const + Rows: [][]INT = [[1, 2], [3], []]; + Deep: [][][]U8 = [[['a', 'b'], "cd"], [], [['e']]]; + Rules: []Rule = [{name: "up", keys: [107, 65]}, {name: "none", keys: []}]; + Shapes: [2]Shape = [poly([1, 2, 3]), dot]; + Words: [][]U8 = [['h', 'i'], "there"]; + +proc sum(xs: []INT): INT; +var i, s: INT; +begin + s := 0; + for i := 0 to LEN(xs) - 1 do s := s + xs[i]; end; + return s; +end; + +proc main*; +var i, j: INT; +begin + for i := 0 to LEN(Rows) - 1 do + Out.int(LEN(Rows[i])); Out.str(":"); Out.int(sum(Rows[i])); Out.str(" "); + end; + Out.ln; + for i := 0 to LEN(Deep) - 1 do + Out.str("["); + for j := 0 to LEN(Deep[i]) - 1 do Out.str(Deep[i][j]); Out.str(" "); end; + Out.str("]"); + end; + Out.ln; + for i := 0 to LEN(Rules) - 1 do + Out.str(Rules[i].name); Out.str(" "); Out.int(LEN(Rules[i].keys)); Out.str(" "); + Out.int(sum(Rules[i].keys)); Out.ln; + end; + match Shapes[0] of + | poly(xs): Out.int(sum(xs)); Out.ln; + | dot: + end; + Out.str(Words[0]); Out.str(" "); Out.str(Words[1]); Out.ln; +end; + +end SliceLiteralConst. diff --git a/doc/compiler.md b/doc/compiler.md index bbcf54d..f32be3f 100644 --- a/doc/compiler.md +++ b/doc/compiler.md @@ -23,7 +23,7 @@ | `src/X64.raz` | 1,594 | 1,310 | The instruction encoder, jumps within a procedure, the assembler | | `src/Gen.raz` | 2,688 | 1,902 | The stack machine, for x86-64: values, operations, checks, calls, frames | | `src/Item.raz` | 2,378 | 1,909 | Items: types, constants, variables, operators, conversions, calls | -| `src/Parse.raz` | 4,476 | 3,864 | Declarations, statements, expressions, built-ins, recovery, warnings | +| `src/Parse.raz` | 4,513 | 3,887 | Declarations, statements, expressions, built-ins, recovery, warnings | | `src/Link.raz` | 407 | 315 | Tree shaking, layout, patching, the places | | `src/Elf.raz` | 449 | 355 | Static and dynamic ELF, symbol table, line numbers | | `src/Raz.raz` | 418 | 333 | The command line | @@ -1394,6 +1394,19 @@ by leaving the part's bytes zero: const E: []I64 = []; (* read as the bytes of *) var E: []I64; + +A slice part written as an array literal is an array constant of its +own: `constPart` has `literalArray` make its type `[n]T`, and +`constChunk` read it into a chunk of its own, with no name, before the +constant around it is done; the part is then its address, a data fixup, +and its length, as for a named array constant. `constChunk` keeps the +outer constant's bytes aside while it reads the inner one, and puts the +inner one's fixups in `fixes` after the outer one's, taking them off +again when its chunk is made, so literals nest to any depth, in records +and variants too: + + const Rows: [][]INT = [[1, 2], [3]]; (* read as *) + const R0: [2]INT = [1, 2]; R1: [1]INT = [3]; Rows: [][]INT = [R0, R1]; `makeItem` makes it an `mVar` there that isn't `isVar`, so it is read, indexed, sliced and diff --git a/src/Parse.raz b/src/Parse.raz index b76e26b..7c1baff 100644 --- a/src/Parse.raz +++ b/src/Parse.raz @@ -3382,13 +3382,33 @@ return n; end; +(* A constant typed []T, or an array literal for a slice part of one, + 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). 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 t, nil; end; + a := Sym.newType(Sym.fArray, I64(n) * t^.base^.size, t^.base^.align); + a^.len := I64(n); + a^.base := t^.base; + return a, nil; +end; + +proc constPart(t: PType; off: INT): ERROR; forward; +proc constChunk(t: PType; n: INT): (INT, ERROR); forward; + (* A variant's case built from constants, at offset off of the constant (report section 4): its number, then each field where the case lays it out, as constructCase builds one in a program; the rest stays zero. The case is read here, by its name, since expression would make the code that builds one. *) -proc constPart(t: PType; off: INT): ERROR; forward; - proc constCase(t: PType; off: INT): ERROR; var c, f: PObj; at: Diag.Pos; begin @@ -3415,13 +3435,27 @@ (* 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, fx: INT; +var e: TItem; f: PObj; k, c: INT; r: F32; start: Diag.Pos; src: ^U8; st, fx: INT; a: PType; 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); + if (t^.form = Sym.fSlice) and (Scan.tok = Scan.sLbrak) then + a := try literalArray(t); + if a = t then + (* [], the empty slice (report, 4): its bytes stay zero, ptr nil and len 0 *) + Scan.next; + return expect(Scan.sRbrak); + end; + (* an array literal (report, 4) is an array constant of its own, + in a chunk of its own, made before this one is; the slice + points at it, as it would at a named one: + const Rows: [][]INT = [[1, 2], [3]]; + is laid out as + const R0: [2]INT = [1, 2]; R1: [1]INT = [3]; + const Rows: [][]INT = [R0, R1]; *) + c := try constChunk(a, -1); + try constFix(off, c); + putBytes(off + 8, 8, a^.len); + return nil; end; if (t^.form = Sym.fArray) and (Scan.tok = Scan.sLbrak) then Scan.next; @@ -3460,6 +3494,9 @@ return expect(Scan.sRbrace); end; if (t^.form = Sym.fVariant) and (Scan.tok = Scan.sIdent) then return constCase(t, off); end; + if Scan.tok = Scan.sLbrak then (* an array literal where neither an array nor a slice is *) + return Scan.mark(say1("expected {}, found an array literal", Sym.typeName(t))); + end; try expression(e, t); if (e.mode = Item.mStr) and ((t^.form = Sym.fSlice) or (t^.form = Sym.fArray)) and (t^.base = Sym.tU8) then if t^.form = Sym.fSlice then @@ -3476,7 +3513,7 @@ it, which has a type of its own: the element's, or a slice's *) if (e.mode # Item.mVar) or (e.obj = nil) or (e.obj^.cls # Sym.oConst) or (e.typ # e.obj^.typ) or not (e.typ^.form in {Sym.fArray, Sym.fSlice}) or not Sym.same(e.typ^.base, t^.base) then - return Scan.markAt(e.pos, say1("a constant slice is a string, an array constant of its elements or [], found {}", Item.what(e))); + 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))); end; if e.typ^.form = Sym.fArray then (* else an empty slice constant, whose bytes stay zero *) try constFix(off, e.chunk); @@ -3517,37 +3554,37 @@ return nil; end; -(* The chunk of n, a constant of type t whose literal is next *) -proc aggregateConst(t: PType; n: INT): (INT, ERROR); -var c, i, size: INT; +(* The chunk of n, a constant of type t whose literal is next, or of + an array literal in a constant's slice part, with n -1. That one is + made while the constant around it is still being read: its bytes are + its own, and the constant's are put back after; its addresses go on + in fixes after the constant's, from base, and are taken off again. *) +proc constChunk(t: PType; n: INT): (INT, ERROR); +var c, i, size, base: INT; outer: ^U8; begin + outer := constBytes; + base := numFixes; size := INT(t^.size); constBytes := Scan.grab(size + 8); for i := 0 to size + 7 do constBytes[i] := 0; end; - numFixes := 0; try constPart(t, 0); - c := Chunk.newChunk(Chunk.secRodata, Sym.curModule^.sname, n); + if n < 0 then + c := Chunk.newChunk(Chunk.secRodata, -1, -1); + else + c := Chunk.newChunk(Chunk.secRodata, Sym.curModule^.sname, n); + end; Chunk.setData(c, constBytes, size, INT(t^.align)); - for i := 0 to numFixes - 1 do Chunk.addDataFixup(c, fixes[i].off, fixes[i].chunk, 0); end; + for i := base to numFixes - 1 do Chunk.addDataFixup(c, fixes[i].off, fixes[i].chunk, 0); end; + numFixes := base; + constBytes := outer; return c, nil; 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). 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; +(* The chunk of n, a constant of type t whose literal is next *) +proc aggregateConst(t: PType; n: INT): (INT, ERROR); 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 t, nil; end; - a := Sym.newType(Sym.fArray, I64(n) * t^.base^.size, t^.base^.align); - a^.len := I64(n); - a^.base := t^.base; - return a, nil; + numFixes := 0; (* none left over from a constant an error broke off *) + return constChunk(t, n); end; proc constDecl(top: BOOL): ERROR; diff --git a/test/msg/SliceLiteralElem.err b/test/msg/SliceLiteralElem.err new file mode 100644 index 0000000..92028c4 --- /dev/null +++ b/test/msg/SliceLiteralElem.err @@ -0,0 +1,4 @@ +conform/err/SliceLiteralElem.raz:2:36: error: can't use BOOL as INT + 2 | const Rows: [][]INT = [[1, 2], [3, true]]; + | ^^^^ +raz: 1 error diff --git a/test/msg/SliceLiteralPart.err b/test/msg/SliceLiteralPart.err new file mode 100644 index 0000000..4e4a71c --- /dev/null +++ b/test/msg/SliceLiteralPart.err @@ -0,0 +1,4 @@ +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 | const Rows: [][]INT = [[1, 2], 3]; + | ^ +raz: 1 error diff --git a/test/msg/SliceLiteralRecord.err b/test/msg/SliceLiteralRecord.err new file mode 100644 index 0000000..509e7a9 --- /dev/null +++ b/test/msg/SliceLiteralRecord.err @@ -0,0 +1,4 @@ +conform/err/SliceLiteralRecord.raz:3:45: error: expected INT, found an array literal + 3 | const Rules: [1]Rule = [{name: "up", keys: [[1]]}]; + | ^ +raz: 1 error