Skip to content

Commit

Permalink
Merge pull request #41942 from poorna2152/const_list
Browse files Browse the repository at this point in the history
Fix error on accessing a constant list defined in a module
  • Loading branch information
MaryamZi authored Mar 20, 2024
2 parents ffe0d36 + 0016c1c commit c7c5ee4
Show file tree
Hide file tree
Showing 5 changed files with 229 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1355,8 +1355,10 @@ private BType checkTupleType(BTupleType tupleType, BLangListConstructorExpr list
}

private BTupleType createNewTupleType(Location pos, List<BType> 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<BTupleMember> members = new ArrayList<>();
memberTypes.forEach(m ->
members.add(new BTupleMember(m, Symbols.createVarSymbolForTupleMember(m))));
Expand Down
Original file line number Diff line number Diff line change
@@ -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.9.0
*/
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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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()}'`);
}
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit c7c5ee4

Please sign in to comment.