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

Compiler does not set `.typ` inside macros when creating literal NimNodes #7792

Closed
jyapayne opened this issue May 7, 2018 · 0 comments

Comments

Projects
None yet
2 participants
@jyapayne
Copy link
Contributor

commented May 7, 2018

On the latest nim devel as of right now, this code fails with the stack trace included below.

Edit: I should add that if you remove the str string argument from the proc and the newCall constructor, it works fine.

Edit2: Looking into it further, it seems like the compiler doesn't set the .typ for literal NimNodes created inside a macro. Is this expected behavior?

Edit3: Looking into it even further, it appears that this only happens if the macro is invoked via the pragma syntax. I've added code below that should be equivalent.

The expected behavior is that the program prints:

"calling!stuff"
"calling!stuff"
import macros


proc callProc(str: string) =
  echo "calling!" & str


macro testMacro(code: typed): untyped =
  let stmtList = newNimNode(nnkStmtList)

  let stmts = code[6]

  for n in stmts.children:
    # the error happens here
    stmtList.add(newCall(bindSym("callProc"), newLit("stuff")))

  code[6] = stmtList

  result = newEmptyNode()


proc main() {.testMacro.} =
  echo "test"
  echo "test2"


when isMainModule:
  main()

Stacktrace:

bug.nim(22, 1) Error: internal error: genLiteral: ty is nil
Traceback (most recent call last)
nim.nim(113)             nim
nim.nim(77)              handleCmdLine
main.nim(173)            mainCommand
main.nim(77)             commandCompileToC
modules.nim(137)         compileProject
modules.nim(77)          compileModule
passes.nim(236)          processModule
passes.nim(125)          processTopLevelStmt
cgen.nim(1371)           myProcess
ccgstmts.nim(1141)       genStmts
ccgexprs.nim(2212)       expr
ccgcalls.nim(549)        genCall
ccgcalls.nim(196)        genPrefixCall
cgen.nim(498)            initLocExpr
ccgexprs.nim(2151)       expr
cgen.nim(895)            genProc
cgen.nim(860)            genProcNoForward
cgen.nim(759)            genProcAux
ccgstmts.nim(1141)       genStmts
ccgexprs.nim(2246)       expr
ccgexprs.nim(2030)       genStmtList
ccgstmts.nim(1141)       genStmts
ccgexprs.nim(2212)       expr
ccgcalls.nim(549)        genCall
ccgcalls.nim(188)        genPrefixCall
ccgcalls.nim(171)        genArg
cgen.nim(503)            initLocExprSingleUse
ccgexprs.nim(2198)       expr
ccgexprs.nim(83)         genLiteral
ccgexprs.nim(34)         genLiteral
msgs.nim(1088)           internalError
msgs.nim(1059)           liMessage
msgs.nim(908)            handleError
msgs.nim(898)            quit

Edit3: Code that works that should be equivalent:

import macros

proc callProc*(str: string) =
  echo "calling!" & str

macro testMacro(code: typed): untyped =
  let stmtList = newNimNode(nnkStmtList)

  for n in code.children:
    stmtList.add(newCall("callProc", newLit("stuff")))

  result = stmtList

proc main() =
  testMacro:
    echo("test")
    echo("test2")

when isMainModule:
  main()

@jyapayne jyapayne changed the title Macro error when adding newCall with string arguments Compiler does not set `.typ` inside macros when creating literal NimNodes May 8, 2018

@Araq Araq added Macros VM labels Jan 14, 2019

@narimiran narimiran closed this in 6f5eba4 May 29, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.