Skip to content

Commit

Permalink
[erl-frontend] Be less restrictive on map pattern keys
Browse files Browse the repository at this point in the history
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
  • Loading branch information
hajduakos authored and facebook-github-bot committed Dec 22, 2022
1 parent 2a50097 commit dbdcf48
Showing 1 changed file with 5 additions and 6 deletions.
11 changes: 5 additions & 6 deletions infer/src/erlang/ErlangAstValidator.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 )
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit dbdcf48

Please sign in to comment.