Function that calls itself on every path
WarningA function or a method that calls itself on every path before it can return is a warning, and the program still builds. A path ends at the first call to the function itself, or at a way out. The ways out are return, panic, todo, break, continue, a loop that can finish a pass, and the end of the body. The warning marks each call that ends a path. A call to another function does not count, so two functions that call each other do not warn. A function with an error of its own is not checked, so that its error stands alone. There is no fix, because only you know the base case.
Why
A function that calls itself on every path can never return. At run time the calls can use all of the stack, and then the program dies with SIGSEGV (exit code 139), far from the mistake. Sisu does not promise that a tail call runs in constant stack (ADR 0029), so a function that calls itself to repeat forever can crash too. Write a loop for that.
Broken
fn count(n: i64) -> i64 {
1 + count(n - 1)
}
fn main() {
print(count(3))
}
What sisu says
warning[W0010]: function `count` calls itself on every path --> W0010.sisu:1:4 | 1 | fn count(n: i64) -> i64 { | ^^^^^ cannot return without calling itself 2 | 1 + count(n - 1) | ------------ calls itself here | = help: give `count` a path that returns without calling itself, such as a base case; to repeat forever, write a `loop` in its body sisu: 1 warning sisu: for more on a code, run `sisu explain <code>`: W0010
Fixed
fn count(n: i64) -> i64 {
if n == 0 {
return 0
}
1 + count(n - 1)
}
fn main() {
print(count(3))
}
Sisu