From 57a5dff9e4efae85c13fc4f5f94aef52f2e08d5f Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Tue, 13 Oct 2026 18:10:04 +0100 Subject: [PATCH] Report 2026-10.21: an enumeration's match arm may name several names Section 7.4 and 12; the parser takes the list, the arms share one place; conform/ tests the run, a name twice in a list, and a name left out. Co-Authored-By: Claude Sonnet 5.5 --- diff --git a/CLAUDE.md b/CLAUDE.md index bab40b5..cbf96b2 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -102,7 +102,8 @@ defect, and the library grows only by what breaks no program written for it. A wish for more, the owner's or yours, goes on the list in `PLAN.md`, for a later thaw; it isn't built. v0.6.0 was such a thaw, -by the owner's word: report 2026-10.12; the compiler follows 2026-10.20, whose `try` returns an error that isn't +by the owner's word: report 2026-10.12; the compiler follows 2026-10.21, whose `match` on an enumeration takes +several names to an arm (10.21), and 2026-10.20, whose `try` returns an error that isn't nil from the procedure it stands in (10.20, the owner's word), and 2026-10.19, whose `Sys` writes without waiting, for rat's pastes (10.13), and whose blank, `_ := f(x)`, drops a result that is otherwise warned of diff --git a/conform/err/MatchEnumListMissing.raz b/conform/err/MatchEnumListMissing.raz new file mode 100644 index 0000000..36f859f --- /dev/null +++ b/conform/err/MatchEnumListMissing.raz @@ -0,0 +1,10 @@ +module MatchEnumListMissing; +type Op = (plus, minus, times); +proc main*; +var op: Op; +begin + match op of + | plus, minus: + end; +end; +end MatchEnumListMissing. diff --git a/conform/err/MatchEnumListSame.raz b/conform/err/MatchEnumListSame.raz new file mode 100644 index 0000000..9bd0702 --- /dev/null +++ b/conform/err/MatchEnumListSame.raz @@ -0,0 +1,11 @@ +module MatchEnumListSame; +type Op = (plus, minus, times); +proc main*; +var op: Op; +begin + match op of + | plus, plus: + | minus, times: + end; +end; +end MatchEnumListSame. diff --git a/conform/err/MatchEnumListTwice.raz b/conform/err/MatchEnumListTwice.raz new file mode 100644 index 0000000..87a432c --- /dev/null +++ b/conform/err/MatchEnumListTwice.raz @@ -0,0 +1,11 @@ +module MatchEnumListTwice; +type Op = (plus, minus, times); +proc main*; +var op: Op; +begin + match op of + | plus, minus: + | minus, times: + end; +end; +end MatchEnumListTwice. diff --git a/conform/run/MatchEnumList.expect b/conform/run/MatchEnumList.expect new file mode 100644 index 0000000..ab70932 --- /dev/null +++ b/conform/run/MatchEnumList.expect @@ -0,0 +1,5 @@ +1 10 +1 10 +2 20 +3 10 +3 20 diff --git a/conform/run/MatchEnumList.raz b/conform/run/MatchEnumList.raz new file mode 100644 index 0000000..1a8cad8 --- /dev/null +++ b/conform/run/MatchEnumList.raz @@ -0,0 +1,34 @@ +module MatchEnumList; +import Out; + +type Op = (plus, minus, times, over, rest); + +proc kind(op: Op): INT; +begin + match op of + | plus, minus: return 1; + | times: return 2; + | over, rest: return 3; + end; +end; + +proc wide(op: Op): INT; +var n: INT; +begin + n := 0; + match op of + | minus, over, plus: n := 10; + | else: n := 20; + end; + return n; +end; + +proc main*; +var op: Op; +begin + for op := plus to rest do + Out.int(kind(op)); Out.str(" "); Out.int(wide(op)); Out.ln; + end; +end; + +end MatchEnumList. diff --git a/doc/raz.md b/doc/raz.md index 2a54ffd..1fd20bd 100644 --- a/doc/raz.md +++ b/doc/raz.md @@ -9,7 +9,7 @@ This report defines the language. It is short on purpose. Whatever it doesn't allow, raz doesn't have. -This is version 2026-10.20 of the report; section 12 says what each +This is version 2026-10.21 of the report; section 12 says what each version changed. 2026-10.11 and 2026-10.12 are a thaw the owner chose, for a language anyone can build again. They break programs written for 2026-10.10 and before: for 2026-10.9, say, the report of compiler @@ -902,7 +902,20 @@ `| else:` arm, which must come last. `match` on an enumeration works the same way: each arm names one of the -enumeration's names. +enumeration's names, or several, separated by commas, for the arm to +run when the value is any of them: + +``` +match op of +| sPlus, sMinus: kind := additive; +| sTimes: kind := multiplicative; +| else: +end; +``` + +Every name must have an arm, and only one, unless there is an `| else:` +arm. A variant's arm names one case: the fields it may name belong to +that case alone. `match` on an integer runs the arm whose labels hold its value: @@ -2041,6 +2054,8 @@ `ERROR`, which makes the procedure it stands in return that error when it isn't `nil` (sections 2, 5.8, 6, 7.8 and appendix B). `try` becomes a reserved word, which breaks a program with a name `try`. +- **2026-10.21.** An arm of a `match` on an enumeration may name + several of its names (section 7.4). ## Appendix A. What an implementation chooses diff --git a/src/parse.pas b/src/parse.pas index 0c00972..58ca27a 100644 --- a/src/parse.pas +++ b/src/parse.pas @@ -1600,7 +1600,7 @@ procedure MatchStatement(const start: TPos; ind: longint); var - x: TItem; t: PType; c, f, o: PObj; p: int64; L, endL, n, disp, elseAt: longint; + x: TItem; t: PType; c, f, o: PObj; p: int64; L, endL, n, first, disp, elseAt: longint; seen: array[0..255] of boolean; hasElse, all, byValue: boolean; endAt: TPos; ls: TLabels; begin Next; @@ -1633,18 +1633,23 @@ end else if t^.form = fInt then IntArm(t, ls, endL) else begin - if tok <> sIdent then Expected('a case'); - c := t^.fields; - while (c <> nil) and (c^.name <> id) do c := c^.next; - if c = nil then Mark(NameOf(id) + ' isn''t a case of ' + TypeName(t)); - if (c^.val <= High(seen)) and seen[c^.val] then Mark(NameOf(c^.name) + ' has two arms'); - if c^.val <= High(seen) then seen[c^.val] := true; - Next; + first := ls.n; L := NoLabel; - if t^.form = fEnum then begin + repeat + if tok <> sIdent then Expected('a case'); + c := t^.fields; + while (c <> nil) and (c^.name <> id) do c := c^.next; + if c = nil then Mark(NameOf(id) + ' isn''t a case of ' + TypeName(t)); + if (c^.val <= High(seen)) and seen[c^.val] then Mark(NameOf(c^.name) + ' has two arms'); + if c^.val <= High(seen) then seen[c^.val] := true; + Next; + if t^.form <> fEnum then begin MatchTag(p, c^.val, L); break end; + { an enumeration's arm may name several, which share its place } ls.lo[ls.n] := c^.val; ls.hi[ls.n] := c^.val; inc(ls.n); - end else MatchTag(p, c^.val, L); + if tok <> sComma then break; + Next; + until false; OpenScope(NewScope); if tok = sLparen then begin Next; @@ -1667,7 +1672,7 @@ Expect(sRparen); end; Expect(sColon); - if t^.form = fEnum then ls.at[ls.n - 1] := pc; + for n := first to ls.n - 1 do ls.at[n] := pc; Statements; CloseScope; Jump(endL); diff --git a/src/raz.pas b/src/raz.pas index f1bb9cb..c0ef892 100644 --- a/src/raz.pas +++ b/src/raz.pas @@ -34,7 +34,7 @@ { what git describe says, made by make: 0.3.0 at the tag v0.3.0, 0.3.0-5-g38d29a8 five commits past it } Version = {$I version.inc}; - Report = '2026-10.20'; { the version of doc/raz.md it follows } + Report = '2026-10.21'; { the version of doc/raz.md it follows } MemfdCreate = 319; { the system call; FPC 3.2.2 has no name for it } MfdCloexec = 1; { an ELF still executes: the kernel opens it first } diff --git a/test/msg/MatchEnumListMissing.err b/test/msg/MatchEnumListMissing.err new file mode 100644 index 0000000..a7d360d --- /dev/null +++ b/test/msg/MatchEnumListMissing.err @@ -0,0 +1,4 @@ +conform/err/MatchEnumListMissing.raz:8:5: error: no arm for times + 8 | end; + | ^^^ +raz: 1 error diff --git a/test/msg/MatchEnumListSame.err b/test/msg/MatchEnumListSame.err new file mode 100644 index 0000000..5892ffd --- /dev/null +++ b/test/msg/MatchEnumListSame.err @@ -0,0 +1,4 @@ +conform/err/MatchEnumListSame.raz:7:13: error: plus has two arms + 7 | | plus, plus: + | ^^^^ +raz: 1 error diff --git a/test/msg/MatchEnumListTwice.err b/test/msg/MatchEnumListTwice.err new file mode 100644 index 0000000..69a7cb2 --- /dev/null +++ b/test/msg/MatchEnumListTwice.err @@ -0,0 +1,4 @@ +conform/err/MatchEnumListTwice.raz:8:7: error: minus has two arms + 8 | | minus, times: + | ^^^^^ +raz: 1 error