diff options
| author | Alasdair Armstrong | 2018-01-25 19:05:39 +0000 |
|---|---|---|
| committer | Alasdair Armstrong | 2018-01-25 19:08:27 +0000 |
| commit | b7e388f0193a89608687760f50e476c059f0f49c (patch) | |
| tree | 8de862216287fd9b5b7a2cd9256b11fd3688bee4 /src/util.ml | |
| parent | 2ee59d0eee7508ebe4e84b4cf2468d8631b2c418 (diff) | |
Add pattern completness check for match statements
Gives warnings when pattern matches are incomplete, when matches are
redundant (in certain cases), or when no unguarded patterns exist. For
example the following file:
enum Test = {A, C, D}
val test1 : Test -> string
function test1 x =
match x {
A => "match A",
B => "this will match anything, because B is unbound!",
C => "match C",
D => "match D"
}
val test2 : Test -> string
function test2 x =
match x {
A => "match A",
C => "match C"
/* No match for D */
}
val test3 : Test -> string
function test3 x =
match x {
A if false => "never match A",
C => "match C",
D => "match D"
}
val test4 : Test -> string
function test4 x =
match x {
A if true => "match A",
C if true => "match C",
D if true => "match D"
}
will produce the following warnings
Warning: Possible redundant pattern match at file "test.sail", line 10, character 5 to line 10, character 5
C => "match C",
Warning: Possible redundant pattern match at file "test.sail", line 11, character 5 to line 11, character 5
D => "match D"
Warning: Possible incomplete pattern match at file "test.sail", line 17, character 3 to line 17, character 7
match x {
Most general matched pattern is A_|C_
Warning: Possible incomplete pattern match at file "test.sail", line 26, character 3 to line 26, character 7
match x {
Most general matched pattern is C_|D_
Warning: No non-guarded patterns at file "test.sail", line 35, character 3 to line 35, character 7
match x {
warnings can be turned of with the -no_warn flag.
Diffstat (limited to 'src/util.ml')
| -rw-r--r-- | src/util.ml | 7 |
1 files changed, 6 insertions, 1 deletions
diff --git a/src/util.ml b/src/util.ml index 6d4b3726..e2dc9b9f 100644 --- a/src/util.ml +++ b/src/util.ml @@ -94,6 +94,8 @@ (* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *) (**************************************************************************) +let opt_warnings = ref true + let rec last = function | [x] -> x | _ :: xs -> last xs @@ -414,4 +416,7 @@ let zencode_string str = "z" ^ List.fold_left (fun s1 s2 -> s1 ^ s2) "" (List.ma let zencode_upper_string str = "Z" ^ List.fold_left (fun s1 s2 -> s1 ^ s2) "" (List.map zchar (string_to_list str)) -let warn str = prerr_endline (("Warning" |> yellow |> clear) ^ ": " ^ str) +let warn str = + if !opt_warnings then + prerr_endline (("Warning" |> yellow |> clear) ^ ": " ^ str) + else () |
