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 #32499, regression in struct expr in assignment rhs #32512

Merged
merged 1 commit into from
Jul 8, 2019
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
31 changes: 16 additions & 15 deletions src/julia-syntax.scm
Original file line number Diff line number Diff line change
Expand Up @@ -1801,6 +1801,14 @@
(else
(error (string "invalid " syntax-str " \"" (deparse el) "\""))))))))

;; move an assignment into the last statement of a block to keep more statements at top level
(define (sink-assignment lhs rhs)
(if (and (pair? rhs) (eq? (car rhs) 'block))
(let ((rr (reverse (cdr rhs))))
`(block ,@(reverse (cdr rr))
(= ,lhs ,(car rr))))
`(= ,lhs ,rhs)))

(define (expand-forms e)
(if (or (atom? e) (memq (car e) '(quote inert top core globalref outerref line module toplevel ssavalue null meta using import export)))
e
Expand Down Expand Up @@ -1889,20 +1897,13 @@
,@(map (lambda (l) `(= ,l ,rr))
lhss)
(unnecessary ,rr)))))))
((and (symbol-like? lhs) (valid-name? lhs))
`(= ,lhs ,(expand-forms (caddr e))))
((or (and (symbol-like? lhs) (valid-name? lhs))
(globalref? lhs))
(sink-assignment lhs (expand-forms (caddr e))))
((atom? lhs)
(error (string "invalid assignment location \"" (deparse lhs) "\"")))
(else
(case (car lhs)
((globalref)
;; M.b =
(let* ((rhs (caddr e))
(rr (if (or (symbol-like? rhs) (atom? rhs)) rhs (make-ssavalue))))
`(block
,.(if (eq? rr rhs) '() `((= ,rr ,(expand-forms rhs))))
(= ,lhs ,rr)
(unnecessary ,rr))))
((|.|)
;; a.b =
(let* ((a (cadr lhs))
Expand All @@ -1916,9 +1917,9 @@
b (make-ssavalue)))
(rr (if (or (symbol-like? rhs) (atom? rhs)) rhs (make-ssavalue))))
`(block
,.(if (eq? aa a) '() `((= ,aa ,(expand-forms a))))
,.(if (eq? bb b) '() `((= ,bb ,(expand-forms b))))
,.(if (eq? rr rhs) '() `((= ,rr ,(expand-forms rhs))))
,.(if (eq? aa a) '() (list (sink-assignment aa (expand-forms a))))
,.(if (eq? bb b) '() (list (sink-assignment bb (expand-forms b))))
,.(if (eq? rr rhs) '() (list (sink-assignment rr (expand-forms rhs))))
(call (top setproperty!) ,aa ,bb ,rr)
(unnecessary ,rr)))))
((tuple)
Expand All @@ -1940,7 +1941,7 @@
(let* ((xx (if (or (and (symbol? x) (not (memq x lhss)))
(ssavalue? x))
x (make-ssavalue)))
(ini (if (eq? x xx) '() `((= ,xx ,(expand-forms x)))))
(ini (if (eq? x xx) '() (list (sink-assignment xx (expand-forms x)))))
(n (length lhss))
(st (gensy)))
`(block
Expand Down Expand Up @@ -1972,7 +1973,7 @@
(stmts (if reuse `((= ,arr ,(expand-forms a))) '()))
(rrhs (and (pair? rhs) (not (ssavalue? rhs)) (not (quoted? rhs))))
(r (if rrhs (make-ssavalue) rhs))
(rini (if rrhs `((= ,r ,(expand-forms rhs))) '())))
(rini (if rrhs (list (sink-assignment r (expand-forms rhs))) '())))
(receive
(new-idxs stuff) (process-indices arr idxs)
`(block
Expand Down
11 changes: 11 additions & 0 deletions test/syntax.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1878,3 +1878,14 @@ let f = identity(identity() do
end)
@test f() == 3
end

# issue #32499
x32499 = begin
struct S32499
function S32499(; x=1)
x
end
end
S32499(x=2)
end
@test x32499 == 2