Skip to content

Commit 193fee8

Browse files
KenoKristofferC
authored and
KristofferC
committed
lowering: Recognize argument destructuring inside macro hygiene (#54702)
Fixes #54701 (cherry picked from commit 75951ec)
1 parent a6d098e commit 193fee8

File tree

2 files changed

+25
-13
lines changed

2 files changed

+25
-13
lines changed

src/macroexpand.scm

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -219,30 +219,26 @@
219219
lst)))
220220

221221
;; get the name from a function formal argument expression, allowing `(escape x)`
222-
(define (try-arg-name v)
223-
(cond ((symbol? v) (list v))
222+
(define (try-arg-name v (escaped #f))
223+
(cond ((symbol? v) (if escaped '() (list v)))
224224
((atom? v) '())
225225
(else
226226
(case (car v)
227-
((|::|) (if (length= v 2) '() (try-arg-name (cadr v))))
228-
((... kw =) (try-arg-name (cadr v)))
229-
((escape) (list v))
230-
((hygienic-scope) (try-arg-name (cadr v)))
227+
((|::|) (if (length= v 2) '() (try-arg-name (cadr v) escaped)))
228+
((... kw =) (try-arg-name (cadr v) escaped))
229+
((escape) (if escaped (list (cadr v)) '()))
230+
((hygienic-scope) (try-arg-name (cadr v) escaped))
231+
((tuple) (apply nconc (map (lambda (e) (try-arg-name e escaped)) (cdr v))))
231232
((meta) ;; allow certain per-argument annotations
232233
(if (nospecialize-meta? v #t)
233-
(try-arg-name (caddr v))
234+
(try-arg-name (caddr v) escaped)
234235
'()))
235236
(else '())))))
236237

237238
;; get names from a formal argument list, specifying whether to include escaped ones
238239
(define (safe-arg-names lst (escaped #f))
239240
(apply nconc
240-
(map (lambda (v)
241-
(let ((vv (try-arg-name v)))
242-
(if (eq? escaped (and (pair? vv) (pair? (car vv)) (eq? (caar vv) 'escape)))
243-
(if escaped (list (cadar vv)) vv)
244-
'())))
245-
lst)))
241+
(map (lambda (v) (try-arg-name v escaped)) lst)))
246242

247243
;; arg names, looking only at positional args
248244
(define (safe-llist-positional-args lst (escaped #f))

test/syntax.jl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3637,3 +3637,19 @@ end
36373637
@test array == [7]
36383638
@test execs == 4
36393639
end
3640+
3641+
# Issue #54701 - Macro hygiene of argument destructuring
3642+
macro makef54701()
3643+
quote
3644+
call(f) = f((1, 2))
3645+
function $(esc(:f54701))()
3646+
call() do (a54701, b54701)
3647+
return a54701+b54701
3648+
end
3649+
end
3650+
end
3651+
end
3652+
@makef54701
3653+
@test f54701() == 3
3654+
@test !@isdefined(a54701)
3655+
@test !@isdefined(b54701)

0 commit comments

Comments
 (0)