It's incredibly easy to accidentally forget to copy a NimNode inside a macro.

In this example: 
    
    
    macro doStuffTo1to5(a : untyped, body : untyped) : untyped =
      result = newStmtList()
      for i in 1..5:
        let i =
          newLit(i)
        let
          blockStmtList = nnkStmtList.newTree()
        blockStmtList.add quote do:
          const `a` = `i`
        blockStmtList.add body
        #should be
        #blockStmtList.add body.copy
        let
          blockStmt = nnkBlockStmt.newTree(newEmptyNode(),blockStmtList)
        result.add blockStmt
    doStuffTo1to5(y):
      echo y
    
    
    Run

1 will be printed 5 times instead of 1 to 5 because body was not copied. This 
kind of bug is even harder to catch because if I had added body inside quote 
do: it would automatically do the copy for me encouraging the bad practice of 
forgetting to copy in future.

I only ask because I was thinking of making my own version of treeRepr that 
points out this type of bug and makes it real obvious. Having a version of 
treeRepr that automatically pointed out these type of bugs would be especially 
helpful for people new to Nim that don't understand the implementation details 
behind Nim.

On another note I wouldn't mind if treeRepr had an optional parameter that 
shortened openSymChoice and closedSymChoices to one element each as they tend 
to clutter output and most of the time all the choices have the same name 
anyway. Maybe even make that the default with the option to disable it.

Reply via email to