Skip to content

Commit

Permalink
fix #32499, regression in struct expr in assignment rhs (#32512)
Browse files Browse the repository at this point in the history
caused by c6c3d72

This is a bit of a hack that just moves things around a bit to
get a structure more likely to work.

(cherry picked from commit a25e722)
  • Loading branch information
JeffBezanson authored and KristofferC committed Jul 8, 2019
1 parent bd9f411 commit 2e72bca
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 15 deletions.
31 changes: 16 additions & 15 deletions src/julia-syntax.scm
Original file line number Diff line number Diff line change
Expand Up @@ -1794,6 +1794,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 @@ -1882,20 +1890,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 @@ -1909,9 +1910,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 @@ -1933,7 +1934,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 @@ -1965,7 +1966,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 @@ -1864,3 +1864,14 @@ let
a32325(x) = a32325()
end
@test a32325(0) === a32325()

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

0 comments on commit 2e72bca

Please sign in to comment.