Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
yoshihiro503 opened this issue Jan 18, 2019 · 1 comment
Open

Comments

@yoshihiro503
Copy link
Contributor

yoshihiro503 commented Jan 18, 2019

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.

  • The argument of a function abstraction is a pattern other than a single variable
  • There are already bound variables in the pattern (the pattern has shadowing variables)
  • The variable has different types inside and outside the function

Priority

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

How to resolve this

  • Change the parameter of Ast.Abs so that function abstraction can have patterns
@amutake
Copy link
Contributor

amutake commented Jan 18, 2019

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants