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

NimNodes from parseStmt are passed by value instead of by reference #14373

Open
exelotl opened this issue May 16, 2020 · 0 comments
Open

NimNodes from parseStmt are passed by value instead of by reference #14373

exelotl opened this issue May 16, 2020 · 0 comments

Comments

@exelotl
Copy link
Contributor

exelotl commented May 16, 2020

When trying to store a NimNode in a container object, I found that my subsequent changes to the NimNode don't show up in other references to it.

This only happens if the NimNode originated from parseStmt or parseExpr, and only when storing the NimNode in a container (object, seq, etc.) or a global variable.

  • If you use newTree or quote do, the bug doesn't occur.
  • If you put the NimNode in a local variable or pass it as an argument, the bug doesn't occur.

Example

import macros

type Container = object
  n: NimNode

proc addBam(n: NimNode) =
  n.add newCall("echo", newLit("bam"))

var z {.compileTime.}: NimNode

macro test() =
  #var a = newStmtList()          # use this instead and the bug goes away
  var a = parseStmt("discard")
  let c = Container(n:a)
  let x = a
  z = a
  a.add newCall("echo", newLit("foo"))    # added
  c.n.add newCall("echo", newLit("bar"))  # not added!
  x.add newCall("echo", newLit("baz"))    # added
  x.addBam()                              # added
  z.add newCall("echo", newLit("bat"))    # not added!
  a

test()

Current Output

foo
baz
bam

Expected Output

foo
bar
baz
bam
bat

Additional Information

I tested as far back as Nim 0.18.0, the behaviour still happens exactly as shown.

$ nim -v
Nim Compiler Version 1.2.0 [Windows: amd64]
Compiled at 2020-04-03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant