Skip to content

Commit

Permalink
fix bug in let when a global var is both shadowed and used in an RHS (
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffBezanson committed Feb 9, 2021
1 parent 340734f commit bc4e207
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 39 deletions.
44 changes: 14 additions & 30 deletions src/julia-syntax.scm
Original file line number Diff line number Diff line change
Expand Up @@ -1221,41 +1221,25 @@
(decl? (cadar binds)))
(let ((vname (decl-var (cadar binds))))
(loop (cdr binds)
(if (expr-contains-eq vname (caddar binds))
(let ((tmp (make-ssavalue)))
`(scope-block
(block ,@hs
(= ,tmp ,(caddar binds))
(scope-block
(block
(local-def ,(cadar binds))
(= ,vname ,tmp)
,blk)))))
`(scope-block
(block ,@hs
(local-def ,(cadar binds))
(= ,vname ,(caddar binds))
,blk))))))
(let ((tmp (make-ssavalue)))
`(block (= ,tmp ,(caddar binds))
(scope-block
(block ,@hs
(local-def ,(cadar binds))
(= ,vname ,tmp)
,blk)))))))
;; (a, b, c, ...) = rhs
((and (pair? (cadar binds))
(eq? (caadar binds) 'tuple))
(let ((vars (lhs-vars (cadar binds))))
(loop (cdr binds)
(if (expr-contains-p (lambda (x) (memq x vars)) (caddr (car binds)))
;; use more careful lowering if there are name conflicts. issue #25652
(let ((temp (make-ssavalue)))
`(block
(= ,temp ,(caddr (car binds)))
(scope-block
(block ,@hs
,@(map (lambda (v) `(local-def ,v)) vars)
(= ,(cadr (car binds)) ,temp)
,blk))))
`(scope-block
(block ,@hs
,@(map (lambda (v) `(local-def ,v)) vars)
,(car binds)
,blk))))))
(let ((tmp (make-ssavalue)))
`(block (= ,tmp ,(caddar binds))
(scope-block
(block ,@hs
,@(map (lambda (v) `(local-def ,v)) vars)
(= ,(cadar binds) ,tmp)
,blk)))))))
(else (error "invalid let syntax"))))
(else (error "invalid let syntax")))))))))

Expand Down
9 changes: 0 additions & 9 deletions test/core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4124,15 +4124,6 @@ let ex = quote
@test ex.args[2] == :test
end

# issue #25652
x25652 = 1
x25652_2 = let (x25652, _) = (x25652, nothing)
x25652 = x25652 + 1
x25652
end
@test x25652_2 == 2
@test x25652 == 1

# issue #15180
function f15180(x::T) where T
X = Vector{T}(undef, 1)
Expand Down
18 changes: 18 additions & 0 deletions test/syntax.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2678,3 +2678,21 @@ end
@test f((b=3, c=2, a=4)) == (4, 3)
@test_throws ErrorException f((;))
end

# issue #25652
x25652 = 1
x25652_2 = let (x25652, _) = (x25652, nothing)
x25652 = x25652 + 1
x25652
end
@test x25652_2 == 2
@test x25652 == 1

@test let x = x25652
x25652 = x+3
x25652
end == 4
@test let (x,) = (x25652,)
x25652 = x+3
x25652
end == 4

0 comments on commit bc4e207

Please sign in to comment.