status-im / nim-chronos

Chronos - An efficient library for asynchronous programming
https://status-im.github.io/nim-chronos/docs/chronos
Apache License 2.0
353 stars 51 forks source link

Compilation error: `invalid pragma: stackTrace: false` #367

Closed SirNickolas closed 11 months ago

SirNickolas commented 1 year ago

In some cases, when generating an .async procedure by a macro, you get the aforementioned error. Example:

import std/macros
import chronos/asyncloop

func recurse(tree: NimNode): NimNode =
  tree.expectKind {nnkProcDef, nnkIteratorDef, nnkStmtList}
  if tree.kind == nnkStmtList:
    for i, child in tree:
      tree[i] = child.recurse
    tree
  else:
    let name = tree[0]
    quote do:
      proc `name` {.async.} = discard

macro turnIntoProc(tree: untyped) =
  tree.recurse

proc test0 {.turnIntoProc.}
# OK

iterator test1 {.turnIntoProc.}
# Error: invalid pragma: stackTrace: false

turnIntoProc:
  iterator test2
# OK

(The last test confuses me the most, to be honest.) The error is caused by these lines:

https://github.com/status-im/nim-chronos/blob/f3b77d86615813690c5ab5e7cbe841521be2a20f/chronos/asyncmacro2.nim#L250-L251

Could you explain their meaning please?

Any ideas on how to make the example compileable are much appreciated. (Checking if tree is an nnkStmtList and, if not, rewriting to bindSym"turnIntoProc".newCall newStmtList tree doesn’t work. Even with tree.copyNimTree.)

Chronos 3.0.11, Nim 1.6.10.

arnetheduck commented 11 months ago

this is a Nim bug that has been fixed in more recent versions, see https://github.com/nim-lang/Nim/issues/22198

SirNickolas commented 11 months ago

Thanks for reporting it. I’m afraid, there’s no much we can do with it then. I think I’ll just write in the Readme that it has been fixed in Nim 2.0.