dwango / fialyzer

[WIP] Faster Implementation of Dialyzer
https://dwango.github.io/fialyzer/
Apache License 2.0
56 stars 8 forks source link

Patterns in function arguments must not convert to `case` expression #135

Open yoshihiro503 opened 5 years ago

yoshihiro503 commented 5 years ago

In the current system, pattern matching on the argument side of function abstraction is treated as a syntax sugar of a case expression. But this is not sound in some cases. For example:

foo() ->
    X = 1,
    (fun ({ok, X}) -> X + 1 end)({ok, 5}).

our From_erlang.expr_of_erlang_expr convert this code to:

foo() ->
    X = 1,
    case {ok, 5} of
        {ok, X} -> X + 1
    end.

The two codes have different semantics.

This difference appears when all of the following conditions are satisfied.

Priority

The priority is low because this problem appears in a rare case.

How to resolve this

amutake commented 5 years ago
foo() ->
    X = 1,
    (fun (__A__) -> case __A__ of {ok, X} -> X + 1 end end)({ok, 5}).

causes case clause error too because function literals capture its environment.