Skip to content

Commit

Permalink
add error message for attempting to capture a local variable as a clo…
Browse files Browse the repository at this point in the history
…sure type signature (JuliaLang#23527)

fix JuliaLang#18730
  • Loading branch information
vtjnash committed Sep 5, 2017
1 parent 54b99d0 commit 0f7ca71
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/julia-syntax.scm
Original file line number Diff line number Diff line change
Expand Up @@ -3261,13 +3261,18 @@ f(x) = yt(x)
(capt-vars (diff all-capt-vars capt-sp)) ; remove capt-sp from capt-vars
(find-locals-in-method-sig (lambda (methdef)
(expr-find-all
(lambda (e) (and (pair? e) (eq? (car e) 'outerref)
(let ((s (cadr e)))
(lambda (e) (and (or (symbol? e) (and (pair? e) (eq? (car e) 'outerref)))
(let ((s (if (symbol? e) e (cadr e))))
(and (symbol? s)
(not (eq? name s))
(not (memq s capt-sp))
(or ;(local? s) ; TODO: error for local variables
(memq s (lam:sp lam)))))))
(if (and (local? s) (length> (lam:args lam) 0))
; error for local variables except in toplevel thunks
(error (string "local variable " s
" cannot be used in closure declaration"))
#t)
; allow captured variables
(memq s (lam:sp lam))))))
(caddr methdef)
(lambda (e) (cadr e)))))
(sig-locals (simple-sort
Expand Down
8 changes: 8 additions & 0 deletions test/parse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1319,3 +1319,11 @@ let
@test f() == 0
@test f(2) == 2
end

# issue #18730
@test expand(Main, quote
function f()
local Int
x::Int -> 2
end
end) == Expr(:error, "local variable Int cannot be used in closure declaration")

0 comments on commit 0f7ca71

Please sign in to comment.