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

[Improvement]: Provide code actions to fix invalid access of mutable storage #42331

Closed
nipunayf opened this issue Mar 15, 2024 · 1 comment · Fixed by #42708
Closed

[Improvement]: Provide code actions to fix invalid access of mutable storage #42331

nipunayf opened this issue Mar 15, 2024 · 1 comment · Fixed by #42708
Assignees
Labels
Area/CodeAction Language Server Code Actions Team/LanguageServer Language Server Implementation related issues. #Compiler Type/Improvement
Milestone

Comments

@nipunayf
Copy link
Contributor

nipunayf commented Mar 15, 2024

Description

The fix for the error message invalid access of mutable storage in an 'isolated' function (BCE3943) is decisive based on the user's preference. If the user only wants to read the values, they can make the variable immutable. On the contrary, if they intend to manipulate the data structure in a concurrent-safe manner, they can decide to make the variable isolated. Since both fixes are rather straightforward, the LS can provide CAs as quick fixes.

Describe your problem(s)

No response

Describe your solution(s)

Consider the following source code with the respective diagnostic.

int[] arr = [];

isolated function length() returns int {
    return arr.length();
}

FIX1: Allow the user to make the variable immutable by making it final and/or readonly.

final int[] & readonly arr = []  // changed

isolated function length() returns int {
    return arr.length();
}

FIX2: Allow the user to make the variable isolated when the user knows that they have to manipulate the data structure in a concurrent-safe manner.

isolated int[] arr = []; // changed

isolated function length() returns int {
    return arr.length();
}

Related area

-> Compilation

Related issue(s) (optional)

#28681

Suggested label(s) (optional)

No response

Suggested assignee(s) (optional)

No response

@nipunayf nipunayf added Type/Improvement Team/LanguageServer Language Server Implementation related issues. #Compiler Area/CodeAction Language Server Code Actions labels Mar 15, 2024
@nipunayf nipunayf self-assigned this Mar 15, 2024
@MaryamZi
Copy link
Member

There's also the case of when the type of the variable is a subtype of isolated object {}. Here also. we can suggest adding final. Generally, if the type is a subtype of readonly|isolated object {}, adding final is enough.

isolated class Counter {
    private int current = 1;

    isolated function next() returns int {
        lock {
            int value = self.current;
            self.current += 1;
            return value;
        }
    }
}

Counter counter = new; // make this final

public isolated function main() {
    int _ = counter.next();
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area/CodeAction Language Server Code Actions Team/LanguageServer Language Server Implementation related issues. #Compiler Type/Improvement
Projects
Status: Done
2 participants