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

NimNode in array/seq secretly copies the node. #4821

Closed
krux02 opened this issue Sep 22, 2016 · 2 comments · Fixed by #5253
Closed

NimNode in array/seq secretly copies the node. #4821

krux02 opened this issue Sep 22, 2016 · 2 comments · Fixed by #5253
Labels
VM see also `const` label

Comments

@krux02
Copy link
Contributor

krux02 commented Sep 22, 2016

Definitely this bug gave me a lot of head scratching, because I was writing a macro that stored some backreferences to the ast in a seq. Turned out that all my references were useless, because they were secretly copies of the syntax tree at the time I created the references.

Fixing this bug could break a lot of code, that only works because of this misbehavior, but time doesn't make the bug easier to fix.

import macros

macro testmacro*(): untyped =
  let innerStmtList = quote do:
    discard

  let localLet = innerStmtList
  var localVar = innerStmtList
  let localArray = [innerStmtList,localLet]
  var localSeq   = newSeq[NimNode](0)
  localSeq.add(innerStmtList)
  localSeq.add(localLet)

  # since NimNode is a ref type, and all seq type
  # and local variables only contain copies of the
  # references, this should get rid of the discard statement
  # everywhere, but it doesn't.
  innerStmtList.del(0)

  echo "# all represent the same value (OK) #"    
  echo innerStmtList.treeRepr
  echo localLet.treeRepr
  echo localVar.treeRepr
  echo "# secretly copied (totally not ok) ##"
  echo localArray[0].treeRepr
  echo localArray[1].treeRepr
  echo localSeq[0].treeRepr
  echo localSeq[1].treeRepr
  echo "#####################################"

  discard

discard testmacro()

output:

# all represent the same value (OK) #
StmtList
StmtList
StmtList
# secretly copied (totally not ok) ##
StmtList
  DiscardStmt
    Empty
StmtList
  DiscardStmt
    Empty
StmtList
  DiscardStmt
    Empty
StmtList
  DiscardStmt
    Empty
#####################################

expected:

# all represent the same value (OK) #
StmtList
StmtList
StmtList
# secretly copied (totally not ok) ##
StmtList
StmtList
StmtList
StmtList
#####################################
@Araq Araq added the VM see also `const` label label Sep 23, 2016
@jxy
Copy link
Contributor

jxy commented Oct 7, 2016

Is it related to the issue, #4427, I submitted a while ago?

@krux02
Copy link
Contributor Author

krux02 commented Oct 7, 2016

@jxy It's hard to say, because honestly at the moment I don't know what your code want's to do since it has no comments nor any meaningful names for anything. I also don't know what you expect your code to behave.

Araq pushed a commit that referenced this issue Jan 19, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
VM see also `const` label
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants