From 8bc30fae43a34cea9fb938a71a8c532c3e9825a5 Mon Sep 17 00:00:00 2001 From: Tobias Mock Date: Thu, 12 Sep 2024 16:49:03 +0200 Subject: [PATCH] Fix detection of recursive calls --- lib/monomorph_tree.ml | 8 +++----- test/functions.t/nested_recursive.smu | 15 +++++++++++++++ test/functions.t/run.t | 6 ++++++ 3 files changed, 24 insertions(+), 5 deletions(-) create mode 100644 test/functions.t/nested_recursive.smu diff --git a/lib/monomorph_tree.ml b/lib/monomorph_tree.ml index 94e45902..0ab1bbc9 100644 --- a/lib/monomorph_tree.ml +++ b/lib/monomorph_tree.ml @@ -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 @@ -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 diff --git a/test/functions.t/nested_recursive.smu b/test/functions.t/nested_recursive.smu new file mode 100644 index 00000000..5e6cb06f --- /dev/null +++ b/test/functions.t/nested_recursive.smu @@ -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) diff --git a/test/functions.t/run.t b/test/functions.t/run.t index 89a548ce..afa91568 100644 --- a/test/functions.t/run.t +++ b/test/functions.t/run.t @@ -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