From dbdcf4863ee2751a6b671f072850b29b4916bf5b Mon Sep 17 00:00:00 2001 From: Akos Hajdu Date: Thu, 22 Dec 2022 02:12:27 -0800 Subject: [PATCH] [erl-frontend] Be less restrictive on map pattern keys Summary: [This document](https://www.erlang.org/doc/apps/erts/absform.html#patterns) says > If P is a map pattern #{A_1, ..., A_k}, where each A_i is an association P_i_1 := P_i_2, which implied to us that both keys and values have to be patterns themselves. But [this other document](https://www.erlang.org/doc/reference_manual/expressions.html#maps-in-patterns) says > The key K must be a guard expression, with all variables already bound. V can be any pattern with either bound or unbound variables. and seems like the compiler also implements the latter. So adjusting our validation. Reviewed By: mmarescotti Differential Revision: D42191306 fbshipit-source-id: 6d5d96c51380719916a10f11c2a511b176afa2a4 --- infer/src/erlang/ErlangAstValidator.ml | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/infer/src/erlang/ErlangAstValidator.ml b/infer/src/erlang/ErlangAstValidator.ml index 3e261e859d..42fcbef78a 100644 --- a/infer/src/erlang/ErlangAstValidator.ml +++ b/infer/src/erlang/ErlangAstValidator.ml @@ -63,7 +63,7 @@ let rec validate_pattern env (p : Ast.expression) = let validate_assoc (a : Ast.association) = match a.kind with | Exact -> - validate_pattern env a.key && validate_pattern env a.value + validate_guard_test env a.key && validate_pattern env a.value | _ -> L.debug Capture Verbose "Invalid map association kind (not :=) in pattern@." ; false @@ -94,13 +94,12 @@ let rec validate_pattern env (p : Ast.expression) = false -let validate_patterns env = List.for_all ~f:(validate_pattern env) +and validate_patterns env = List.for_all ~f:(validate_pattern env) (** {2 Guards} *) - (** Guards are expressions in general, but with restrictions and constraints. *) -let validate_guard_call_func (e : Ast.expression) = +and validate_guard_call_func (e : Ast.expression) = match e.simple_expression with | Literal lit -> ( match lit with Atom _ -> true | _ -> false ) @@ -109,7 +108,7 @@ let validate_guard_call_func (e : Ast.expression) = false -let validate_guard_call_module (eo : Ast.expression option) = +and validate_guard_call_module (eo : Ast.expression option) = match eo with | None -> true @@ -127,7 +126,7 @@ let validate_guard_call_module (eo : Ast.expression option) = false ) -let rec validate_guard_test env (gt : Ast.expression) = +and validate_guard_test env (gt : Ast.expression) = match gt.simple_expression with | BinaryOperator (e1, _, e2) -> validate_guard_test env e1 && validate_guard_test env e2