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

Writing a macro generates code utilizing NamedTuple whose value is calculated #32121

Closed
runapp opened this issue May 23, 2019 · 8 comments
Closed
Assignees
Labels
bug Indicates an unexpected problem or unintended behavior macros @macros

Comments

@runapp
Copy link

runapp commented May 23, 2019

I'm writing a macro tha packs it's parameter and @FILE into a NamedTuple and push it into a Vector, like this:

testvec=Vector{NamedTuple{(:f,:s),Tuple{Int,String}}}()
# push!(testvec,(f=0,s="abc"))
macro testm(a)
    return quote
        b=a+1
        push!(testvec,$(esc(
            (f=b,s=@__FILE__)
        )))
    end
end
@testm 2

My think is to try avoiding other ways than esc to prevent f and s from being renamed, or to make b work in escaped environment. However I tried $(esc(b))=a+1 and other escape forms, with none of them succeeded.

Any suggestions?

@KristofferC
Copy link
Member

Please use https://discourse.julialang.org/ for general Julia questions.

@runapp
Copy link
Author

runapp commented May 23, 2019

Forget about it.
Finally got $(esc(begin b=a+1 end)) to work.
I post it here rather than asking on discourse or StackOverflow only to see if anybody wants some new syntax to make it more elegent, such as makes global accept symbols.

@JeffBezanson
Copy link
Member

I think there is also a hygiene bug here though:

julia> macro id(x)
       x
       end
@id (macro with 1 method)

julia> @id (a=1,)
(#12#a = 1,)

@JeffBezanson JeffBezanson reopened this May 24, 2019
@JeffBezanson JeffBezanson added bug Indicates an unexpected problem or unintended behavior macros @macros labels May 24, 2019
@JeffBezanson JeffBezanson self-assigned this May 24, 2019
@Keno Keno closed this as completed in d8798be May 24, 2019
Keno added a commit that referenced this issue May 24, 2019
fix #32121, macro hygiene bug in named tuples
adamnemecek pushed a commit to adamnemecek/julia that referenced this issue May 29, 2019
JeffBezanson added a commit that referenced this issue Jun 6, 2019
JeffBezanson added a commit that referenced this issue Jun 7, 2019
KristofferC pushed a commit that referenced this issue Jun 9, 2019
KristofferC pushed a commit that referenced this issue Jul 3, 2019
KristofferC pushed a commit that referenced this issue Aug 26, 2019
(cherry picked from commit d8798be)
(cherry picked from commit d128ca1)
KristofferC pushed a commit that referenced this issue Aug 26, 2019
(cherry picked from commit d8798be)
(cherry picked from commit d128ca1)
KristofferC pushed a commit that referenced this issue Aug 27, 2019
KristofferC pushed a commit that referenced this issue Aug 28, 2019
KristofferC pushed a commit that referenced this issue Aug 28, 2019
@tkluck
Copy link
Contributor

tkluck commented Sep 25, 2019

@KristofferC I'm seeing this behaviour on Julia master, also related to named tuple hygiene:

$ julia
               _
   _       _ _(_)_     |  Documentation: https://docs.julialang.org
  (_)     | (_) (_)    |
   _ _   _| |_  __ _   |  Type "?" for help, "]?" for Pkg help.
  | | | | | | |/ _` |  |
  | | |_| | | | (_| |  |  Version 1.4.0-DEV.189 (2019-09-21)
 _/ |\__'_|_|_|\__'_|  |  Commit 36220ed9c7* (3 days old master)
|__/                   |

julia> macro named_tuple_hygiene(key, value); return :( (a=1, $key => $value) ); end
@named_tuple_hygiene (macro with 1 method)

julia> @named_tuple_hygiene(:b, 2)
ERROR: syntax: invalid named tuple element "Main.=>(:b, 2)"
Stacktrace:
 [1] top-level scope at REPL[2]:1

I think the root cause is that named tuple syntax Angers The Lisp Gods: within a Expr(:tuple, ...), the expression Expr(:call, :(=>), key, value) has a meaning different from "call => on key and value".

One proper fix would be to parse it differently: e.g. we should have (a = b, c => d) parse to something like

Expr(:tuple, Expr(:(=), a, b), Expr(:(=>), c, d))

An alternative fix is to allow pair-valued expressions as elements of named tuples (currently a syntax error), e.g.

julia> keyval = :b => 2
:b => 2

julia> (a=1, keyval)
ERROR: syntax: invalid named tuple element "keyval"
Stacktrace:
 [1] top-level scope at REPL[4]:1

But given that you've worked on this recently, maybe we can come up with a solution that only touches the macro hygiene logic. What do you think?

@KristofferC
Copy link
Member

I'm not sure why I was pinged here?

@tkluck
Copy link
Contributor

tkluck commented Sep 25, 2019

I pinged you because of this:
afbeelding
Is there someone else who is in a better position to check it out?

@KristofferC
Copy link
Member

Oh, that's just cause I backported that commit to some release. #32464 is the original PR.

@tkluck
Copy link
Contributor

tkluck commented Sep 25, 2019

Ah that makes sense, sorry for the noise! Added a comment there as well.

KristofferC pushed a commit that referenced this issue Feb 20, 2020
(cherry picked from commit d8798be)
(cherry picked from commit d128ca1)
KristofferC pushed a commit that referenced this issue Feb 20, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Indicates an unexpected problem or unintended behavior macros @macros
Projects
None yet
Development

No branches or pull requests

4 participants