raz / commit 122c3f10

commit
122c3f103932fbea72369b899cc581303b798677
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
e3021fa1
Messages: a match arm's unused field, a blanked field and _ for the other arms say what to write instead

patch · log

15 files changed, +133 -1

STANDARDS.md (history) modified +7 -0

@@ -61,3 +61,10 @@
6161 `Prop`, drawing bytes or numbers from its `Source`. A test that passes
6262 says nothing. The core modules, and those the compiler imports, keep
6363 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.

src/Parse.raz (history) modified +11 -1

@@ -2165,6 +2165,10 @@
21652165 while Scan.tok = Scan.sBar do
21662166 Scan.next;
21672167 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;
21682172 if Scan.tok = Scan.sElse then
21692173 Scan.next;
21702174 try expect(Scan.sColon);
@@ -2208,6 +2212,10 @@
22082212 f := c^.fields;
22092213 n := 0;
22102214 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;
22112219 if Scan.tok # Scan.sIdent then return expected("a name"); end;
22122220 if f = nil then
22132221 return Scan.mark(say1("more names than {} has fields", nameOf(c^.name)));
@@ -3763,8 +3771,10 @@
37633771 else
37643772 Diag.warning(o^.pos, say1("{} is never used", t));
37653773 end;
3766- if not o^.used and (o^.param or o^.ind) then
3774+ if not o^.used and o^.param then
37673775 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));
37683778 end;
37693779 end;
37703780

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

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

test/err/MatchBlankEnum.raz (history) added +14 -0

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

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

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

test/err/MatchBlankField.raz (history) added +14 -0

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

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

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

test/err/MatchBlankInt.raz (history) added +12 -0

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

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

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

test/err/MatchBlankString.raz (history) added +12 -0

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

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

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

test/err/MatchBlankVariant.raz (history) added +14 -0

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

test/err/MatchUnusedField.args (history) added +1 -0

@@ -0,0 +1 @@
1+-Werror

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

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

test/err/MatchUnusedField.raz (history) added +18 -0

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