Skip to content

Commit c1bc3c9

Browse files
committed
fix slurping into function definition
Discovered while working on #40737. Currently, this throws an unintuitive error: ```julia julia> a, f()... = 1, 2, 3 ERROR: syntax: ssavalue with no def Stacktrace: [1] top-level scope @ REPL[1]:1 ``` Might as well fix this properly - Why not allow function definitions to slurp a little from time to time? ;)
1 parent fbe28e4 commit c1bc3c9

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

src/julia-syntax.scm

+8-4
Original file line numberDiff line numberDiff line change
@@ -1432,7 +1432,7 @@
14321432
(cons R elts)))
14331433
((vararg? L)
14341434
(if (null? (cdr lhss))
1435-
(let ((temp (make-ssavalue)))
1435+
(let ((temp (if (eventually-call? (cadr L)) (gensy) (make-ssavalue))))
14361436
`(block ,@(reverse stmts)
14371437
(= ,temp (tuple ,@rhss))
14381438
,@(reverse after)
@@ -2024,9 +2024,13 @@
20242024
((eq? l x) #t)
20252025
(else (in-lhs? x (cdr lhss)))))))
20262026
;; in-lhs? also checks for invalid syntax, so always call it first
2027-
(let* ((xx (if (or (and (not (in-lhs? x lhss)) (symbol? x))
2028-
(ssavalue? x))
2029-
x (make-ssavalue)))
2027+
(let* ((xx (cond ((or (and (not (in-lhs? x lhss)) (symbol? x))
2028+
(ssavalue? x))
2029+
x)
2030+
((and (pair? lhss) (vararg? (last lhss))
2031+
(eventually-call? (cadr (last lhss))))
2032+
(gensy))
2033+
(else (make-ssavalue))))
20302034
(ini (if (eq? x xx) '() (list (sink-assignment xx (expand-forms x)))))
20312035
(n (length lhss))
20322036
;; skip last assignment if it is an all-underscore vararg

test/syntax.jl

+10
Original file line numberDiff line numberDiff line change
@@ -2787,3 +2787,13 @@ macro m_nospecialize_unnamed_hygiene()
27872787
end
27882788

27892789
@test @m_nospecialize_unnamed_hygiene()(1) === Any
2790+
2791+
@testset "slurping into function def" begin
2792+
x, f()... = [1, 2, 3]
2793+
@test x == 1
2794+
@test f() == [2, 3]
2795+
2796+
x, f()... = 1, 2, 3
2797+
@test x == 1
2798+
@test f() == (2, 3)
2799+
end

0 commit comments

Comments
 (0)