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 c20b7447571f..35889a40e7aa 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 @@ -603,7 +603,7 @@ private BType silentIntTypeCheck(BLangLiteral literalExpr, Object literalValue, AnalyzerData data) { boolean prevNonErrorLoggingCheck = data.commonAnalyzerData.nonErrorLoggingCheck; data.commonAnalyzerData.nonErrorLoggingCheck = true; - GlobalStateData previousGlobalState = getGlobalStateSnapshotAndResetGlobalState(); + GlobalStateSnapshot previousGlobalState = getGlobalStateSnapshotAndResetGlobalState(); this.dlog.mute(); BType exprCompatibleType = getIntegerLiteralType(nodeCloner.cloneNode(literalExpr), literalValue, expType, @@ -1226,7 +1226,7 @@ public void visit(BLangTableConstructorExpr tableConstructorExpr, AnalyzerData d boolean prevNonErrorLoggingCheck = data.commonAnalyzerData.nonErrorLoggingCheck; data.commonAnalyzerData.nonErrorLoggingCheck = true; - GlobalStateData previousGlobalState = getGlobalStateSnapshotAndResetGlobalState(); + GlobalStateSnapshot previousGlobalState = getGlobalStateSnapshotAndResetGlobalState(); this.dlog.mute(); List matchingTypes = new ArrayList<>(); @@ -1805,7 +1805,7 @@ private BType checkListConstructorCompatibility(BType referredType, BType origin int tag = referredType.tag; if (tag == TypeTags.UNION) { boolean prevNonErrorLoggingCheck = data.commonAnalyzerData.nonErrorLoggingCheck; - GlobalStateData previousGlobalState = getGlobalStateSnapshotAndResetGlobalState(); + GlobalStateSnapshot previousGlobalState = getGlobalStateSnapshotAndResetGlobalState(); data.commonAnalyzerData.nonErrorLoggingCheck = true; this.dlog.mute(); @@ -2547,7 +2547,7 @@ public BType checkMappingConstructorCompatibility(BType bType, BLangRecordLitera if (tag == TypeTags.UNION) { boolean prevNonErrorLoggingCheck = data.commonAnalyzerData.nonErrorLoggingCheck; data.commonAnalyzerData.nonErrorLoggingCheck = true; - GlobalStateData previousGlobalState = getGlobalStateSnapshotAndResetGlobalState(); + GlobalStateSnapshot previousGlobalState = getGlobalStateSnapshotAndResetGlobalState(); this.dlog.mute(); List compatibleTypes = new ArrayList<>(); @@ -3931,7 +3931,7 @@ protected BType checkExprSilent(BLangExpression expr, BType expType, AnalyzerDat boolean prevNonErrorLoggingCheck = data.commonAnalyzerData.nonErrorLoggingCheck; data.commonAnalyzerData.nonErrorLoggingCheck = true; this.dlog.mute(); - GlobalStateData previousGlobalState = getGlobalStateSnapshotAndResetGlobalState(); + GlobalStateSnapshot previousGlobalState = getGlobalStateSnapshotAndResetGlobalState(); BType type = checkExpr(expr, expType, data); @@ -5471,7 +5471,7 @@ public boolean isOptionalFloatOrDecimal(BType expectedType) { private BType checkAndGetType(BLangExpression expr, SymbolEnv env, BLangBinaryExpr binaryExpr, AnalyzerData data) { boolean prevNonErrorLoggingCheck = data.commonAnalyzerData.nonErrorLoggingCheck; data.commonAnalyzerData.nonErrorLoggingCheck = true; - GlobalStateData previousGlobalState = getGlobalStateSnapshotAndResetGlobalState(); + GlobalStateSnapshot previousGlobalState = getGlobalStateSnapshotAndResetGlobalState(); this.dlog.mute(); expr.cloneAttempt++; @@ -5732,7 +5732,7 @@ public boolean silentCompatibleFiniteMembersInUnionTypeCheck(BLangUnaryExpr unar AnalyzerData data) { boolean prevNonErrorLoggingCheck = data.commonAnalyzerData.nonErrorLoggingCheck; data.commonAnalyzerData.nonErrorLoggingCheck = true; - GlobalStateData previousGlobalState = getGlobalStateSnapshotAndResetGlobalState(); + GlobalStateSnapshot previousGlobalState = getGlobalStateSnapshotAndResetGlobalState(); this.dlog.mute(); BType compatibleTypeOfUnaryExpression; @@ -5748,7 +5748,7 @@ public boolean silentCompatibleFiniteMembersInUnionTypeCheck(BLangUnaryExpr unar return false; } - private void unmuteDlog(AnalyzerData data, boolean prevNonErrorLoggingCheck, GlobalStateData previousGlobalState) { + private void unmuteDlog(AnalyzerData data, boolean prevNonErrorLoggingCheck, GlobalStateSnapshot previousGlobalState) { data.commonAnalyzerData.nonErrorLoggingCheck = prevNonErrorLoggingCheck; restoreGlobalState(previousGlobalState); if (!prevNonErrorLoggingCheck) { @@ -5759,7 +5759,7 @@ private void unmuteDlog(AnalyzerData data, boolean prevNonErrorLoggingCheck, Glo public BType silentTypeCheckExpr(BLangExpression expr, BType referredType, AnalyzerData data) { boolean prevNonErrorLoggingCheck = data.commonAnalyzerData.nonErrorLoggingCheck; data.commonAnalyzerData.nonErrorLoggingCheck = true; - GlobalStateData previousGlobalState = getGlobalStateSnapshotAndResetGlobalState(); + GlobalStateSnapshot previousGlobalState = getGlobalStateSnapshotAndResetGlobalState(); this.dlog.mute(); BType exprCompatibleType = checkExpr(nodeCloner.cloneNode(expr), referredType, data); @@ -5822,7 +5822,7 @@ public void visit(BLangTypeConversionExpr conversionExpr, AnalyzerData data) { boolean prevNonErrorLoggingCheck = data.commonAnalyzerData.nonErrorLoggingCheck; data.commonAnalyzerData.nonErrorLoggingCheck = true; - GlobalStateData previousGlobalState = getGlobalStateSnapshotAndResetGlobalState(); + GlobalStateSnapshot previousGlobalState = getGlobalStateSnapshotAndResetGlobalState(); this.dlog.mute(); BType exprCompatibleType = checkExpr(nodeCloner.cloneNode(expr), targetType, data); @@ -6608,7 +6608,7 @@ private BType getCandidateType(BLangCheckedExpr checkedExpr, BType checkExprCand boolean prevNonErrorLoggingCheck = data.commonAnalyzerData.nonErrorLoggingCheck; data.commonAnalyzerData.nonErrorLoggingCheck = true; this.dlog.mute(); - GlobalStateData previousGlobalState = getGlobalStateSnapshotAndResetGlobalState(); + GlobalStateSnapshot previousGlobalState = getGlobalStateSnapshotAndResetGlobalState(); checkedExpr.expr.cloneAttempt++; BLangExpression clone = nodeCloner.cloneNode(checkedExpr.expr); @@ -8240,7 +8240,7 @@ private List concatSimilarKindXMLNodes(List ex for (BLangExpression expr : exprs) { boolean prevNonErrorLoggingCheck = data.commonAnalyzerData.nonErrorLoggingCheck; data.commonAnalyzerData.nonErrorLoggingCheck = true; - GlobalStateData previousGlobalState = getGlobalStateSnapshotAndResetGlobalState(); + GlobalStateSnapshot previousGlobalState = getGlobalStateSnapshotAndResetGlobalState(); this.dlog.mute(); BType exprType = checkExpr(nodeCloner.cloneNode(expr), xmlElementEnv, symTable.xmlType, data); @@ -9914,20 +9914,21 @@ String recordsToString(Set recordTypeSet) { } } - public GlobalStateData getGlobalStateSnapshotAndResetGlobalState() { + public GlobalStateSnapshot getGlobalStateSnapshotAndResetGlobalState() { // Preserve global state - GlobalStateData globalStateData = new GlobalStateData(typeResolver.unknownTypeRefs, this.dlog.errorCount()); + GlobalStateSnapshot globalStateSnapshot = new GlobalStateSnapshot(typeResolver.getUnknownTypeRefs(), + this.dlog.errorCount()); // Reset global state - typeResolver.unknownTypeRefs = new HashSet<>(); + typeResolver.setUnknownTypeRefs(new HashSet<>()); this.dlog.resetErrorCount(); - return globalStateData; + return globalStateSnapshot; } - public void restoreGlobalState(GlobalStateData globalStateData) { - typeResolver.unknownTypeRefs = globalStateData.unknownTypeRefs; - this.dlog.setErrorCount(globalStateData.errorCount); + public void restoreGlobalState(GlobalStateSnapshot globalStateSnapshot) { + typeResolver.setUnknownTypeRefs(globalStateSnapshot.unknownTypeRefs); + this.dlog.setErrorCount(globalStateSnapshot.errorCount); } /** @@ -9947,8 +9948,9 @@ public static class AnalyzerData { } /** + * This record is used to hold a snapshot of the global fields of multiple class objects. * @since 2201.12.0 */ - public record GlobalStateData(HashSet unknownTypeRefs, int errorCount) { + public record GlobalStateSnapshot(HashSet unknownTypeRefs, int errorCount) { } } diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/TypeResolver.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/TypeResolver.java index 08df6895c5c4..e1b6179cfe28 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/TypeResolver.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/TypeResolver.java @@ -164,7 +164,7 @@ public class TypeResolver { private final HashSet resolvedClassDef = new HashSet<>(); private final Map modTable = new LinkedHashMap<>(); private final Map constantMap = new HashMap<>(); - public HashSet unknownTypeRefs; + private HashSet unknownTypeRefs; private SymbolEnv pkgEnv; private int currentDepth; private Deque resolvingTypes; @@ -211,6 +211,14 @@ public static TypeResolver getInstance(CompilerContext context) { return typeResolver; } + public HashSet getUnknownTypeRefs() { + return unknownTypeRefs; + } + + public void setUnknownTypeRefs(HashSet unknownTypeRefs) { + this.unknownTypeRefs = unknownTypeRefs; + } + public void defineBTypes(List moduleDefs, SymbolEnv pkgEnv) { this.pkgEnv = pkgEnv; typePrecedence = 0;