Skip to content

Quote captures result of containing macro #7323

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

Closed
StasRuinsEverything opened this issue Mar 11, 2018 · 8 comments
Closed

Quote captures result of containing macro #7323

StasRuinsEverything opened this issue Mar 11, 2018 · 8 comments
Labels

Comments

@StasRuinsEverything
Copy link

Hello! Here's a contrived reproduction:

import macros

macro mac(): untyped =
  quote do:
    proc test(): int =
      (proc(): int = result = 123)()

mac() # Error: type mismatch: got <int literal(123)> but expected 'NimNode = ref NimNodeObj'
echo test()

Using return instead circumvents the issue.

@PMunch
Copy link
Contributor

PMunch commented Mar 14, 2018

Yeah this is a really strange and annoying bug. One way I've found to still be able to use result in quote do is to do this:

import macros

macro mac(): untyped =
  let res = newIdentNode("result")
  quote do:
    proc test(): int =
      (proc(): int = `res` = 123)()

mac()
echo test() # Works  fine, outputs 123

@PMunch
Copy link
Contributor

PMunch commented Mar 15, 2018

I have figured out a nice simple fix for this now: PMunch@6089654. It's still not completely done but I hope to have it done by tomorrow and create a PR for this.

PMunch added a commit to PMunch/Nim that referenced this issue Mar 16, 2018
A simple test case based on GitHub issue nim-lang#7323 on how you can't put
result in a quote do block. This test verifies that it actually works
correctly now.
@PMunch
Copy link
Contributor

PMunch commented Mar 16, 2018

Done, here is the PR for my fix: #7343

@krux02
Copy link
Contributor

krux02 commented May 12, 2018

maybe this problem is resolved when the quote do macro is limited to bind proc- macro- and template-symbols. To my experience binding var-, let-, param, etc symbols always goes wrong.

LemonBoy added a commit to LemonBoy/Nim that referenced this issue Aug 9, 2018
PMunch added a commit to PMunch/Nim that referenced this issue Oct 31, 2018
A simple test case based on GitHub issue nim-lang#7323 on how you can't put
result in a quote do block. This test verifies that it actually works
correctly now.
krux02 pushed a commit that referenced this issue Oct 31, 2018
* Fix result not being able to use in quote do

This fixes the annoying issue of not be able to use result inside a
quote do block. It works by a simple trick. The quote do mechanic is
based on dynamically creating a template and immediately calling it with
the arguments found within the quote do block. Since this is called in
the scope of the macro the result variable is shadowed. This trick works
by changing all occurences of result (which shouldn't cause any issues
as result isn't used for anything else for the same reason) to another
name and then passing in an IdentNode with result as a named parameter
with that name.

Note that currently this just replaces it with a fixed named variable
"res" which should be changed to a non-colliding, dynamically created
name.

* Fix hard coded parameter "res" to anonymous symbol

This fixes the hard coded parameter "res" to be an anonymous symbol
instead so it won't collide with other parts of the argument list.

* Add test case for result in quote do block

A simple test case based on GitHub issue #7323 on how you can't put
result in a quote do block. This test verifies that it actually works
correctly now.

* Add test for explicit capturing of result

* Rebased against devel
@PMunch
Copy link
Contributor

PMunch commented Oct 31, 2018

@krux02 maybe close this issue as well?

@krux02
Copy link
Contributor

krux02 commented Oct 31, 2018

I can confirm, this works now.

@krux02 krux02 closed this as completed Oct 31, 2018
narimiran pushed a commit to narimiran/Nim that referenced this issue Nov 1, 2018
* Fix result not being able to use in quote do

This fixes the annoying issue of not be able to use result inside a
quote do block. It works by a simple trick. The quote do mechanic is
based on dynamically creating a template and immediately calling it with
the arguments found within the quote do block. Since this is called in
the scope of the macro the result variable is shadowed. This trick works
by changing all occurences of result (which shouldn't cause any issues
as result isn't used for anything else for the same reason) to another
name and then passing in an IdentNode with result as a named parameter
with that name.

Note that currently this just replaces it with a fixed named variable
"res" which should be changed to a non-colliding, dynamically created
name.

* Fix hard coded parameter "res" to anonymous symbol

This fixes the hard coded parameter "res" to be an anonymous symbol
instead so it won't collide with other parts of the argument list.

* Add test case for result in quote do block

A simple test case based on GitHub issue nim-lang#7323 on how you can't put
result in a quote do block. This test verifies that it actually works
correctly now.

* Add test for explicit capturing of result

* Rebased against devel
@mem-memov
Copy link

I've just copied the code from the top of the report and run it with Nim 0.19.2. It produced the same error message.

@SolitudeSF
Copy link
Contributor

works on devel

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

6 participants