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

fix unescaping in global expressions #47719

Merged
merged 1 commit into from
Dec 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 21 additions & 8 deletions src/macroexpand.scm
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,19 @@
(cadr e)
e))

(define (unescape-global-lhs e env m parent-scope inarg)
(cond ((not (pair? e)) e)
((eq? (car e) 'escape) (cadr e))
((memq (car e) '(parameters tuple))
(list* (car e) (map (lambda (e)
(unescape-global-lhs e env m parent-scope inarg))
(cdr e))))
((and (memq (car e) '(|::| kw)) (length= e 3))
(list (car e) (unescape-global-lhs (cadr e) env m parent-scope inarg)
(resolve-expansion-vars-with-new-env (caddr e) env m parent-scope inarg)))
(else
(resolve-expansion-vars-with-new-env e env m parent-scope inarg))))

(define (typedef-expr-name e)
(cond ((atom? e) e)
((or (eq? (car e) 'curly) (eq? (car e) '<:)) (typedef-expr-name (cadr e)))
Expand Down Expand Up @@ -344,14 +357,14 @@
(m (cadr scope))
(parent-scope (cdr parent-scope)))
(resolve-expansion-vars-with-new-env (cadr e) env m parent-scope inarg))))
((global) (let ((arg (cadr e)))
(cond ((symbol? arg) e)
((assignment? arg)
`(global
(= ,(unescape (cadr arg))
,(resolve-expansion-vars-with-new-env (caddr arg) env m parent-scope inarg))))
(else
`(global ,(resolve-expansion-vars-with-new-env arg env m parent-scope inarg))))))
((global)
`(global
,@(map (lambda (arg)
(if (assignment? arg)
`(= ,(unescape-global-lhs (cadr arg) env m parent-scope inarg)
,(resolve-expansion-vars-with-new-env (caddr arg) env m parent-scope inarg))
(unescape-global-lhs arg env m parent-scope inarg)))
(cdr e))))
((using import export meta line inbounds boundscheck loopinfo inline noinline) (map unescape e))
((macrocall) e) ; invalid syntax anyways, so just act like it's quoted.
((symboliclabel) e)
Expand Down
23 changes: 23 additions & 0 deletions test/syntax.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3429,3 +3429,26 @@ end
elseif false || (()->true)()
42
end)) == 42

macro _macroexpand(x, m=__module__)
:($__source__; macroexpand($m, Expr(:var"hygienic-scope", $(esc(Expr(:quote, x))), $m)))
end

@testset "unescaping in :global expressions" begin
m = @__MODULE__
@test @_macroexpand(global x::T) == :(global x::$(GlobalRef(m, :T)))
@test @_macroexpand(global (x, $(esc(:y)))) == :(global (x, y))
@test @_macroexpand(global (x::S, $(esc(:y))::$(esc(:T)))) ==
:(global (x::$(GlobalRef(m, :S)), y::T))
@test @_macroexpand(global (; x, $(esc(:y)))) == :(global (; x, y))
@test @_macroexpand(global (; x::S, $(esc(:y))::$(esc(:T)))) ==
:(global (; x::$(GlobalRef(m, :S)), y::T))

@test @_macroexpand(global x::T = a) == :(global x::$(GlobalRef(m, :T)) = $(GlobalRef(m, :a)))
@test @_macroexpand(global (x, $(esc(:y))) = a) == :(global (x, y) = $(GlobalRef(m, :a)))
@test @_macroexpand(global (x::S, $(esc(:y))::$(esc(:T))) = a) ==
:(global (x::$(GlobalRef(m, :S)), y::T) = $(GlobalRef(m, :a)))
@test @_macroexpand(global (; x, $(esc(:y))) = a) == :(global (; x, y) = $(GlobalRef(m, :a)))
@test @_macroexpand(global (; x::S, $(esc(:y))::$(esc(:T))) = a) ==
:(global (; x::$(GlobalRef(m, :S)), y::T) = $(GlobalRef(m, :a)))
end