Skip to content

Commit

Permalink
Restrict allowed operators when converting exprs to guards
Browse files Browse the repository at this point in the history
Summary: Conversion of expressions to guards is only possible for certain allowed operators (for example, `++` is not accepted in a guard expression).

Reviewed By: RobinMorisset

Differential Revision: D41377868

fbshipit-source-id: 02efee2e02a23fe68c546786716ffe85eaede2b0
  • Loading branch information
VLanvin authored and facebook-github-bot committed Nov 18, 2022
1 parent d8afa8e commit 75de28b
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 15 deletions.
35 changes: 33 additions & 2 deletions eqwalizer/src/main/scala/com/whatsapp/eqwalizer/ast/Filters.scala
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,37 @@ object Filters {
"is_tuple",
)

val guards_binop: Set[String] =
Set(
"/",
"*",
"-",
"+",
"div",
"rem",
"band",
"bor",
"bxor",
"bsl",
"bsr",
"or",
"xor",
"and",
">=",
">",
"=<",
"<",
"/=",
"=/=",
"==",
"=:=",
"andalso",
"orelse",
)

val guards_unop: Set[String] =
Set("bnot", "+", "-", "not")

private def isPredicateFun(name: String, arity: Int): Boolean =
(name, arity) match {
case (_, 1) =>
Expand All @@ -54,11 +85,11 @@ object Filters {
for {
argsT <- asTests(args)
} yield TestCall(Id(f, arity), argsT)(expr.pos)
case UnOp(op, arg) =>
case UnOp(op, arg) if guards_unop(op) =>
for {
argT <- asTest(arg)
} yield TestUnOp(op, argT)(expr.pos)
case BinOp(op, arg1, arg2) =>
case BinOp(op, arg1, arg2) if guards_binop(op) =>
for {
arg1T <- asTest(arg1)
arg2T <- asTest(arg2)
Expand Down
18 changes: 9 additions & 9 deletions eqwalizer/test_projects/_cli/checkable_funs.cli
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
all funs:
All : 2773
Checkable : 2512
All : 2774
Checkable : 2513
Well-typed checkable : 1615
Checkable ratio : 91%
Health ratio of checkable : 64%
error count : 896
error count : 897

--------------------------------------------
generated funs:
Expand All @@ -26,19 +26,19 @@ generated non-test funs:

--------------------------------------------
non-generated funs:
All : 2773
Checkable : 2512
All : 2774
Checkable : 2513
Well-typed checkable : 1615
Checkable ratio : 91%
Health ratio of checkable : 64%
error count : 896
error count : 897

--------------------------------------------
non-generated non-test funs (most important):
All : 2771
Checkable : 2510
All : 2772
Checkable : 2511
Well-typed checkable : 1614
Checkable ratio : 91%
Health ratio of checkable : 64%
error count : 895
error count : 896

4 changes: 2 additions & 2 deletions eqwalizer/test_projects/_cli/discarded_specs.cli
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,5 @@ type_aliases:uses_trans_unbound_var/2
type_aliases:uses_ty_w_unbound_var/2
type_aliases:uses_ty_w_unbound_var2/0
Discarded specs: 49
Total specs: 2650
Discarded ratio: 1.8491 %
Total specs: 2651
Discarded ratio: 1.8484 %
2 changes: 1 addition & 1 deletion eqwalizer/test_projects/_cli/long_errors.cli
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Average error message length: 22.09649 characters
Average error message length: 22.086357 characters
Longest error message: 122 characters
Average error message length for the longest 10% of error msgs: 46.886074 characters
3 changes: 2 additions & 1 deletion eqwalizer/test_projects/_cli/misc.json
Original file line number Diff line number Diff line change
Expand Up @@ -2562,7 +2562,8 @@
"use_invalid_opaque_2/1",
"f/1",
"g/1",
"fuzz01/0"
"fuzz01/0",
"fuzz02/0"
]
},
{
Expand Down
4 changes: 4 additions & 0 deletions eqwalizer/test_projects/check_gradual/src/gradual_misc.erl
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,7 @@ g(#{(a #{ b => c }) := _}) ->
-spec fuzz01() -> ok.
fuzz01() when #{(true andalso false) => {}} ->
ok.

-spec fuzz02() -> ok.
fuzz02() ->
<<X || X <- [], (X ++ X) >>.
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,9 @@ g(#{(a #{ b => c }) := _}) -> | OK |
-spec fuzz01() -> ok. | |
fuzz01() when #{(true andalso false) => {}…… OK |
ok. | |
| |
-spec fuzz02() -> ok. | |
fuzz02() -> | ERROR |
<<X || X <- [], (X ++ X) >>. | | << || >>.
| | Expected: 'ok'
| | Got : binary()

0 comments on commit 75de28b

Please sign in to comment.