From 3e1c4e5b91ecd37c4eaf5d3269e7fa999c6b65ea Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Fri, 16 Oct 2026 11:21:31 +0100 Subject: [PATCH] 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. --- diff --git a/conform/err/EnumArmType.raz b/conform/err/EnumArmType.raz new file mode 100644 index 0000000..0b512a7 --- /dev/null +++ b/conform/err/EnumArmType.raz @@ -0,0 +1,13 @@ +module EnumArmType; +type + Style = (bold, italic); + Elem = (para, bold); +proc main*; +var e: Elem; +begin + match e of + | Style.bold: + | else: + end; +end; +end EnumArmType. diff --git a/conform/err/EnumNoName.raz b/conform/err/EnumNoName.raz new file mode 100644 index 0000000..44846f3 --- /dev/null +++ b/conform/err/EnumNoName.raz @@ -0,0 +1,8 @@ +module EnumNoName; +type Style = (bold, italic); +proc main*; +var s: Style; +begin + s := Style.under; +end; +end EnumNoName. diff --git a/conform/err/EnumShared.raz b/conform/err/EnumShared.raz new file mode 100644 index 0000000..9f5610f --- /dev/null +++ b/conform/err/EnumShared.raz @@ -0,0 +1,10 @@ +module EnumShared; +type + Style = (bold, italic); + Elem = (para, bold); +proc main*; +var s: Style; +begin + s := bold; +end; +end EnumShared. diff --git a/conform/err/EnumSharedImported.raz b/conform/err/EnumSharedImported.raz new file mode 100644 index 0000000..6151f31 --- /dev/null +++ b/conform/err/EnumSharedImported.raz @@ -0,0 +1,8 @@ +module EnumSharedImported; +import EnumKinds; +proc main*; +var k: EnumKinds.Elem; +begin + k := EnumKinds.bold; +end; +end EnumSharedImported. diff --git a/conform/err/EnumTwiceOther.raz b/conform/err/EnumTwiceOther.raz new file mode 100644 index 0000000..b06210c --- /dev/null +++ b/conform/err/EnumTwiceOther.raz @@ -0,0 +1,8 @@ +module EnumTwiceOther; +type Style = (bold, italic); +var bold: INT; +const bold = 2; +proc main*; +begin +end; +end EnumTwiceOther. diff --git a/conform/err/mods/EnumKinds.raz b/conform/err/mods/EnumKinds.raz new file mode 100644 index 0000000..b6ca3a8 --- /dev/null +++ b/conform/err/mods/EnumKinds.raz @@ -0,0 +1,8 @@ +(* Two enumerations that share a name, exported, for EnumNames. *) +module EnumKinds; + +type + Style* = (plain, bold, italic); + Elem* = (para, bold, rule); + +end EnumKinds. diff --git a/conform/run/EnumNames.expect b/conform/run/EnumNames.expect new file mode 100644 index 0000000..aa2e940 --- /dev/null +++ b/conform/run/EnumNames.expect @@ -0,0 +1,6 @@ +Style.bold and Elem.bold +the procedure rule +an arm with its type +in the set +ordered +imported, with module and type diff --git a/conform/run/EnumNames.raz b/conform/run/EnumNames.raz new file mode 100644 index 0000000..f5542ca --- /dev/null +++ b/conform/run/EnumNames.raz @@ -0,0 +1,44 @@ +(* A name of an enumeration may be written with its type; two + enumerations of a scope may share a name, and an enumeration may share + one with another declaration: written alone, the name is that other + one (report 2026-10.31, section 5.7). *) +module EnumNames; +import Out, EnumKinds; + +type + Style = (bold, italic); + Elem = (para, bold, rule); + Light = (red, amber, green); + +const Stop = Light.red; + +var s: Style; e: Elem; ss: set of Style; k: EnumKinds.Elem; + +proc rule; (* beside Elem's rule *) +begin + Out.str("the procedure rule\n"); +end; + +proc main*; +begin + s := Style.bold; e := Elem.bold; + if (s = Style.bold) and (e = Elem.bold) and (INT(e) = 1) then Out.str("Style.bold and Elem.bold\n"); end; + e := para; (* Elem's alone *) + e := Elem.rule; + rule; (* the procedure *) + s := italic; + match e of + | Elem.rule: Out.str("an arm with its type\n"); + | para, bold: Out.str("an arm without\n"); + end; + ss := {Style.bold, italic}; + if Style.bold in ss then Out.str("in the set\n"); end; + if (Stop = red) and (Light.green > amber) then Out.str("ordered\n"); end; + k := EnumKinds.Elem.bold; + if (k = EnumKinds.Elem.bold) and (EnumKinds.Style.bold # EnumKinds.Style.plain) then + Out.str("imported, with module and type\n"); + end; + if EnumKinds.para = k then Out.str("no\n"); end; +end; + +end EnumNames. diff --git a/conform/run/mods/EnumKinds.raz b/conform/run/mods/EnumKinds.raz new file mode 100644 index 0000000..b6ca3a8 --- /dev/null +++ b/conform/run/mods/EnumKinds.raz @@ -0,0 +1,8 @@ +(* Two enumerations that share a name, exported, for EnumNames. *) +module EnumKinds; + +type + Style* = (plain, bold, italic); + Elem* = (para, bold, rule); + +end EnumKinds. diff --git a/doc/compiler.md b/doc/compiler.md index 38ba52a..7a520ac 100644 --- a/doc/compiler.md +++ b/doc/compiler.md @@ -15,12 +15,12 @@ |---|---|---|---| | `src/Diag.raz` | 410 | 315 | Messages: positions, source excerpts, colour, counts | | `src/Scan.raz` | 1,127 | 934 | Symbols, literals, comments, positions | -| `src/Sym.raz` | 687 | 556 | Types, objects, scopes, layout | +| `src/Sym.raz` | 707 | 566 | Types, objects, scopes, layout | | `src/Chunk.raz` | 363 | 279 | The compiled program: chunks, fixups, line notes | | `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,037 | 1,651 | Items: types, constants, variables, operators, conversions, calls | -| `src/Parse.raz` | 4,065 | 3,532 | Declarations, statements, expressions, built-ins, recovery, warnings | +| `src/Parse.raz` | 4,123 | 3,578 | Declarations, statements, expressions, built-ins, recovery, warnings | | `src/Link.raz` | 381 | 298 | Tree shaking, layout, patching, the places | | `src/Elf.raz` | 431 | 344 | Static and dynamic ELF, symbol table, line numbers | | `src/Raz.raz` | 305 | 238 | The command line | @@ -979,7 +979,12 @@ procedures, is set up in the module's body. Two routines can find an error: `insert`, for a name declared twice, and -`openScope`, for too many scopes. Each shows it and returns +`openScope`, for too many scopes. A name of an enumeration (`ofEnum`) +may share its scope with another declaration (report 2026-10.31): it is +reached through its type, `Elem.bold`, so `insert` keeps the other +declaration in the scope for the name alone; of two enumerations' +names it keeps the first, and notes the second as its `twin`, which +makes the name alone an error. Each shows it and returns `Scan.Failed`, for the parser to pass up with `try`. A record's module and an object's link, which the seed called `module` and `link`, are `home` and `chain`, since those are reserved words here; `setCurModule` @@ -1452,7 +1457,12 @@ rdx, and has gen's `codeProc` run the target's assembler) and bodies (`body`). `parseType` and its helpers (`recordType`, `variantType`, `signature`, -`enumType`) build `Sym.Type`s. +`enumType`) build `Sym.Type`s. `qualIdent` reads a name, `M.x` too, and +`Elem.bold`, a name of an enumeration with its type (`enumName`); it +refuses a name two enumerations share (`twinError`). An arm of a `match` +on an enumeration takes its names from the type, written alone or with +it (`typeFirst`). `compareIface` passes over an enumeration's names, +which its type's fingerprint has. Last, modules, at the end of the file. `compileFile` reads one module. `importModule` finds a module's file, trying the target's names first, diff --git a/src/Parse.raz b/src/Parse.raz index 3dcdac4..0411d92 100644 --- a/src/Parse.raz +++ b/src/Parse.raz @@ -441,6 +441,33 @@ proc instancesFor(wholeRest: BOOL): ERROR; forward; proc addImport(m, im: PModule); forward; +(* The name of the enumeration t that the current symbol is, after + "t.": Elem.bold (report section 5.7). *) +proc enumName(t: PType): (PObj, ERROR); +var o: PObj; +begin + if Scan.tok # Scan.sIdent then return nil, expected("a name"); end; + o := t^.fields; + while (o # nil) and (o^.name # Scan.id) do o := o^.next; end; + if o = nil then + return nil, Scan.mark(say2("{} has no name {}", Sym.typeName(t), nameOf(Scan.id))); + end; + Scan.next; + return o, nil; +end; + +(* An error at at: o, a name of an enumeration, is a name of another of + its scope too, so it is written with its type. *) +proc twinError(o: PObj; at: Diag.Pos): ERROR; +var a, b: []U8; +begin + a := Sym.typeName(o^.typ); b := Sym.typeName(o^.twin^.typ); + return Scan.markHelp(at, Diag.say("{} is a name of {} and of {}", + [Fmt.str(nameOf(o^.name)), Fmt.str(a), Fmt.str(b)]), + Diag.say("write it with its type: {}.{} or {}.{}", + [Fmt.str(a), Fmt.str(nameOf(o^.name)), Fmt.str(b), Fmt.str(nameOf(o^.name))])); +end; + proc qualIdent: (PObj, ERROR); var o: PObj; m: PModule; at: Diag.Pos; begin @@ -472,6 +499,12 @@ end; Scan.next; end; + if (o^.cls = Sym.oType) and (o^.typ^.form = Sym.fEnum) and (Scan.tok = Scan.sPeriod) then + Scan.next; + o := try enumName(o^.typ); + elsif o^.twin # nil then + return nil, twinError(o, at); + end; return o, nil; end; @@ -690,6 +723,7 @@ if Scan.tok # Scan.sIdent then return nil, expected("a name"); end; o := Sym.newObj(Sym.oConst, Scan.id, t); o^.val := t^.len; + o^.ofEnum := true; o^.exported := declExported; o^.used := true; (* an enumeration's names are its type's, warned of with it *) try declare(o); @@ -1981,6 +2015,27 @@ if elseAt # Gen.NoLabel then Gen.jumpBack(elseAt); end; end; +(* In an arm of a match on the enumeration t, a name may be written + with its type, | Elem.rule: (report section 5.7). If it is, skips the + type and the dot, which must name t itself. *) +proc typeFirst(t: PType): ERROR; +var st: Scan.State; o: PObj; +begin + o := Sym.lookup(Scan.id); + if (o = nil) or (o^.cls # Sym.oType) then return nil; end; + Scan.saveScan(st); + Scan.next; + if Scan.tok # Scan.sPeriod then Scan.restoreScan(st); return nil; end; + if not Sym.same(o^.typ, t) then + Scan.restoreScan(st); + return Scan.mark(say2("the match is on {}, not {}", Sym.typeName(t), Sym.typeName(o^.typ))); + end; + named(o); + Scan.next; + if Scan.tok # Scan.sIdent then return expected("a name"); end; + return nil; +end; + proc matchStatement(start: Diag.Pos; ind: INT): ERROR; var x: TItem; t: PType; c, f, o: PObj; p: I64; L, endL, n, first, disp, elseAt: INT; @@ -2022,6 +2077,7 @@ L := Gen.NoLabel; repeat if Scan.tok # Scan.sIdent then return expected("a case"); end; + if t^.form = Sym.fEnum then try typeFirst(t); end; c := t^.fields; while (c # nil) and (c^.name # Scan.id) do c := c^.next; end; if c = nil then @@ -3810,9 +3866,11 @@ proc compareIface(a, b: PModule); var o, q: PObj; begin + (* an enumeration's names are its type's, which the type's + fingerprint has, and the name alone may be another's *) o := a^.first; while o # nil do - if o^.exported then + if o^.exported and not o^.ofEnum then q := Sym.find(b^.scope, o^.name); if (q = nil) or not q^.exported or (Sym.fingerprint(o) # Sym.fingerprint(q)) then 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 @@ q := b^.first; while q # nil do o := Sym.find(a^.scope, q^.name); - if q^.exported and ((o = nil) or not o^.exported) then + if q^.exported and not q^.ofEnum and ((o = nil) or not o^.exported) then Diag.fatal(Diag.say("{}: {} isn't exported by {}", [Fmt.str(b^.path), Fmt.str(nameOf(q^.name)), Fmt.str(a^.path)])); end; q := q^.chain; diff --git a/src/Sym.raz b/src/Sym.raz index de9db87..7b0bdf4 100644 --- a/src/Sym.raz +++ b/src/Sym.raz @@ -77,6 +77,12 @@ home*: ^Module; (* oModule: the module; others: where declared *) chain*: ^Obj; (* the next declaration of its module, top-level or local *) + ofEnum*: BOOL; (* oConst: a name of its enumeration, which may share + its scope with another declaration of the name + (report section 5.7) *) + twin*: ^Obj; (* an enumeration's name in its scope: another + enumeration's of the same name, which makes the + name alone an error; nil if none *) end; (* The arguments of a generic module's instance: a type, or a @@ -267,6 +273,12 @@ Mem.free(Heap.heap, ^U8(old)); end; +(* Declares obj in the scope s, or reports a name declared twice there. + A name of an enumeration may share its scope with another declaration + of the name (report section 5.7): it is always reached through its + type, Elem.bold, so the scope keeps the other declaration, which the + name alone then means; of two enumerations' names, it keeps the first, + with the second as its twin, and the name alone is an error. *) proc insert*(obj: ^Obj; s: INT): ERROR; var i: INT; o: ^Obj; begin @@ -274,6 +286,14 @@ o := hashTab[i]; while o # nil do if (o^.scope = s) and (o^.name = obj^.name) then + obj^.scope := s; + if obj^.ofEnum then + if o^.ofEnum and (o^.twin = nil) then o^.twin := obj; end; + return nil; + elsif o^.ofEnum then + hashTab[i] := obj; (* the name alone is obj *) + return nil; + end; Diag.error(obj^.pos, Diag.say("{} is declared twice", [Fmt.str(Scan.nameOf(obj^.name))])); Diag.note(o^.pos, "the first one is here"); return Scan.Failed; diff --git a/src/check.sh b/src/check.sh index d8bcc97..b587caf 100755 --- a/src/check.sh +++ b/src/check.sh @@ -15,8 +15,8 @@ # and, through Math, test/bench/NBody.raz use; and returning a slice of # the frame as an error (report 2026-10.27); an array constant typed # []T (report 2026-10.29); [] as the empty slice, seen through a view -# (report 2026-10.30); and U8, I8 and I16 widening to INT, and -# continue (report 2026-10.31). +# (report 2026-10.30); and U8, I8 and I16 widening to INT, continue, +# and enumerations' names with their types (report 2026-10.31). newer="conform/run/RawStrings.raz conform/err/ScanRawOpen.raz conform/err/ScanRawIndent.raz conform/run/MatchStr.raz conform/err/MatchStrTwice.raz conform/err/MatchStrNoElse.raz conform/err/MatchStrVar.raz conform/err/MatchReal.raz conform/err/AtMatch.raz @@ -30,7 +30,9 @@ conform/run/SmallWiden.raz conform/err/SmallWidenMade.raz conform/err/SmallWidenNeg.raz conform/err/SmallWidenI64.raz conform/err/SmallWidenMadeOp.raz conform/run/Continue.raz conform/err/ContinueOutside.raz conform/err/ContinueDefer.raz - conform/err/ContinueName.raz" + conform/err/ContinueName.raz + conform/run/EnumNames.raz conform/err/EnumShared.raz conform/err/EnumNoName.raz + conform/err/EnumArmType.raz conform/err/EnumSharedImported.raz conform/err/EnumTwiceOther.raz" newer() { case $1 in conform/*/View*.raz) return 0 ;; esac for g in $newer; do [ $g = $1 ] && return 0; done diff --git a/test/msg/EnumArmType.err b/test/msg/EnumArmType.err new file mode 100644 index 0000000..b212af7 --- /dev/null +++ b/test/msg/EnumArmType.err @@ -0,0 +1,4 @@ +conform/err/EnumArmType.raz:9:7: error: the match is on Elem, not Style + 9 | | Style.bold: + | ^^^^^ +raz: 1 error diff --git a/test/msg/EnumNoName.err b/test/msg/EnumNoName.err new file mode 100644 index 0000000..061c083 --- /dev/null +++ b/test/msg/EnumNoName.err @@ -0,0 +1,4 @@ +conform/err/EnumNoName.raz:6:16: error: Style has no name under + 6 | s := Style.under; + | ^^^^^ +raz: 1 error diff --git a/test/msg/EnumShared.err b/test/msg/EnumShared.err new file mode 100644 index 0000000..ba08e71 --- /dev/null +++ b/test/msg/EnumShared.err @@ -0,0 +1,5 @@ +conform/err/EnumShared.raz:8:10: error: bold is a name of Style and of Elem + 8 | s := bold; + | ^^^^ +help: write it with its type: Style.bold or Elem.bold +raz: 1 error diff --git a/test/msg/EnumSharedImported.err b/test/msg/EnumSharedImported.err new file mode 100644 index 0000000..8792b14 --- /dev/null +++ b/test/msg/EnumSharedImported.err @@ -0,0 +1,5 @@ +conform/err/EnumSharedImported.raz:6:10: error: bold is a name of EnumKinds.Style and of EnumKinds.Elem + 6 | k := EnumKinds.bold; + | ^^^^^^^^^ +help: write it with its type: EnumKinds.Style.bold or EnumKinds.Elem.bold +raz: 1 error diff --git a/test/msg/EnumTwiceOther.err b/test/msg/EnumTwiceOther.err new file mode 100644 index 0000000..8144906 --- /dev/null +++ b/test/msg/EnumTwiceOther.err @@ -0,0 +1,7 @@ +conform/err/EnumTwiceOther.raz:4:7: error: bold is declared twice + 4 | const bold = 2; + | ^^^^ +conform/err/EnumTwiceOther.raz:3:5: note: the first one is here + 3 | var bold: INT; + | ^^^^ +raz: 1 error