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.
| @@ -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. |
| @@ -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. |
| @@ -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. |
| @@ -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. |
| @@ -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. |
| @@ -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. |
| @@ -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 |
| @@ -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. |
| @@ -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. |
| @@ -15,12 +15,12 @@ | ||
| 15 | 15 | |---|---|---|---| |
| 16 | 16 | | `src/Diag.raz` | 410 | 315 | Messages: positions, source excerpts, colour, counts | |
| 17 | 17 | | `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 | | |
| 19 | 19 | | `src/Chunk.raz` | 363 | 279 | The compiled program: chunks, fixups, line notes | |
| 20 | 20 | | `src/X64.raz` | 1,517 | 1,253 | The instruction encoder, jumps within a procedure, the assembler | |
| 21 | 21 | | `src/Gen.raz` | 2,552 | 1,821 | The stack machine, for x86-64: values, operations, checks, calls, frames | |
| 22 | 22 | | `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 | | |
| 24 | 24 | | `src/Link.raz` | 381 | 298 | Tree shaking, layout, patching, the places | |
| 25 | 25 | | `src/Elf.raz` | 431 | 344 | Static and dynamic ELF, symbol table, line numbers | |
| 26 | 26 | | `src/Raz.raz` | 305 | 238 | The command line | |
| @@ -979,7 +979,12 @@ | ||
| 979 | 979 | procedures, is set up in the module's body. |
| 980 | 980 | |
| 981 | 981 | 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 | |
| 983 | 988 | `Scan.Failed`, for the parser to pass up with `try`. A record's module |
| 984 | 989 | and an object's link, which the seed called `module` and `link`, are |
| 985 | 990 | `home` and `chain`, since those are reserved words here; `setCurModule` |
| @@ -1452,7 +1457,12 @@ | ||
| 1452 | 1457 | rdx, and has gen's `codeProc` run |
| 1453 | 1458 | the target's assembler) and bodies (`body`). |
| 1454 | 1459 | `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. | |
| 1456 | 1466 | |
| 1457 | 1467 | Last, modules, at the end of the file. `compileFile` reads one module. |
| 1458 | 1468 | `importModule` finds a module's file, trying the target's names first, |
| @@ -441,6 +441,33 @@ | ||
| 441 | 441 | proc instancesFor(wholeRest: BOOL): ERROR; forward; |
| 442 | 442 | proc addImport(m, im: PModule); forward; |
| 443 | 443 | |
| 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 | + | |
| 444 | 471 | proc qualIdent: (PObj, ERROR); |
| 445 | 472 | var o: PObj; m: PModule; at: Diag.Pos; |
| 446 | 473 | begin |
| @@ -472,6 +499,12 @@ | ||
| 472 | 499 | end; |
| 473 | 500 | Scan.next; |
| 474 | 501 | 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; | |
| 475 | 508 | return o, nil; |
| 476 | 509 | end; |
| 477 | 510 | |
| @@ -690,6 +723,7 @@ | ||
| 690 | 723 | if Scan.tok # Scan.sIdent then return nil, expected("a name"); end; |
| 691 | 724 | o := Sym.newObj(Sym.oConst, Scan.id, t); |
| 692 | 725 | o^.val := t^.len; |
| 726 | + o^.ofEnum := true; | |
| 693 | 727 | o^.exported := declExported; |
| 694 | 728 | o^.used := true; (* an enumeration's names are its type's, warned of with it *) |
| 695 | 729 | try declare(o); |
| @@ -1981,6 +2015,27 @@ | ||
| 1981 | 2015 | if elseAt # Gen.NoLabel then Gen.jumpBack(elseAt); end; |
| 1982 | 2016 | end; |
| 1983 | 2017 | |
| 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 | + | |
| 1984 | 2039 | proc matchStatement(start: Diag.Pos; ind: INT): ERROR; |
| 1985 | 2040 | var |
| 1986 | 2041 | x: TItem; t: PType; c, f, o: PObj; p: I64; L, endL, n, first, disp, elseAt: INT; |
| @@ -2022,6 +2077,7 @@ | ||
| 2022 | 2077 | L := Gen.NoLabel; |
| 2023 | 2078 | repeat |
| 2024 | 2079 | if Scan.tok # Scan.sIdent then return expected("a case"); end; |
| 2080 | + if t^.form = Sym.fEnum then try typeFirst(t); end; | |
| 2025 | 2081 | c := t^.fields; |
| 2026 | 2082 | while (c # nil) and (c^.name # Scan.id) do c := c^.next; end; |
| 2027 | 2083 | if c = nil then |
| @@ -3810,9 +3866,11 @@ | ||
| 3810 | 3866 | proc compareIface(a, b: PModule); |
| 3811 | 3867 | var o, q: PObj; |
| 3812 | 3868 | 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 *) | |
| 3813 | 3871 | o := a^.first; |
| 3814 | 3872 | while o # nil do |
| 3815 | - if o^.exported then | |
| 3873 | + if o^.exported and not o^.ofEnum then | |
| 3816 | 3874 | q := Sym.find(b^.scope, o^.name); |
| 3817 | 3875 | if (q = nil) or not q^.exported or (Sym.fingerprint(o) # Sym.fingerprint(q)) then |
| 3818 | 3876 | 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 @@ | ||
| 3823 | 3881 | q := b^.first; |
| 3824 | 3882 | while q # nil do |
| 3825 | 3883 | 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 | |
| 3827 | 3885 | Diag.fatal(Diag.say("{}: {} isn't exported by {}", [Fmt.str(b^.path), Fmt.str(nameOf(q^.name)), Fmt.str(a^.path)])); |
| 3828 | 3886 | end; |
| 3829 | 3887 | q := q^.chain; |
| @@ -77,6 +77,12 @@ | ||
| 77 | 77 | home*: ^Module; (* oModule: the module; others: where declared *) |
| 78 | 78 | chain*: ^Obj; (* the next declaration of its module, top-level or |
| 79 | 79 | 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 *) | |
| 80 | 86 | end; |
| 81 | 87 | |
| 82 | 88 | (* The arguments of a generic module's instance: a type, or a |
| @@ -267,6 +273,12 @@ | ||
| 267 | 273 | Mem.free(Heap.heap, ^U8(old)); |
| 268 | 274 | end; |
| 269 | 275 | |
| 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. *) | |
| 270 | 282 | proc insert*(obj: ^Obj; s: INT): ERROR; |
| 271 | 283 | var i: INT; o: ^Obj; |
| 272 | 284 | begin |
| @@ -274,6 +286,14 @@ | ||
| 274 | 286 | o := hashTab[i]; |
| 275 | 287 | while o # nil do |
| 276 | 288 | 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; | |
| 277 | 297 | Diag.error(obj^.pos, Diag.say("{} is declared twice", [Fmt.str(Scan.nameOf(obj^.name))])); |
| 278 | 298 | Diag.note(o^.pos, "the first one is here"); |
| 279 | 299 | return Scan.Failed; |
| @@ -15,8 +15,8 @@ | ||
| 15 | 15 | # and, through Math, test/bench/NBody.raz use; and returning a slice of |
| 16 | 16 | # the frame as an error (report 2026-10.27); an array constant typed |
| 17 | 17 | # []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). | |
| 20 | 20 | newer="conform/run/RawStrings.raz conform/err/ScanRawOpen.raz conform/err/ScanRawIndent.raz |
| 21 | 21 | conform/run/MatchStr.raz conform/err/MatchStrTwice.raz conform/err/MatchStrNoElse.raz |
| 22 | 22 | conform/err/MatchStrVar.raz conform/err/MatchReal.raz conform/err/AtMatch.raz |
| @@ -30,7 +30,9 @@ | ||
| 30 | 30 | conform/run/SmallWiden.raz conform/err/SmallWidenMade.raz conform/err/SmallWidenNeg.raz |
| 31 | 31 | conform/err/SmallWidenI64.raz conform/err/SmallWidenMadeOp.raz |
| 32 | 32 | 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" | |
| 34 | 36 | newer() { |
| 35 | 37 | case $1 in conform/*/View*.raz) return 0 ;; esac |
| 36 | 38 | for g in $newer; do [ $g = $1 ] && return 0; done |
| @@ -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 |
| @@ -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 |
| @@ -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 |
| @@ -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 |
| @@ -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 |