~ninjin/julia-nix

8db1d8909fc768a22a7fb6f3ba12dd75273a0604 — Simeon Schaub 2 years ago 2f47dae
fix #42220: macro hygiene with unary `::` (#42221)

2 files changed, 26 insertions(+), 8 deletions(-)

M src/macroexpand.scm
M test/syntax.jl
M src/macroexpand.scm => src/macroexpand.scm +15 -8
@@ 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)

M test/syntax.jl => test/syntax.jl +11 -0
@@ 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}