CPS-cps recursion leads to stack overflow
zevv opened this issue · comments
Ico Doornekamp commented
Can we TCO this somehow?
import cps
type C = ref object of Continuation
proc child() {.cps:C.} =
echo "Child"
proc parent() {.cps:C.} =
echo "Parent 1"
child()
echo "Parent 2"
parent()
var c: C = whelp parent()
while c.running:
c = C(c.fn(c))
...
/tmp/t.nim(9) parent
/home/ico/sandbox/prjs/cps/cps/spec.nim(231) trampoline
/tmp/t.nim(13) Post Call
/tmp/t.nim(9) parent
/home/ico/sandbox/prjs/cps/cps/spec.nim(231) trampoline
/home/ico/sandbox/prjs/cps/cps/transform.nim(902) parent_385876580
/home/ico/sandbox/prjs/cps/cps/transform.nim(884) whelp
/home/ico/sandbox/prjs/cps/cps.nim(210) alloc
/home/ico/external/Nim/lib/system.nim(902) new
/home/ico/external/Nim/lib/system/gc.nim newObj
Error: call depth limit reached in a debug build (2000 function calls). You can change it with -d:nimCallDepthLimit=<int> but really try to avoid deep recursions instead.
alaviss commented
TCO will come, but the issue here is not that. What happened was that since the proc wasn't tagged with any cps data, we don't know that we should perform a jump like it was a cps proc.