From 71a95b30747f86fd056925969e8bcb08464c589e Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Sat, 17 Oct 2026 08:40:59 +0000 Subject: [PATCH] The compiler: a variant in a constant (report 2026-10.33, section 4) A typed constant may be a variant, or hold variants among its elements and fields. constCase reads the case by its name, not by expression, which would make the code that builds one, and writes its number and then each field where the case lays it out, the rest zero. A constant of a variant type is a variable in read-only data, as an array or record constant is; a match on it gives names that are only read. An untyped variant constant is an error that says to write its type. conform/err's ArrayConstElem, which said a constant holds no variant, now tests a field that isn't a constant. --- diff --git a/conform/err/ArrayConstElem.raz b/conform/err/ArrayConstElem.raz index 352662d..d7e2540 100644 --- a/conform/err/ArrayConstElem.raz +++ b/conform/err/ArrayConstElem.raz @@ -1,4 +1,5 @@ module ArrayConstElem; -type S = variant a; b; end; -const P: [1]S = [a]; +type S = variant a(n: INT); b; end; +var v: INT; +const P: [1]S = [a(v)]; end ArrayConstElem. diff --git a/conform/err/VariantConstMatch.raz b/conform/err/VariantConstMatch.raz new file mode 100644 index 0000000..0e3c1e0 --- /dev/null +++ b/conform/err/VariantConstMatch.raz @@ -0,0 +1,11 @@ +module VariantConstMatch; +type Shape = variant circle(r: INT); dot; end; +const C: Shape = circle(1); +proc main*; +begin + match C of + | circle(r): r := 2; + | dot: + end; +end; +end VariantConstMatch. diff --git a/conform/err/VariantConstUntyped.raz b/conform/err/VariantConstUntyped.raz new file mode 100644 index 0000000..53d8e74 --- /dev/null +++ b/conform/err/VariantConstUntyped.raz @@ -0,0 +1,4 @@ +module VariantConstUntyped; +type Shape = variant circle(r: INT); dot; end; +const D = dot; +end VariantConstUntyped. diff --git a/conform/run/VariantConst.expect b/conform/run/VariantConst.expect new file mode 100644 index 0000000..76d59b2 --- /dev/null +++ b/conform/run/VariantConst.expect @@ -0,0 +1,8 @@ +circle 3 +rect 2 3 +dot +box 7 +rect 4 5 +x 1 +rect 4 5 +20 diff --git a/conform/run/VariantConst.raz b/conform/run/VariantConst.raz new file mode 100644 index 0000000..ef210ce --- /dev/null +++ b/conform/run/VariantConst.raz @@ -0,0 +1,38 @@ +module VariantConst; +import Out; + +type + Shape = variant circle(r: F64); rect(w, h: INT); dot; named(name: []U8; n: U8); end; + Rule = record tag: INT; s: Shape; end; + +const + Shapes: []Shape = [circle(1.5), rect(2, 3), dot, named("box", 7)]; + One: Shape = rect(4, 5); + Rules: [2]Rule = [{tag: 1, s: dot}, {tag: 2, s: named("x", 1)}]; + +proc show(const s: Shape); +begin + match s of + | circle(r): Out.str("circle "); Out.int(I64(r * 2.0)); + | rect(w, h): Out.str("rect "); Out.int(w); Out.char(' '); Out.int(h); + | dot: Out.str("dot"); + | named(name, n): Out.str(name); Out.char(' '); Out.int(I64(n)); + end; + Out.ln; +end; + +proc main*; +var i: INT; s: Shape; +begin + for i := 0 to LEN(Shapes) - 1 do show(Shapes[i]); end; + show(One); + show(Rules[1].s); + s := One; + show(s); + match One of + | rect(w, h): Out.int(w * h); Out.ln; + | else: + end; +end; + +end VariantConst. diff --git a/conform/run/VariantConst.reals b/conform/run/VariantConst.reals new file mode 100644 index 0000000..e69de29 diff --git a/doc/compiler.md b/doc/compiler.md index 8a17358..473419b 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,091 | 1,682 | Items: types, constants, variables, operators, conversions, calls | -| `src/Parse.raz` | 4,182 | 3,619 | Declarations, statements, expressions, built-ins, recovery, warnings | +| `src/Parse.raz` | 4,218 | 3,648 | Declarations, statements, expressions, built-ins, recovery, warnings | | `src/Link.raz` | 381 | 298 | Tree shaking, layout, patching, the places | | `src/Elf.raz` | 449 | 355 | Static and dynamic ELF, symbol table, line numbers | | `src/Raz.raz` | 326 | 253 | The command line | @@ -1286,6 +1286,15 @@ `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]; + +A variant in a constant, `const S: Shape = circle(1.0)` or one among +an array constant's elements, is read by `constCase`, not by +`expression`, which would make the code that builds a case: it finds +the case by its name (`qualIdent`), writes its number in the first 8 +bytes, then each field at the offset the case gives it, by `constPart`, +leaving the rest zero, as a variable starts. A constant of a variant +type is then a variable in read-only data, as an array or record +constant is, and a `match` on it gives names that are only read. 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, diff --git a/doc/raz.7 b/doc/raz.7 index f3e3da6..b1f0e3a 100644 --- a/doc/raz.7 +++ b/doc/raz.7 @@ -213,6 +213,8 @@ .Cm dot , and read only by .Cm match . +A typed constant may hold variants: +.Cm "const Shapes: []Shape = [circle(1.0), dot];" .It .Cm "^VOLATILE T" points at what something else may change: each access is one access, diff --git a/src/Item.raz b/src/Item.raz index 99118fe..0c0a770 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, 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 *) + elsif o^.typ^.form in {Sym.fArray, Sym.fRecord, Sym.fSlice, Sym.fVariant} then + (* an array, record, variant 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 f3485dc..b8acd00 100644 --- a/src/Parse.raz +++ b/src/Parse.raz @@ -3182,6 +3182,37 @@ return n; end; +(* 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 + at := Scan.tokPos(); + c := try qualIdent(); + if (c^.cls # Sym.oCase) or (c^.typ # t) then + return Scan.markAt(Scan.spanFrom(at), say1("expected a case of {}", Sym.typeName(t))); + end; + putBytes(off, 8, c^.val); + f := c^.fields; + if Scan.tok = Scan.sLparen then + Scan.next; + while f # nil do + try constPart(f^.typ, off + INT(f^.val)); + f := f^.next; + if f # nil then try expect(Scan.sComma); end; + end; + return expect(Scan.sRparen); + elsif f # nil then + return Scan.markAt(Scan.spanFrom(at), say1("{} needs its fields", nameOf(c^.name))); + end; + return nil; +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; @@ -3228,6 +3259,7 @@ end; return expect(Scan.sRbrace); end; + if (t^.form = Sym.fVariant) and (Scan.tok = Scan.sIdent) then return constCase(t, off); 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 @@ -3251,7 +3283,7 @@ putBytes(off + 8, 8, e.typ^.len); end; elsif t^.form = Sym.fVariant then - return Scan.markAt(e.pos, "a constant holds no variant"); + return Scan.markAt(e.pos, say1("expected a case of {}", Sym.typeName(t))); elsif (t^.form = Sym.fArray) or (t^.form = Sym.fRecord) then return Scan.markAt(e.pos, say1("expected a literal of {}", Sym.typeName(t))); else @@ -3319,13 +3351,13 @@ start := Scan.tokPos(); t := try parseType(); if (t # Sym.tError) and not ((t^.form = Sym.fArray) or (t^.form = Sym.fSlice) - or ((t^.form = Sym.fRecord) and not t^.tuple)) then - return Scan.markAt(Scan.spanFrom(start), "only an ERROR, an array or a record constant has a type"); + or ((t^.form = Sym.fRecord) and not t^.tuple) or (t^.form = Sym.fVariant)) then + return Scan.markAt(Scan.spanFrom(start), "only an ERROR, an array, a record or a variant constant has a type"); end; end; try expect(Scan.sEql); 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 + if (t # nil) and (t^.form in {Sym.fArray, Sym.fRecord, Sym.fSlice, Sym.fVariant}) then o := Sym.newObj(Sym.oConst, n, t); o^.chunk := try aggregateConst(t, n); o^.pos := at; @@ -3334,6 +3366,10 @@ return expect(Scan.sSemicolon); end; try expression(x, nil); + if x.typ^.form = Sym.fVariant then + return Scan.markHelp(x.pos, "a variant constant needs its type", + say2("write const {}: {} = ...", nameOf(n), Sym.typeName(x.typ))); + end; o := Sym.newObj(Sym.oConst, n, x.typ); o^.pos := at; o^.exported := exp and top; diff --git a/test/msg/ArrayConstElem.err b/test/msg/ArrayConstElem.err index c957086..a9361ff 100644 --- a/test/msg/ArrayConstElem.err +++ b/test/msg/ArrayConstElem.err @@ -1,4 +1,4 @@ -conform/err/ArrayConstElem.raz:3:18: error: a constant holds no variant - 3 | const P: [1]S = [a]; - | ^ +conform/err/ArrayConstElem.raz:4:20: error: expected a constant, found INT + 4 | const P: [1]S = [a(v)]; + | ^ raz: 1 error diff --git a/test/msg/StmtErrType.err b/test/msg/StmtErrType.err index bd38fbc..3466a6a 100644 --- a/test/msg/StmtErrType.err +++ b/test/msg/StmtErrType.err @@ -1,4 +1,4 @@ -conform/err/StmtErrType.raz:2:10: error: only an ERROR, an array or a record constant has a type +conform/err/StmtErrType.raz:2:10: error: only an ERROR, an array, a record or a variant constant has a type 2 | const k: I64 = "a"; | ^^^ raz: 1 error diff --git a/test/msg/VariantConstMatch.err b/test/msg/VariantConstMatch.err new file mode 100644 index 0000000..cffaa16 --- /dev/null +++ b/test/msg/VariantConstMatch.err @@ -0,0 +1,5 @@ +conform/err/VariantConstMatch.raz:7:18: error: r is only read here, and can't be changed + 7 | | circle(r): r := 2; + | ^ +help: a const parameter, a constant, and the names a match gives their fields, are only read: take a var parameter to change it +raz: 1 error diff --git a/test/msg/VariantConstUntyped.err b/test/msg/VariantConstUntyped.err new file mode 100644 index 0000000..65e164c --- /dev/null +++ b/test/msg/VariantConstUntyped.err @@ -0,0 +1,5 @@ +conform/err/VariantConstUntyped.raz:3:11: error: a variant constant needs its type + 3 | const D = dot; + | ^^^ +help: write const D: Shape = ... +raz: 1 error