Skip to content

Commit 2e6d908

Browse files
authored
Merge pull request #23561 from JuliaLang/jb/fix23558
fix #23558, bug in recursive let-bound functions
2 parents d0bae2b + aee1aab commit 2e6d908

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

src/julia-syntax.scm

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1144,23 +1144,24 @@
11441144
;; some kind of assignment
11451145
(cond
11461146
((eventually-call? (cadar binds))
1147-
;; f()=c
1147+
;; f() = c
11481148
(let ((asgn (butlast (expand-forms (car binds))))
11491149
(name (assigned-name (cadar binds))))
11501150
(if (not (symbol? name))
11511151
(error "invalid let syntax"))
11521152
(loop (cdr binds)
11531153
`(scope-block
11541154
(block
1155-
(local-def ,name)
1155+
,(if (expr-contains-eq name (caddar binds))
1156+
`(local ,name) ;; might need a Box for recursive functions
1157+
`(local-def ,name))
11561158
,asgn
11571159
,blk)))))
11581160
((or (symbol? (cadar binds))
11591161
(decl? (cadar binds)))
11601162
(let ((vname (decl-var (cadar binds))))
11611163
(loop (cdr binds)
1162-
(if (contains (lambda (x) (eq? x vname))
1163-
(caddar binds))
1164+
(if (expr-contains-eq vname (caddar binds))
11641165
(let ((tmp (make-ssavalue)))
11651166
`(scope-block
11661167
(block (= ,tmp ,(caddar binds))

test/core.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,13 @@ let f = i18408()
328328
@test_throws UndefRefError f(0)
329329
end
330330

331+
# issue #23558
332+
c23558(n,k) =
333+
let fact(n) = if (n == 0) 1 else n*fact(n-1) end
334+
fact(n)/fact(k)/fact(n-k)
335+
end
336+
@test c23558(10, 5) == 252
337+
331338
# variable scope, globals
332339
glob_x = 23
333340
function glotest()

0 commit comments

Comments
 (0)