From 16ccda37eb0d24af01b2537bc9551cb8e5007e04 Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Fri, 23 Oct 2026 13:11:42 +0100 Subject: [PATCH] A match arm names its case alone or qualified as anywhere else, Kind.done, Xml.done or Xml.Kind.done, read by qualIdent and checked against the matched type, as report 2026-10.36 has it --- diff --git a/CLAUDE.md b/CLAUDE.md index bb78aaa..fb37d8f 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -294,8 +294,11 @@ widen; a conversion's does, `U8(a + b)` too. - An enumeration's name shared in its scope with another enumeration's is written with its type; shared with anything else, the name alone is - that other declaration. A `match` arm on an enumeration takes the - type's names alone or with the type, which must be the matched one. + that other declaration. A `match` arm on an enumeration or a variant + (report 2026-10.36, 7.4) takes a case alone, found among the matched + type's own whatever the scope holds, or qualified as anywhere else + (`Kind.done`, `Xml.done`, `Xml.Kind.done`), read by `qualIdent`; a + qualified case of another type is an error naming both. - `Width` follows glibc's `wcwidth` but gives a control 0, and 0 to the format characters glibc counts as 1 (Egyptian hieroglyph controls, interlinear annotation), but for the prepended concatenation marks. diff --git a/conform/err/ArmNotCase.raz b/conform/err/ArmNotCase.raz new file mode 100644 index 0000000..f7f53c3 --- /dev/null +++ b/conform/err/ArmNotCase.raz @@ -0,0 +1,13 @@ +module ArmNotCase; +import EnumKinds; +type Kind = (start, done); +proc main*; +var k: Kind; +begin + match k of + | Kind.start: + | EnumKinds.Style: + | else: + end; +end; +end ArmNotCase. diff --git a/conform/err/ArmOtherCase.raz b/conform/err/ArmOtherCase.raz new file mode 100644 index 0000000..425cd57 --- /dev/null +++ b/conform/err/ArmOtherCase.raz @@ -0,0 +1,12 @@ +module ArmOtherCase; +import EnumKinds; +type Style = (plain, bold); +proc main*; +var s: Style; +begin + match s of + | EnumKinds.Style.bold: + | else: + end; +end; +end ArmOtherCase. diff --git a/conform/err/ArmTwice.raz b/conform/err/ArmTwice.raz new file mode 100644 index 0000000..73ba9cd --- /dev/null +++ b/conform/err/ArmTwice.raz @@ -0,0 +1,12 @@ +module ArmTwice; +type Kind = (start, done); +proc main*; +var k: Kind; +begin + match k of + | done: + | Kind.done: + | else: + end; +end; +end ArmTwice. diff --git a/conform/run/MatchQualified.expect b/conform/run/MatchQualified.expect new file mode 100644 index 0000000..2ae8f48 --- /dev/null +++ b/conform/run/MatchQualified.expect @@ -0,0 +1 @@ +start done para bold rule dot 3 diff --git a/conform/run/MatchQualified.raz b/conform/run/MatchQualified.raz new file mode 100644 index 0000000..da8554d --- /dev/null +++ b/conform/run/MatchQualified.raz @@ -0,0 +1,41 @@ +(* An arm names its case as it is written anywhere else: alone, with + its type, with its module, or with both (report section 7.4). *) +module MatchQualified; +import Out, EnumKinds, Shared; + +type Kind = (start, done); + +proc kind(k: Kind); +begin + match k of + | Kind.start: Out.str("start "); + | done: Out.str("done "); + end; +end; + +proc elem(e: EnumKinds.Elem); +begin + match e of + | EnumKinds.Elem.para: Out.str("para "); + | EnumKinds.rule: Out.str("rule "); + | bold: Out.str("bold "); + end; +end; + +proc shape(s: Shared.Shape); +begin + match s of + | Shared.dot: Out.str("dot "); + | Shared.circle(r): Out.int(r); Out.str(" "); + end; +end; + +proc main*; +begin + kind(start); kind(done); + elem(EnumKinds.Elem.para); elem(EnumKinds.Elem.bold); elem(EnumKinds.rule); + shape(Shared.dot); shape(Shared.circle(3)); + Out.ln; +end; + +end MatchQualified. diff --git a/doc/compiler.md b/doc/compiler.md index adda2e7..bbcf54d 100644 --- a/doc/compiler.md +++ b/doc/compiler.md @@ -23,7 +23,7 @@ | `src/X64.raz` | 1,594 | 1,310 | The instruction encoder, jumps within a procedure, the assembler | | `src/Gen.raz` | 2,688 | 1,902 | The stack machine, for x86-64: values, operations, checks, calls, frames | | `src/Item.raz` | 2,378 | 1,909 | Items: types, constants, variables, operators, conversions, calls | -| `src/Parse.raz` | 4,459 | 3,851 | Declarations, statements, expressions, built-ins, recovery, warnings | +| `src/Parse.raz` | 4,476 | 3,864 | Declarations, statements, expressions, built-ins, recovery, warnings | | `src/Link.raz` | 407 | 315 | Tree shaking, layout, patching, the places | | `src/Elf.raz` | 449 | 355 | Static and dynamic ELF, symbol table, line numbers | | `src/Raz.raz` | 418 | 333 | The command line | @@ -1611,8 +1611,12 @@ `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, +on an enumeration or a variant names its case (`armCase`): a name +alone is looked for among the type's own cases (`caseNamed`), whatever +the scope holds, so `| done:` serves a type of another module; a name +followed by a dot, `Kind.done`, `Xml.done` or `Xml.Kind.done`, is read +by `qualIdent`, as it is in an expression, and must then be a case of +the matched type, or the message names both types. `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. diff --git a/src/Parse.raz b/src/Parse.raz index 4f94676..b76e26b 100644 --- a/src/Parse.raz +++ b/src/Parse.raz @@ -2237,31 +2237,53 @@ 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; +(* The case of t, an enumeration or a variant, named n, or nil *) +proc caseNamed(t: PType; n: INT): PObj; +var c: PObj; +begin + c := t^.fields; + while (c # nil) and (c^.name # n) do c := c^.next; end; + return c; +end; + +(* The case of t, an enumeration or a variant, that an arm of a match + names (report section 7.4). A name alone is looked for among t's own, + whatever the scope holds. A qualified one, Kind.done, Xml.done or + Xml.Kind.done, is read by qualIdent, as it is anywhere else, and must + be one of t's. *) +proc armCase(t: PType): (PObj, ERROR); +var st: Scan.State; c: PObj; at: Diag.Pos; qualified: BOOL; msg: []U8; begin - o := Sym.lookup(Scan.id); - if (o = nil) or (o^.cls # Sym.oType) then return nil; end; + at := Scan.tokPos(); 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))); + qualified := Scan.tok = Scan.sPeriod; + Scan.restoreScan(st); + if not qualified then + c := caseNamed(t, Scan.id); + if c = nil then + return nil, Scan.mark(say2("{} isn't a case of {}", nameOf(Scan.id), Sym.typeName(t))); + end; + Scan.next; + return c, nil; end; - named(o); - Scan.next; - if Scan.tok # Scan.sIdent then return expected("a name"); end; - return nil; + c := try qualIdent(); + if (c^.cls # Sym.oCase) and not c^.ofEnum then + return nil, Scan.markAt(Scan.spanFrom(at), say1("expected a case of {}", Sym.typeName(t))); + end; + if not Sym.same(c^.typ, t) then + msg := say2("the match is on {}, not {}", Sym.typeName(t), Sym.typeName(c^.typ)); + if caseNamed(t, c^.name) = nil then return nil, Scan.markAt(Scan.spanFrom(at), msg); end; + return nil, Scan.markHelp(Scan.spanFrom(at), msg, + say2("{} has a {} of its own: write it alone", Sym.typeName(t), nameOf(c^.name))); + end; + return c, 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; - seen: [256]BOOL; hasElse, all, byValue, isStr, onlyRead: BOOL; endAt: Diag.Pos; ls: Labels; + seen: [256]BOOL; hasElse, all, byValue, isStr, onlyRead: BOOL; endAt, at: Diag.Pos; ls: Labels; begin Scan.next; try expression(x, nil); @@ -2305,17 +2327,12 @@ 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 - return Scan.mark(say2("{} isn't a case of {}", nameOf(Scan.id), Sym.typeName(t))); - end; + at := Scan.tokPos(); + c := try armCase(t); if (c^.val <= 255) and seen[c^.val] then - return Scan.mark(say1("{} has two arms", nameOf(c^.name))); + return Scan.markAt(Scan.spanFrom(at), say1("{} has two arms", nameOf(c^.name))); end; if c^.val <= 255 then seen[c^.val] := true; end; - Scan.next; if t^.form # Sym.fEnum then Item.matchTag(p, c^.val, L); break; diff --git a/test/msg/ArmNotCase.err b/test/msg/ArmNotCase.err new file mode 100644 index 0000000..a874ccb --- /dev/null +++ b/test/msg/ArmNotCase.err @@ -0,0 +1,4 @@ +conform/err/ArmNotCase.raz:9:7: error: expected a case of Kind + 9 | | EnumKinds.Style: + | ^^^^^^^^^^^^^^^ +raz: 1 error diff --git a/test/msg/ArmOtherCase.err b/test/msg/ArmOtherCase.err new file mode 100644 index 0000000..b337d02 --- /dev/null +++ b/test/msg/ArmOtherCase.err @@ -0,0 +1,5 @@ +conform/err/ArmOtherCase.raz:8:7: error: the match is on Style, not EnumKinds.Style + 8 | | EnumKinds.Style.bold: + | ^^^^^^^^^^^^^^^^^^^^ +help: Style has a bold of its own: write it alone +raz: 1 error diff --git a/test/msg/ArmTwice.err b/test/msg/ArmTwice.err new file mode 100644 index 0000000..4d77f5b --- /dev/null +++ b/test/msg/ArmTwice.err @@ -0,0 +1,4 @@ +conform/err/ArmTwice.raz:8:7: error: done has two arms + 8 | | Kind.done: + | ^^^^^^^^^ +raz: 1 error diff --git a/test/msg/EnumArmType.err b/test/msg/EnumArmType.err index b212af7..e7a1633 100644 --- a/test/msg/EnumArmType.err +++ b/test/msg/EnumArmType.err @@ -1,4 +1,5 @@ conform/err/EnumArmType.raz:9:7: error: the match is on Elem, not Style 9 | | Style.bold: - | ^^^^^ + | ^^^^^^^^^^ +help: Elem has a bold of its own: write it alone raz: 1 error