[erl-frontend] Small fix to validator on map expressions

Summary: In map expressions, associations of the form `K := V` should only be accepted if it is a map update (expression is not empty), and should not be accepted for map creation. This is because such associations check if the key actually exist, which obviously does not hold during map creation. See also [documentation](https://erlang.org/doc/apps/erts/absform.html#expressions). We already had this check for maps in guards, but forgot to include it for maps in expressions. Fixed it now.

Reviewed By: rgrig

Differential Revision: D30103350

fbshipit-source-id: 39973a137
master
Akos Hajdu 3 years ago committed by Facebook GitHub Bot
parent f90153b428
commit 8efc8b637c

@ -168,9 +168,18 @@ let rec validate_expr (expr : Ast.expression) =
validate_expr e && List.for_all ~f:validate_qualifier qs validate_expr e && List.for_all ~f:validate_qualifier qs
| Literal _ -> | Literal _ ->
true true
| Map {map; updates} -> | Map {map; updates} -> (
let validate_assoc (a : Ast.association) = validate_expr a.key && validate_expr a.value in (* Map create only accepts '=>' *)
validate_expr_opt map && List.for_all ~f:validate_assoc updates let validate_create (a : Ast.association) =
match a.kind with Arrow -> validate_expr a.key && validate_expr a.value | _ -> false
in
(* Map update accepts '=>' and ':=' *)
let validate_update (a : Ast.association) = validate_expr a.key && validate_expr a.value in
match map with
| None ->
List.for_all ~f:validate_create updates
| Some expr ->
validate_expr expr && List.for_all ~f:validate_update updates )
| Match {pattern; body} -> | Match {pattern; body} ->
validate_pattern pattern && validate_expr body validate_pattern pattern && validate_expr body
| Nil -> | Nil ->

Loading…
Cancel
Save