diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/ConstantTypeChecker.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/ConstantTypeChecker.java index 59f16e44eda7..685d8fc46f91 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/ConstantTypeChecker.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/ConstantTypeChecker.java @@ -1357,8 +1357,10 @@ private BType checkTupleType(BTupleType tupleType, BLangListConstructorExpr list } private BTupleType createNewTupleType(Location pos, List memberTypes, AnalyzerData data) { - BTypeSymbol tupleTypeSymbol = Symbols.createTypeSymbol(SymTag.TUPLE_TYPE, 0, Names.EMPTY, - data.env.enclPkg.symbol.pkgID, null, data.env.scope.owner, pos, SOURCE); + SymbolEnv symbolEnv = data.env; + BTypeSymbol tupleTypeSymbol = + Symbols.createTypeSymbol(SymTag.TUPLE_TYPE, Flags.PUBLIC, Names.EMPTY, symbolEnv.enclPkg.symbol.pkgID, + null, symbolEnv.scope.owner, pos, SOURCE); List members = new ArrayList<>(); memberTypes.forEach(m -> members.add(new BTupleMember(m, Symbols.createVarSymbolForTupleMember(m)))); diff --git a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/bala/constant/ListConstantInBalaTest.java b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/bala/constant/ListConstantInBalaTest.java new file mode 100644 index 000000000000..57ef498aae48 --- /dev/null +++ b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/bala/constant/ListConstantInBalaTest.java @@ -0,0 +1,98 @@ +/* + * Copyright (c) 2024, WSO2 LLC. (https://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. + */ +package org.ballerinalang.test.bala.constant; + +import org.ballerinalang.test.BAssertUtil; +import org.ballerinalang.test.BCompileUtil; +import org.ballerinalang.test.BRunUtil; +import org.ballerinalang.test.CompileResult; +import org.testng.Assert; +import org.testng.annotations.AfterClass; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.DataProvider; +import org.testng.annotations.Test; + +/** + * Test cases for reading list constants. + * + * @since 2201.8.8 + */ +public class ListConstantInBalaTest { + + private CompileResult compileResult; + + @BeforeClass + public void setup() { + BCompileUtil.compileAndCacheBala("test-src/bala/test_projects/test_project"); + compileResult = BCompileUtil.compile("test-src/bala/test_bala/constant/list_literal_constant.bal"); + } + + @Test(dataProvider = "constantListAccessTestDataProvider") + public void testConstantListAccess(String testCase) { + BRunUtil.invoke(compileResult, testCase); + } + + @DataProvider(name = "constantListAccessTestDataProvider") + public Object[] constantListAccessTestDataProvider() { + return new Object[]{ + "testSimpleArrayAccess", + "testSimpleTupleAccess", + "test2DTupleAccess", + "test2DArrayAccess", + "testFixedLengthArrayAccess", + "testArrayWithRestAccess", + "test2DUnionArrayAccess" + }; + } + + @Test + public void testConstantListAccessNegative() { + CompileResult compileResult = BCompileUtil.compile( + "test-src/bala/test_bala/constant/list_constant_negative.bal"); + int i = 0; + BAssertUtil.validateError(compileResult, i++, + "incompatible types: expected '[string,string,int...]', found '[\"a\",\"b\",\"c\"] & readonly'", 20, + 34); + BAssertUtil.validateError(compileResult, i++, + "incompatible types: expected 'int[]', found '[true,false,true] & readonly'", 21, 15); + BAssertUtil.validateError(compileResult, i++, + "cannot update 'readonly' value of type '[\"a\",\"b\",\"c\"] & readonly'", 26, 5); + BAssertUtil.validateError(compileResult, i++, + "cannot update 'readonly' value of type '[\"a\",\"b\",\"c\"] & readonly'", 27, 5); + BAssertUtil.validateError(compileResult, i++, + "incompatible types: expected '(\"a\"|\"b\"|\"c\")', found 'string:Char'", 27, 12); + BAssertUtil.validateError(compileResult, i++, + "cannot update 'readonly' value of type '[1,\"f\",\"g\"] & readonly'", 30, 5); + BAssertUtil.validateError(compileResult, i++, "incompatible types: expected '(1|\"f\"|\"g\")', found 'string'", + 30, 12); + BAssertUtil.validateError(compileResult, i++, "undefined symbol 'l13'", 34, 9); + BAssertUtil.validateError(compileResult, i++, "undefined symbol 'l14'", 35, 9); + BAssertUtil.validateError(compileResult, i++, "attempt to refer to non-accessible symbol 'l10'", 39, 9); + BAssertUtil.validateError(compileResult, i++, "undefined symbol 'l10'", 39, 9); + BAssertUtil.validateError(compileResult, i++, "attempt to refer to non-accessible symbol 'l11'", 40, 9); + BAssertUtil.validateError(compileResult, i++, "undefined symbol 'l11'", 40, 9); + BAssertUtil.validateError(compileResult, i++, "attempt to refer to non-accessible symbol 'l12'", 41, 9); + BAssertUtil.validateError(compileResult, i++, "undefined symbol 'l12'", 41, 9); + Assert.assertEquals(compileResult.getErrorCount(), i); + } + + @AfterClass + public void tearDown() { + compileResult = null; + } +} diff --git a/tests/jballerina-unit-test/src/test/resources/test-src/bala/test_bala/constant/list_constant_negative.bal b/tests/jballerina-unit-test/src/test/resources/test-src/bala/test_bala/constant/list_constant_negative.bal new file mode 100644 index 000000000000..afbb3ba33f80 --- /dev/null +++ b/tests/jballerina-unit-test/src/test/resources/test-src/bala/test_bala/constant/list_constant_negative.bal @@ -0,0 +1,42 @@ +// 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 testorg/foo; + +function testIncompatibleAssignment() { + [string, string, int...] _ = foo:l1; + int[] _ = foo:l5; +} + +function testInvalidUpdates() { + var a = foo:l1; + a[0] = "1"; + a.push("2"); + + var b = foo:l7; + b.push("l7"); +} + +function testUndefinedSymbolAccess() { + _ = foo:l13; + _ = foo:l14; +} + +function testNonPublicConstAccess() { + _ = foo:l10; + _ = foo:l11; + _ = foo:l12; +} diff --git a/tests/jballerina-unit-test/src/test/resources/test-src/bala/test_bala/constant/list_literal_constant.bal b/tests/jballerina-unit-test/src/test/resources/test-src/bala/test_bala/constant/list_literal_constant.bal new file mode 100644 index 000000000000..dd56e80b1f53 --- /dev/null +++ b/tests/jballerina-unit-test/src/test/resources/test-src/bala/test_bala/constant/list_literal_constant.bal @@ -0,0 +1,53 @@ +// Copyright (c) 2024, WSO2 LLC. (https://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 testorg/foo; + +function testSimpleArrayAccess() { + assertEqual(foo:l1, ["a", "b", "c"]); +} + +function testSimpleTupleAccess() { + assertEqual(foo:l2, [1, "d"]); +} + +function test2DTupleAccess() { + assertEqual(foo:l3, [1, ["e", 2]]); +} + +function test2DArrayAccess() { + assertEqual(foo:l4, [[1, 2, 3], [4, 5, 6]]); +} + +function testFixedLengthArrayAccess() { + assertEqual(foo:l5, [true, false, true]); +} + +function testArrayWithRestAccess() { + assertEqual(foo:l7, [1, "f", "g"]); +} + +function test2DUnionArrayAccess() { + assertEqual(foo:l8, [[1, "2", 3], [4, 5, 6]]); + assertEqual(foo:l9, [[1, 2, 3], ["4", "5", "6"]]); +} + +function assertEqual(anydata actual, anydata expected) { + if expected == actual { + return; + } + panic error(string `expected '${expected.toBalString()}', found '${actual.toBalString()}'`); +} diff --git a/tests/jballerina-unit-test/src/test/resources/test-src/bala/test_projects/test_project/list_constant.bal b/tests/jballerina-unit-test/src/test/resources/test-src/bala/test_projects/test_project/list_constant.bal new file mode 100644 index 000000000000..cae666c03b71 --- /dev/null +++ b/tests/jballerina-unit-test/src/test/resources/test-src/bala/test_projects/test_project/list_constant.bal @@ -0,0 +1,32 @@ +// Copyright (c) 2024, WSO2 LLC. (https://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 + +type IntArr int[]; + +const int length = 3; + +public const string[] l1 = ["a", "b", "c"]; +public const [int, string] l2 = [1, "d"]; +public const [int, [string, int]] l3 = [1, ["e", 2]]; +public const int[][] l4 = [[1, 2, 3], [4, 5, 6]]; +public const boolean[length] l5 = [true, false, true]; +public const IntArr l6 = [1, 2, 3]; +public const [int, string...] l7 = [1, "f", "g"]; +public const (string|int)[][] l8 = [[1, "2", 3], [4, 5, 6]]; +public const [(string[]|int[])...] l9 = [[1, 2, 3], ["4", "5", "6"]]; +const int[] l10 = [1, 2, 3]; +const [int, int, boolean] l11 = [1, 2, true]; +const l12 = l1;