Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
SasinduDilshara committed Sep 19, 2022
2 parents d3dbd02 + 3cd6b38 commit 315abbb
Show file tree
Hide file tree
Showing 18 changed files with 172 additions and 30 deletions.
8 changes: 8 additions & 0 deletions .github/ISSUE_TEMPLATE/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
blank_issues_enabled: false
contact_links:
- name: General Question
url: https://stackoverflow.com/questions/tagged/ballerina
about: "If you have a question then please ask on Stack Overflow using the #ballerina tag."
- name: Chat
url: https://discord.com/invite/wAJYFbMrG2
about: "Chat about anything else with the community."
2 changes: 1 addition & 1 deletion SECURITY.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ Ballerina project maintainers take security issues very seriously and all the vu

>**IMPORTANT:** Please use the **[[email protected]](mailto:[email protected])** mailing list to report all security-related issues.
For more information on the Security Policy of Ballerina, go to the [Security Policy](https://ballerina.io/security/).
For more information on the Security Policy of Ballerina, go to the [Security Policy](https://ballerina.io/security-policy/).

Original file line number Diff line number Diff line change
Expand Up @@ -6587,6 +6587,9 @@ private void rewriteInvocation(BLangInvocation invocation, boolean async) {
reorderArguments(invocation);

rewriteExprs(invocation.requiredArgs);

// Disallow desugaring the same expression twice.
// For langlib invocations, expr is duplicated as the first required arg.
if (invocation.langLibInvocation && !invocation.requiredArgs.isEmpty()) {
invocation.expr = invocation.requiredArgs.get(0);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -633,15 +633,16 @@ private BType getIntegerLiteralType(BLangLiteral literalExpr, Object literalValu
return getFiniteTypeMatchWithIntLiteral(literalExpr, finiteType, literalValue, data);
}
} else if (expectedType.tag == TypeTags.UNION) {
for (BType memType : types.getAllTypes(expectedType, true)) {
BType memberRefType = Types.getReferredType(memType);
if (TypeTags.isIntegerTypeTag(memberRefType.tag) || memberRefType.tag == TypeTags.BYTE) {
BUnionType expectedUnionType = (BUnionType) expectedType;
List<BType> memberTypes = types.getAllReferredTypes(expectedUnionType);
for (BType memType : memberTypes) {
int tag = memType.tag;
if (TypeTags.isIntegerTypeTag(tag) || tag == TypeTags.BYTE) {
BType intLiteralType = getIntLiteralType(memType, literalValue, data);
if (intLiteralType == memberRefType) {
if (intLiteralType == memType) {
return intLiteralType;
}
} else if (memberRefType.tag == TypeTags.JSON || memberRefType.tag == TypeTags.ANYDATA ||
memberRefType.tag == TypeTags.ANY) {
} else if (tag == TypeTags.JSON || tag == TypeTags.ANYDATA || tag == TypeTags.ANY) {
if (literalValue instanceof Double) {
return symTable.floatType;
}
Expand All @@ -652,16 +653,15 @@ private BType getIntegerLiteralType(BLangLiteral literalExpr, Object literalValu
}
}

BType finiteType = getFiniteTypeWithValuesOfSingleType((BUnionType) expectedType, symTable.intType);
BType finiteType = getFiniteTypeWithValuesOfSingleType(expectedUnionType, symTable.intType);
if (finiteType != symTable.semanticError) {
BType setType = setLiteralValueAndGetType(literalExpr, finiteType, data);
if (literalExpr.isFiniteContext) {
// i.e., a match was found for a finite type
return setType;
}
}
BType finiteTypeMatchingByte = getFiniteTypeWithValuesOfSingleType((BUnionType) expectedType,
symTable.byteType);
BType finiteTypeMatchingByte = getFiniteTypeWithValuesOfSingleType(expectedUnionType, symTable.byteType);
if (finiteTypeMatchingByte != symTable.semanticError) {
finiteType = finiteTypeMatchingByte;
BType setType = setLiteralValueAndGetType(literalExpr, finiteType, data);
Expand All @@ -670,8 +670,7 @@ private BType getIntegerLiteralType(BLangLiteral literalExpr, Object literalValu
return setType;
}
}
Set<BType> memberTypes = ((BUnionType) expectedType).getMemberTypes();
return getTypeMatchingFloatOrDecimal(finiteType, memberTypes, literalExpr, (BUnionType) expectedType, data);
return getTypeMatchingFloatOrDecimal(finiteType, memberTypes, literalExpr, expectedUnionType, data);
}
if (!(literalValue instanceof Long)) {
dlog.error(literalExpr.pos, DiagnosticErrorCode.OUT_OF_RANGE, literalExpr.originalValue,
Expand Down Expand Up @@ -869,7 +868,7 @@ public BType setLiteralValueAndGetType(BLangLiteral literalExpr, BType expType,
return literalType;
}

private BType getTypeMatchingFloatOrDecimal(BType finiteType, Set<BType> memberTypes, BLangLiteral literalExpr,
private BType getTypeMatchingFloatOrDecimal(BType finiteType, List<BType> memberTypes, BLangLiteral literalExpr,
BUnionType expType, AnalyzerData data) {
for (int tag = TypeTags.FLOAT; tag <= TypeTags.DECIMAL; tag++) {
if (finiteType == symTable.semanticError) {
Expand Down Expand Up @@ -1184,7 +1183,9 @@ public void visit(BLangTableConstructorExpr tableConstructorExpr, AnalyzerData d
}
data.resultType = symTable.semanticError;
} else {
data.resultType = symTable.semanticError;
data.resultType = types.checkType(tableConstructorExpr.pos,
getInferredTableType(nodeCloner.cloneNode(tableConstructorExpr), data), expType,
DiagnosticErrorCode.INCOMPATIBLE_TYPES);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5835,6 +5835,18 @@ public List<BType> getAllTypes(BType type, boolean getReferenced) {
return memberTypes;
}

public List<BType> getAllReferredTypes(BUnionType unionType) {
List<BType> memberTypes = new LinkedList<>();
for (BType type : unionType.getMemberTypes()) {
if (type.tag == UNION) {
memberTypes.addAll(getAllReferredTypes((BUnionType) type));
} else {
memberTypes.add(Types.getReferredType(type));
}
}
return memberTypes;
}

public boolean isAllowedConstantType(BType type) {
switch (type.tag) {
case TypeTags.BOOLEAN:
Expand Down
2 changes: 1 addition & 1 deletion distribution/zip/jballerina/bin/bal
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
# OS specific support. $var _must_ be set to either true or false.

# Set JAVA_HOME for installers
JAVA_PATH=$BALLERINA_HOME/../../dependencies/jdk-11.0.8+10-jre
JAVA_PATH=$BALLERINA_HOME/../../dependencies/jdk-11.0.15+10-jre
if test -d "$JAVA_PATH"; then
JAVA_HOME=$JAVA_PATH
fi
Expand Down
4 changes: 2 additions & 2 deletions distribution/zip/jballerina/bin/bal.bat
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ rem ----- if JAVA_HOME is not set we're not happy ------------------------------
:checkJava

set BALLERINA_HOME=%~sdp0..
if exist %BALLERINA_HOME%\..\..\dependencies\jdk-11.0.8+10-jre (
set "JAVA_HOME=%BALLERINA_HOME%\..\..\dependencies\jdk-11.0.8+10-jre"
if exist %BALLERINA_HOME%\..\..\dependencies\jdk-11.0.15+10-jre (
set "JAVA_HOME=%BALLERINA_HOME%\..\..\dependencies\jdk-11.0.15+10-jre"
)

if "%JAVA_HOME%" == "" goto noJavaHome
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright (c) 2022, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. 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.types;

import org.ballerinalang.test.BAssertUtil;
import org.ballerinalang.test.BCompileUtil;
import org.ballerinalang.test.CompileResult;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

/**
* Negative Test for variable declaration with table constructor.
*/
public class TableCtrAssignmentTest {
private CompileResult negativeResult;

@BeforeClass
public void setup() {
BCompileUtil.compileAndCacheBala("test-src/bala/test_projects/test_project");
negativeResult = BCompileUtil.compile("test-src/bala/test_bala/types/table_ctr_assignment.bal");
}

@Test
public void testNegative() {
BAssertUtil.validateError(negativeResult, 0, "incompatible types: expected 'testorg/foo:1.0.0:AB', " +
"found 'table<record {| |}>'", 20, 25);
Assert.assertEquals(negativeResult.getErrorCount(), 1);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -296,32 +296,38 @@ public void testNegativeScenarios() {
validateError(negativeResult, index++,
"incompatible types: '(table<record {| readonly int id; string value; |}> key(id)|error)' " +
"is not an iterable collection", 432, 100);
validateError(negativeResult, index++, "incompatible types: expected 'int', found 'table<record {| |}>'",
438, 13);
validateError(negativeResult, index++, "incompatible types: expected '(int|float)', " +
"found 'table<record {| |}>'", 439, 19);
validateError(negativeResult, index++, "incompatible types: expected 'string', " +
"found 'table<record {| int a; int b; |}>'", 440, 16);
validateError(negativeResult, index++, "incompatible type 'int', expected a construct type" +
" of 'string'", 448, 49);
" of 'string'", 454, 49);
validateError(negativeResult, index++, "incompatible type '[int,int,int,int]', expected a" +
" construct type of 'string'", 449, 49);
" construct type of 'string'", 455, 49);
validateError(negativeResult, index++, "incompatible type 'string', expected a construct" +
" type of 'xml'", 453, 42);
" type of 'xml'", 459, 42);
validateError(negativeResult, index++, "incompatible type 'int', expected a construct type" +
" of 'xml'", 454, 42);
" of 'xml'", 460, 42);
validateError(negativeResult, index++, "incompatible type '[int,int,int,int]', expected a" +
" construct type of 'xml'", 455, 42);
" construct type of 'xml'", 461, 42);
validateError(negativeResult, index++, "incompatible type 'int', expected a construct type of" +
" 'table<Employee> key(name)'", 462, 41);
" 'table<Employee> key(name)'", 468, 41);
validateError(negativeResult, index++, "incompatible type '[string,int]', expected a construct" +
" type of 'table<Employee> key(name)'", 463, 41);
" type of 'table<Employee> key(name)'", 469, 41);
validateError(negativeResult, index++, "incompatible type 'table<Employee> key(name)'," +
" expected a construct type of 'table<Employee> key(name)'", 464, 41);
" expected a construct type of 'table<Employee> key(name)'", 470, 41);
validateError(negativeResult, index++, "query expression that constructs a mapping must " +
"start with the map keyword", 468, 36);
"start with the map keyword", 474, 36);
validateError(negativeResult, index++, "query expression that constructs a mapping must " +
"start with the map keyword", 469, 36);
"start with the map keyword", 475, 36);
validateError(negativeResult, index++, "query expression that constructs a mapping must " +
"start with the map keyword", 470, 36);
"start with the map keyword", 476, 36);
validateError(negativeResult, index++, "incompatible type in select clause: expected " +
"[string,any|error], found 'int'", 471, 40);
"[string,any|error], found 'int'", 477, 40);
validateError(negativeResult, index++, "incompatible type in select clause: expected " +
"[string,any|error], found 'record {| int A; |}'", 472, 40);
"[string,any|error], found 'record {| int A; |}'", 478, 40);
Assert.assertEquals(negativeResult.getErrorCount(), index);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,10 @@ public void testAssignmentNegativeCases() {
"'function (any...) returns ()'", 123, 47);
BAssertUtil.validateError(resultNegative, i++, "incompatible types: expected " +
"'[\"list\",int?,Type]', found 'Type'", 133, 14);
BAssertUtil.validateError(resultNegative, i++, "incompatible types: expected 'int', " +
"found 'table<record {| |}>'", 138, 11);
BAssertUtil.validateError(resultNegative, i++, "incompatible types: expected 'int', " +
"found 'table<record {| |}>'", 141, 12);
Assert.assertEquals(resultNegative.getErrorCount(), i);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,11 @@ public void testDecimalValUsingIntLiterals() {
BRunUtil.invoke(result, "testDecimalValUsingIntLiterals");
}

@Test()
public void testDecimalTypeRef() {
BRunUtil.invoke(result, "testDecimalTypeRef");
}

@AfterClass
public void tearDown() {
result = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ public void testImmutableTypesNegative() {

validateError(result, index++, "invalid intersection type with 'readonly', 'table<Bar> key(name)' can never " +
"be 'readonly'", 159, 5);
validateError(result, index++, "cannot infer type of the object from 'other'", 160, 26);
validateError(result, index++, "invalid intersection type with 'readonly', 'Baz' can never be 'readonly'", 171,
5);
validateError(result, index++, "cannot update 'readonly' value of type '(Config & readonly)'", 194, 5);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright (c) 2022, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
//
// WSO2 Inc. 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 as test_project;

function testInvalidTableCtrAssignment() {
test_project:AB _ = table []; // error
}
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,12 @@ function testFn() {
assertEquality(true, tr:ConstInt is TypeConstInt);

assertEquality({b : {}}, Y);

tr:Seconds? sec1 = 10;
assertEquality(sec1 is decimal, true);

tr:SecondsOrNil sec2 = 11;
assertEquality(sec2 is decimal, true);
}

function getImmutable(ImmutableIntArrayOrStringArray x) returns tr:ImmutableIntArray {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,7 @@ type Record2 record {|
FloatBooleanTuple2 a;
() b;
|};

public type Seconds decimal;

public type SecondsOrNil Seconds?;
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,12 @@ function testQueryConstructingMapsAndTablesWithClausesMayCompleteSEarlyWithError
select item;
}

function testInvalidTableCtrAssignment() {
int _ = table []; // error
int|float _ = table []; // error
string _ = table [{a: 1, b: 2}]; // error
}

type Employee record {
readonly string name;
int salary;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,11 @@ function incompatibilityAssignInTupleTypes() {
Type a = ["tuple", "A", "A", "A"];
List b = a; // error: incompatible types: expected '["list",int?,Type]', found 'Type'
}

function assignTableCtrToIncompatibleType() {
record{int a;} b = {a: 1};
b.a = table[]; // error

[int, string] a = [1, "a"];
a[0] = table[]; // error
}
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,17 @@ function testDecimalValUsingIntLiterals() {
assertEquality("9.223372036854776E+80", result.toString());
}

public type Seconds decimal;
public type SecondsOrNil Seconds?;

function testDecimalTypeRef() {
Seconds? sec1 = 10;
assertEquality(sec1 is decimal, true);

SecondsOrNil sec2 = 11;
assertEquality(sec2 is decimal, true);
}

type AssertionError distinct error;

const ASSERTION_ERROR_REASON = "AssertionError";
Expand Down

0 comments on commit 315abbb

Please sign in to comment.