Skip to content

Commit

Permalink
Avoid BUnionType's getMemberTypes() usages part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
lochana-chathura committed Oct 3, 2024
1 parent 4f137ee commit 9af942a
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import io.ballerina.identifier.Utils;
import io.ballerina.tools.diagnostics.Location;
import io.ballerina.types.PredefinedType;
import org.ballerinalang.compiler.BLangCompilerException;
import org.ballerinalang.model.elements.Flag;
import org.ballerinalang.model.elements.PackageID;
Expand Down Expand Up @@ -53,6 +54,7 @@
import org.wso2.ballerinalang.compiler.bir.model.VarKind;
import org.wso2.ballerinalang.compiler.bir.model.VarScope;
import org.wso2.ballerinalang.compiler.diagnostic.BLangDiagnosticLocation;
import org.wso2.ballerinalang.compiler.semantics.analyzer.SemTypeHelper;
import org.wso2.ballerinalang.compiler.semantics.model.Scope;
import org.wso2.ballerinalang.compiler.semantics.model.SymbolTable;
import org.wso2.ballerinalang.compiler.semantics.model.symbols.BAttachedFunction;
Expand Down Expand Up @@ -985,20 +987,7 @@ private boolean isObservable(Call callIns) {
* @return True if an error can be assigned and false otherwise
*/
private boolean isErrorAssignable(BIRVariableDcl variableDcl) {
boolean isErrorAssignable = false;
if (variableDcl.type instanceof BUnionType returnUnionType) {
boolean b = false;
for (BType type : returnUnionType.getMemberTypes()) {
if (type instanceof BErrorType) {
b = true;
break;
}
}
isErrorAssignable = b;
} else if (variableDcl.type instanceof BErrorType) {
isErrorAssignable = true;
}
return isErrorAssignable;
return SemTypeHelper.containsBasicType(variableDcl.type, PredefinedType.ERROR);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6351,7 +6351,7 @@ private void rewriteFieldBasedAccess(BLangFieldBasedAccess fieldAccessExpr) {
// can change the type of the expression, if it is type narrowed.
BType varRefType = types.getTypeWithEffectiveIntersectionTypes(fieldAccessExpr.expr.getBType());
fieldAccessExpr.expr = rewriteExpr(fieldAccessExpr.expr);
if (!types.isSameType(fieldAccessExpr.expr.getBType(), varRefType)) {
if (!types.isSameType2(fieldAccessExpr.expr.getBType(), varRefType)) {
fieldAccessExpr.expr = types.addConversionExprIfRequired(fieldAccessExpr.expr, varRefType);
}

Expand Down Expand Up @@ -6674,7 +6674,7 @@ public void visit(BLangIndexBasedAccess indexAccessExpr) {
BType effectiveType = types.getTypeWithEffectiveIntersectionTypes(indexAccessExpr.expr.getBType());
BType varRefType = Types.getImpliedType(effectiveType);
indexAccessExpr.expr = rewriteExpr(indexAccessExpr.expr);
if (!types.isSameType(indexAccessExpr.expr.getBType(), varRefType)) {
if (!types.isSameType2(indexAccessExpr.expr.getBType(), varRefType)) {
indexAccessExpr.expr = types.addConversionExprIfRequired(indexAccessExpr.expr, varRefType);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import io.ballerina.identifier.Utils;
import io.ballerina.tools.diagnostics.Location;
import io.ballerina.types.Core;
import io.ballerina.types.PredefinedType;
import io.ballerina.types.SemType;
import io.ballerina.types.Value;
import org.ballerinalang.compiler.CompilerPhase;
import org.ballerinalang.model.elements.Flag;
Expand Down Expand Up @@ -2271,25 +2273,8 @@ private void addErrorTypesToSet(BType returnType, LinkedHashSet<BType> errorType
}

private boolean hasNonErrorType(BType returnType) {
if (returnType == null) {
return false;
}

BType effType = Types.getImpliedType(types.getTypeWithEffectiveIntersectionTypes(returnType));
if (effType.tag == TypeTags.ERROR) {
return false;
}

if (effType.tag == TypeTags.UNION) {
for (BType memberType : ((BUnionType) returnType).getMemberTypes()) {
if (hasNonErrorType(memberType)) {
return true;
}
}
return false;
}

return true;
SemType s = SemTypeHelper.semType(returnType);
return !Core.isEmpty(types.typeCtx(), Core.diff(s, PredefinedType.ERROR));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ private void notifyServiceTypeProcessors(BLangService serviceNode, List<BLangAnn
BType listenerType;
if ((listenerType = serviceListenerMap.get(plugin)) != null) {
for (BLangExpression expr : serviceNode.getAttachedExprs()) {
if (!types.isSameType(expr.getBType(), listenerType)) {
if (!types.isSameType2(expr.getBType(), listenerType)) {
continue;
}
isCurrentPluginProcessed = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.wso2.ballerinalang.compiler.semantics.analyzer;

import io.ballerina.tools.diagnostics.Location;
import io.ballerina.types.PredefinedType;
import org.ballerinalang.compiler.CompilerPhase;
import org.ballerinalang.model.elements.Flag;
import org.ballerinalang.model.symbols.SymbolKind;
Expand Down Expand Up @@ -2717,7 +2718,7 @@ private void checkFinalObjectFieldUpdate(BLangFieldBasedAccess fieldAccess) {

BType exprType = Types.getImpliedType(expr.getBType());

if (types.isSubTypeOfBaseType(exprType, TypeTags.OBJECT) &&
if (types.isSubTypeOfBaseType(exprType, PredefinedType.OBJECT) &&
isFinalFieldInAllObjects(fieldAccess.pos, exprType, fieldAccess.field.value)) {
dlog.error(fieldAccess.pos, DiagnosticErrorCode.CANNOT_UPDATE_FINAL_OBJECT_FIELD,
fieldAccess.symbol.originalName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.wso2.ballerinalang.compiler.semantics.analyzer;

import io.ballerina.tools.diagnostics.Location;
import io.ballerina.types.PredefinedType;
import org.ballerinalang.model.clauses.OrderKeyNode;
import org.ballerinalang.model.elements.PackageID;
import org.ballerinalang.model.symbols.SymbolOrigin;
Expand Down Expand Up @@ -671,12 +672,12 @@ private BType getNonContextualQueryType(BType constraintType, BType basicType, L
dlog.error(pos, INVALID_QUERY_CONSTRUCT_INFERRED_MAP);
return symTable.semanticError;
case TypeTags.XML:
if (types.isSubTypeOfBaseType(constraintType, symTable.xmlType.tag)) {
if (types.isSubTypeOfBaseType(constraintType, PredefinedType.XML)) {
return new BXMLType(constraintType, null);
}
break;
case TypeTags.STRING:
if (types.isSubTypeOfBaseType(constraintType, TypeTags.STRING)) {
if (types.isSubTypeOfBaseType(constraintType, PredefinedType.STRING)) {
return symTable.stringType;
}
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import io.ballerina.compiler.api.symbols.DiagnosticState;
import io.ballerina.projects.ModuleDescriptor;
import io.ballerina.tools.diagnostics.Location;
import io.ballerina.types.PredefinedType;
import org.ballerinalang.compiler.CompilerPhase;
import org.ballerinalang.model.TreeBuilder;
import org.ballerinalang.model.elements.AttachPoint;
Expand Down Expand Up @@ -1413,21 +1414,9 @@ private boolean isSupportedConfigType(BType type, List<String> errors, String va
}

private boolean isNilableDefaultField(BField field, BType fieldType) {
if (!Symbols.isFlagOn(field.symbol.flags, Flags.REQUIRED) && !Symbols.isFlagOn(field.symbol.flags,
Flags.OPTIONAL)) {
if (fieldType.tag == TypeTags.NIL) {
return true;
}
if (fieldType.tag == TypeTags.UNION) {
BUnionType unionType = (BUnionType) fieldType;
for (BType memberType : unionType.getMemberTypes()) {
if (memberType.tag == TypeTags.NIL) {
return true;
}
}
}
}
return false;
return !Symbols.isFlagOn(field.symbol.flags, Flags.REQUIRED) &&
!Symbols.isFlagOn(field.symbol.flags, Flags.OPTIONAL) &&
fieldType.isNullable();
}

private void validateListenerCompatibility(BLangSimpleVariable varNode, BType rhsType) {
Expand Down Expand Up @@ -2066,7 +2055,7 @@ void handleDeclaredVarInForeach(BLangVariable variable, BType rhsType, SymbolEnv
BLangTupleVariable tupleVariable = (BLangTupleVariable) variable;
if ((TypeTags.TUPLE != referredRhsType.tag && TypeTags.ARRAY != referredRhsType.tag &&
TypeTags.UNION != referredRhsType.tag) ||
(variable.isDeclaredWithVar && !types.isSubTypeOfBaseType(rhsType, TypeTags.TUPLE))) {
(variable.isDeclaredWithVar && !types.isSubTypeOfBaseType(rhsType, PredefinedType.LIST))) {
dlog.error(variable.pos, DiagnosticErrorCode.INVALID_LIST_BINDING_PATTERN_INFERENCE, rhsType);
recursivelyDefineVariables(tupleVariable, blockEnv);
return;
Expand Down Expand Up @@ -3983,7 +3972,7 @@ public void visit(BLangFail failNode, AnalyzerData data) {
}
}
if (errorExpressionType != symTable.semanticError &&
!types.isSubTypeOfBaseType(errorExpressionType, symTable.errorType.tag)) {
!types.isSubTypeOfBaseType(errorExpressionType, PredefinedType.ERROR)) {
dlog.error(errorExpression.pos, DiagnosticErrorCode.ERROR_TYPE_EXPECTED, errorExpressionType);
}
data.notCompletedNormally = true;
Expand Down Expand Up @@ -4932,7 +4921,7 @@ private void validateIsolatedParamUsage(boolean inIsolatedFunction, BLangSimpleV

BType type = isRestParam ? ((BArrayType) variable.getBType()).eType : variable.getBType();

if (!types.isSubTypeOfBaseType(type, TypeTags.INVOKABLE)) {
if (!types.isSubTypeOfBaseType(type, PredefinedType.FUNCTION)) {
dlog.error(variable.pos, DiagnosticErrorCode.ISOLATED_PARAM_USED_WITH_INVALID_TYPE);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4333,7 +4333,7 @@ private boolean hasDifferentTypeThanRest(BTupleType tupleType) {
}

for (BType member : tupleType.getTupleTypes()) {
if (!types.isSameType(tupleType.restType, member)) {
if (!types.isSameType2(tupleType.restType, member)) {
return true;
}
}
Expand Down Expand Up @@ -5437,17 +5437,13 @@ private BType getXmlStringBinaryOpResultType(BType opType, BType constituentType
}

public boolean isOptionalFloatOrDecimal(BType expectedType) {
if (expectedType.tag == TypeTags.UNION && expectedType.isNullable() && expectedType.tag != TypeTags.ANY) {
Iterator<BType> memberTypeIterator = ((BUnionType) expectedType).getMemberTypes().iterator();
while (memberTypeIterator.hasNext()) {
BType memberType = Types.getImpliedType(memberTypeIterator.next());
if (memberType.tag == TypeTags.FLOAT || memberType.tag == TypeTags.DECIMAL) {
return true;
}
}

if (!expectedType.isNullable()) {
return false;
}
return false;

SemType s = SemTypeHelper.semType(expectedType);
SemType t = Core.diff(s, PredefinedType.NIL);
return PredefinedType.FLOAT.equals(t) || PredefinedType.DECIMAL.equals(t);
}

private BType checkAndGetType(BLangExpression expr, SymbolEnv env, BLangBinaryExpr binaryExpr, AnalyzerData data) {
Expand Down Expand Up @@ -8653,7 +8649,7 @@ private BType getLaxFieldAccessType(BType exprType) {
return ((BMapType) exprType).constraint;
case TypeTags.UNION:
BUnionType unionType = (BUnionType) exprType;
if (types.isSameType(symTable.jsonType, unionType)) {
if (types.isSameType(Core.createJson(types.semTypeCtx), unionType.semType())) {
return symTable.jsonType;
}
LinkedHashSet<BType> memberTypes = new LinkedHashSet<>();
Expand Down Expand Up @@ -9594,10 +9590,11 @@ private boolean isUniqueType(Iterable<BType> typeList, BType type) {
for (BType bType : typeList) {
bType = Types.getImpliedType(bType);
if (isRecord) {
// Seems defaultable values too are considered when checking uniqueness.
if (type == bType) {
return false;
}
} else if (types.isSameType(type, bType)) {
} else if (types.isSameType2(type, bType)) {
return false;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.wso2.ballerinalang.compiler.semantics.analyzer;

import io.ballerina.tools.diagnostics.Location;
import io.ballerina.types.PredefinedType;
import org.ballerinalang.model.Name;
import org.ballerinalang.model.elements.PackageID;
import org.ballerinalang.model.tree.NodeKind;
Expand Down Expand Up @@ -771,7 +772,7 @@ private void findTypeParamInError(Location loc, BErrorType expType, BType actual
findTypeParam(loc, expType.detailType, ((BErrorType) actualType).detailType, env, resolvedTypes,
result);
}
if (actualType.tag == TypeTags.UNION && types.isSubTypeOfBaseType(actualType, TypeTags.ERROR)) {
if (actualType.tag == TypeTags.UNION && types.isSubTypeOfBaseType(actualType, PredefinedType.ERROR)) {
BUnionType errorUnion = (BUnionType) actualType;
LinkedHashSet<BType> errorDetailTypes = new LinkedHashSet<>();
for (BType errorType : errorUnion.getMemberTypes()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import io.ballerina.types.PredefinedType;
import io.ballerina.types.SemType;
import io.ballerina.types.SemTypes;
import io.ballerina.types.definition.ObjectDefinition;
import io.ballerina.types.definition.ObjectQualifiers;
import org.ballerinalang.model.Name;
import org.ballerinalang.model.TreeBuilder;
import org.ballerinalang.model.elements.Flag;
Expand Down Expand Up @@ -362,6 +364,14 @@ private boolean isLaxType(SemType t) {
return matList.stream().allMatch(mat -> mat.names().length == 0 && isLaxType(Core.cellInnerVal(mat.rest())));
}

public boolean isSameType2(BType source, BType target) {
return isSameType(SemTypeHelper.semType(source), SemTypeHelper.semType(target));
}

public boolean isSameType(SemType source, SemType target) {
return SemTypes.isSameType(semTypeCtx, source, target);
}

public boolean isSameType(BType source, BType target) {
return isSameType(source, target, new HashSet<>());
}
Expand Down Expand Up @@ -716,6 +726,14 @@ public boolean isSubTypeOfMapping(BType bType) {
return SemTypeHelper.isSubtypeSimpleNotNever(bType, PredefinedType.MAPPING);
}

public boolean isSubTypeOfBaseType(BType bType, BasicTypeBitSet bbs) {
return SemTypeHelper.isSubtypeSimpleNotNever(bType, bbs);
}

/**
* @deprecated Use {@link #isSubTypeOfBaseType(BType, BasicTypeBitSet)} instead.
*/
@Deprecated
public boolean isSubTypeOfBaseType(BType bType, int baseTypeTag) {
BType type = getImpliedType(bType);

Expand Down Expand Up @@ -5127,7 +5145,7 @@ private boolean checkFillerValue(BUnionType type) {
}

private boolean isSameBasicType(BType source, BType target) {
if (isSameType(source, target)) {
if (isSameType2(source, target)) {
return true;
}
int sourceTag = getImpliedType(source).tag;
Expand Down Expand Up @@ -5486,7 +5504,7 @@ public static String getPackageIdString(PackageID packageID) {
return packageID.isTestPkg ? packageID.toString() + "_testable" : packageID.toString();
}

private static class ListenerValidationModel {
private class ListenerValidationModel {
private final Types types;
private final SymbolTable symtable;
private final BType serviceNameType;
Expand Down Expand Up @@ -5606,21 +5624,9 @@ private boolean checkAttachMethod(BAttachedFunction func) {
}

private boolean isServiceObject(BType bType) {
BType type = getImpliedType(bType);
if (type.tag == TypeTags.UNION) {
for (BType memberType : ((BUnionType) type).getMemberTypes()) {
if (!isServiceObject(memberType)) {
return false;
}
}
return true;
}

if (type.tag != TypeTags.OBJECT) {
return false;
}

return Symbols.isService(type.tsymbol);
ObjectQualifiers quals = new ObjectQualifiers(false, false, ObjectQualifiers.NetworkQualifier.Service);
SemType serviceObjTy = new ObjectDefinition().define(typeEnv(), quals, new ArrayList<>(0));
return types.isSubtype(bType, serviceObjTy);
}
}

Expand Down Expand Up @@ -6059,4 +6065,8 @@ public boolean isMappingConstructorCompatibleType(BType type) {
public Env typeEnv() {
return semTypeCtx.env;
}

public Context typeCtx() {
return semTypeCtx;
}
}

0 comments on commit 9af942a

Please sign in to comment.