Messages: a match arm's unused field, a blanked field and _ for the other arms say what to write instead
| @@ -61,3 +61,10 @@ | ||
| 61 | 61 | `Prop`, drawing bytes or numbers from its `Source`. A test that passes |
| 62 | 62 | says nothing. The core modules, and those the compiler imports, keep |
| 63 | 63 | their own checks, since they can't import a standard-library module. |
| 64 | + | |
| 65 | +## A match arm names every field | |
| 66 | + | |
| 67 | +An arm of a `match` on a variant names all of a case's fields, in | |
| 68 | +order, or none. A field the arm doesn't read is named with `_` before | |
| 69 | +its field's name, as in `| real(r, _places):`, so a reader sees it was | |
| 70 | +left unread on purpose and `-Werror` doesn't warn of it. |
| @@ -2165,6 +2165,10 @@ | ||
| 2165 | 2165 | while Scan.tok = Scan.sBar do |
| 2166 | 2166 | Scan.next; |
| 2167 | 2167 | if hasElse then return Scan.mark("else must be the last arm"); end; |
| 2168 | + if Scan.tok = Scan.sBlank then | |
| 2169 | + return Scan.markHelp(Scan.tokPos(), "_ matches nothing: it is the blank, which stands only left of :=", | |
| 2170 | + "the arm for every other case is | else:"); | |
| 2171 | + end; | |
| 2168 | 2172 | if Scan.tok = Scan.sElse then |
| 2169 | 2173 | Scan.next; |
| 2170 | 2174 | try expect(Scan.sColon); |
| @@ -2208,6 +2212,10 @@ | ||
| 2208 | 2212 | f := c^.fields; |
| 2209 | 2213 | n := 0; |
| 2210 | 2214 | repeat |
| 2215 | + if (Scan.tok = Scan.sBlank) and (f # nil) then | |
| 2216 | + return Scan.markHelp(Scan.tokPos(), "a field is named, not blanked", | |
| 2217 | + say1("call one the arm doesn't read _{}", nameOf(f^.name))); | |
| 2218 | + end; | |
| 2211 | 2219 | if Scan.tok # Scan.sIdent then return expected("a name"); end; |
| 2212 | 2220 | if f = nil then |
| 2213 | 2221 | return Scan.mark(say1("more names than {} has fields", nameOf(c^.name))); |
| @@ -3763,8 +3771,10 @@ | ||
| 3763 | 3771 | else |
| 3764 | 3772 | Diag.warning(o^.pos, say1("{} is never used", t)); |
| 3765 | 3773 | end; |
| 3766 | - if not o^.used and (o^.param or o^.ind) then | |
| 3774 | + if not o^.used and o^.param then | |
| 3767 | 3775 | Diag.help(say1("if it must be there, call it _{}", s)); |
| 3776 | + elsif not o^.used and o^.ind then (* not a parameter, so a name a match arm gives *) | |
| 3777 | + Diag.help(say1("an arm names all the fields of a case or none: call one it doesn't read _{}", s)); | |
| 3768 | 3778 | end; |
| 3769 | 3779 | end; |
| 3770 | 3780 |
| @@ -0,0 +1,5 @@ | ||
| 1 | +test/err/MatchBlankEnum.raz:9:7: error: _ matches nothing: it is the blank, which stands only left of := | |
| 2 | + 9 | | _: return; | |
| 3 | + | ^ | |
| 4 | +help: the arm for every other case is | else: | |
| 5 | +raz: 1 error |
| @@ -0,0 +1,14 @@ | ||
| 1 | +module MatchBlankEnum; | |
| 2 | +(* The arm for every other case of an enumeration is else, not _ *) | |
| 3 | +type Colour = (red, green); | |
| 4 | + | |
| 5 | +proc f(c: Colour); | |
| 6 | +begin | |
| 7 | + match c of | |
| 8 | + | red: return; | |
| 9 | + | _: return; | |
| 10 | + end; | |
| 11 | +end; | |
| 12 | + | |
| 13 | +begin | |
| 14 | +end MatchBlankEnum. |
| @@ -0,0 +1,5 @@ | ||
| 1 | +test/err/MatchBlankField.raz:9:15: error: a field is named, not blanked | |
| 2 | + 9 | | real(r, _): return; | |
| 3 | + | ^ | |
| 4 | +help: call one the arm doesn't read _places | |
| 5 | +raz: 1 error |
| @@ -0,0 +1,14 @@ | ||
| 1 | +module MatchBlankField; | |
| 2 | +(* An arm names a field, it doesn't blank it: the help gives the name to use *) | |
| 3 | +type Arg = variant int(i: INT); real(r: F64; places: INT); end; | |
| 4 | + | |
| 5 | +proc f(a: Arg); | |
| 6 | +begin | |
| 7 | + match a of | |
| 8 | + | int(i): return; | |
| 9 | + | real(r, _): return; | |
| 10 | + end; | |
| 11 | +end; | |
| 12 | + | |
| 13 | +begin | |
| 14 | +end MatchBlankField. |
| @@ -0,0 +1,5 @@ | ||
| 1 | +test/err/MatchBlankInt.raz:7:7: error: _ matches nothing: it is the blank, which stands only left of := | |
| 2 | + 7 | | _: return; | |
| 3 | + | ^ | |
| 4 | +help: the arm for every other case is | else: | |
| 5 | +raz: 1 error |
| @@ -0,0 +1,12 @@ | ||
| 1 | +module MatchBlankInt; | |
| 2 | +(* The arm for every other value of an integer is else, not _ *) | |
| 3 | +proc f(n: INT); | |
| 4 | +begin | |
| 5 | + match n of | |
| 6 | + | 1: return; | |
| 7 | + | _: return; | |
| 8 | + end; | |
| 9 | +end; | |
| 10 | + | |
| 11 | +begin | |
| 12 | +end MatchBlankInt. |
| @@ -0,0 +1,5 @@ | ||
| 1 | +test/err/MatchBlankString.raz:7:7: error: _ matches nothing: it is the blank, which stands only left of := | |
| 2 | + 7 | | _: return; | |
| 3 | + | ^ | |
| 4 | +help: the arm for every other case is | else: | |
| 5 | +raz: 1 error |
| @@ -0,0 +1,12 @@ | ||
| 1 | +module MatchBlankString; | |
| 2 | +(* The arm for every other string is else, not _ *) | |
| 3 | +proc f(s: []U8); | |
| 4 | +begin | |
| 5 | + match s of | |
| 6 | + | "x": return; | |
| 7 | + | _: return; | |
| 8 | + end; | |
| 9 | +end; | |
| 10 | + | |
| 11 | +begin | |
| 12 | +end MatchBlankString. |
| @@ -0,0 +1,5 @@ | ||
| 1 | +test/err/MatchBlankVariant.raz:9:7: error: _ matches nothing: it is the blank, which stands only left of := | |
| 2 | + 9 | | _: return; | |
| 3 | + | ^ | |
| 4 | +help: the arm for every other case is | else: | |
| 5 | +raz: 1 error |
| @@ -0,0 +1,14 @@ | ||
| 1 | +module MatchBlankVariant; | |
| 2 | +(* The arm for every other case of a variant is else, not _ *) | |
| 3 | +type Arg = variant int(i: INT); real(r: F64; places: INT); end; | |
| 4 | + | |
| 5 | +proc f(a: Arg); | |
| 6 | +begin | |
| 7 | + match a of | |
| 8 | + | int(i): return; | |
| 9 | + | _: return; | |
| 10 | + end; | |
| 11 | +end; | |
| 12 | + | |
| 13 | +begin | |
| 14 | +end MatchBlankVariant. |
| @@ -0,0 +1 @@ | ||
| 1 | +-Werror |
| @@ -0,0 +1,5 @@ | ||
| 1 | +test/err/MatchUnusedField.raz:11:15: error: variable places is never used | |
| 2 | + 11 | | real(r, places): neg := r < 0.0; | |
| 3 | + | ^^^^^^ | |
| 4 | +help: an arm names all the fields of a case or none: call one it doesn't read _places | |
| 5 | +raz: 1 error |
| @@ -0,0 +1,18 @@ | ||
| 1 | +module MatchUnusedField; | |
| 2 | +(* 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 *) | |
| 3 | +type Arg = variant int(i: INT); real(r: F64; places: INT); end; | |
| 4 | + | |
| 5 | +proc negative(a: Arg): BOOL; | |
| 6 | +var neg: BOOL; | |
| 7 | +begin | |
| 8 | + neg := false; | |
| 9 | + match a of | |
| 10 | + | int(i): neg := i < 0; | |
| 11 | + | real(r, places): neg := r < 0.0; | |
| 12 | + end; | |
| 13 | + return neg; | |
| 14 | +end; | |
| 15 | + | |
| 16 | +begin | |
| 17 | + _ := negative(int(1)); | |
| 18 | +end MatchUnusedField. |