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
| @@ -294,8 +294,11 @@ | ||
| 294 | 294 | widen; a conversion's does, `U8(a + b)` too. |
| 295 | 295 | - An enumeration's name shared in its scope with another enumeration's |
| 296 | 296 | is written with its type; shared with anything else, the name alone is |
| 297 | - that other declaration. A `match` arm on an enumeration takes the | |
| 298 | - type's names alone or with the type, which must be the matched one. | |
| 297 | + that other declaration. A `match` arm on an enumeration or a variant | |
| 298 | + (report 2026-10.36, 7.4) takes a case alone, found among the matched | |
| 299 | + type's own whatever the scope holds, or qualified as anywhere else | |
| 300 | + (`Kind.done`, `Xml.done`, `Xml.Kind.done`), read by `qualIdent`; a | |
| 301 | + qualified case of another type is an error naming both. | |
| 299 | 302 | - `Width` follows glibc's `wcwidth` but gives a control 0, and 0 to the |
| 300 | 303 | format characters glibc counts as 1 (Egyptian hieroglyph controls, |
| 301 | 304 | interlinear annotation), but for the prepended concatenation marks. |
| @@ -0,0 +1,13 @@ | ||
| 1 | +module ArmNotCase; | |
| 2 | +import EnumKinds; | |
| 3 | +type Kind = (start, done); | |
| 4 | +proc main*; | |
| 5 | +var k: Kind; | |
| 6 | +begin | |
| 7 | + match k of | |
| 8 | + | Kind.start: | |
| 9 | + | EnumKinds.Style: | |
| 10 | + | else: | |
| 11 | + end; | |
| 12 | +end; | |
| 13 | +end ArmNotCase. |
| @@ -0,0 +1,12 @@ | ||
| 1 | +module ArmOtherCase; | |
| 2 | +import EnumKinds; | |
| 3 | +type Style = (plain, bold); | |
| 4 | +proc main*; | |
| 5 | +var s: Style; | |
| 6 | +begin | |
| 7 | + match s of | |
| 8 | + | EnumKinds.Style.bold: | |
| 9 | + | else: | |
| 10 | + end; | |
| 11 | +end; | |
| 12 | +end ArmOtherCase. |
| @@ -0,0 +1,12 @@ | ||
| 1 | +module ArmTwice; | |
| 2 | +type Kind = (start, done); | |
| 3 | +proc main*; | |
| 4 | +var k: Kind; | |
| 5 | +begin | |
| 6 | + match k of | |
| 7 | + | done: | |
| 8 | + | Kind.done: | |
| 9 | + | else: | |
| 10 | + end; | |
| 11 | +end; | |
| 12 | +end ArmTwice. |
| @@ -0,0 +1 @@ | ||
| 1 | +start done para bold rule dot 3 |
| @@ -0,0 +1,41 @@ | ||
| 1 | +(* An arm names its case as it is written anywhere else: alone, with | |
| 2 | + its type, with its module, or with both (report section 7.4). *) | |
| 3 | +module MatchQualified; | |
| 4 | +import Out, EnumKinds, Shared; | |
| 5 | + | |
| 6 | +type Kind = (start, done); | |
| 7 | + | |
| 8 | +proc kind(k: Kind); | |
| 9 | +begin | |
| 10 | + match k of | |
| 11 | + | Kind.start: Out.str("start "); | |
| 12 | + | done: Out.str("done "); | |
| 13 | + end; | |
| 14 | +end; | |
| 15 | + | |
| 16 | +proc elem(e: EnumKinds.Elem); | |
| 17 | +begin | |
| 18 | + match e of | |
| 19 | + | EnumKinds.Elem.para: Out.str("para "); | |
| 20 | + | EnumKinds.rule: Out.str("rule "); | |
| 21 | + | bold: Out.str("bold "); | |
| 22 | + end; | |
| 23 | +end; | |
| 24 | + | |
| 25 | +proc shape(s: Shared.Shape); | |
| 26 | +begin | |
| 27 | + match s of | |
| 28 | + | Shared.dot: Out.str("dot "); | |
| 29 | + | Shared.circle(r): Out.int(r); Out.str(" "); | |
| 30 | + end; | |
| 31 | +end; | |
| 32 | + | |
| 33 | +proc main*; | |
| 34 | +begin | |
| 35 | + kind(start); kind(done); | |
| 36 | + elem(EnumKinds.Elem.para); elem(EnumKinds.Elem.bold); elem(EnumKinds.rule); | |
| 37 | + shape(Shared.dot); shape(Shared.circle(3)); | |
| 38 | + Out.ln; | |
| 39 | +end; | |
| 40 | + | |
| 41 | +end MatchQualified. |
| @@ -23,7 +23,7 @@ | ||
| 23 | 23 | | `src/X64.raz` | 1,594 | 1,310 | The instruction encoder, jumps within a procedure, the assembler | |
| 24 | 24 | | `src/Gen.raz` | 2,688 | 1,902 | The stack machine, for x86-64: values, operations, checks, calls, frames | |
| 25 | 25 | | `src/Item.raz` | 2,378 | 1,909 | Items: types, constants, variables, operators, conversions, calls | |
| 26 | -| `src/Parse.raz` | 4,459 | 3,851 | Declarations, statements, expressions, built-ins, recovery, warnings | | |
| 26 | +| `src/Parse.raz` | 4,476 | 3,864 | Declarations, statements, expressions, built-ins, recovery, warnings | | |
| 27 | 27 | | `src/Link.raz` | 407 | 315 | Tree shaking, layout, patching, the places | |
| 28 | 28 | | `src/Elf.raz` | 449 | 355 | Static and dynamic ELF, symbol table, line numbers | |
| 29 | 29 | | `src/Raz.raz` | 418 | 333 | The command line | |
| @@ -1611,8 +1611,12 @@ | ||
| 1611 | 1611 | `enumType`) build `Sym.Type`s. `qualIdent` reads a name, `M.x` too, and |
| 1612 | 1612 | `Elem.bold`, a name of an enumeration with its type (`enumName`); it |
| 1613 | 1613 | refuses a name two enumerations share (`twinError`). An arm of a `match` |
| 1614 | -on an enumeration takes its names from the type, written alone or with | |
| 1615 | -it (`typeFirst`). `compareIface` passes over an enumeration's names, | |
| 1614 | +on an enumeration or a variant names its case (`armCase`): a name | |
| 1615 | +alone is looked for among the type's own cases (`caseNamed`), whatever | |
| 1616 | +the scope holds, so `| done:` serves a type of another module; a name | |
| 1617 | +followed by a dot, `Kind.done`, `Xml.done` or `Xml.Kind.done`, is read | |
| 1618 | +by `qualIdent`, as it is in an expression, and must then be a case of | |
| 1619 | +the matched type, or the message names both types. `compareIface` passes over an enumeration's names, | |
| 1616 | 1620 | which its type's fingerprint has. |
| 1617 | 1621 | |
| 1618 | 1622 | Last, modules, at the end of the file. `compileFile` reads one module. |
| @@ -2237,31 +2237,53 @@ | ||
| 2237 | 2237 | if elseAt # Gen.NoLabel then Gen.jumpBack(elseAt); end; |
| 2238 | 2238 | end; |
| 2239 | 2239 | |
| 2240 | -(* In an arm of a match on the enumeration t, a name may be written | |
| 2241 | - with its type, | Elem.rule: (report section 5.7). If it is, skips the | |
| 2242 | - type and the dot, which must name t itself. *) | |
| 2243 | -proc typeFirst(t: PType): ERROR; | |
| 2244 | -var st: Scan.State; o: PObj; | |
| 2240 | +(* The case of t, an enumeration or a variant, named n, or nil *) | |
| 2241 | +proc caseNamed(t: PType; n: INT): PObj; | |
| 2242 | +var c: PObj; | |
| 2243 | +begin | |
| 2244 | + c := t^.fields; | |
| 2245 | + while (c # nil) and (c^.name # n) do c := c^.next; end; | |
| 2246 | + return c; | |
| 2247 | +end; | |
| 2248 | + | |
| 2249 | +(* The case of t, an enumeration or a variant, that an arm of a match | |
| 2250 | + names (report section 7.4). A name alone is looked for among t's own, | |
| 2251 | + whatever the scope holds. A qualified one, Kind.done, Xml.done or | |
| 2252 | + Xml.Kind.done, is read by qualIdent, as it is anywhere else, and must | |
| 2253 | + be one of t's. *) | |
| 2254 | +proc armCase(t: PType): (PObj, ERROR); | |
| 2255 | +var st: Scan.State; c: PObj; at: Diag.Pos; qualified: BOOL; msg: []U8; | |
| 2245 | 2256 | begin |
| 2246 | - o := Sym.lookup(Scan.id); | |
| 2247 | - if (o = nil) or (o^.cls # Sym.oType) then return nil; end; | |
| 2257 | + at := Scan.tokPos(); | |
| 2248 | 2258 | Scan.saveScan(st); |
| 2249 | 2259 | Scan.next; |
| 2250 | - if Scan.tok # Scan.sPeriod then Scan.restoreScan(st); return nil; end; | |
| 2251 | - if not Sym.same(o^.typ, t) then | |
| 2252 | - Scan.restoreScan(st); | |
| 2253 | - return Scan.mark(say2("the match is on {}, not {}", Sym.typeName(t), Sym.typeName(o^.typ))); | |
| 2260 | + qualified := Scan.tok = Scan.sPeriod; | |
| 2261 | + Scan.restoreScan(st); | |
| 2262 | + if not qualified then | |
| 2263 | + c := caseNamed(t, Scan.id); | |
| 2264 | + if c = nil then | |
| 2265 | + return nil, Scan.mark(say2("{} isn't a case of {}", nameOf(Scan.id), Sym.typeName(t))); | |
| 2266 | + end; | |
| 2267 | + Scan.next; | |
| 2268 | + return c, nil; | |
| 2254 | 2269 | end; |
| 2255 | - named(o); | |
| 2256 | - Scan.next; | |
| 2257 | - if Scan.tok # Scan.sIdent then return expected("a name"); end; | |
| 2258 | - return nil; | |
| 2270 | + c := try qualIdent(); | |
| 2271 | + if (c^.cls # Sym.oCase) and not c^.ofEnum then | |
| 2272 | + return nil, Scan.markAt(Scan.spanFrom(at), say1("expected a case of {}", Sym.typeName(t))); | |
| 2273 | + end; | |
| 2274 | + if not Sym.same(c^.typ, t) then | |
| 2275 | + msg := say2("the match is on {}, not {}", Sym.typeName(t), Sym.typeName(c^.typ)); | |
| 2276 | + if caseNamed(t, c^.name) = nil then return nil, Scan.markAt(Scan.spanFrom(at), msg); end; | |
| 2277 | + return nil, Scan.markHelp(Scan.spanFrom(at), msg, | |
| 2278 | + say2("{} has a {} of its own: write it alone", Sym.typeName(t), nameOf(c^.name))); | |
| 2279 | + end; | |
| 2280 | + return c, nil; | |
| 2259 | 2281 | end; |
| 2260 | 2282 | |
| 2261 | 2283 | proc matchStatement(start: Diag.Pos; ind: INT): ERROR; |
| 2262 | 2284 | var |
| 2263 | 2285 | x: TItem; t: PType; c, f, o: PObj; p: I64; L, endL, n, first, disp, elseAt: INT; |
| 2264 | - seen: [256]BOOL; hasElse, all, byValue, isStr, onlyRead: BOOL; endAt: Diag.Pos; ls: Labels; | |
| 2286 | + seen: [256]BOOL; hasElse, all, byValue, isStr, onlyRead: BOOL; endAt, at: Diag.Pos; ls: Labels; | |
| 2265 | 2287 | begin |
| 2266 | 2288 | Scan.next; |
| 2267 | 2289 | try expression(x, nil); |
| @@ -2305,17 +2327,12 @@ | ||
| 2305 | 2327 | L := Gen.NoLabel; |
| 2306 | 2328 | repeat |
| 2307 | 2329 | if Scan.tok # Scan.sIdent then return expected("a case"); end; |
| 2308 | - if t^.form = Sym.fEnum then try typeFirst(t); end; | |
| 2309 | - c := t^.fields; | |
| 2310 | - while (c # nil) and (c^.name # Scan.id) do c := c^.next; end; | |
| 2311 | - if c = nil then | |
| 2312 | - return Scan.mark(say2("{} isn't a case of {}", nameOf(Scan.id), Sym.typeName(t))); | |
| 2313 | - end; | |
| 2330 | + at := Scan.tokPos(); | |
| 2331 | + c := try armCase(t); | |
| 2314 | 2332 | if (c^.val <= 255) and seen[c^.val] then |
| 2315 | - return Scan.mark(say1("{} has two arms", nameOf(c^.name))); | |
| 2333 | + return Scan.markAt(Scan.spanFrom(at), say1("{} has two arms", nameOf(c^.name))); | |
| 2316 | 2334 | end; |
| 2317 | 2335 | if c^.val <= 255 then seen[c^.val] := true; end; |
| 2318 | - Scan.next; | |
| 2319 | 2336 | if t^.form # Sym.fEnum then |
| 2320 | 2337 | Item.matchTag(p, c^.val, L); |
| 2321 | 2338 | break; |
| @@ -0,0 +1,4 @@ | ||
| 1 | +conform/err/ArmNotCase.raz:9:7: error: expected a case of Kind | |
| 2 | + 9 | | EnumKinds.Style: | |
| 3 | + | ^^^^^^^^^^^^^^^ | |
| 4 | +raz: 1 error |
| @@ -0,0 +1,5 @@ | ||
| 1 | +conform/err/ArmOtherCase.raz:8:7: error: the match is on Style, not EnumKinds.Style | |
| 2 | + 8 | | EnumKinds.Style.bold: | |
| 3 | + | ^^^^^^^^^^^^^^^^^^^^ | |
| 4 | +help: Style has a bold of its own: write it alone | |
| 5 | +raz: 1 error |
| @@ -0,0 +1,4 @@ | ||
| 1 | +conform/err/ArmTwice.raz:8:7: error: done has two arms | |
| 2 | + 8 | | Kind.done: | |
| 3 | + | ^^^^^^^^^ | |
| 4 | +raz: 1 error |
| @@ -1,4 +1,5 @@ | ||
| 1 | 1 | conform/err/EnumArmType.raz:9:7: error: the match is on Elem, not Style |
| 2 | 2 | 9 | | Style.bold: |
| 3 | - | ^^^^^ | |
| 3 | + | ^^^^^^^^^^ | |
| 4 | +help: Elem has a bold of its own: write it alone | |
| 4 | 5 | raz: 1 error |