[erl-frontend] Support if expressions

Summary: Add support for `if` expressions.

Reviewed By: rgrig

Differential Revision: D29637230

fbshipit-source-id: 5dee00345
master
Akos Hajdu 3 years ago committed by Facebook GitHub Bot
parent 88beede7dc
commit e04bc38fde

@ -461,6 +461,12 @@ and translate_expression env {Ast.line; simple_expression} =
Sil.Call ((ret_var, any), fun_exp, args, env.location, CallFlags.default)
in
Block.all env [head_block; tail_block; Block.make_instruction env [call_instruction]]
| If clauses ->
let blocks = Block.any env (List.map ~f:(translate_case_clause env []) clauses) in
let crash_node = Node.make_pattern_fail env in
blocks.exit_failure |~~> [crash_node] ;
let blocks = {blocks with exit_failure= crash_node} in
blocks
| Literal (Atom atom) ->
let hash =
(* With this hack, an atom may accidentaly be considered equal to an unrelated integer.

@ -22,6 +22,8 @@ codetoanalyze/erlang/nonmatch/src/function_guards.erl, possible_exception/1, 0,
codetoanalyze/erlang/nonmatch/src/function_guards.erl, test_accepts_positive2_Bad/0, -23, NONEXHAUSTIVE_PATTERN_MATCH, no_bucket, ERROR, [calling context starts here,in call to `accepts_positive2/1`,no pattern match here]
codetoanalyze/erlang/nonmatch/src/function_guards.erl, test_accepts_positive_Bad/0, -19, NONEXHAUSTIVE_PATTERN_MATCH, no_bucket, ERROR, [calling context starts here,in call to `accepts_positive/1`,no pattern match here]
codetoanalyze/erlang/nonmatch/src/function_guards.erl, test_possible_exception_Bad/0, -37, NONEXHAUSTIVE_PATTERN_MATCH, no_bucket, ERROR, [calling context starts here,in call to `possible_exception/1`,no pattern match here]
codetoanalyze/erlang/nonmatch/src/if_expression.erl, accepts_positive/1, 1, NONEXHAUSTIVE_PATTERN_MATCH, no_bucket, ERROR, [*** LATENT ***,no pattern match here]
codetoanalyze/erlang/nonmatch/src/if_expression.erl, test_accepts_positive_Bad/0, -13, NONEXHAUSTIVE_PATTERN_MATCH, no_bucket, ERROR, [calling context starts here,in call to `accepts_positive/1`,no pattern match here]
codetoanalyze/erlang/nonmatch/src/match.erl, fp_match_test_c_Ok/0, 1, NONEXHAUSTIVE_PATTERN_MATCH, no_bucket, ERROR, [no pattern match here]
codetoanalyze/erlang/nonmatch/src/match.erl, fp_match_test_d_Ok/0, -13, NONEXHAUSTIVE_PATTERN_MATCH, no_bucket, ERROR, [calling context starts here,in call to `tail/1`,no pattern match here]
codetoanalyze/erlang/nonmatch/src/match.erl, match_test_b_Bad/0, 1, CONSTANT_ADDRESS_DEREFERENCE, no_bucket, WARNING, [in call to `two/0`,is the constant 2,assigned,returned,return from call to `two/0`,invalid access occurs here]

@ -0,0 +1,33 @@
% Copyright (c) Facebook, Inc. and its affiliates.
%
% This source code is licensed under the MIT license found in the
% LICENSE file in the root directory of this source tree.
-module(if_expression).
-export([
test_accepts_positive_Ok/0,
test_accepts_positive_Bad/0,
test_accepts_all_Ok/0
]).
accepts_positive(X) ->
if
X > 0 -> ok
end.
accepts_all(X) ->
if
X > 0 -> ok;
not (X > 0) -> ok
end.
test_accepts_positive_Ok() ->
accepts_positive(1).
test_accepts_positive_Bad() ->
accepts_positive(0).
test_accepts_all_Ok() ->
accepts_all(0),
accepts_all(1).
Loading…
Cancel
Save