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

[Bug]: Possible isolated analysis bug when using alternate wait on workers #41661

Closed
sameerajayasoma opened this issue Nov 7, 2023 · 3 comments
Assignees
Labels
Area/Compiler Reason/Other None of the other reasons. Team/CompilerFE All issues related to Language implementation and Compiler, this exclude run times. Type/Bug userCategory/Compilation

Comments

@sameerajayasoma
Copy link
Contributor

sameerajayasoma commented Nov 7, 2023

Description

The compiler incorrectly exits with an error when I use alternate wait with named workers.

Steps to Reproduce

Sample code

isolated function foo() returns string|error {
    worker A returns string {
        return "Hello from A";
    }

    worker B returns string {
        return "Hello from B";
    }

    worker C {
        string c = wait A | B; // line 11
        c -> function;
    }

    return <- C;
}

bal build output

Compiling source
        sameera/hello_world:0.1.0
ERROR [main.bal:(11:25,11:26)] invalid access of mutable storage in an 'isolated' function
ERROR [main.bal:(11:29,11:30)] invalid access of mutable storage in an 'isolated' function
error: compilation contains errors

Affected Version(s)

No response

OS, DB, other environment details and versions

No response

Related area

-> Compilation

Related issue(s) (optional)

No response

Suggested label(s) (optional)

No response

Suggested assignee(s) (optional)

No response

@hasithaa
Copy link
Contributor

hasithaa commented Nov 8, 2023

I suspect worker references are not treated as isolated variables. In the implementation, each worker is desuggered into a top-level function. So for the wait action, those variable references are global, which results mentioned error. This needs to be fixed from the compiler side.

@MaryamZi
Copy link
Member

MaryamZi commented Nov 9, 2023

Same with single wait.

This actually isn't allowed since the reference to A (and/or B) is considered a captured variable and the type is not a subtype of readonly|isolated object {}. I think the current behaviour is correct and in-line with the spec.

https://ballerina.io/spec/lang/master/#isolated_functions

A variable reference to a captured variable must refer to variables that are both final (either explicitly or implicitly) and have a static type that is a subtype of readonly|isolated object {}, where a variable reference is captured if it occurs within an anonymous-function-expr or named-worker-decl and refers to a variable declared within the function but outside of that anonymous-function-expr or named-worker-decl.

A, which is treated as a captured variable, is considered final, but the type is not a subtype of readonly|isolated object {}.

Similar to

isolated function foo() returns string|error {
    final future<string> A = start fn(); // final, but not immutable or an isolated object

    worker C {
        string c = checkpanic wait A; // also invalid access error for `A`
        c -> function;
    }

    return <- C;
}


isolated function fn() returns string => "Hello from A";

@MaryamZi MaryamZi added Team/CompilerFE All issues related to Language implementation and Compiler, this exclude run times. and removed needTriage The issue has to be inspected and labeled manually labels Nov 9, 2023
@MaryamZi MaryamZi added the Reason/Other None of the other reasons. label Aug 30, 2024
@MaryamZi
Copy link
Member

Closing this issue since the current behaviour is in line with the spec, and there's a spec issue (ballerina-platform/ballerina-spec#1275) to check on the possibility of allowing this.

@MaryamZi MaryamZi closed this as not planned Won't fix, can't repro, duplicate, stale Aug 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area/Compiler Reason/Other None of the other reasons. Team/CompilerFE All issues related to Language implementation and Compiler, this exclude run times. Type/Bug userCategory/Compilation
Projects
None yet
Development

No branches or pull requests

4 participants