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

Can't use self of a readonly or isolated object as a captured variable in an isolated function #40296

Closed
hasithaa opened this issue Apr 25, 2023 · 3 comments · Fixed by #40336
Assignees
Labels
Lang/ClassDefinition issues related to classes Lang/ModuleVarDecl/Isolated issues related to isolated variables Lang/Readonly Type `readonly` related issues Team/CompilerFE All issues related to Language implementation and Compiler, this exclude run times. Type/Task
Milestone

Comments

@hasithaa
Copy link
Contributor

Description

The following program gives a compile time error.

import ballerina/io;

readonly class Test {
    
    string[] words;
    int length;

    isolated function init(string[] & readonly words, int length) {
        self.words = words;
        self.length = length;
    }

    isolated function getCount() returns int {
       // An Error at self.length.
        return self.words.filter(  isolated function (string word) returns boolean  =>  word.length() == self.length ).length();
    }
}

public function main() {
    string[] words = ["hello", "world", "this", "is", "a", "test"];
    Test test = new Test(words.cloneReadOnly(), 4);
    int count = test.getCount();
    io:println(count);
}

But it should work.

Workaround: Assign the affected field locally and pass that to the closure.

Steps to Reproduce

No response

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 hasithaa added Type/Bug Team/CompilerFE All issues related to Language implementation and Compiler, this exclude run times. Lang/Readonly Type `readonly` related issues Lang/ClassDefinition issues related to classes Lang/ModuleVarDecl/Isolated issues related to isolated variables labels Apr 25, 2023
@MaryamZi
Copy link
Member

I believe this is because self isn't considered final. The following seems to be allowed atm too, so the error in the isolated function is correct atm (because it's not safe).

readonly class Class {
    int i;

    function init(int i) {
        self.i = i;
    }

    function updateSelf() {
        self = new (2); // assigns to self
    }
}

The spec doesn't seem to mention this, will create an issue.

@MaryamZi
Copy link
Member

Spec issue - ballerina-platform/ballerina-spec#1235

@MaryamZi
Copy link
Member

MaryamZi commented Apr 27, 2023

This should get fixed once self is marked as implicitly final as finalized in the spec. Changing the type since this isn't quite a bug given self isn't final atm.

Issue to make self implicitly final - #40308

@MaryamZi MaryamZi self-assigned this Apr 27, 2023
@MaryamZi MaryamZi changed the title [Bug]: Isolate analysis failed to identify self as a readonly object. Can't use self of a readonly or isolated object as a captured variable in an isolated function Apr 27, 2023
@MaryamZi MaryamZi added this to the 2201.6.0 milestone May 2, 2023
@MaryamZi MaryamZi mentioned this issue May 2, 2023
13 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Lang/ClassDefinition issues related to classes Lang/ModuleVarDecl/Isolated issues related to isolated variables Lang/Readonly Type `readonly` related issues Team/CompilerFE All issues related to Language implementation and Compiler, this exclude run times. Type/Task
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants