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.
  • Loading branch information
JeffBezanson authored and KristofferC committed Jul 8, 2019
1 parent d611e1c commit a25e722
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 @@ -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

0 comments on commit a25e722

Please sign in to comment.