You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
import macros
typeEnv=ptrobject
a: intmacrotestAux(foo: untyped): untyped=# This macro takes the body of proc `foo`, puts its body into new proc `newFoo`, that accepts Env as argument :env,# and replaces reference to `a` with `:env.a`
foo.expectKind(nnkProcDef)
let
envParamId =genSym(nskParam, ":env")
newParamName =ident"a"let newFoo =newProc(ident"newFoo",
params = [newEmptyNode(), newIdentDefs(envParamId, ident"Env")],
body = foo.body)
let echoCall = newFoo.body
echoCall.expectKind(nnkCommand)
let hiddenCallConv = echoCall[1][1][0]
hiddenCallConv.expectKind(nnkHiddenCallConv)
# Make sure we're replacing the right thingdoAssert(hiddenCallConv[1].kind == nnkSym and$hiddenCallConv[1] =="a")
# Replace `a` with :env.a# Internal error line. Replace `a` with `:env.a`
hiddenCallConv[1] =newDotExpr(envParamId, newParamName)
# Nim crash line. Replace `a` with `:env[].a`
hiddenCallConv[1] =newDotExpr(newTree(nnkDerefExpr, envParamId), newParamName)
result= newFoo
echorepr(result) # This gets printed nicely and looks correct# Force some initial typingmacrotest(foo: typed): untyped=result=newCall("testAux", foo)
procfoo(a: int) {.test.} =echo a
newFoo(nil)
The crash will happen after macro evaluation. repr result at the end of the macro looks valid to me. If you comment the "crash" line in the sample, there will be ICE Error: internal error: genRecordFieldAux. The repr result is slightly different but still valid.
The text was updated successfully, but these errors were encountered:
It seems the problem appears only when replacing the sym in echo. If echo is changed to a non-magic function, the code will work (with some modifications, as it only works with echo-specific ast)
The crash will happen after macro evaluation.
repr result
at the end of the macro looks valid to me. If you comment the "crash" line in the sample, there will be ICEError: internal error: genRecordFieldAux
. Therepr result
is slightly different but still valid.The text was updated successfully, but these errors were encountered: