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

Case Statement Macros do not work in functions above exported functions. #11556

Closed
kayabaNerve opened this issue Jun 20, 2019 · 0 comments

Comments

@kayabaNerve
Copy link
Collaborator

commented Jun 20, 2019

Calling a case statement macro in a function causes an exported function below it to no longer be in the top level.

{.experimental: "caseStmtMacros".}

import macros

macro match(n: tuple): untyped =
  result = newTree(nnkIfStmt)
  let selector = n[0]
  for i in 1 ..< n.len:
    let it = n[i]
    case it.kind
    of nnkElse, nnkElifBranch, nnkElifExpr, nnkElseExpr:
      result.add it
    of nnkOfBranch:
      for j in 0..it.len-2:
        let cond = newCall("==", selector, it[j])
        result.add newTree(nnkElifBranch, cond, it[^1])
    else:
      error "'match' cannot handle this node", it

  
proc a*() =
  case ("foo", 78)
  of ("foo", 78): echo "yes"
  of ("bar", 88): echo "no"
  else: discard

proc b*() =
  discard

a()

Current Output

/usercode/in.nim(27, 7) Error: 'export' is only allowed at top level

Expected Output

"yes"

Possible Solution

Removing b(), allowing the program to compile, does then call a(). Because of this, I thought a different top-level statement in between may be a temporary workaround. Neither a discard nor non-exported function worked.

Additional Information

$ nim -v
Nim Compiler Version 0.19.9 [Linux: amd64]
Compiled at 2019-06-03
Copyright (c) 2006-2019 by Andreas Rumpf

active boot switches: -d:release

kayabaNerve pushed a commit to MerosCrypto/Meros that referenced this issue Jun 21, 2019

@Araq Araq closed this in 35df59b Jun 21, 2019

narimiran added a commit that referenced this issue Jun 27, 2019

[bugfix] fixes #11556
(cherry picked from commit 35df59b)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
1 participant
You can’t perform that action at this time.