When `setLambda` is used inside a procedure, I got error `testlamdba.nim(24, 
18) Error: internal error: environment misses: x`. It works in outside of 
procedures. Is `setLambda` macro making wrong AST? Or is this compiler bug?
    
    
    import macros
    
    macro setLambda(procVar: var proc; body: untyped): untyped =
      #echo procVar.getTypeImpl().treeRepr
      let
        typeImpl = procVar.getTypeImpl()
        formalParams = typeImpl[0].copyNimTree
        pragma = typeImpl[1].copyNimTree
        empty = newEmptyNode()
      
      newAssignment(procVar,
                    newTree(nnkLambda,
                            empty, empty, empty,
                            formalParams,
                            pragma,
                            empty,
                            body))
    
    var foo: proc (x: int): int
    setLambda foo, x
    doAssert foo(123) == 123
    
    proc main =
      var bar: proc (x: int): int   # testlamdba.nim(24, 18) Error: internal 
error: environment misses: x
      setLambda bar, x
      doAssert bar(321) == 321
    
    main()
    
    Run
    
    
    $ nim -v
    Nim Compiler Version 1.7.1 [Linux: amd64]
    Compiled at 2022-07-17
    Copyright (c) 2006-2022 by Andreas Rumpf
    
    git hash: 0d8bec695606a65c5916d0da7fcb0a976a4e1f7b
    active boot switches: -d:release
    
    Run

Reply via email to