Skip to content

Commit a25e722

Browse files
JeffBezansonKristofferC
authored andcommitted
fix #32499, regression in struct expr in assignment rhs (#32512)
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.
1 parent d611e1c commit a25e722

File tree

2 files changed

+27
-15
lines changed

2 files changed

+27
-15
lines changed

src/julia-syntax.scm

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1801,6 +1801,14 @@
18011801
(else
18021802
(error (string "invalid " syntax-str " \"" (deparse el) "\""))))))))
18031803

1804+
;; move an assignment into the last statement of a block to keep more statements at top level
1805+
(define (sink-assignment lhs rhs)
1806+
(if (and (pair? rhs) (eq? (car rhs) 'block))
1807+
(let ((rr (reverse (cdr rhs))))
1808+
`(block ,@(reverse (cdr rr))
1809+
(= ,lhs ,(car rr))))
1810+
`(= ,lhs ,rhs)))
1811+
18041812
(define (expand-forms e)
18051813
(if (or (atom? e) (memq (car e) '(quote inert top core globalref outerref line module toplevel ssavalue null meta using import export)))
18061814
e
@@ -1889,20 +1897,13 @@
18891897
,@(map (lambda (l) `(= ,l ,rr))
18901898
lhss)
18911899
(unnecessary ,rr)))))))
1892-
((and (symbol-like? lhs) (valid-name? lhs))
1893-
`(= ,lhs ,(expand-forms (caddr e))))
1900+
((or (and (symbol-like? lhs) (valid-name? lhs))
1901+
(globalref? lhs))
1902+
(sink-assignment lhs (expand-forms (caddr e))))
18941903
((atom? lhs)
18951904
(error (string "invalid assignment location \"" (deparse lhs) "\"")))
18961905
(else
18971906
(case (car lhs)
1898-
((globalref)
1899-
;; M.b =
1900-
(let* ((rhs (caddr e))
1901-
(rr (if (or (symbol-like? rhs) (atom? rhs)) rhs (make-ssavalue))))
1902-
`(block
1903-
,.(if (eq? rr rhs) '() `((= ,rr ,(expand-forms rhs))))
1904-
(= ,lhs ,rr)
1905-
(unnecessary ,rr))))
19061907
((|.|)
19071908
;; a.b =
19081909
(let* ((a (cadr lhs))
@@ -1916,9 +1917,9 @@
19161917
b (make-ssavalue)))
19171918
(rr (if (or (symbol-like? rhs) (atom? rhs)) rhs (make-ssavalue))))
19181919
`(block
1919-
,.(if (eq? aa a) '() `((= ,aa ,(expand-forms a))))
1920-
,.(if (eq? bb b) '() `((= ,bb ,(expand-forms b))))
1921-
,.(if (eq? rr rhs) '() `((= ,rr ,(expand-forms rhs))))
1920+
,.(if (eq? aa a) '() (list (sink-assignment aa (expand-forms a))))
1921+
,.(if (eq? bb b) '() (list (sink-assignment bb (expand-forms b))))
1922+
,.(if (eq? rr rhs) '() (list (sink-assignment rr (expand-forms rhs))))
19221923
(call (top setproperty!) ,aa ,bb ,rr)
19231924
(unnecessary ,rr)))))
19241925
((tuple)
@@ -1940,7 +1941,7 @@
19401941
(let* ((xx (if (or (and (symbol? x) (not (memq x lhss)))
19411942
(ssavalue? x))
19421943
x (make-ssavalue)))
1943-
(ini (if (eq? x xx) '() `((= ,xx ,(expand-forms x)))))
1944+
(ini (if (eq? x xx) '() (list (sink-assignment xx (expand-forms x)))))
19441945
(n (length lhss))
19451946
(st (gensy)))
19461947
`(block
@@ -1972,7 +1973,7 @@
19721973
(stmts (if reuse `((= ,arr ,(expand-forms a))) '()))
19731974
(rrhs (and (pair? rhs) (not (ssavalue? rhs)) (not (quoted? rhs))))
19741975
(r (if rrhs (make-ssavalue) rhs))
1975-
(rini (if rrhs `((= ,r ,(expand-forms rhs))) '())))
1976+
(rini (if rrhs (list (sink-assignment r (expand-forms rhs))) '())))
19761977
(receive
19771978
(new-idxs stuff) (process-indices arr idxs)
19781979
`(block

test/syntax.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1878,3 +1878,14 @@ let f = identity(identity() do
18781878
end)
18791879
@test f() == 3
18801880
end
1881+
1882+
# issue #32499
1883+
x32499 = begin
1884+
struct S32499
1885+
function S32499(; x=1)
1886+
x
1887+
end
1888+
end
1889+
S32499(x=2)
1890+
end
1891+
@test x32499 == 2

0 commit comments

Comments
 (0)