From 8db1d8909fc768a22a7fb6f3ba12dd75273a0604 Mon Sep 17 00:00:00 2001 From: Simeon Schaub Date: Tue, 14 Sep 2021 14:19:58 -0400 Subject: [PATCH] fix #42220: macro hygiene with unary `::` (#42221) --- src/macroexpand.scm | 23 +++++++++++++++-------- test/syntax.jl | 11 +++++++++++ 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/src/macroexpand.scm b/src/macroexpand.scm index f17f4d3510dc6..34414d24bde75 100644 --- a/src/macroexpand.scm +++ b/src/macroexpand.scm @@ -271,7 +271,9 @@ (define (other x) (resolve-expansion-vars-with-new-env x env m parent-scope inarg)) (case (car e) ((where) `(where ,(recur (cadr e)) ,@(map other (cddr e)))) - ((|::|) `(|::| ,(recur (cadr e)) ,(other (caddr e)))) + ((|::|) (if (length= e 2) + `(|::| ,(other (cadr e))) + `(|::| ,(recur (cadr e)) ,(other (caddr e))))) ((call) `(call ,(other (cadr e)) ,@(map (lambda (x) (resolve-expansion-vars-with-new-env x env m parent-scope #t)) @@ -397,13 +399,18 @@ ((not (length> e 2)) e) ((and (pair? (cadr e)) (eq? (caadr e) '|::|)) - `(kw (|::| - ,(if inarg - (resolve-expansion-vars- (cadr (cadr e)) env m parent-scope inarg) - ;; in keyword arg A=B, don't transform "A" - (unescape (cadr (cadr e)))) - ,(resolve-expansion-vars- (caddr (cadr e)) env m parent-scope inarg)) - ,(resolve-expansion-vars-with-new-env (caddr e) env m parent-scope inarg))) + (let* ((type-decl (cadr e)) ;; [argname]::type + (argname (and (length> type-decl 2) (cadr type-decl))) + (type (if argname (caddr type-decl) (cadr type-decl)))) + `(kw (|::| + ,@(if argname + (list (if inarg + (resolve-expansion-vars- argname env m parent-scope inarg) + ;; in keyword arg A=B, don't transform "A" + (unescape argname))) + '()) + ,(resolve-expansion-vars- type env m parent-scope inarg)) + ,(resolve-expansion-vars-with-new-env (caddr e) env m parent-scope inarg)))) (else `(kw ,(if inarg (resolve-expansion-vars- (cadr e) env m parent-scope inarg) diff --git a/test/syntax.jl b/test/syntax.jl index 09a91a31305f3..14b17d6eaa0b2 100644 --- a/test/syntax.jl +++ b/test/syntax.jl @@ -2966,3 +2966,14 @@ end @generated g25678(x) = return :x @test g25678(7) === 7 + +# issue 42220 +macro m42220() + return quote + function foo(::Type{T}=Float64) where {T} + return Vector{T}(undef, 10) + end + end +end +@test @m42220()() isa Vector{Float64} +@test @m42220()(Bool) isa Vector{Bool}