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

Inner proc in macro breaks parameters #9509

Open
copygirl opened this issue Oct 25, 2018 · 6 comments
Open

Inner proc in macro breaks parameters #9509

copygirl opened this issue Oct 25, 2018 · 6 comments
Labels

Comments

@copygirl
Copy link

copygirl commented Oct 25, 2018

When using a static parameter in an inner proc of a macro, that parameter will somehow have the value 0 (if using static[int]) instead of the value passed to the macro.

edit: @zah mentioned this happens for nim nodes, too.

Example

import macros

macro foo(i: static[int]): untyped =
  echo $i

foo(1)
foo(2)

macro foo2(i: static[int]): untyped =
  echo $i
  proc doNothing() =
    discard i
  doNothing()

foo2(1)
foo2(2)

Current Output

1
2
0
0

Expected Output

1
2
1
2

Workaround

Can be avoided by assigning to a local variable first.

macro foo2(i: static[int]): untyped =
  let i2 = i
  echo $i2
  proc doNothing() =
    discard i2
  doNothing()
@krux02 krux02 added the Macros label Oct 25, 2018
@krux02 krux02 self-assigned this Oct 25, 2018
@zah
Copy link
Member

zah commented Oct 25, 2018

This is not limited to static parameters. Regular AST parameters will be read as nil too if you don't re-assign them to a variable.

@copygirl copygirl changed the title Inner proc in macro breaks static parameter Inner proc in macro breaks parameters Oct 25, 2018
@kaushalmodi
Copy link
Contributor

/cc @narimiran Was it you who pasted an example on the IRC where for an AST var e, you had to first assign e[0] to a new var e0, and so on .. ?

@narimiran
Copy link
Member

Yes, it was me. (Nice memory! :))

Here is the full example:

macro with(obj: MyObject, body: untyped): untyped =
  result = newStmtList()
  for b in body:
    let
      b0 = b[0]
      b1 = b[1]
    result.add quote do:
      # `obj`.`b[0]` = `b[1]`
      `obj`.`b0` = `b1`

We cannot do the thing that is commented out, so we must introduce b0 and b1.

@timotheecour
Copy link
Member

maybe also a fix for this would test for bug from closed as duplicate issue #9656 :

BUG: code below prints forwardResult1:false instead of forwardResult1:true

macro dispatchGen7*(forwardResult: static bool = true): untyped =
  echo "forwardResult1:", forwardResult # BUG: will show: forwardResult1:false
  proc callWrapped(): NimNode =
    let ignore = forwardResult # comment this => will show: forwardResult1:true
    discard

  let callWrapd=callWrapped()

dispatchGen7(forwardResult=true)

@stale
Copy link

stale bot commented Aug 4, 2020

This issue has been automatically marked as stale because it has not had recent activity. If you think it is still a valid issue, write a comment below; otherwise it will be closed. Thank you for your contributions.

@stale stale bot unassigned krux02 Aug 4, 2020
@stale stale bot added the stale Staled PR/issues; remove the label after fixing them label Aug 4, 2020
@Clyybber
Copy link
Contributor

Clyybber commented Aug 4, 2020

Still an issue..

@stale stale bot removed the stale Staled PR/issues; remove the label after fixing them label Aug 4, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

7 participants