Skip to content

Commit

Permalink
Fix detection of recursive calls
Browse files Browse the repository at this point in the history
  • Loading branch information
tjammer committed Sep 12, 2024
1 parent 441fc83 commit 8bc30fa
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
8 changes: 3 additions & 5 deletions lib/monomorph_tree.ml
Original file line number Diff line number Diff line change
Expand Up @@ -390,8 +390,8 @@ let set_tailrec p name =
(* We have to check the name (of the function) here, because a nested function
could call recursively its parent *)
| (nm, _) :: tl when String.equal name nm ->
{ p with recursion_stack = (nm, Rtail) :: tl }
| _ :: _ -> p
(true, { p with recursion_stack = (nm, Rtail) :: tl })
| _ :: _ -> (false, p)
| [] -> failwith "Internal Error: Recursion stack empty (set)"

let set_upward = function
Expand Down Expand Up @@ -1172,9 +1172,7 @@ and morph_app mk p callee args ret_typ =
let tailrec, p =
if ret then
match callee.monomorph with
| Recursive name ->
let p = set_tailrec p name.nonmono in
(true, p)
| Recursive name -> set_tailrec p name.nonmono
| _ -> (false, p)
else (false, p)
in
Expand Down
15 changes: 15 additions & 0 deletions test/functions.t/nested_recursive.smu
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
let arr = [("heya", 1)]

fun rec consume(storage, opt) {
match storage.[opt] {
#none: println("none")
#some(arr): {
array/iter(arr, fun ((str, expr)) {
println(str)
consume(storage, expr)
})
}
}
}

consume([#some(copy(arr)), #none], 0)
6 changes: 6 additions & 0 deletions test/functions.t/run.t
Original file line number Diff line number Diff line change
Expand Up @@ -2177,3 +2177,9 @@ Upward closures are moved closures
on iteration: 7
on iteration: 8
on iteration: 9

Only direct recursive calls count as recursive
$ schmu nested_recursive.smu
$ valgrind -q --leak-check=yes --show-reachable=yes ./nested_recursive
heya
none

0 comments on commit 8bc30fa

Please sign in to comment.