V1020 recursive_lambda
Local lambdas cannot refer to themselves.
An error from the resolve phase. sakura explain V1020 prints this explanation offline.
Local lambdas cannot refer to themselves. Lift the function to the top level, where functions may be mutually recursive.
Wrong:
let fact = fn(n) { if n == 0 { 1 } else { n * fact(n - 1) } }Fixed:
fn fact(n: Int) -> Int { if n == 0 { 1 } else { n * fact(n - 1) } }Wrong program
compiler/tests/ui/V1020_recursive_lambda.sakura, which the compiler's ui suite runs through sakura check:
fn fact_of_five() -> Int {
let fact = fn(n) { if n == 0 { 1 } else { n * fact(n - 1) } }
fact(5)
}sakura check --json reports this record (pretty-printed; $DIR is the fixture's directory):
{
"schema_version": "1.0",
"kind": "diagnostic",
"id": "d:296aa8452551a950",
"code": "V1020",
"severity": "error",
"phase": "resolve",
"message": "local lambda `fact` cannot call itself; lift it to a top-level `fn` (design §4.5)",
"snapshot": "sha256:$SNAPSHOT",
"symbol_id": "main::V1020_recursive_lambda::fact_of_five",
"spans": [
{
"uri": "file://$DIR/V1020_recursive_lambda.sakura",
"content_hash": "sha256:b3195773f224d3690fe85396ada7c0c4b7be8a53a36bdc3cd1e635e61d74760b",
"start_byte": 109,
"end_byte": 113,
"role": "primary"
}
],
"related_ids": [],
"expected": null,
"actual": null,
"obligation": null,
"counterexample": null,
"explanation_uri": "sakura://explain/V1020",
"fixes": []
}Fixed program
compiler/tests/ui/V1020_recursive_lambda.fixed.sakura, which sakura check accepts with no diagnostics:
fn fact(n: Int) -> Int { if n == 0 { 1 } else { n * fact(n - 1) } }
fn fact_of_five() -> Int { fact(5) }