raz / commit 16ccda37

commit
16ccda37eb0d24af01b2537bc9551cb8e5007e04
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
7a01d98d
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

patch · log

12 files changed, +147 -30

CLAUDE.md modified +5 -2

@@ -294,8 +294,11 @@
294294 widen; a conversion's does, `U8(a + b)` too.
295295 - An enumeration's name shared in its scope with another enumeration's
296296 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.
299302 - `Width` follows glibc's `wcwidth` but gives a control 0, and 0 to the
300303 format characters glibc counts as 1 (Egyptian hieroglyph controls,
301304 interlinear annotation), but for the prepended concatenation marks.

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

@@ -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.

conform/err/ArmOtherCase.raz (history) added +12 -0

@@ -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.

conform/err/ArmTwice.raz (history) added +12 -0

@@ -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.

conform/run/MatchQualified.expect (history) added +1 -0

@@ -0,0 +1 @@
1+start done para bold rule dot 3

conform/run/MatchQualified.raz (history) added +41 -0

@@ -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.

doc/compiler.md (history) modified +7 -3

@@ -23,7 +23,7 @@
2323 | `src/X64.raz` | 1,594 | 1,310 | The instruction encoder, jumps within a procedure, the assembler |
2424 | `src/Gen.raz` | 2,688 | 1,902 | The stack machine, for x86-64: values, operations, checks, calls, frames |
2525 | `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 |
2727 | `src/Link.raz` | 407 | 315 | Tree shaking, layout, patching, the places |
2828 | `src/Elf.raz` | 449 | 355 | Static and dynamic ELF, symbol table, line numbers |
2929 | `src/Raz.raz` | 418 | 333 | The command line |
@@ -1611,8 +1611,12 @@
16111611 `enumType`) build `Sym.Type`s. `qualIdent` reads a name, `M.x` too, and
16121612 `Elem.bold`, a name of an enumeration with its type (`enumName`); it
16131613 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,
16161620 which its type's fingerprint has.
16171621
16181622 Last, modules, at the end of the file. `compileFile` reads one module.

src/Parse.raz (history) modified +41 -24

@@ -2237,31 +2237,53 @@
22372237 if elseAt # Gen.NoLabel then Gen.jumpBack(elseAt); end;
22382238 end;
22392239
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;
22452256 begin
2246- o := Sym.lookup(Scan.id);
2247- if (o = nil) or (o^.cls # Sym.oType) then return nil; end;
2257+ at := Scan.tokPos();
22482258 Scan.saveScan(st);
22492259 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;
22542269 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;
22592281 end;
22602282
22612283 proc matchStatement(start: Diag.Pos; ind: INT): ERROR;
22622284 var
22632285 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;
22652287 begin
22662288 Scan.next;
22672289 try expression(x, nil);
@@ -2305,17 +2327,12 @@
23052327 L := Gen.NoLabel;
23062328 repeat
23072329 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);
23142332 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)));
23162334 end;
23172335 if c^.val <= 255 then seen[c^.val] := true; end;
2318- Scan.next;
23192336 if t^.form # Sym.fEnum then
23202337 Item.matchTag(p, c^.val, L);
23212338 break;

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

@@ -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

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

@@ -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

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

@@ -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

test/msg/EnumArmType.err (history) modified +2 -1

@@ -1,4 +1,5 @@
11 conform/err/EnumArmType.raz:9:7: error: the match is on Elem, not Style
22 9 | | Style.bold:
3- | ^^^^^
3+ | ^^^^^^^^^^
4+help: Elem has a bold of its own: write it alone
45 raz: 1 error