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]: Inconsistent runtime error messages when trying to modify a readonly record field #42520

Closed
Thushara-Piyasekara opened this issue Apr 9, 2024 · 2 comments · Fixed by #42521
Assignees
Labels
Reason/EngineeringMistake The issue occurred due to a mistake made in the past. Team/jBallerina All the issues related to BIR, JVM backend code generation and runtime Type/Bug

Comments

@Thushara-Piyasekara
Copy link
Contributor

Description

An error message inconsistency can be observed when keeping and removing an unused function. Since the existence and removal of a dead function should not affect the runtime, this behavior can be considered as inconsistent.

Steps to Reproduce

Run the following code snippet with bal run.

public function main() returns error? {
    check testFrozenAnyArrayElementUpdate();
}

function testFrozenAnyArrayElementUpdate() returns error? {
    Employee e1 = {name: "Em", id: 1000};
    int[] i = [1, 2];
    anydata[] i1 = [i, e1];
    anydata[] i2 = i1.cloneReadOnly();
    Employee e2 = check trap <Employee>i2[1];
    e2["name"] = "Zee";
    return ();
}

function testFrozenTupleUpdate() {
    Employee e1 = {name: "Em", id: 1000};
    [int, Employee] t1 = [1, e1];
    [int, Employee] t2 = t1.cloneReadOnly();
    Employee e2 = {name: "Zee", id: 1200};
    t2[1] = e2;
}

type Employee record {|
    int id;
    string name;
|};

You will get the following error,

error: {ballerina/lang.map}InherentTypeViolation {"message":"cannot update 'readonly' field 'name' in record of type 'test:(thushara_piyasekara/test:0:Employee & readonly)'"}
        at thushara_piyasekara.test.0:testFrozenAnyArrayElementUpdate(helloWorld.bal:27)
           thushara_piyasekara.test.0:main(helloWorld.bal:18)

Modify the source code as follows and run again. You can either comment or delete the lines.

public function main() returns error? {
    check testFrozenAnyArrayElementUpdate();
}

function testFrozenAnyArrayElementUpdate() returns error? {
    Employee e1 = {name: "Em", id: 1000};
    int[] i = [1, 2];
    anydata[] i1 = [i, e1];
    anydata[] i2 = i1.cloneReadOnly();
    Employee e2 = check trap <Employee>i2[1];
    e2["name"] = "Zee";
    return ();
}

// function testFrozenTupleUpdate() {
//     Employee e1 = {name: "Em", id: 1000};
//     [int, Employee] t1 = [1, e1];
//     [int, Employee] t2 = t1.cloneReadOnly();
//     Employee e2 = {name: "Zee", id: 1200};
//     t2[1] = e2;
// }

type Employee record {|
    int id;
    string name;
|};

You will get the following error message,

error: {ballerina/lang.map}InvalidUpdate {"message":"Invalid update of record field: modification not allowed on readonly value"}
        at thushara_piyasekara.test.0:testFrozenAnyArrayElementUpdate(helloWorld.bal:27)
           thushara_piyasekara.test.0:main(helloWorld.bal:18)

Affected Version(s)

Ballerina 2201.9.0 SNAPSHOT

OS, DB, other environment details and versions

No response

Related area

-> Runtime

Related issue(s) (optional)

No response

Suggested label(s) (optional)

No response

Suggested assignee(s) (optional)

No response

@ballerina-bot ballerina-bot added the Team/jBallerina All the issues related to BIR, JVM backend code generation and runtime label Apr 9, 2024
@Thushara-Piyasekara
Copy link
Contributor Author

Turned out this error inconsistency was due to BFields of generated immutable BRecords not being marked with SymbolFlags.READONLY flag inside the ReadOnlyUtils class.

Copy link

github-actions bot commented Oct 4, 2024

This issue is NOT closed with a proper Reason/ label. Make sure to add proper reason label before closing. Please add or leave a comment with the proper reason label now.

      - Reason/EngineeringMistake - The issue occurred due to a mistake made in the past.
      - Reason/Regression - The issue has introduced a regression.
      - Reason/MultipleComponentInteraction - Issue occured due to interactions in multiple components.
      - Reason/Complex - Issue occurred due to complex scenario.
      - Reason/Invalid - Issue is invalid.
      - Reason/Other - None of the above cases.

@gimantha gimantha added the Reason/EngineeringMistake The issue occurred due to a mistake made in the past. label Oct 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Reason/EngineeringMistake The issue occurred due to a mistake made in the past. Team/jBallerina All the issues related to BIR, JVM backend code generation and runtime Type/Bug
Projects
Status: Done
4 participants