raz / commit 71a95b30

commit
71a95b30747f86fd056925969e8bcb08464c589e
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
b13f87f8
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.

patch · log

14 files changed, +133 -14

conform/err/ArrayConstElem.raz (history) modified +3 -2

@@ -1,4 +1,5 @@
11 module ArrayConstElem;
2-type S = variant a; b; end;
3-const P: [1]S = [a];
2+type S = variant a(n: INT); b; end;
3+var v: INT;
4+const P: [1]S = [a(v)];
45 end ArrayConstElem.

conform/err/VariantConstMatch.raz (history) added +11 -0

@@ -0,0 +1,11 @@
1+module VariantConstMatch;
2+type Shape = variant circle(r: INT); dot; end;
3+const C: Shape = circle(1);
4+proc main*;
5+begin
6+ match C of
7+ | circle(r): r := 2;
8+ | dot:
9+ end;
10+end;
11+end VariantConstMatch.

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

@@ -0,0 +1,4 @@
1+module VariantConstUntyped;
2+type Shape = variant circle(r: INT); dot; end;
3+const D = dot;
4+end VariantConstUntyped.

conform/run/VariantConst.expect (history) added +8 -0

@@ -0,0 +1,8 @@
1+circle 3
2+rect 2 3
3+dot
4+box 7
5+rect 4 5
6+x 1
7+rect 4 5
8+20

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

@@ -0,0 +1,38 @@
1+module VariantConst;
2+import Out;
3+
4+type
5+ Shape = variant circle(r: F64); rect(w, h: INT); dot; named(name: []U8; n: U8); end;
6+ Rule = record tag: INT; s: Shape; end;
7+
8+const
9+ Shapes: []Shape = [circle(1.5), rect(2, 3), dot, named("box", 7)];
10+ One: Shape = rect(4, 5);
11+ Rules: [2]Rule = [{tag: 1, s: dot}, {tag: 2, s: named("x", 1)}];
12+
13+proc show(const s: Shape);
14+begin
15+ match s of
16+ | circle(r): Out.str("circle "); Out.int(I64(r * 2.0));
17+ | rect(w, h): Out.str("rect "); Out.int(w); Out.char(' '); Out.int(h);
18+ | dot: Out.str("dot");
19+ | named(name, n): Out.str(name); Out.char(' '); Out.int(I64(n));
20+ end;
21+ Out.ln;
22+end;
23+
24+proc main*;
25+var i: INT; s: Shape;
26+begin
27+ for i := 0 to LEN(Shapes) - 1 do show(Shapes[i]); end;
28+ show(One);
29+ show(Rules[1].s);
30+ s := One;
31+ show(s);
32+ match One of
33+ | rect(w, h): Out.int(w * h); Out.ln;
34+ | else:
35+ end;
36+end;
37+
38+end VariantConst.

conform/run/VariantConst.reals (history) added +0 -0

An empty file.

doc/compiler.md (history) modified +10 -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,091 | 1,682 | Items: types, constants, variables, operators, conversions, calls |
23-| `src/Parse.raz` | 4,182 | 3,619 | Declarations, statements, expressions, built-ins, recovery, warnings |
23+| `src/Parse.raz` | 4,218 | 3,648 | Declarations, statements, expressions, built-ins, recovery, warnings |
2424 | `src/Link.raz` | 381 | 298 | Tree shaking, layout, patching, the places |
2525 | `src/Elf.raz` | 449 | 355 | Static and dynamic ELF, symbol table, line numbers |
2626 | `src/Raz.raz` | 326 | 253 | The command line |
@@ -1286,6 +1286,15 @@
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+A variant in a constant, `const S: Shape = circle(1.0)` or one among
1291+an array constant's elements, is read by `constCase`, not by
1292+`expression`, which would make the code that builds a case: it finds
1293+the case by its name (`qualIdent`), writes its number in the first 8
1294+bytes, then each field at the offset the case gives it, by `constPart`,
1295+leaving the rest zero, as a variable starts. A constant of a variant
1296+type is then a variable in read-only data, as an array or record
1297+constant is, and a `match` on it gives names that are only read.
12891298
12901299 With no element, `[]`, the type stays `[]T`, and the constant is 16
12911300 zero bytes in read-only data: the empty slice, `ptr` `nil` and `len` 0,

doc/raz.7 (history) modified +2 -0

@@ -213,6 +213,8 @@
213213 .Cm dot ,
214214 and read only by
215215 .Cm match .
216+A typed constant may hold variants:
217+.Cm "const Shapes: []Shape = [circle(1.0), dot];"
216218 .It
217219 .Cm "^VOLATILE T"
218220 points at what something else may change: each access is one access,

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, 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 *)
261+ elsif o^.typ^.form in {Sym.fArray, Sym.fRecord, Sym.fSlice, Sym.fVariant} then
262+ (* an array, record, variant or empty slice constant: a variable in
263+ read-only 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 +40 -4

@@ -3182,6 +3182,37 @@
31823182 return n;
31833183 end;
31843184
3185+(* A variant's case built from constants, at offset off of the constant
3186+ (report section 4): its number, then each field where the case lays
3187+ it out, as constructCase builds one in a program; the rest stays
3188+ zero. The case is read here, by its name, since expression would
3189+ make the code that builds one. *)
3190+proc constPart(t: PType; off: INT): ERROR; forward;
3191+
3192+proc constCase(t: PType; off: INT): ERROR;
3193+var c, f: PObj; at: Diag.Pos;
3194+begin
3195+ at := Scan.tokPos();
3196+ c := try qualIdent();
3197+ if (c^.cls # Sym.oCase) or (c^.typ # t) then
3198+ return Scan.markAt(Scan.spanFrom(at), say1("expected a case of {}", Sym.typeName(t)));
3199+ end;
3200+ putBytes(off, 8, c^.val);
3201+ f := c^.fields;
3202+ if Scan.tok = Scan.sLparen then
3203+ Scan.next;
3204+ while f # nil do
3205+ try constPart(f^.typ, off + INT(f^.val));
3206+ f := f^.next;
3207+ if f # nil then try expect(Scan.sComma); end;
3208+ end;
3209+ return expect(Scan.sRparen);
3210+ elsif f # nil then
3211+ return Scan.markAt(Scan.spanFrom(at), say1("{} needs its fields", nameOf(c^.name)));
3212+ end;
3213+ return nil;
3214+end;
3215+
31853216 (* The part of type t at offset off of the constant *)
31863217 proc constPart(t: PType; off: INT): ERROR;
31873218 var e: TItem; f: PObj; k: INT; r: F32; start: Diag.Pos; src: ^U8; st: INT;
@@ -3228,6 +3259,7 @@
32283259 end;
32293260 return expect(Scan.sRbrace);
32303261 end;
3262+ if (t^.form = Sym.fVariant) and (Scan.tok = Scan.sIdent) then return constCase(t, off); end;
32313263 try expression(e, t);
32323264 if (e.mode = Item.mStr) and ((t^.form = Sym.fSlice) or (t^.form = Sym.fArray)) and (t^.base = Sym.tU8) then
32333265 if t^.form = Sym.fSlice then
@@ -3251,7 +3283,7 @@
32513283 putBytes(off + 8, 8, e.typ^.len);
32523284 end;
32533285 elsif t^.form = Sym.fVariant then
3254- return Scan.markAt(e.pos, "a constant holds no variant");
3286+ return Scan.markAt(e.pos, say1("expected a case of {}", Sym.typeName(t)));
32553287 elsif (t^.form = Sym.fArray) or (t^.form = Sym.fRecord) then
32563288 return Scan.markAt(e.pos, say1("expected a literal of {}", Sym.typeName(t)));
32573289 else
@@ -3319,13 +3351,13 @@
33193351 start := Scan.tokPos();
33203352 t := try parseType();
33213353 if (t # Sym.tError) and not ((t^.form = Sym.fArray) or (t^.form = Sym.fSlice)
3322- or ((t^.form = Sym.fRecord) and not t^.tuple)) then
3323- return Scan.markAt(Scan.spanFrom(start), "only an ERROR, an array or a record constant has a type");
3354+ or ((t^.form = Sym.fRecord) and not t^.tuple) or (t^.form = Sym.fVariant)) then
3355+ return Scan.markAt(Scan.spanFrom(start), "only an ERROR, an array, a record or a variant constant has a type");
33243356 end;
33253357 end;
33263358 try expect(Scan.sEql);
33273359 if (t # nil) and (t^.form = Sym.fSlice) then t := try literalArray(t); end;
3328- if (t # nil) and (t^.form in {Sym.fArray, Sym.fRecord, Sym.fSlice}) then
3360+ if (t # nil) and (t^.form in {Sym.fArray, Sym.fRecord, Sym.fSlice, Sym.fVariant}) then
33293361 o := Sym.newObj(Sym.oConst, n, t);
33303362 o^.chunk := try aggregateConst(t, n);
33313363 o^.pos := at;
@@ -3334,6 +3366,10 @@
33343366 return expect(Scan.sSemicolon);
33353367 end;
33363368 try expression(x, nil);
3369+ if x.typ^.form = Sym.fVariant then
3370+ return Scan.markHelp(x.pos, "a variant constant needs its type",
3371+ say2("write const {}: {} = ...", nameOf(n), Sym.typeName(x.typ)));
3372+ end;
33373373 o := Sym.newObj(Sym.oConst, n, x.typ);
33383374 o^.pos := at;
33393375 o^.exported := exp and top;

test/msg/ArrayConstElem.err (history) modified +3 -3

@@ -1,4 +1,4 @@
1-conform/err/ArrayConstElem.raz:3:18: error: a constant holds no variant
2- 3 | const P: [1]S = [a];
3- | ^
1+conform/err/ArrayConstElem.raz:4:20: error: expected a constant, found INT
2+ 4 | const P: [1]S = [a(v)];
3+ | ^
44 raz: 1 error

test/msg/StmtErrType.err (history) modified +1 -1

@@ -1,4 +1,4 @@
1-conform/err/StmtErrType.raz:2:10: error: only an ERROR, an array or a record constant has a type
1+conform/err/StmtErrType.raz:2:10: error: only an ERROR, an array, a record or a variant constant has a type
22 2 | const k: I64 = "a";
33 | ^^^
44 raz: 1 error

test/msg/VariantConstMatch.err (history) added +5 -0

@@ -0,0 +1,5 @@
1+conform/err/VariantConstMatch.raz:7:18: error: r is only read here, and can't be changed
2+ 7 | | circle(r): r := 2;
3+ | ^
4+help: a const parameter, a constant, and the names a match gives their fields, are only read: take a var parameter to change it
5+raz: 1 error

test/msg/VariantConstUntyped.err (history) added +5 -0

@@ -0,0 +1,5 @@
1+conform/err/VariantConstUntyped.raz:3:11: error: a variant constant needs its type
2+ 3 | const D = dot;
3+ | ^^^
4+help: write const D: Shape = ...
5+raz: 1 error