Skip to content

Commit f9f2300

Browse files
simeonschaubKristofferC
authored andcommitted
fix #44013: aliasing in property destructuring (#44020)
(cherry picked from commit 60a811c)
1 parent e4e5b2d commit f9f2300

File tree

2 files changed

+37
-14
lines changed

2 files changed

+37
-14
lines changed

src/julia-syntax.scm

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2105,11 +2105,21 @@
21052105
`(call ,@hvncat ,dims ,(tf is-row-first) ,@aflat))
21062106
`(call ,@hvncat ,(tuplize shape) ,(tf is-row-first) ,@aflat))))))))
21072107

2108-
(define (expand-property-destruct lhss x)
2109-
(if (not (length= lhss 1))
2110-
(error (string "invalid assignment location \"" (deparse lhs) "\"")))
2111-
(let* ((xx (if (symbol-like? x) x (make-ssavalue)))
2112-
(ini (if (eq? x xx) '() (list (sink-assignment xx (expand-forms x))))))
2108+
(define (maybe-ssavalue lhss x in-lhs?)
2109+
(cond ((or (and (not (in-lhs? x lhss)) (symbol? x))
2110+
(ssavalue? x))
2111+
x)
2112+
((and (pair? lhss) (vararg? (last lhss))
2113+
(eventually-call? (cadr (last lhss))))
2114+
(gensy))
2115+
(else (make-ssavalue))))
2116+
2117+
(define (expand-property-destruct lhs x)
2118+
(if (not (length= lhs 1))
2119+
(error (string "invalid assignment location \"" (deparse `(tuple ,lhs)) "\"")))
2120+
(let* ((lhss (cdar lhs))
2121+
(xx (maybe-ssavalue lhss x memq))
2122+
(ini (if (eq? x xx) '() (list (sink-assignment xx (expand-forms x))))))
21132123
`(block
21142124
,@ini
21152125
,@(map
@@ -2118,9 +2128,9 @@
21182128
((and (pair? field) (eq? (car field) '|::|) (symbol? (cadr field)))
21192129
(cadr field))
21202130
(else
2121-
(error (string "invalid assignment location \"" (deparse lhs) "\""))))))
2131+
(error (string "invalid assignment location \"" (deparse `(tuple ,lhs)) "\""))))))
21222132
(expand-forms `(= ,field (call (top getproperty) ,xx (quote ,prop))))))
2123-
(cdar lhss))
2133+
lhss)
21242134
(unnecessary ,xx))))
21252135

21262136
(define (expand-tuple-destruct lhss x)
@@ -2153,13 +2163,7 @@
21532163
((eq? l x) #t)
21542164
(else (in-lhs? x (cdr lhss)))))))
21552165
;; in-lhs? also checks for invalid syntax, so always call it first
2156-
(let* ((xx (cond ((or (and (not (in-lhs? x lhss)) (symbol? x))
2157-
(ssavalue? x))
2158-
x)
2159-
((and (pair? lhss) (vararg? (last lhss))
2160-
(eventually-call? (cadr (last lhss))))
2161-
(gensy))
2162-
(else (make-ssavalue))))
2166+
(let* ((xx (maybe-ssavalue lhss x in-lhs?))
21632167
(ini (if (eq? x xx) '() (list (sink-assignment xx (expand-forms x)))))
21642168
(n (length lhss))
21652169
;; skip last assignment if it is an all-underscore vararg

test/syntax.jl

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3012,3 +3012,22 @@ end
30123012

30133013
@generated g25678(x) = return :x
30143014
@test g25678(7) === 7
3015+
3016+
struct Foo44013
3017+
x
3018+
f
3019+
end
3020+
3021+
@testset "issue #44013" begin
3022+
f = Foo44013(1, 2)
3023+
res = begin (; x, f) = f end
3024+
@test res == Foo44013(1, 2)
3025+
@test x == 1
3026+
@test f == 2
3027+
3028+
f = Foo44013(1, 2)
3029+
res = begin (; f, x) = f end
3030+
@test res == Foo44013(1, 2)
3031+
@test x == 1
3032+
@test f == 2
3033+
end

0 commit comments

Comments
 (0)