From 122c3f103932fbea72369b899cc581303b798677 Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Fri, 23 Oct 2026 11:25:18 +0100 Subject: [PATCH] Messages: a match arm's unused field, a blanked field and _ for the other arms say what to write instead --- diff --git a/STANDARDS.md b/STANDARDS.md index 6b5f3ac..e81ca67 100644 --- a/STANDARDS.md +++ b/STANDARDS.md @@ -61,3 +61,10 @@ `Prop`, drawing bytes or numbers from its `Source`. A test that passes says nothing. The core modules, and those the compiler imports, keep their own checks, since they can't import a standard-library module. + +## A match arm names every field + +An arm of a `match` on a variant names all of a case's fields, in +order, or none. A field the arm doesn't read is named with `_` before +its field's name, as in `| real(r, _places):`, so a reader sees it was +left unread on purpose and `-Werror` doesn't warn of it. diff --git a/src/Parse.raz b/src/Parse.raz index 1fd4736..4815dc7 100644 --- a/src/Parse.raz +++ b/src/Parse.raz @@ -2165,6 +2165,10 @@ while Scan.tok = Scan.sBar do Scan.next; if hasElse then return Scan.mark("else must be the last arm"); end; + if Scan.tok = Scan.sBlank then + return Scan.markHelp(Scan.tokPos(), "_ matches nothing: it is the blank, which stands only left of :=", + "the arm for every other case is | else:"); + end; if Scan.tok = Scan.sElse then Scan.next; try expect(Scan.sColon); @@ -2208,6 +2212,10 @@ f := c^.fields; n := 0; repeat + if (Scan.tok = Scan.sBlank) and (f # nil) then + return Scan.markHelp(Scan.tokPos(), "a field is named, not blanked", + say1("call one the arm doesn't read _{}", nameOf(f^.name))); + end; if Scan.tok # Scan.sIdent then return expected("a name"); end; if f = nil then return Scan.mark(say1("more names than {} has fields", nameOf(c^.name))); @@ -3763,8 +3771,10 @@ else Diag.warning(o^.pos, say1("{} is never used", t)); end; - if not o^.used and (o^.param or o^.ind) then + if not o^.used and o^.param then Diag.help(say1("if it must be there, call it _{}", s)); + elsif not o^.used and o^.ind then (* not a parameter, so a name a match arm gives *) + Diag.help(say1("an arm names all the fields of a case or none: call one it doesn't read _{}", s)); end; end; diff --git a/test/err/MatchBlankEnum.err b/test/err/MatchBlankEnum.err new file mode 100644 index 0000000..86450e0 --- /dev/null +++ b/test/err/MatchBlankEnum.err @@ -0,0 +1,5 @@ +test/err/MatchBlankEnum.raz:9:7: error: _ matches nothing: it is the blank, which stands only left of := + 9 | | _: return; + | ^ +help: the arm for every other case is | else: +raz: 1 error diff --git a/test/err/MatchBlankEnum.raz b/test/err/MatchBlankEnum.raz new file mode 100644 index 0000000..a5a6597 --- /dev/null +++ b/test/err/MatchBlankEnum.raz @@ -0,0 +1,14 @@ +module MatchBlankEnum; +(* The arm for every other case of an enumeration is else, not _ *) +type Colour = (red, green); + +proc f(c: Colour); +begin + match c of + | red: return; + | _: return; + end; +end; + +begin +end MatchBlankEnum. diff --git a/test/err/MatchBlankField.err b/test/err/MatchBlankField.err new file mode 100644 index 0000000..19b9e37 --- /dev/null +++ b/test/err/MatchBlankField.err @@ -0,0 +1,5 @@ +test/err/MatchBlankField.raz:9:15: error: a field is named, not blanked + 9 | | real(r, _): return; + | ^ +help: call one the arm doesn't read _places +raz: 1 error diff --git a/test/err/MatchBlankField.raz b/test/err/MatchBlankField.raz new file mode 100644 index 0000000..9596dfb --- /dev/null +++ b/test/err/MatchBlankField.raz @@ -0,0 +1,14 @@ +module MatchBlankField; +(* An arm names a field, it doesn't blank it: the help gives the name to use *) +type Arg = variant int(i: INT); real(r: F64; places: INT); end; + +proc f(a: Arg); +begin + match a of + | int(i): return; + | real(r, _): return; + end; +end; + +begin +end MatchBlankField. diff --git a/test/err/MatchBlankInt.err b/test/err/MatchBlankInt.err new file mode 100644 index 0000000..5e30c3c --- /dev/null +++ b/test/err/MatchBlankInt.err @@ -0,0 +1,5 @@ +test/err/MatchBlankInt.raz:7:7: error: _ matches nothing: it is the blank, which stands only left of := + 7 | | _: return; + | ^ +help: the arm for every other case is | else: +raz: 1 error diff --git a/test/err/MatchBlankInt.raz b/test/err/MatchBlankInt.raz new file mode 100644 index 0000000..9af0ea8 --- /dev/null +++ b/test/err/MatchBlankInt.raz @@ -0,0 +1,12 @@ +module MatchBlankInt; +(* The arm for every other value of an integer is else, not _ *) +proc f(n: INT); +begin + match n of + | 1: return; + | _: return; + end; +end; + +begin +end MatchBlankInt. diff --git a/test/err/MatchBlankString.err b/test/err/MatchBlankString.err new file mode 100644 index 0000000..2e2f942 --- /dev/null +++ b/test/err/MatchBlankString.err @@ -0,0 +1,5 @@ +test/err/MatchBlankString.raz:7:7: error: _ matches nothing: it is the blank, which stands only left of := + 7 | | _: return; + | ^ +help: the arm for every other case is | else: +raz: 1 error diff --git a/test/err/MatchBlankString.raz b/test/err/MatchBlankString.raz new file mode 100644 index 0000000..44ff919 --- /dev/null +++ b/test/err/MatchBlankString.raz @@ -0,0 +1,12 @@ +module MatchBlankString; +(* The arm for every other string is else, not _ *) +proc f(s: []U8); +begin + match s of + | "x": return; + | _: return; + end; +end; + +begin +end MatchBlankString. diff --git a/test/err/MatchBlankVariant.err b/test/err/MatchBlankVariant.err new file mode 100644 index 0000000..6f7a790 --- /dev/null +++ b/test/err/MatchBlankVariant.err @@ -0,0 +1,5 @@ +test/err/MatchBlankVariant.raz:9:7: error: _ matches nothing: it is the blank, which stands only left of := + 9 | | _: return; + | ^ +help: the arm for every other case is | else: +raz: 1 error diff --git a/test/err/MatchBlankVariant.raz b/test/err/MatchBlankVariant.raz new file mode 100644 index 0000000..ce45b16 --- /dev/null +++ b/test/err/MatchBlankVariant.raz @@ -0,0 +1,14 @@ +module MatchBlankVariant; +(* The arm for every other case of a variant is else, not _ *) +type Arg = variant int(i: INT); real(r: F64; places: INT); end; + +proc f(a: Arg); +begin + match a of + | int(i): return; + | _: return; + end; +end; + +begin +end MatchBlankVariant. diff --git a/test/err/MatchUnusedField.args b/test/err/MatchUnusedField.args new file mode 100644 index 0000000..2f41be6 --- /dev/null +++ b/test/err/MatchUnusedField.args @@ -0,0 +1 @@ +-Werror diff --git a/test/err/MatchUnusedField.err b/test/err/MatchUnusedField.err new file mode 100644 index 0000000..8dd7ad0 --- /dev/null +++ b/test/err/MatchUnusedField.err @@ -0,0 +1,5 @@ +test/err/MatchUnusedField.raz:11:15: error: variable places is never used + 11 | | real(r, places): neg := r < 0.0; + | ^^^^^^ +help: an arm names all the fields of a case or none: call one it doesn't read _places +raz: 1 error diff --git a/test/err/MatchUnusedField.raz b/test/err/MatchUnusedField.raz new file mode 100644 index 0000000..974674c --- /dev/null +++ b/test/err/MatchUnusedField.raz @@ -0,0 +1,18 @@ +module MatchUnusedField; +(* A name an arm gives a field it never reads is warned of, and the help says to call it _ and the field's name *) +type Arg = variant int(i: INT); real(r: F64; places: INT); end; + +proc negative(a: Arg): BOOL; +var neg: BOOL; +begin + neg := false; + match a of + | int(i): neg := i < 0; + | real(r, places): neg := r < 0.0; + end; + return neg; +end; + +begin + _ := negative(int(1)); +end MatchUnusedField.