From 1f2bda90fac3ae0992ab1d867ee39bc17419d81d Mon Sep 17 00:00:00 2001 From: poorna2152 Date: Fri, 17 May 2024 14:01:34 +0530 Subject: [PATCH] Update the use of isAssignable --- .../semantics/analyzer/SemanticAnalyzer.java | 2 +- .../compiler/semantics/analyzer/Types.java | 17 +++++------------ .../QueryExprWithQueryConstructTypeTest.java | 4 ++-- .../test/query/XMLQueryExpressionTest.java | 4 ++-- .../test/types/xml/XMLIterationTest.java | 4 ++-- 5 files changed, 12 insertions(+), 19 deletions(-) diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/SemanticAnalyzer.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/SemanticAnalyzer.java index 0a59dc58acae..4217ed63181b 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/SemanticAnalyzer.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/SemanticAnalyzer.java @@ -4473,7 +4473,7 @@ private void handleForeachDefinitionVariables(VariableDefinitionNode variableDef // If the type node is available, we get the type from it. BType typeNodeType = symResolver.resolveTypeNode(variableNode.typeNode, blockEnv); // Checking whether the RHS type is assignable to LHS type. - if (types.constituentTypesAssignable(varType, typeNodeType)) { + if (types.isAssignable(varType, typeNodeType)) { if (onFail && varType == symTable.neverType) { varType = typeNodeType; } 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 d219507712f6..37b2bfaa50d7 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 @@ -1457,7 +1457,7 @@ private boolean isFunctionTypeAssignable(BInvokableType source, BInvokableType t boolean isTypeParam = TypeParamAnalyzer.isTypeParam(targetParam); if (isTypeParam) { - if (!constituentTypesAssignable(sourceParam, targetParam)) { + if (!isTypeParamAssignable(sourceParam, targetParam)) { return false; } } else { @@ -1482,15 +1482,9 @@ private boolean isFunctionTypeAssignable(BInvokableType source, BInvokableType t return checkFunctionTypeEquality(source, target, unresolvedTypes, (s, t, ut) -> isAssignable(t, s, ut)); } - public boolean constituentTypesAssignable(BType varType, BType typeNodeType) { - if (varType.tag == TypeTags.XML) { - typeNodeType = getReferredType(typeNodeType); - if (typeNodeType.tag == TypeTags.UNION) { - return isAssignable(symTable.xmlItemType, typeNodeType); - } - return typeNodeType.tag == TypeTags.XML; - } - return isAssignable(varType, typeNodeType); + private boolean isTypeParamAssignable(BType sourceParam, BType targetParam) { + return isAssignable(sourceParam, targetParam) || + (isAssignable(sourceParam, symTable.xmlType) && isAssignable(targetParam, sourceParam)); } public boolean isInherentlyImmutableType(BType type) { @@ -2072,8 +2066,7 @@ private BType getTypedBindingPatternTypeForXmlCollection(BType collectionType) { Set collectionTypes = getEffectiveMemberTypes((BUnionType) constraint); Set builtinXMLConstraintTypes = getEffectiveMemberTypes ((BUnionType) ((BXMLType) symTable.xmlType).constraint); - return collectionTypes.size() == 4 && builtinXMLConstraintTypes.equals(collectionTypes) ? - collectionType : BUnionType.create(null, (LinkedHashSet) collectionTypes); + return BUnionType.create(null, (LinkedHashSet) collectionTypes); default: return null; } 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 01c2e7513834..7c4dc1b4cadc 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 @@ -195,8 +195,8 @@ public void testNegativeScenarios() { validateError(negativeResult, index++, "incompatible types: expected '(Type1 & readonly)', found '([int,int]|string|[int,int])'", 258, 51); validateError(negativeResult, index++, - "incompatible types: expected '(xml & readonly)', found " + - "'xml'", 263, 41); + "incompatible types: expected '(xml & readonly)', found '" + + "(xml:Element|xml:Comment|xml:ProcessingInstruction|xml:Text)'", 263, 41); validateError(negativeResult, index++, "incompatible types: expected '(int[2] & readonly)', found 'int[2]'", 279, 69); validateError(negativeResult, index++, diff --git a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/query/XMLQueryExpressionTest.java b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/query/XMLQueryExpressionTest.java index 6fe2729ab2d9..25a74646d719 100644 --- a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/query/XMLQueryExpressionTest.java +++ b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/query/XMLQueryExpressionTest.java @@ -48,8 +48,8 @@ public void setup() { public void testNegativeQueryExprForXML() { int index = 0; validateError(negativeResult, index++, "incompatible types: expected " + - "'xml<((xml:Element|xml:Comment|xml:ProcessingInstruction|xml:Text) & readonly)> & readonly'," + - " found 'xml'", 21, 16); + "'xml<((xml:Element|xml:Comment|xml:ProcessingInstruction|xml:Text) & readonly)> & readonly'," + + " found '(xml:Element|xml:Comment|xml:ProcessingInstruction|xml:Text)'", 21, 16); validateError(negativeResult, index++, "incompatible types: expected 'xml:Element & readonly', " + "found 'xml:Element'", 25, 16); validateError(negativeResult, index++, diff --git a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/types/xml/XMLIterationTest.java b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/types/xml/XMLIterationTest.java index 03425577fffe..d82b6ce73e5d 100644 --- a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/types/xml/XMLIterationTest.java +++ b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/types/xml/XMLIterationTest.java @@ -51,8 +51,8 @@ public void testNegative() { int index = 0; BAssertUtil.validateError(negative, index++, - "invalid list binding pattern: attempted to infer a list type, but found 'xml'", - 13, 13); + "invalid list binding pattern: attempted to infer a list type, but found '" + + "(xml:Element|xml:Comment|xml:ProcessingInstruction|xml:Text)'", 13, 13); BAssertUtil.validateError(negative, index++, "incompatible types: " + "expected 'function (ballerina/lang.xml:0.0.0:ItemType) returns ()', " + "found 'function ([int,xml,string]) returns ()'", 18, 19);