Skip to content

Commit 1b09f69

Browse files
committed
allow vararg in lhs of assignment
1 parent eb64b89 commit 1b09f69

File tree

4 files changed

+114
-21
lines changed

4 files changed

+114
-21
lines changed

base/abstractarray.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2399,3 +2399,11 @@ function hash(A::AbstractArray, h::UInt)
23992399

24002400
return h
24012401
end
2402+
2403+
# The semantics of `collect` are weird. Better to write our own
2404+
function rest(a::AbstractArray{T}, state...) where {T}
2405+
v = Vector{T}(undef, 0)
2406+
# assume only very few items are taken from the front
2407+
sizehint!(v, length(a))
2408+
return foldl(push!, Iterators.rest(a, state...), init=v)
2409+
end

base/tuple.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,11 @@ function indexed_iterate(I, i, state)
9696
x
9797
end
9898

99+
rest(t::Tuple) = t
100+
rest(t::NTuple{N}, i::Int) where {N} = ntuple(x -> getfield(t, x+i-1), N-i+1)
101+
rest(a::Array, i::Int=1) = a[i:end]
102+
rest(itr, state...) = Iterators.rest(itr, state...)
103+
99104
# Use dispatch to avoid a branch in first
100105
first(::Tuple{}) = throw(ArgumentError("tuple must be non-empty"))
101106
first(t::Tuple) = t[1]

src/julia-syntax.scm

Lines changed: 50 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1430,7 +1430,8 @@
14301430
,@(reverse after)
14311431
(unnecessary (tuple ,@(reverse elts))))
14321432
(let ((L (car lhss))
1433-
(R (car rhss)))
1433+
;; rhss can be null iff L is a vararg
1434+
(R (if (null? rhss) '() (car rhss))))
14341435
(cond ((and (symbol-like? L)
14351436
(or (not (pair? R)) (quoted? R) (equal? R '(null)))
14361437
;; overwrite var immediately if it doesn't occur elsewhere
@@ -1442,6 +1443,16 @@
14421443
(cons (make-assignment L R) stmts)
14431444
after
14441445
(cons R elts)))
1446+
((vararg? L)
1447+
(if (null? (cdr lhss))
1448+
(let ((temp (make-ssavalue)))
1449+
`(block ,@(reverse stmts)
1450+
(= ,temp (tuple ,@rhss))
1451+
,@(reverse after)
1452+
(= ,(cadr L) ,temp)
1453+
(unnecessary (tuple ,@(reverse elts) (... ,temp)))))
1454+
(error (string "invalid \"...\" on non-final assignment location \""
1455+
(cadr L) "\""))))
14451456
((vararg? R)
14461457
(let ((temp (make-ssavalue)))
14471458
`(block ,@(reverse stmts)
@@ -2066,6 +2077,7 @@
20662077
(define (sides-match? l r)
20672078
;; l and r either have equal lengths, or r has a trailing ...
20682079
(cond ((null? l) (null? r))
2080+
((vararg? (car l)) #t)
20692081
((null? r) #f)
20702082
((vararg? (car r)) (null? (cdr r)))
20712083
(else (sides-match? (cdr l) (cdr r)))))
@@ -2075,26 +2087,43 @@
20752087
(expand-forms
20762088
(tuple-to-assignments lhss x))
20772089
;; (a, b, ...) = other
2078-
(let* ((xx (if (or (and (symbol? x) (not (memq x lhss)))
2079-
(ssavalue? x))
2080-
x (make-ssavalue)))
2081-
(ini (if (eq? x xx) '() (list (sink-assignment xx (expand-forms x)))))
2082-
(n (length lhss))
2083-
(st (gensy)))
2084-
`(block
2085-
(local ,st)
2086-
,@ini
2087-
,.(map (lambda (i lhs)
2088-
(expand-forms
2089-
(lower-tuple-assignment
2090-
(if (= i (- n 1))
2091-
(list lhs)
2092-
(list lhs st))
2093-
`(call (top indexed_iterate)
2094-
,xx ,(+ i 1) ,.(if (eq? i 0) '() `(,st))))))
2095-
(iota n)
2096-
lhss)
2097-
(unnecessary ,xx))))))
2090+
(begin
2091+
;; like memq, but if last element of lhss is (... sym),
2092+
;; check against sym instead
2093+
(define (in-lhs? x lhss)
2094+
(if (null? lhss)
2095+
#f
2096+
(let ((l (car lhss)))
2097+
(cond ((and (pair? l) (eq? (car l) '|...|))
2098+
(if (null? (cdr lhss))
2099+
(eq? (cadr l) x)
2100+
(error (string "invalid \"...\" on non-final assignment location \""
2101+
(cadr l) "\""))))
2102+
((eq? l x) #t)
2103+
(else (in-lhs? x (cdr lhss)))))))
2104+
;; in-lhs? also checks for invalid syntax, so always call it first
2105+
(let* ((xx (if (or (and (not (in-lhs? x lhss)) (symbol? x))
2106+
(ssavalue? x))
2107+
x (make-ssavalue)))
2108+
(ini (if (eq? x xx) '() (list (sink-assignment xx (expand-forms x)))))
2109+
(n (length lhss))
2110+
(st (gensy)))
2111+
`(block
2112+
(local ,st)
2113+
,@ini
2114+
,.(map (lambda (i lhs)
2115+
(expand-forms
2116+
(if (and (pair? lhs) (eq? (car lhs) '|...|))
2117+
`(= ,(cadr lhs) (call (top rest) ,xx ,.(if (eq? i 0) '() `(,st))))
2118+
(lower-tuple-assignment
2119+
(if (= i (- n 1))
2120+
(list lhs)
2121+
(list lhs st))
2122+
`(call (top indexed_iterate)
2123+
,xx ,(+ i 1) ,.(if (eq? i 0) '() `(,st)))))))
2124+
(iota n)
2125+
lhss)
2126+
(unnecessary ,xx)))))))
20982127
((typed_hcat)
20992128
(error "invalid spacing in left side of indexed assignment"))
21002129
((typed_vcat)

test/syntax.jl

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2493,3 +2493,54 @@ end
24932493

24942494
# PR #37973
24952495
@test Meta.parse("1¦2⌿3") == Expr(:call, , 1, Expr(:call, :⌿, 2, 3))
2496+
2497+
@testset "slurp in assignments" begin
2498+
res = begin x, y, z... = 1:7 end
2499+
@test res == 1:7
2500+
@test x == 1 && y == 2
2501+
@test z == Vector(3:7)
2502+
2503+
res = begin x, y, z... = [1, 2] end
2504+
@test res == [1, 2]
2505+
@test x == 1 && y == 2
2506+
@test z == Int[]
2507+
2508+
x = 1
2509+
res = begin x..., = x end
2510+
@test res == 1
2511+
@test x == 1
2512+
2513+
x, y, z... = 1:7
2514+
res = begin y, z, x... = z..., x, y end
2515+
@test res == ((3:7)..., 1, 2)
2516+
@test y == 3
2517+
@test z == 4
2518+
@test x == ((5:7)..., 1, 2)
2519+
2520+
res = begin x, _, y... = 1, 2 end
2521+
@test res == (1, 2)
2522+
@test x == 1
2523+
@test y == ()
2524+
2525+
res = begin x, y... = 1 end
2526+
@test res == 1
2527+
@test x == 1
2528+
@test y == Iterators.rest(1, nothing)
2529+
2530+
res = begin x, y, z... = 1, 2, 3:5 end
2531+
@test res == (1, 2, 3:5)
2532+
@test x == 1 && y == 2
2533+
@test z == (3:5,)
2534+
2535+
@test Meta.isexpr(Meta.@lower(begin a, b..., c = 1:3 end), :error)
2536+
@test Meta.isexpr(Meta.@lower(begin a, b..., c = 1, 2, 3 end), :error)
2537+
@test Meta.isexpr(Meta.@lower(begin a, b..., c... = 1, 2, 3 end), :error)
2538+
2539+
@test_throws BoundsError begin x, y, z... = 1:1 end
2540+
@test_throws BoundsError begin x, y, _, z... = 1, 2 end
2541+
2542+
car((a, d...)) = a
2543+
cdr((a, d...)) = d
2544+
@test car(1:3) == 1
2545+
@test cdr(1:3) == [2, 3]
2546+
end

0 commit comments

Comments
 (0)