Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix #44013: aliasing in property destructuring #44020

Merged
merged 3 commits into from
Feb 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 18 additions & 14 deletions src/julia-syntax.scm
Original file line number Diff line number Diff line change
Expand Up @@ -2118,11 +2118,21 @@
`(call ,@hvncat ,dims ,(tf is-row-first) ,@aflat))
`(call ,@hvncat ,(tuplize shape) ,(tf is-row-first) ,@aflat))))))))

(define (expand-property-destruct lhss x)
(if (not (length= lhss 1))
(error (string "invalid assignment location \"" (deparse lhs) "\"")))
(let* ((xx (if (symbol-like? x) x (make-ssavalue)))
(ini (if (eq? x xx) '() (list (sink-assignment xx (expand-forms x))))))
(define (maybe-ssavalue lhss x in-lhs?)
(cond ((or (and (not (in-lhs? x lhss)) (symbol? x))
(ssavalue? x))
x)
((and (pair? lhss) (vararg? (last lhss))
(eventually-call? (cadr (last lhss))))
(gensy))
(else (make-ssavalue))))

(define (expand-property-destruct lhs x)
(if (not (length= lhs 1))
(error (string "invalid assignment location \"" (deparse `(tuple ,lhs)) "\"")))
simeonschaub marked this conversation as resolved.
Show resolved Hide resolved
(let* ((lhss (cdar lhs))
(xx (maybe-ssavalue lhss x memq))
(ini (if (eq? x xx) '() (list (sink-assignment xx (expand-forms x))))))
`(block
,@ini
,@(map
Expand All @@ -2131,9 +2141,9 @@
((and (pair? field) (eq? (car field) '|::|) (symbol? (cadr field)))
(cadr field))
(else
(error (string "invalid assignment location \"" (deparse lhs) "\""))))))
(error (string "invalid assignment location \"" (deparse `(tuple ,lhs)) "\""))))))
(expand-forms `(= ,field (call (top getproperty) ,xx (quote ,prop))))))
(cdar lhss))
lhss)
(unnecessary ,xx))))

(define (expand-tuple-destruct lhss x)
Expand Down Expand Up @@ -2166,13 +2176,7 @@
((eq? l x) #t)
(else (in-lhs? x (cdr lhss)))))))
;; in-lhs? also checks for invalid syntax, so always call it first
(let* ((xx (cond ((or (and (not (in-lhs? x lhss)) (symbol? x))
(ssavalue? x))
x)
((and (pair? lhss) (vararg? (last lhss))
(eventually-call? (cadr (last lhss))))
(gensy))
(else (make-ssavalue))))
(let* ((xx (maybe-ssavalue lhss x in-lhs?))
(ini (if (eq? x xx) '() (list (sink-assignment xx (expand-forms x)))))
(n (length lhss))
;; skip last assignment if it is an all-underscore vararg
Expand Down
19 changes: 19 additions & 0 deletions test/syntax.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3144,3 +3144,22 @@ end
let ex = :(const $(esc(:x)) = 1; (::typeof(2))() = $(esc(:x)))
@test macroexpand(Main, Expr(:var"hygienic-scope", ex, Main)).args[3].args[1] == :((::$(GlobalRef(Main, :typeof))(2))())
end

struct Foo44013
x
f
end

@testset "issue #44013" begin
f = Foo44013(1, 2)
res = begin (; x, f) = f end
@test res == Foo44013(1, 2)
@test x == 1
@test f == 2

f = Foo44013(1, 2)
res = begin (; f, x) = f end
@test res == Foo44013(1, 2)
@test x == 1
@test f == 2
end