Skip to content

Commit 0b521f0

Browse files
committed
fix #43960, evaluation order of splat inside ref
1 parent cf8338a commit 0b521f0

File tree

2 files changed

+31
-25
lines changed

2 files changed

+31
-25
lines changed

src/julia-syntax.scm

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -130,31 +130,29 @@
130130
;; returns (values index-list stmts) where stmts are statements that need
131131
;; to execute first.
132132
(define (process-indices a i)
133-
(let loop ((lst i)
134-
(n 1)
135-
(stmts '())
136-
(tuples '())
137-
(ret '()))
138-
(if (null? lst)
139-
(values (reverse ret) (reverse stmts))
140-
(let ((idx (car lst))
141-
(last (null? (cdr lst))))
142-
(if (and (pair? idx) (eq? (car idx) '...))
143-
(if (symbol-like? (cadr idx))
144-
(loop (cdr lst) (+ n 1)
145-
stmts
146-
(cons (cadr idx) tuples)
147-
(cons `(... ,(replace-beginend (cadr idx) a n tuples last))
148-
ret))
149-
(let ((g (make-ssavalue)))
150-
(loop (cdr lst) (+ n 1)
151-
(cons `(= ,g ,(replace-beginend (cadr idx) a n tuples last))
152-
stmts)
153-
(cons g tuples)
154-
(cons `(... ,g) ret))))
155-
(loop (cdr lst) (+ n 1)
156-
stmts tuples
157-
(cons (replace-beginend idx a n tuples last) ret)))))))
133+
(let* ((ex `(ref ,a ,@i))
134+
(r-a-s-e (if (any vararg? i)
135+
(remove-argument-side-effects ex)
136+
(cons ex '())))
137+
(a (cadr (car r-a-s-e)))
138+
(i (cddr (car r-a-s-e)))
139+
(stmts (cdr r-a-s-e)))
140+
(let loop ((lst i)
141+
(n 1)
142+
(tuples '())
143+
(ret '()))
144+
(if (null? lst)
145+
(values (reverse ret) stmts)
146+
(let ((idx (car lst))
147+
(last (null? (cdr lst))))
148+
(if (vararg? idx)
149+
(loop (cdr lst) (+ n 1)
150+
(cons (cadr idx) tuples)
151+
(cons `(... ,(replace-beginend (cadr idx) a n tuples last))
152+
ret))
153+
(loop (cdr lst) (+ n 1)
154+
tuples
155+
(cons (replace-beginend idx a n tuples last) ret))))))))
158156

159157
;; GF method does not need to keep decl expressions on lambda args
160158
;; except for rest arg

test/syntax.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1204,6 +1204,14 @@ end
12041204
@test [(0,0)... 1] == [0 0 1]
12051205
@test Float32[(0,0)... 1] == Float32[0 0 1]
12061206

1207+
# issue #43960, evaluation order of splatting in `ref`
1208+
let a = [], b = [4,3,2,1]
1209+
f() = (push!(a, 1); 2)
1210+
g() = (push!(a, 2); ())
1211+
@test b[f(), g()...] == 3
1212+
@test a == [1,2]
1213+
end
1214+
12071215
@testset "raw_str macro" begin
12081216
@test raw"$" == "\$"
12091217
@test raw"\n" == "\\n"

0 commit comments

Comments
 (0)