Skip to content

Commit 8ebbd94

Browse files
simeonschaubKristofferC
authored andcommitted
fix slurping into function definition (#40738)
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? ;) (cherry picked from commit 5c49a0d)
1 parent 26bfefa commit 8ebbd94

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

src/julia-syntax.scm

+8-4
Original file line numberDiff line numberDiff line change
@@ -1456,7 +1456,7 @@
14561456
(cons R elts)))
14571457
((vararg? L)
14581458
(if (null? (cdr lhss))
1459-
(let ((temp (make-ssavalue)))
1459+
(let ((temp (if (eventually-call? (cadr L)) (gensy) (make-ssavalue))))
14601460
`(block ,@(reverse stmts)
14611461
(= ,temp (tuple ,@rhss))
14621462
,@(reverse after)
@@ -2155,9 +2155,13 @@
21552155
((eq? l x) #t)
21562156
(else (in-lhs? x (cdr lhss)))))))
21572157
;; in-lhs? also checks for invalid syntax, so always call it first
2158-
(let* ((xx (if (or (and (not (in-lhs? x lhss)) (symbol? x))
2159-
(ssavalue? x))
2160-
x (make-ssavalue)))
2158+
(let* ((xx (cond ((or (and (not (in-lhs? x lhss)) (symbol? x))
2159+
(ssavalue? x))
2160+
x)
2161+
((and (pair? lhss) (vararg? (last lhss))
2162+
(eventually-call? (cadr (last lhss))))
2163+
(gensy))
2164+
(else (make-ssavalue))))
21612165
(ini (if (eq? x xx) '() (list (sink-assignment xx (expand-forms x)))))
21622166
(n (length lhss))
21632167
;; skip last assignment if it is an all-underscore vararg

test/syntax.jl

+12
Original file line numberDiff line numberDiff line change
@@ -2937,3 +2937,15 @@ end
29372937
@test eval(Meta.parse("`a\\\r\nb`")) == `ab`
29382938
@test eval(Meta.parse("`a\\\rb`")) == `ab`
29392939
end
2940+
2941+
@testset "slurping into function def" begin
2942+
x, f()... = [1, 2, 3]
2943+
@test x == 1
2944+
@test f() == [2, 3]
2945+
# test that call to `Base.rest` is outside the definition of `f`
2946+
@test f() === f()
2947+
2948+
x, f()... = 1, 2, 3
2949+
@test x == 1
2950+
@test f() == (2, 3)
2951+
end

0 commit comments

Comments
 (0)