From 7981b768a00a0fac0aa9113edff23e0f800d2c0a Mon Sep 17 00:00:00 2001 From: Thushara-Piyasekara Date: Thu, 18 Apr 2024 09:56:04 +0530 Subject: [PATCH 1/3] Fix invokable type fields not inheriting readonly --- .../wso2/ballerinalang/compiler/util/TypeDefBuilderHelper.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/util/TypeDefBuilderHelper.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/util/TypeDefBuilderHelper.java index 857710fbdb95..a388e98ae5d3 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/util/TypeDefBuilderHelper.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/util/TypeDefBuilderHelper.java @@ -327,7 +327,7 @@ public static void populateStructureFieldsAndTypeInclusions(Types types, SymbolT invokableSymbol.params = tsymbol.params == null ? null : new ArrayList<>(tsymbol.params); invokableSymbol.restParam = tsymbol.restParam; invokableSymbol.retType = tsymbol.returnType; - invokableSymbol.flags = tsymbol.flags; + invokableSymbol.flags = invokableSymbol.flags | tsymbol.flags; } else if (fieldType == symTable.semanticError) { // Can only happen for records. fieldSymbol = new BVarSymbol(origField.symbol.flags | flag | Flags.OPTIONAL, From a4f21bd1059db9605f5a85332417952c6160a857 Mon Sep 17 00:00:00 2001 From: Thushara-Piyasekara Date: Thu, 18 Apr 2024 12:05:29 +0530 Subject: [PATCH 2/3] Add tests --- .../test/record/ReadonlyRecordFieldTest.java | 9 +++++++ .../Ballerina.toml | 4 +++ .../util.bal | 25 +++++++++++++++++++ .../Ballerina.toml | 10 ++++++++ .../main.bal | 25 +++++++++++++++++++ 5 files changed, 73 insertions(+) create mode 100644 tests/jballerina-unit-test/src/test/resources/test-src/record/cloned_record_field_flag_inheritance/imported_module_with_readonly_records/Ballerina.toml create mode 100644 tests/jballerina-unit-test/src/test/resources/test-src/record/cloned_record_field_flag_inheritance/imported_module_with_readonly_records/util.bal create mode 100644 tests/jballerina-unit-test/src/test/resources/test-src/record/cloned_record_field_flag_inheritance/module_with_field_access_for_readonly_record/Ballerina.toml create mode 100644 tests/jballerina-unit-test/src/test/resources/test-src/record/cloned_record_field_flag_inheritance/module_with_field_access_for_readonly_record/main.bal diff --git a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/record/ReadonlyRecordFieldTest.java b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/record/ReadonlyRecordFieldTest.java index 2f8ce30afd2d..6acfa020b0d0 100644 --- a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/record/ReadonlyRecordFieldTest.java +++ b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/record/ReadonlyRecordFieldTest.java @@ -40,6 +40,15 @@ public void testReadonlyRecordFields(String testFunction) { BRunUtil.invoke(result, testFunction); } + @Test + public void testReadonlyInheritanceForInvokableTypeRecordField() { + BCompileUtil.compileAndCacheBala( + "test-src/record/cloned_record_field_flag_inheritance/imported_module_with_readonly_records"); + CompileResult result = BCompileUtil.compile( + "test-src/record/cloned_record_field_flag_inheritance/module_with_field_access_for_readonly_record"); + assertEquals(result.getErrorCount(), 0); + } + @DataProvider(name = "readonlyRecordFieldTestFunctions") public Object[][] readonlyRecordFieldTestFunctions() { return new Object[][]{ diff --git a/tests/jballerina-unit-test/src/test/resources/test-src/record/cloned_record_field_flag_inheritance/imported_module_with_readonly_records/Ballerina.toml b/tests/jballerina-unit-test/src/test/resources/test-src/record/cloned_record_field_flag_inheritance/imported_module_with_readonly_records/Ballerina.toml new file mode 100644 index 000000000000..4b755b87d98b --- /dev/null +++ b/tests/jballerina-unit-test/src/test/resources/test-src/record/cloned_record_field_flag_inheritance/imported_module_with_readonly_records/Ballerina.toml @@ -0,0 +1,4 @@ +[package] +org = "thushara_piyasekara" +name = "ReadonlyRecord" +version = "0.1.0" diff --git a/tests/jballerina-unit-test/src/test/resources/test-src/record/cloned_record_field_flag_inheritance/imported_module_with_readonly_records/util.bal b/tests/jballerina-unit-test/src/test/resources/test-src/record/cloned_record_field_flag_inheritance/imported_module_with_readonly_records/util.bal new file mode 100644 index 000000000000..8f113cffdf16 --- /dev/null +++ b/tests/jballerina-unit-test/src/test/resources/test-src/record/cloned_record_field_flag_inheritance/imported_module_with_readonly_records/util.bal @@ -0,0 +1,25 @@ +// Copyright (c) 2024, WSO2 LLC. (http://www.wso2.com). +// +// WSO2 LLC. licenses this file to you under the Apache License, +// Version 2.0 (the "License"); you may not use this file except +// in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +public type InvokableTypeDef function (int) returns int; + +public type RecA record {| + readonly & InvokableTypeDef preProcessor?; +|}; + +public type RecB record {| + readonly RecA[] recArr; +|}; diff --git a/tests/jballerina-unit-test/src/test/resources/test-src/record/cloned_record_field_flag_inheritance/module_with_field_access_for_readonly_record/Ballerina.toml b/tests/jballerina-unit-test/src/test/resources/test-src/record/cloned_record_field_flag_inheritance/module_with_field_access_for_readonly_record/Ballerina.toml new file mode 100644 index 000000000000..c4d586387886 --- /dev/null +++ b/tests/jballerina-unit-test/src/test/resources/test-src/record/cloned_record_field_flag_inheritance/module_with_field_access_for_readonly_record/Ballerina.toml @@ -0,0 +1,10 @@ +[package] +org = "thushara_piyasekara" +name = "LocalPkgUsage" +version = "0.1.0" + +[[dependency]] +org = "thushara_piyasekara" +name = "ReadonlyRecord" +version = "0.1.0" +repository="local" diff --git a/tests/jballerina-unit-test/src/test/resources/test-src/record/cloned_record_field_flag_inheritance/module_with_field_access_for_readonly_record/main.bal b/tests/jballerina-unit-test/src/test/resources/test-src/record/cloned_record_field_flag_inheritance/module_with_field_access_for_readonly_record/main.bal new file mode 100644 index 000000000000..9c45063dc114 --- /dev/null +++ b/tests/jballerina-unit-test/src/test/resources/test-src/record/cloned_record_field_flag_inheritance/module_with_field_access_for_readonly_record/main.bal @@ -0,0 +1,25 @@ +// Copyright (c) 2024, WSO2 LLC. (http://www.wso2.com). +// +// WSO2 LLC. licenses this file to you under the Apache License, +// Version 2.0 (the "License"); you may not use this file except +// in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +import thushara_piyasekara/ReadonlyRecord; + +ReadonlyRecord:RecB rec = { + recArr: [] +}; + +public function main() { + ReadonlyRecord:RecA[] & readonly ops = rec.recArr; +} From bf8bf0bd84166e25c3253035b80905914e365ec4 Mon Sep 17 00:00:00 2001 From: Thushara-Piyasekara Date: Tue, 28 May 2024 15:42:49 +0530 Subject: [PATCH 3/3] Refactor test project org name --- .../imported_module_with_readonly_records/Ballerina.toml | 2 +- .../Ballerina.toml | 4 ++-- .../module_with_field_access_for_readonly_record/main.bal | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/jballerina-unit-test/src/test/resources/test-src/record/cloned_record_field_flag_inheritance/imported_module_with_readonly_records/Ballerina.toml b/tests/jballerina-unit-test/src/test/resources/test-src/record/cloned_record_field_flag_inheritance/imported_module_with_readonly_records/Ballerina.toml index 4b755b87d98b..a606d18f8e35 100644 --- a/tests/jballerina-unit-test/src/test/resources/test-src/record/cloned_record_field_flag_inheritance/imported_module_with_readonly_records/Ballerina.toml +++ b/tests/jballerina-unit-test/src/test/resources/test-src/record/cloned_record_field_flag_inheritance/imported_module_with_readonly_records/Ballerina.toml @@ -1,4 +1,4 @@ [package] -org = "thushara_piyasekara" +org = "test_org" name = "ReadonlyRecord" version = "0.1.0" diff --git a/tests/jballerina-unit-test/src/test/resources/test-src/record/cloned_record_field_flag_inheritance/module_with_field_access_for_readonly_record/Ballerina.toml b/tests/jballerina-unit-test/src/test/resources/test-src/record/cloned_record_field_flag_inheritance/module_with_field_access_for_readonly_record/Ballerina.toml index c4d586387886..99f212727cb3 100644 --- a/tests/jballerina-unit-test/src/test/resources/test-src/record/cloned_record_field_flag_inheritance/module_with_field_access_for_readonly_record/Ballerina.toml +++ b/tests/jballerina-unit-test/src/test/resources/test-src/record/cloned_record_field_flag_inheritance/module_with_field_access_for_readonly_record/Ballerina.toml @@ -1,10 +1,10 @@ [package] -org = "thushara_piyasekara" +org = "test_org" name = "LocalPkgUsage" version = "0.1.0" [[dependency]] -org = "thushara_piyasekara" +org = "test_org" name = "ReadonlyRecord" version = "0.1.0" repository="local" diff --git a/tests/jballerina-unit-test/src/test/resources/test-src/record/cloned_record_field_flag_inheritance/module_with_field_access_for_readonly_record/main.bal b/tests/jballerina-unit-test/src/test/resources/test-src/record/cloned_record_field_flag_inheritance/module_with_field_access_for_readonly_record/main.bal index 9c45063dc114..53a9534447c3 100644 --- a/tests/jballerina-unit-test/src/test/resources/test-src/record/cloned_record_field_flag_inheritance/module_with_field_access_for_readonly_record/main.bal +++ b/tests/jballerina-unit-test/src/test/resources/test-src/record/cloned_record_field_flag_inheritance/module_with_field_access_for_readonly_record/main.bal @@ -14,7 +14,7 @@ // specific language governing permissions and limitations // under the License. -import thushara_piyasekara/ReadonlyRecord; +import test_org/ReadonlyRecord; ReadonlyRecord:RecB rec = { recArr: []