raz / commit 3e1c4e5b

commit
3e1c4e5b91ecd37c4eaf5d3269e7fa999c6b65ea
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
bfb7ab7c
Enumerations' names with their types (report 2026-10.31)

Elem.bold, and M.Elem.bold, by qualIdent (enumName). Sym.insert lets
an enumeration's name (ofEnum) share its scope: another declaration
keeps the name alone; a second enumeration's is the first's twin, and
the name alone is then an error (twinError). A match arm may write the
name with its type (typeFirst). The interface check passes over an
enumeration's names, which its type's fingerprint has. Tests in
conform/, messages in test/msg.

patch · log

18 files changed, +237 -9

conform/err/EnumArmType.raz (history) added +13 -0

@@ -0,0 +1,13 @@
1+module EnumArmType;
2+type
3+ Style = (bold, italic);
4+ Elem = (para, bold);
5+proc main*;
6+var e: Elem;
7+begin
8+ match e of
9+ | Style.bold:
10+ | else:
11+ end;
12+end;
13+end EnumArmType.

conform/err/EnumNoName.raz (history) added +8 -0

@@ -0,0 +1,8 @@
1+module EnumNoName;
2+type Style = (bold, italic);
3+proc main*;
4+var s: Style;
5+begin
6+ s := Style.under;
7+end;
8+end EnumNoName.

conform/err/EnumShared.raz (history) added +10 -0

@@ -0,0 +1,10 @@
1+module EnumShared;
2+type
3+ Style = (bold, italic);
4+ Elem = (para, bold);
5+proc main*;
6+var s: Style;
7+begin
8+ s := bold;
9+end;
10+end EnumShared.

conform/err/EnumSharedImported.raz (history) added +8 -0

@@ -0,0 +1,8 @@
1+module EnumSharedImported;
2+import EnumKinds;
3+proc main*;
4+var k: EnumKinds.Elem;
5+begin
6+ k := EnumKinds.bold;
7+end;
8+end EnumSharedImported.

conform/err/EnumTwiceOther.raz (history) added +8 -0

@@ -0,0 +1,8 @@
1+module EnumTwiceOther;
2+type Style = (bold, italic);
3+var bold: INT;
4+const bold = 2;
5+proc main*;
6+begin
7+end;
8+end EnumTwiceOther.

conform/err/mods/EnumKinds.raz (history) added +8 -0

@@ -0,0 +1,8 @@
1+(* Two enumerations that share a name, exported, for EnumNames. *)
2+module EnumKinds;
3+
4+type
5+ Style* = (plain, bold, italic);
6+ Elem* = (para, bold, rule);
7+
8+end EnumKinds.

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

@@ -0,0 +1,6 @@
1+Style.bold and Elem.bold
2+the procedure rule
3+an arm with its type
4+in the set
5+ordered
6+imported, with module and type

conform/run/EnumNames.raz (history) added +44 -0

@@ -0,0 +1,44 @@
1+(* A name of an enumeration may be written with its type; two
2+ enumerations of a scope may share a name, and an enumeration may share
3+ one with another declaration: written alone, the name is that other
4+ one (report 2026-10.31, section 5.7). *)
5+module EnumNames;
6+import Out, EnumKinds;
7+
8+type
9+ Style = (bold, italic);
10+ Elem = (para, bold, rule);
11+ Light = (red, amber, green);
12+
13+const Stop = Light.red;
14+
15+var s: Style; e: Elem; ss: set of Style; k: EnumKinds.Elem;
16+
17+proc rule; (* beside Elem's rule *)
18+begin
19+ Out.str("the procedure rule\n");
20+end;
21+
22+proc main*;
23+begin
24+ s := Style.bold; e := Elem.bold;
25+ if (s = Style.bold) and (e = Elem.bold) and (INT(e) = 1) then Out.str("Style.bold and Elem.bold\n"); end;
26+ e := para; (* Elem's alone *)
27+ e := Elem.rule;
28+ rule; (* the procedure *)
29+ s := italic;
30+ match e of
31+ | Elem.rule: Out.str("an arm with its type\n");
32+ | para, bold: Out.str("an arm without\n");
33+ end;
34+ ss := {Style.bold, italic};
35+ if Style.bold in ss then Out.str("in the set\n"); end;
36+ if (Stop = red) and (Light.green > amber) then Out.str("ordered\n"); end;
37+ k := EnumKinds.Elem.bold;
38+ if (k = EnumKinds.Elem.bold) and (EnumKinds.Style.bold # EnumKinds.Style.plain) then
39+ Out.str("imported, with module and type\n");
40+ end;
41+ if EnumKinds.para = k then Out.str("no\n"); end;
42+end;
43+
44+end EnumNames.

conform/run/mods/EnumKinds.raz (history) added +8 -0

@@ -0,0 +1,8 @@
1+(* Two enumerations that share a name, exported, for EnumNames. *)
2+module EnumKinds;
3+
4+type
5+ Style* = (plain, bold, italic);
6+ Elem* = (para, bold, rule);
7+
8+end EnumKinds.

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

@@ -15,12 +15,12 @@
1515 |---|---|---|---|
1616 | `src/Diag.raz` | 410 | 315 | Messages: positions, source excerpts, colour, counts |
1717 | `src/Scan.raz` | 1,127 | 934 | Symbols, literals, comments, positions |
18-| `src/Sym.raz` | 687 | 556 | Types, objects, scopes, layout |
18+| `src/Sym.raz` | 707 | 566 | Types, objects, scopes, layout |
1919 | `src/Chunk.raz` | 363 | 279 | The compiled program: chunks, fixups, line notes |
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,037 | 1,651 | Items: types, constants, variables, operators, conversions, calls |
23-| `src/Parse.raz` | 4,065 | 3,532 | Declarations, statements, expressions, built-ins, recovery, warnings |
23+| `src/Parse.raz` | 4,123 | 3,578 | Declarations, statements, expressions, built-ins, recovery, warnings |
2424 | `src/Link.raz` | 381 | 298 | Tree shaking, layout, patching, the places |
2525 | `src/Elf.raz` | 431 | 344 | Static and dynamic ELF, symbol table, line numbers |
2626 | `src/Raz.raz` | 305 | 238 | The command line |
@@ -979,7 +979,12 @@
979979 procedures, is set up in the module's body.
980980
981981 Two routines can find an error: `insert`, for a name declared twice, and
982-`openScope`, for too many scopes. Each shows it and returns
982+`openScope`, for too many scopes. A name of an enumeration (`ofEnum`)
983+may share its scope with another declaration (report 2026-10.31): it is
984+reached through its type, `Elem.bold`, so `insert` keeps the other
985+declaration in the scope for the name alone; of two enumerations'
986+names it keeps the first, and notes the second as its `twin`, which
987+makes the name alone an error. Each shows it and returns
983988 `Scan.Failed`, for the parser to pass up with `try`. A record's module
984989 and an object's link, which the seed called `module` and `link`, are
985990 `home` and `chain`, since those are reserved words here; `setCurModule`
@@ -1452,7 +1457,12 @@
14521457 rdx, and has gen's `codeProc` run
14531458 the target's assembler) and bodies (`body`).
14541459 `parseType` and its helpers (`recordType`, `variantType`, `signature`,
1455-`enumType`) build `Sym.Type`s.
1460+`enumType`) build `Sym.Type`s. `qualIdent` reads a name, `M.x` too, and
1461+`Elem.bold`, a name of an enumeration with its type (`enumName`); it
1462+refuses a name two enumerations share (`twinError`). An arm of a `match`
1463+on an enumeration takes its names from the type, written alone or with
1464+it (`typeFirst`). `compareIface` passes over an enumeration's names,
1465+which its type's fingerprint has.
14561466
14571467 Last, modules, at the end of the file. `compileFile` reads one module.
14581468 `importModule` finds a module's file, trying the target's names first,

src/Parse.raz (history) modified +60 -2

@@ -441,6 +441,33 @@
441441 proc instancesFor(wholeRest: BOOL): ERROR; forward;
442442 proc addImport(m, im: PModule); forward;
443443
444+(* The name of the enumeration t that the current symbol is, after
445+ "t.": Elem.bold (report section 5.7). *)
446+proc enumName(t: PType): (PObj, ERROR);
447+var o: PObj;
448+begin
449+ if Scan.tok # Scan.sIdent then return nil, expected("a name"); end;
450+ o := t^.fields;
451+ while (o # nil) and (o^.name # Scan.id) do o := o^.next; end;
452+ if o = nil then
453+ return nil, Scan.mark(say2("{} has no name {}", Sym.typeName(t), nameOf(Scan.id)));
454+ end;
455+ Scan.next;
456+ return o, nil;
457+end;
458+
459+(* An error at at: o, a name of an enumeration, is a name of another of
460+ its scope too, so it is written with its type. *)
461+proc twinError(o: PObj; at: Diag.Pos): ERROR;
462+var a, b: []U8;
463+begin
464+ a := Sym.typeName(o^.typ); b := Sym.typeName(o^.twin^.typ);
465+ return Scan.markHelp(at, Diag.say("{} is a name of {} and of {}",
466+ [Fmt.str(nameOf(o^.name)), Fmt.str(a), Fmt.str(b)]),
467+ Diag.say("write it with its type: {}.{} or {}.{}",
468+ [Fmt.str(a), Fmt.str(nameOf(o^.name)), Fmt.str(b), Fmt.str(nameOf(o^.name))]));
469+end;
470+
444471 proc qualIdent: (PObj, ERROR);
445472 var o: PObj; m: PModule; at: Diag.Pos;
446473 begin
@@ -472,6 +499,12 @@
472499 end;
473500 Scan.next;
474501 end;
502+ if (o^.cls = Sym.oType) and (o^.typ^.form = Sym.fEnum) and (Scan.tok = Scan.sPeriod) then
503+ Scan.next;
504+ o := try enumName(o^.typ);
505+ elsif o^.twin # nil then
506+ return nil, twinError(o, at);
507+ end;
475508 return o, nil;
476509 end;
477510
@@ -690,6 +723,7 @@
690723 if Scan.tok # Scan.sIdent then return nil, expected("a name"); end;
691724 o := Sym.newObj(Sym.oConst, Scan.id, t);
692725 o^.val := t^.len;
726+ o^.ofEnum := true;
693727 o^.exported := declExported;
694728 o^.used := true; (* an enumeration's names are its type's, warned of with it *)
695729 try declare(o);
@@ -1981,6 +2015,27 @@
19812015 if elseAt # Gen.NoLabel then Gen.jumpBack(elseAt); end;
19822016 end;
19832017
2018+(* In an arm of a match on the enumeration t, a name may be written
2019+ with its type, | Elem.rule: (report section 5.7). If it is, skips the
2020+ type and the dot, which must name t itself. *)
2021+proc typeFirst(t: PType): ERROR;
2022+var st: Scan.State; o: PObj;
2023+begin
2024+ o := Sym.lookup(Scan.id);
2025+ if (o = nil) or (o^.cls # Sym.oType) then return nil; end;
2026+ Scan.saveScan(st);
2027+ Scan.next;
2028+ if Scan.tok # Scan.sPeriod then Scan.restoreScan(st); return nil; end;
2029+ if not Sym.same(o^.typ, t) then
2030+ Scan.restoreScan(st);
2031+ return Scan.mark(say2("the match is on {}, not {}", Sym.typeName(t), Sym.typeName(o^.typ)));
2032+ end;
2033+ named(o);
2034+ Scan.next;
2035+ if Scan.tok # Scan.sIdent then return expected("a name"); end;
2036+ return nil;
2037+end;
2038+
19842039 proc matchStatement(start: Diag.Pos; ind: INT): ERROR;
19852040 var
19862041 x: TItem; t: PType; c, f, o: PObj; p: I64; L, endL, n, first, disp, elseAt: INT;
@@ -2022,6 +2077,7 @@
20222077 L := Gen.NoLabel;
20232078 repeat
20242079 if Scan.tok # Scan.sIdent then return expected("a case"); end;
2080+ if t^.form = Sym.fEnum then try typeFirst(t); end;
20252081 c := t^.fields;
20262082 while (c # nil) and (c^.name # Scan.id) do c := c^.next; end;
20272083 if c = nil then
@@ -3810,9 +3866,11 @@
38103866 proc compareIface(a, b: PModule);
38113867 var o, q: PObj;
38123868 begin
3869+ (* an enumeration's names are its type's, which the type's
3870+ fingerprint has, and the name alone may be another's *)
38133871 o := a^.first;
38143872 while o # nil do
3815- if o^.exported then
3873+ if o^.exported and not o^.ofEnum then
38163874 q := Sym.find(b^.scope, o^.name);
38173875 if (q = nil) or not q^.exported or (Sym.fingerprint(o) # Sym.fingerprint(q)) then
38183876 Diag.fatal(Diag.say("{}: {} isn't exported as in {}", [Fmt.str(b^.path), Fmt.str(nameOf(o^.name)), Fmt.str(a^.path)]));
@@ -3823,7 +3881,7 @@
38233881 q := b^.first;
38243882 while q # nil do
38253883 o := Sym.find(a^.scope, q^.name);
3826- if q^.exported and ((o = nil) or not o^.exported) then
3884+ if q^.exported and not q^.ofEnum and ((o = nil) or not o^.exported) then
38273885 Diag.fatal(Diag.say("{}: {} isn't exported by {}", [Fmt.str(b^.path), Fmt.str(nameOf(q^.name)), Fmt.str(a^.path)]));
38283886 end;
38293887 q := q^.chain;

src/Sym.raz (history) modified +20 -0

@@ -77,6 +77,12 @@
7777 home*: ^Module; (* oModule: the module; others: where declared *)
7878 chain*: ^Obj; (* the next declaration of its module, top-level or
7979 local *)
80+ ofEnum*: BOOL; (* oConst: a name of its enumeration, which may share
81+ its scope with another declaration of the name
82+ (report section 5.7) *)
83+ twin*: ^Obj; (* an enumeration's name in its scope: another
84+ enumeration's of the same name, which makes the
85+ name alone an error; nil if none *)
8086 end;
8187
8288 (* The arguments of a generic module's instance: a type, or a
@@ -267,6 +273,12 @@
267273 Mem.free(Heap.heap, ^U8(old));
268274 end;
269275
276+(* Declares obj in the scope s, or reports a name declared twice there.
277+ A name of an enumeration may share its scope with another declaration
278+ of the name (report section 5.7): it is always reached through its
279+ type, Elem.bold, so the scope keeps the other declaration, which the
280+ name alone then means; of two enumerations' names, it keeps the first,
281+ with the second as its twin, and the name alone is an error. *)
270282 proc insert*(obj: ^Obj; s: INT): ERROR;
271283 var i: INT; o: ^Obj;
272284 begin
@@ -274,6 +286,14 @@
274286 o := hashTab[i];
275287 while o # nil do
276288 if (o^.scope = s) and (o^.name = obj^.name) then
289+ obj^.scope := s;
290+ if obj^.ofEnum then
291+ if o^.ofEnum and (o^.twin = nil) then o^.twin := obj; end;
292+ return nil;
293+ elsif o^.ofEnum then
294+ hashTab[i] := obj; (* the name alone is obj *)
295+ return nil;
296+ end;
277297 Diag.error(obj^.pos, Diag.say("{} is declared twice", [Fmt.str(Scan.nameOf(obj^.name))]));
278298 Diag.note(o^.pos, "the first one is here");
279299 return Scan.Failed;

src/check.sh modified +5 -3

@@ -15,8 +15,8 @@
1515 # and, through Math, test/bench/NBody.raz use; and returning a slice of
1616 # the frame as an error (report 2026-10.27); an array constant typed
1717 # []T (report 2026-10.29); [] as the empty slice, seen through a view
18-# (report 2026-10.30); and U8, I8 and I16 widening to INT, and
19-# continue (report 2026-10.31).
18+# (report 2026-10.30); and U8, I8 and I16 widening to INT, continue,
19+# and enumerations' names with their types (report 2026-10.31).
2020 newer="conform/run/RawStrings.raz conform/err/ScanRawOpen.raz conform/err/ScanRawIndent.raz
2121 conform/run/MatchStr.raz conform/err/MatchStrTwice.raz conform/err/MatchStrNoElse.raz
2222 conform/err/MatchStrVar.raz conform/err/MatchReal.raz conform/err/AtMatch.raz
@@ -30,7 +30,9 @@
3030 conform/run/SmallWiden.raz conform/err/SmallWidenMade.raz conform/err/SmallWidenNeg.raz
3131 conform/err/SmallWidenI64.raz conform/err/SmallWidenMadeOp.raz
3232 conform/run/Continue.raz conform/err/ContinueOutside.raz conform/err/ContinueDefer.raz
33- conform/err/ContinueName.raz"
33+ conform/err/ContinueName.raz
34+ conform/run/EnumNames.raz conform/err/EnumShared.raz conform/err/EnumNoName.raz
35+ conform/err/EnumArmType.raz conform/err/EnumSharedImported.raz conform/err/EnumTwiceOther.raz"
3436 newer() {
3537 case $1 in conform/*/View*.raz) return 0 ;; esac
3638 for g in $newer; do [ $g = $1 ] && return 0; done

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

@@ -0,0 +1,4 @@
1+conform/err/EnumArmType.raz:9:7: error: the match is on Elem, not Style
2+ 9 | | Style.bold:
3+ | ^^^^^
4+raz: 1 error

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

@@ -0,0 +1,4 @@
1+conform/err/EnumNoName.raz:6:16: error: Style has no name under
2+ 6 | s := Style.under;
3+ | ^^^^^
4+raz: 1 error

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

@@ -0,0 +1,5 @@
1+conform/err/EnumShared.raz:8:10: error: bold is a name of Style and of Elem
2+ 8 | s := bold;
3+ | ^^^^
4+help: write it with its type: Style.bold or Elem.bold
5+raz: 1 error

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

@@ -0,0 +1,5 @@
1+conform/err/EnumSharedImported.raz:6:10: error: bold is a name of EnumKinds.Style and of EnumKinds.Elem
2+ 6 | k := EnumKinds.bold;
3+ | ^^^^^^^^^
4+help: write it with its type: EnumKinds.Style.bold or EnumKinds.Elem.bold
5+raz: 1 error

test/msg/EnumTwiceOther.err (history) added +7 -0

@@ -0,0 +1,7 @@
1+conform/err/EnumTwiceOther.raz:4:7: error: bold is declared twice
2+ 4 | const bold = 2;
3+ | ^^^^
4+conform/err/EnumTwiceOther.raz:3:5: note: the first one is here
5+ 3 | var bold: INT;
6+ | ^^^^
7+raz: 1 error