Skip to content

Commit 28ff641

Browse files
no call to Base.rest if vararg is all-underscore (#38403)
* no call to Base.rest if vararg is all-underscore * Apply suggestions from code review Co-authored-by: Jeff Bezanson <[email protected]> * small cleanup Co-authored-by: Jeff Bezanson <[email protected]>
1 parent 3dc54a2 commit 28ff641

File tree

2 files changed

+24
-9
lines changed

2 files changed

+24
-9
lines changed

src/julia-syntax.scm

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2112,20 +2112,27 @@
21122112
x (make-ssavalue)))
21132113
(ini (if (eq? x xx) '() (list (sink-assignment xx (expand-forms x)))))
21142114
(n (length lhss))
2115+
;; skip last assignment if it is an all-underscore vararg
2116+
(n (if (> n 0)
2117+
(let ((l (last lhss)))
2118+
(if (and (vararg? l) (underscore-symbol? (cadr l)))
2119+
(- n 1)
2120+
n))
2121+
n))
21152122
(st (gensy)))
21162123
`(block
2117-
(local ,st)
2124+
,@(if (> n 0) `((local ,st)) '())
21182125
,@ini
2119-
,.(map (lambda (i lhs)
2126+
,@(map (lambda (i lhs)
21202127
(expand-forms
2121-
(if (and (pair? lhs) (eq? (car lhs) '|...|))
2122-
`(= ,(cadr lhs) (call (top rest) ,xx ,.(if (eq? i 0) '() `(,st))))
2128+
(if (vararg? lhs)
2129+
`(= ,(cadr lhs) (call (top rest) ,xx ,@(if (eq? i 0) '() `(,st))))
21232130
(lower-tuple-assignment
2124-
(if (= i (- n 1))
2125-
(list lhs)
2126-
(list lhs st))
2127-
`(call (top indexed_iterate)
2128-
,xx ,(+ i 1) ,.(if (eq? i 0) '() `(,st)))))))
2131+
(if (= i (- n 1))
2132+
(list lhs)
2133+
(list lhs st))
2134+
`(call (top indexed_iterate)
2135+
,xx ,(+ i 1) ,@(if (eq? i 0) '() `(,st)))))))
21292136
(iota n)
21302137
lhss)
21312138
(unnecessary ,xx)))))))

test/syntax.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2613,3 +2613,11 @@ end
26132613
end
26142614

26152615
@test eval(Expr(:if, Expr(:block, Expr(:&&, true, Expr(:call, :(===), 1, 1))), 1, 2)) == 1
2616+
2617+
@testset "all-underscore varargs on the rhs" begin
2618+
@test ncalls_in_lowered(quote _..., = a end, GlobalRef(Base, :rest)) == 0
2619+
@test ncalls_in_lowered(quote ___..., = a end, GlobalRef(Base, :rest)) == 0
2620+
@test ncalls_in_lowered(quote a, _... = b end, GlobalRef(Base, :rest)) == 0
2621+
@test ncalls_in_lowered(quote a, _... = b, c end, GlobalRef(Base, :rest)) == 0
2622+
@test ncalls_in_lowered(quote a, _... = (b...,) end, GlobalRef(Base, :rest)) == 0
2623+
end

0 commit comments

Comments
 (0)