From 029fbb0937370890f33e31362d890c84f59a12e0 Mon Sep 17 00:00:00 2001 From: Kavindu Gimhan Zoysa Date: Thu, 15 Sep 2022 14:50:18 +0530 Subject: [PATCH 1/8] Fix type inferring for decimal type references --- .../semantics/analyzer/TypeChecker.java | 23 +++++++++---------- .../compiler/semantics/analyzer/Types.java | 12 ++++++++++ .../types/decimaltype/BDecimalValueTest.java | 5 ++++ .../types/type_reference_type_bala_test.bal | 6 +++++ .../types.bal | 4 ++++ .../test-src/types/decimal/decimal_value.bal | 11 +++++++++ 6 files changed, 49 insertions(+), 12 deletions(-) diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/TypeChecker.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/TypeChecker.java index f02420b9dcf1..ceaaf3e154e3 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/TypeChecker.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/TypeChecker.java @@ -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 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; } @@ -652,7 +653,7 @@ 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) { @@ -660,8 +661,7 @@ private BType getIntegerLiteralType(BLangLiteral literalExpr, Object literalValu 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); @@ -670,8 +670,7 @@ private BType getIntegerLiteralType(BLangLiteral literalExpr, Object literalValu return setType; } } - Set 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, @@ -869,7 +868,7 @@ public BType setLiteralValueAndGetType(BLangLiteral literalExpr, BType expType, return literalType; } - private BType getTypeMatchingFloatOrDecimal(BType finiteType, Set memberTypes, BLangLiteral literalExpr, + private BType getTypeMatchingFloatOrDecimal(BType finiteType, List memberTypes, BLangLiteral literalExpr, BUnionType expType, AnalyzerData data) { for (int tag = TypeTags.FLOAT; tag <= TypeTags.DECIMAL; tag++) { if (finiteType == symTable.semanticError) { diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/Types.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/Types.java index 1de57b98699f..7f260694876c 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/Types.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/Types.java @@ -5835,6 +5835,18 @@ public List getAllTypes(BType type, boolean getReferenced) { return memberTypes; } + public List getAllReferredTypes(BUnionType unionType) { + List 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: diff --git a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/types/decimaltype/BDecimalValueTest.java b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/types/decimaltype/BDecimalValueTest.java index 46101890a37b..28fd6b30cedc 100644 --- a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/types/decimaltype/BDecimalValueTest.java +++ b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/types/decimaltype/BDecimalValueTest.java @@ -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; diff --git a/tests/jballerina-unit-test/src/test/resources/test-src/bala/test_bala/types/type_reference_type_bala_test.bal b/tests/jballerina-unit-test/src/test/resources/test-src/bala/test_bala/types/type_reference_type_bala_test.bal index 4af6564970a9..ee671929ac97 100644 --- a/tests/jballerina-unit-test/src/test/resources/test-src/bala/test_bala/types/type_reference_type_bala_test.bal +++ b/tests/jballerina-unit-test/src/test/resources/test-src/bala/test_bala/types/type_reference_type_bala_test.bal @@ -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 { diff --git a/tests/jballerina-unit-test/src/test/resources/test-src/bala/test_projects/test_project_type_reference_types/types.bal b/tests/jballerina-unit-test/src/test/resources/test-src/bala/test_projects/test_project_type_reference_types/types.bal index e5e9b10975ee..5c3ce18b09b6 100644 --- a/tests/jballerina-unit-test/src/test/resources/test-src/bala/test_projects/test_project_type_reference_types/types.bal +++ b/tests/jballerina-unit-test/src/test/resources/test-src/bala/test_projects/test_project_type_reference_types/types.bal @@ -71,3 +71,7 @@ type Record2 record {| FloatBooleanTuple2 a; () b; |}; + +public type Seconds decimal; + +public type SecondsOrNil Seconds?; diff --git a/tests/jballerina-unit-test/src/test/resources/test-src/types/decimal/decimal_value.bal b/tests/jballerina-unit-test/src/test/resources/test-src/types/decimal/decimal_value.bal index 9c902abe2c41..411ede26323d 100644 --- a/tests/jballerina-unit-test/src/test/resources/test-src/types/decimal/decimal_value.bal +++ b/tests/jballerina-unit-test/src/test/resources/test-src/types/decimal/decimal_value.bal @@ -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"; From 9148717525da5b86e6a9a87b69c5c3fb5c246b7e Mon Sep 17 00:00:00 2001 From: Anupama Pathirage Date: Thu, 15 Sep 2022 15:00:20 -0500 Subject: [PATCH 2/8] Update security.md with correct policy link --- SECURITY.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SECURITY.md b/SECURITY.md index 97f497ec6e7e..e691f40e4dc4 100644 --- a/SECURITY.md +++ b/SECURITY.md @@ -4,5 +4,5 @@ Ballerina project maintainers take security issues very seriously and all the vu >**IMPORTANT:** Please use the **[security@ballerina.io](mailto:security@ballerina.io)** 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/). From cd63edd26f7f250752901009e723b3cc33556830 Mon Sep 17 00:00:00 2001 From: Anupama Pathirage Date: Thu, 15 Sep 2022 15:59:33 -0500 Subject: [PATCH 3/8] Update issue template with stackoverflow and discord links --- .github/ISSUE_TEMPLATE/config.yml | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 .github/ISSUE_TEMPLATE/config.yml diff --git a/.github/ISSUE_TEMPLATE/config.yml b/.github/ISSUE_TEMPLATE/config.yml new file mode 100644 index 000000000000..2a99f07ee08a --- /dev/null +++ b/.github/ISSUE_TEMPLATE/config.yml @@ -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." From 3e8ce67adc54d3bc97a7d101f6b06325ed34eadc Mon Sep 17 00:00:00 2001 From: prakanth <50439067+prakanth97@users.noreply.github.com> Date: Fri, 16 Sep 2022 09:14:05 +0530 Subject: [PATCH 4/8] Add comments to explain the logic --- .../java/org/wso2/ballerinalang/compiler/desugar/Desugar.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/Desugar.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/Desugar.java index 380a82bf4875..9c3e05d7841a 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/Desugar.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/Desugar.java @@ -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 { From 82f57f5fd61a5c6656560f9d1303548763ec8f32 Mon Sep 17 00:00:00 2001 From: Charuka Tharindu Date: Fri, 16 Sep 2022 10:23:32 +0530 Subject: [PATCH 5/8] Update JDKs for distribution --- distribution/zip/jballerina/bin/bal | 2 +- distribution/zip/jballerina/bin/bal.bat | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/distribution/zip/jballerina/bin/bal b/distribution/zip/jballerina/bin/bal index 3b2b87ba022e..56dd462a2779 100755 --- a/distribution/zip/jballerina/bin/bal +++ b/distribution/zip/jballerina/bin/bal @@ -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 diff --git a/distribution/zip/jballerina/bin/bal.bat b/distribution/zip/jballerina/bin/bal.bat index 6ba0516ab219..dc935f00da17 100644 --- a/distribution/zip/jballerina/bin/bal.bat +++ b/distribution/zip/jballerina/bin/bal.bat @@ -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 From 07d9a7f292f73c98d5c09b4cf6b5a4453bfd761d Mon Sep 17 00:00:00 2001 From: Kavindu Gimhan Zoysa Date: Tue, 30 Aug 2022 11:35:31 +0530 Subject: [PATCH 6/8] Log error for table-ctr assignment to non table type --- .../compiler/semantics/analyzer/TypeChecker.java | 4 +++- .../test/query/QueryExprWithQueryConstructTypeTest.java | 3 +++ .../test/types/readonly/SelectivelyImmutableTypeTest.java | 1 + .../query/query-expr-query-construct-type-negative.bal | 6 ++++++ 4 files changed, 13 insertions(+), 1 deletion(-) diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/TypeChecker.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/TypeChecker.java index f02420b9dcf1..3574b4f5f811 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/TypeChecker.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/TypeChecker.java @@ -1184,7 +1184,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); } } diff --git a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/query/QueryExprWithQueryConstructTypeTest.java b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/query/QueryExprWithQueryConstructTypeTest.java index 99b1b0bab85e..1b8e7f014dce 100644 --- a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/query/QueryExprWithQueryConstructTypeTest.java +++ b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/query/QueryExprWithQueryConstructTypeTest.java @@ -261,6 +261,9 @@ public void testNegativeScenarios() { validateError(negativeResult, index++, "incompatible types: '(table key(id)|error)' " + "is not an iterable collection", 432, 100); + validateError(negativeResult, index++, "incompatible types: expected 'int', found 'table'", 401, 13); + validateError(negativeResult, index++, "incompatible types: expected '(int|float)', found 'table'", 402, 19); + validateError(negativeResult, index++, "incompatible types: expected 'string', found 'table'", 403, 16); Assert.assertEquals(negativeResult.getErrorCount(), index); } diff --git a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/types/readonly/SelectivelyImmutableTypeTest.java b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/types/readonly/SelectivelyImmutableTypeTest.java index bcfabffc708a..4b4252e3bee4 100644 --- a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/types/readonly/SelectivelyImmutableTypeTest.java +++ b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/types/readonly/SelectivelyImmutableTypeTest.java @@ -117,6 +117,7 @@ public void testImmutableTypesNegative() { validateError(result, index++, "invalid intersection type with 'readonly', 'table 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); diff --git a/tests/jballerina-unit-test/src/test/resources/test-src/query/query-expr-query-construct-type-negative.bal b/tests/jballerina-unit-test/src/test/resources/test-src/query/query-expr-query-construct-type-negative.bal index 371a749d6648..a7e2496d159d 100644 --- a/tests/jballerina-unit-test/src/test/resources/test-src/query/query-expr-query-construct-type-negative.bal +++ b/tests/jballerina-unit-test/src/test/resources/test-src/query/query-expr-query-construct-type-negative.bal @@ -433,3 +433,9 @@ function testQueryConstructingMapsAndTablesWithClausesMayCompleteSEarlyWithError select {id: firstNo, value: firstNo.toBalString()} on conflict error("Error")) select item; } + +function testInvalidTableCtrAssignment() { + int _ = table []; // error + int|float _ = table []; // error + string _ = table [{a: 1, b: 2}]; // error +} From 8c54dd43919f45a3948e46dc2dc78bd50ccc0611 Mon Sep 17 00:00:00 2001 From: Kavindu Gimhan Zoysa Date: Mon, 5 Sep 2022 10:23:13 +0530 Subject: [PATCH 7/8] Add more tests --- .../semantics/analyzer/TypeChecker.java | 6 +-- .../bala/types/TableCtrAssignmentTest.java | 46 +++++++++++++++++++ .../QueryExprWithQueryConstructTypeTest.java | 9 ++-- .../statements/assign/AssignStmtTest.java | 4 ++ .../test_bala/types/table_ctr_assignment.bal | 21 +++++++++ .../assign/assign-stmt-negative.bal | 8 ++++ 6 files changed, 88 insertions(+), 6 deletions(-) create mode 100644 tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/bala/types/TableCtrAssignmentTest.java create mode 100644 tests/jballerina-unit-test/src/test/resources/test-src/bala/test_bala/types/table_ctr_assignment.bal diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/TypeChecker.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/TypeChecker.java index 3574b4f5f811..440a52553c24 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/TypeChecker.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/TypeChecker.java @@ -1184,9 +1184,9 @@ public void visit(BLangTableConstructorExpr tableConstructorExpr, AnalyzerData d } data.resultType = symTable.semanticError; } else { - data.resultType = types.checkType(tableConstructorExpr.pos, - getInferredTableType(nodeCloner.cloneNode(tableConstructorExpr), data), expType, - DiagnosticErrorCode.INCOMPATIBLE_TYPES); + dlog.error(tableConstructorExpr.pos, DiagnosticErrorCode.INCOMPATIBLE_TYPES, expType, + getInferredTableType(nodeCloner.cloneNode(tableConstructorExpr), data)); + data.resultType = symTable.semanticError; } } diff --git a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/bala/types/TableCtrAssignmentTest.java b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/bala/types/TableCtrAssignmentTest.java new file mode 100644 index 000000000000..719b5961acda --- /dev/null +++ b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/bala/types/TableCtrAssignmentTest.java @@ -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'", 20, 25); + Assert.assertEquals(negativeResult.getErrorCount(), 1); + } +} diff --git a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/query/QueryExprWithQueryConstructTypeTest.java b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/query/QueryExprWithQueryConstructTypeTest.java index 1b8e7f014dce..3b8e2d3a7500 100644 --- a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/query/QueryExprWithQueryConstructTypeTest.java +++ b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/query/QueryExprWithQueryConstructTypeTest.java @@ -261,9 +261,12 @@ public void testNegativeScenarios() { validateError(negativeResult, index++, "incompatible types: '(table key(id)|error)' " + "is not an iterable collection", 432, 100); - validateError(negativeResult, index++, "incompatible types: expected 'int', found 'table'", 401, 13); - validateError(negativeResult, index++, "incompatible types: expected '(int|float)', found 'table'", 402, 19); - validateError(negativeResult, index++, "incompatible types: expected 'string', found 'table'", 403, 16); + validateError(negativeResult, index++, "incompatible types: expected 'int', found 'table'", + 401, 13); + validateError(negativeResult, index++, "incompatible types: expected '(int|float)', " + + "found 'table'", 402, 19); + validateError(negativeResult, index++, "incompatible types: expected 'string', " + + "found 'table'", 403, 16); Assert.assertEquals(negativeResult.getErrorCount(), index); } diff --git a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/statements/assign/AssignStmtTest.java b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/statements/assign/AssignStmtTest.java index b0e34e0276e5..75b1181418f4 100644 --- a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/statements/assign/AssignStmtTest.java +++ b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/statements/assign/AssignStmtTest.java @@ -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'", 138, 11); + BAssertUtil.validateError(resultNegative, i++, "incompatible types: expected 'int', " + + "found 'table'", 141, 12); Assert.assertEquals(resultNegative.getErrorCount(), i); } diff --git a/tests/jballerina-unit-test/src/test/resources/test-src/bala/test_bala/types/table_ctr_assignment.bal b/tests/jballerina-unit-test/src/test/resources/test-src/bala/test_bala/types/table_ctr_assignment.bal new file mode 100644 index 000000000000..1f5407e53620 --- /dev/null +++ b/tests/jballerina-unit-test/src/test/resources/test-src/bala/test_bala/types/table_ctr_assignment.bal @@ -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 +} diff --git a/tests/jballerina-unit-test/src/test/resources/test-src/statements/assign/assign-stmt-negative.bal b/tests/jballerina-unit-test/src/test/resources/test-src/statements/assign/assign-stmt-negative.bal index 57f2af49e8a0..cd8e30a5eb49 100644 --- a/tests/jballerina-unit-test/src/test/resources/test-src/statements/assign/assign-stmt-negative.bal +++ b/tests/jballerina-unit-test/src/test/resources/test-src/statements/assign/assign-stmt-negative.bal @@ -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 +} From 08fff4e878bf6ea715f51c84961725b593efad59 Mon Sep 17 00:00:00 2001 From: Kavindu Gimhan Zoysa Date: Tue, 13 Sep 2022 19:56:22 +0530 Subject: [PATCH 8/8] Use checkTypes to properly handle errors --- .../compiler/semantics/analyzer/TypeChecker.java | 6 +++--- .../test/query/QueryExprWithQueryConstructTypeTest.java | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/TypeChecker.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/TypeChecker.java index 440a52553c24..3574b4f5f811 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/TypeChecker.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/TypeChecker.java @@ -1184,9 +1184,9 @@ public void visit(BLangTableConstructorExpr tableConstructorExpr, AnalyzerData d } data.resultType = symTable.semanticError; } else { - dlog.error(tableConstructorExpr.pos, DiagnosticErrorCode.INCOMPATIBLE_TYPES, expType, - getInferredTableType(nodeCloner.cloneNode(tableConstructorExpr), data)); - data.resultType = symTable.semanticError; + data.resultType = types.checkType(tableConstructorExpr.pos, + getInferredTableType(nodeCloner.cloneNode(tableConstructorExpr), data), expType, + DiagnosticErrorCode.INCOMPATIBLE_TYPES); } } diff --git a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/query/QueryExprWithQueryConstructTypeTest.java b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/query/QueryExprWithQueryConstructTypeTest.java index 3b8e2d3a7500..5d8d7bb6260d 100644 --- a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/query/QueryExprWithQueryConstructTypeTest.java +++ b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/query/QueryExprWithQueryConstructTypeTest.java @@ -262,11 +262,11 @@ public void testNegativeScenarios() { "incompatible types: '(table key(id)|error)' " + "is not an iterable collection", 432, 100); validateError(negativeResult, index++, "incompatible types: expected 'int', found 'table'", - 401, 13); + 438, 13); validateError(negativeResult, index++, "incompatible types: expected '(int|float)', " + - "found 'table'", 402, 19); + "found 'table'", 439, 19); validateError(negativeResult, index++, "incompatible types: expected 'string', " + - "found 'table'", 403, 16); + "found 'table'", 440, 16); Assert.assertEquals(negativeResult.getErrorCount(), index); }