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

Error in variable pattern matching #294

Closed
PhucVH888 opened this issue Nov 30, 2015 · 3 comments
Closed

Error in variable pattern matching #294

PhucVH888 opened this issue Nov 30, 2015 · 3 comments
Assignees

Comments

@PhucVH888
Copy link
Contributor

In the following example, the second clause of pattern matching does not bind to variable z.

class Main
  def main() : void {
    let f = \(a:int) ->
      match a with
        0 => print "match"
        z => print "does not match"
    in ()
}

It raises the error when I compile the above snippet.

phuc:workspace vo$ encorec test.enc
Importing module String from /Users/vo/code/encore/bundles/standard/String.enc
test_src/Main.encore.c:108:41: error: use of undeclared identifier '_enc__closure_z'
  (*_enc__env_closure0)._enc__field_z = _enc__closure_z;
                                        ^
1 error generated.
 *** Compilation failed with exit code 1 ***
@PhucVH888 PhucVH888 added the bug label Nov 30, 2015
@EliasC
Copy link
Contributor

EliasC commented Nov 30, 2015

The problem is that the variables in the pattern of a match expression is mistaken for free variables, meaning that the closure thinks it should be part of the environment.

Fix:

Add the following as the second to last case of freeVariables' in src/AST/Util.hs:

      freeVariables' bound Match {arg, clauses} =
          freeVariables' bound arg ++ clausesFreeVars
          where
            clausesFreeVars = concatMap clauseFreeVars clauses
            clauseFreeVars MatchClause{mcpattern, mcguard, mchandler} =
                let boundInPattern = map fst $ freeVariables' [] mcpattern
                    bound' = boundInPattern ++ bound
                    freeInGuard = freeVariables' bound' mcguard
                    freeInHandler = freeVariables' bound' mchandler
                in
                  freeInGuard ++ freeInHandler

@glundi: Feel free to double check my work (and submit a PR) :)

@TheGrandmother
Copy link
Contributor

Has this issue been fixed yet ?

@kikofernandez
Copy link
Contributor

this issue has been fixed!

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

Successfully merging a pull request may close this issue.

5 participants