From 8efc8b637c4a44f98bdadcfbcdc27ecda4100afd Mon Sep 17 00:00:00 2001 From: Akos Hajdu Date: Wed, 4 Aug 2021 09:47:07 -0700 Subject: [PATCH] [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 --- infer/src/erlang/ErlangAstValidator.ml | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/infer/src/erlang/ErlangAstValidator.ml b/infer/src/erlang/ErlangAstValidator.ml index a62947b78..297e94072 100644 --- a/infer/src/erlang/ErlangAstValidator.ml +++ b/infer/src/erlang/ErlangAstValidator.ml @@ -168,9 +168,18 @@ let rec validate_expr (expr : Ast.expression) = validate_expr e && List.for_all ~f:validate_qualifier qs | Literal _ -> true - | Map {map; updates} -> - let validate_assoc (a : Ast.association) = validate_expr a.key && validate_expr a.value in - validate_expr_opt map && List.for_all ~f:validate_assoc updates + | Map {map; updates} -> ( + (* Map create only accepts '=>' *) + 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} -> validate_pattern pattern && validate_expr body | Nil ->