Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validation error: Recursion #667

Closed
lars-reimann opened this issue Oct 22, 2023 · 1 comment · Fixed by #783
Closed

Validation error: Recursion #667

lars-reimann opened this issue Oct 22, 2023 · 1 comment · Fixed by #783
Assignees
Labels
enhancement 💡 New feature or request released Included in a release validation ✔️ Improved or new static checks

Comments

@lars-reimann
Copy link
Member

lars-reimann commented Oct 22, 2023

Since we don't have conditional execution, any recursion is endless. We should detect this and show an error. Example tests:

package tests.validation.other.expressions.calls.recursion


pipeline p {
    // $TEST$ error "Recursive calls are not allowed."
    »a«();
    // $TEST$ error "Recursive calls are not allowed."
    »b«();
    // $TEST$ error "Recursive calls are not allowed."
    »c«();
    // $TEST$ no error "Recursive calls are not allowed."
    »d«();
    // $TEST$ no error "Recursive calls are not allowed."
    »f«();
}

segment a() {
    // $TEST$ error "Recursive calls are not allowed."
    »a«();
    // $TEST$ error "Recursive calls are not allowed."
    »b«();
    // $TEST$ error "Recursive calls are not allowed."
    »c«();
    // $TEST$ no error "Recursive calls are not allowed."
    »d«();
    // $TEST$ no error "Recursive calls are not allowed."
    »f«();

    () {
        // $TEST$ error "Recursive calls are not allowed."
        »a«();
        // $TEST$ error "Recursive calls are not allowed."
        »b«();
        // $TEST$ error "Recursive calls are not allowed."
        »c«();
        // $TEST$ no error "Recursive calls are not allowed."
        »d«();
        // $TEST$ no error "Recursive calls are not allowed."
        »f«();
    };

    val lambda1 = () {
        // $TEST$ no error "Recursive calls are not allowed."
        »lambda1«();
    };

    val lambda2 = () {
        // $TEST$ no error "Recursive calls are not allowed."
        »lambda3«();
    };

    val lambda3 = () {
        // $TEST$ no error "Recursive calls are not allowed."
        »lambda2«();
    };
}

segment b() {
    // $TEST$ error "Recursive calls are not allowed."
    »c«();
}

segment c() {
    // $TEST$ error "Recursive calls are not allowed."
    »b«();
}

segment d() {}
package tests.staticAnalysis.recursion

// Positive examples -----------------------------------------------------------

annotation CallsShouldBeRecursive

// Direct recursion

@CallsShouldBeRecursive
step directRecursion(a: Any or directRecursion()) {
    directRecursion();
    1 + directRecursion();
    val a = directRecursion();
}

// Transitive recursion

@CallsShouldBeRecursive
step transitiveRecursion1() {
    transitiveRecursion2();
    val a = transitiveRecursion2();
}

@CallsShouldBeRecursive
step transitiveRecursion2() {
    transitiveRecursion3();
    val a = transitiveRecursion3();
}

@CallsShouldBeRecursive
step transitiveRecursion3() {
    transitiveRecursion2();
    val a = transitiveRecursion2();
}

// Deferred recursion in lambda

@CallsShouldBeRecursive
step deferredRecursionInLambda() {
    (() { directRecursion(); })();
    (() -> directRecursion())();
}

// Negative examples -----------------------------------------------------------

annotation CallsShouldNotBeRecursive

// Normal calls

@CallsShouldNotBeRecursive
step normalCall(f: () -> ()) {
    f();
    (() {})();
    (() -> null)();

    MyClass();
    MyEnum.Variant();
    myFun();
    myStep();
}

class MyClass()
enum MyEnum {
    Variant()
}
fun myFun()
step myStep() {}

// Uncalled lambda

@CallsShouldNotBeRecursive
step uncalledLambda() {
    () { uncalledLambda(); };
    () -> uncalledLambda();
}

// Lambda recursion (already handled by scoping)

@CallsShouldNotBeRecursive
step lambdaRecursion() {
    val a = () { a(); };
    val b = () -> b();
}

// Unresolved callable

@CallsShouldNotBeRecursive
step unresolvedCallable() {
    unresolved();
}

Recursion

Functions as parameters can also lead to recursion:

segment a() {
    b(a);
}

segment b(f: () -> ()) {
    f();
}

This is prevented since function pointers are not allowed. However, it's still possible with lambdas:

segment a() {
    b(() -> a());
}

segment  b(f: () -> ()) {
    f();
}

We could prevent lambdas from calling the containing segment. But it would still be possible to build recursive calls:

segment a() {
    b(() -> c());
}

segment c() {
    a();
}

segment b(f: () -> ()) {
    f();
}

We could generally follow all function pointers, whether they are called or not (or all calls, whether they are contained directly or in a lambda), to detect recursion. Recursion might still happen, however:

segment a() {
    b(() -> b());
}

segment b(f: () -> ()) {
    f();
}

Thus, it seems like we need to remember for a lambda which callables get called if the lambda gets executed.

@lars-reimann lars-reimann changed the title NO_RECURSION Validation error: Recursion Oct 22, 2023
@lars-reimann lars-reimann added the validation ✔️ Improved or new static checks label Oct 22, 2023
@lars-reimann lars-reimann self-assigned this Oct 30, 2023
@lars-reimann lars-reimann added the enhancement 💡 New feature or request label Nov 6, 2023
@lars-reimann lars-reimann added this to the purity-analysis milestone Nov 12, 2023
@lars-reimann lars-reimann linked a pull request Nov 20, 2023 that will close this issue
lars-reimann added a commit that referenced this issue Nov 20, 2023
Closes #667

### Summary of Changes

Show an error if a call leads to infinite recursion.
lars-reimann pushed a commit that referenced this issue Nov 22, 2023
## [0.4.0](v0.3.0...v0.4.0) (2023-11-22)

### Features

* add endless recursion as an impurity reason ([#788](#788)) ([98acdde](98acdde))
* call graph computer (without closures) ([#782](#782)) ([34bf182](34bf182))
* check types of constant parameters ([#775](#775)) ([0a02850](0a02850)), closes [#668](#668)
* check whether purity of callable parameters of functions is set properly ([#777](#777)) ([f8fd907](f8fd907)), closes [#732](#732)
* compute purity/side effects for expressions ([#785](#785)) ([9ed1c08](9ed1c08)), closes [#15](#15)
* compute types of parameters of lambdas that are passed as default value ([#780](#780)) ([01a5c03](01a5c03))
* error if call leads to infinite recursion ([#783](#783)) ([f7eabd8](f7eabd8)), closes [#667](#667)
* error if impure callable is passed to pure parameter ([#792](#792)) ([5536a4a](5536a4a)), closes [#730](#730)
* error if parameter name in impurity reason is invalid ([#772](#772)) ([faa2012](faa2012)), closes [#741](#741)
* error if purity of functions is not specified ([#768](#768)) ([a15b0af](a15b0af)), closes [#731](#731)
* filter statements without effect for code generation ([#786](#786)) ([cd4f2c1](cd4f2c1)), closes [#542](#542)
* improve location of warning about duplicate annotation target ([#771](#771)) ([87d2a48](87d2a48))
* info if `@Pure` annotation is called on parameter of pure function ([#778](#778)) ([c15c70e](c15c70e))
* purity computer ([#784](#784)) ([b09bb3a](b09bb3a))
* remove type parameters from enum variants ([#767](#767)) ([cb6556a](cb6556a)), closes [#766](#766)
* short-circuit `and`, `or`, and `?:` if RHS has no side effects ([#789](#789)) ([9d9f4b7](9d9f4b7)), closes [#15](#15)
* streamline purity information ([#779](#779)) ([75a9e5b](75a9e5b))
* stricter definition of `const` parameters ([#776](#776)) ([73a0d4e](73a0d4e))
* update snippets for functions and methods ([#769](#769)) ([061d3b1](061d3b1))
* validate impurity reasons of overriding methods ([#774](#774)) ([71fc5bd](71fc5bd)), closes [#665](#665)
* warn about duplicate impurity reasons ([#773](#773)) ([8344356](8344356)), closes [#733](#733)
* warn if statement has no effect ([#787](#787)) ([6f45dc4](6f45dc4)), closes [#664](#664)

### Bug Fixes

* signature help for optional parameters ([#793](#793)) ([fd88ce8](fd88ce8)), closes [#791](#791)
* wrong detection of useless statements that call parameters/unknown callables ([#790](#790)) ([a49b4b3](a49b4b3))
* wrong`"assignment/nothing-assigned"` error if RHS calls expression lambda ([#781](#781)) ([b909cb8](b909cb8))
@lars-reimann
Copy link
Member Author

🎉 This issue has been resolved in version 0.4.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@lars-reimann lars-reimann added the released Included in a release label Nov 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement 💡 New feature or request released Included in a release validation ✔️ Improved or new static checks
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

1 participant