Fix too aggressive data-flow clearing of loop-assigned vars This is effectively a revert of 447c127036b9902de4f9f7f4841684a2a40be3b9, which was an (incorrect) fix for KT-22379. Actually, KT-22379 implies that we should clear data-flow which we got from `checkCondition` (it contains information just that condition expression is executed), while actual fix cleared data-flow after applyng knowledge that condition should be 'false' after loop is finished. KT-22379 Open KT-27084 Fixed
diff --git a/compiler/frontend/src/org/jetbrains/kotlin/types/expressions/ControlStructureTypingVisitor.java b/compiler/frontend/src/org/jetbrains/kotlin/types/expressions/ControlStructureTypingVisitor.java index 4c8e3b4..212ccd3 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/types/expressions/ControlStructureTypingVisitor.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/types/expressions/ControlStructureTypingVisitor.java
@@ -286,16 +286,9 @@ dataFlowInfo = dataFlowInfo.and(loopVisitor.clearDataFlowInfoForAssignedLocalVariables(bodyTypeInfo.getJumpFlowInfo(), components.languageVersionSettings)); } - - DataFlowInfo conservativeInfoAfterLoop = - components.languageVersionSettings.supportsFeature(LanguageFeature.SoundSmartcastFromLoopConditionForLoopAssignedVariables) - ? loopVisitor.clearDataFlowInfoForAssignedLocalVariables(dataFlowInfo, components.languageVersionSettings) - : dataFlowInfo; - - return components.dataFlowAnalyzer .checkType(bodyTypeInfo.replaceType(components.builtIns.getUnitType()), expression, contextWithExpectedType) - .replaceDataFlowInfo(conservativeInfoAfterLoop); + .replaceDataFlowInfo(dataFlowInfo); } private boolean containsJumpOutOfLoop(@NotNull KtExpression expression, ExpressionTypingContext context) { @@ -466,14 +459,9 @@ bodyTypeInfo = loopRangeInfo; } - DataFlowInfo conservativeInfoAfterLoop = - components.languageVersionSettings.supportsFeature(LanguageFeature.SoundSmartcastFromLoopConditionForLoopAssignedVariables) - ? loopVisitor.clearDataFlowInfoForAssignedLocalVariables(loopRangeInfo.getDataFlowInfo(), components.languageVersionSettings) - : loopRangeInfo.getDataFlowInfo(); - return components.dataFlowAnalyzer .checkType(bodyTypeInfo.replaceType(components.builtIns.getUnitType()), expression, contextWithExpectedType) - .replaceDataFlowInfo(conservativeInfoAfterLoop); + .replaceDataFlowInfo(loopRangeInfo.getDataFlowInfo()); } private VariableDescriptor createLoopParameterDescriptor(
diff --git a/compiler/testData/diagnostics/tests/smartCasts/loops/whileNullAssignToSomething.kt b/compiler/testData/diagnostics/tests/smartCasts/loops/whileNullAssignToSomething.kt index 44bb1f3..ba9432e 100644 --- a/compiler/testData/diagnostics/tests/smartCasts/loops/whileNullAssignToSomething.kt +++ b/compiler/testData/diagnostics/tests/smartCasts/loops/whileNullAssignToSomething.kt
@@ -5,5 +5,5 @@ if (i == 10) result = "non null" else i++ } - result<!UNSAFE_CALL!>.<!>length + <!DEBUG_INFO_SMARTCAST!>result<!>.length } \ No newline at end of file
diff --git a/compiler/util/src/org/jetbrains/kotlin/config/LanguageVersionSettings.kt b/compiler/util/src/org/jetbrains/kotlin/config/LanguageVersionSettings.kt index d6ee202..c122628 100644 --- a/compiler/util/src/org/jetbrains/kotlin/config/LanguageVersionSettings.kt +++ b/compiler/util/src/org/jetbrains/kotlin/config/LanguageVersionSettings.kt
@@ -84,7 +84,6 @@ NormalizeConstructorCalls(KOTLIN_1_3), StrictJavaNullabilityAssertions(KOTLIN_1_3, kind = BUG_FIX), SoundSmartcastForEnumEntries(KOTLIN_1_3, kind = BUG_FIX), - SoundSmartcastFromLoopConditionForLoopAssignedVariables(KOTLIN_1_3, kind = BUG_FIX), DslMarkerOnFunctionTypeReceiver(KOTLIN_1_4, kind = BUG_FIX), ProhibitErroneousExpressionsInAnnotationsWithUseSiteTargets(KOTLIN_1_3, kind = BUG_FIX), NewCapturedReceiverFieldNamingConvention(KOTLIN_1_3, kind = BUG_FIX), @@ -94,6 +93,8 @@ RestrictReturnStatementTarget(KOTLIN_1_4, kind = BUG_FIX), NoConstantValueAttributeForNonConstVals(KOTLIN_1_4, kind = BUG_FIX), + // Temporarily disabled, see KT-27084/KT-22379 + SoundSmartcastFromLoopConditionForLoopAssignedVariables(sinceVersion = null, kind = BUG_FIX), ProperIeee754Comparisons(sinceVersion = null, defaultState = State.DISABLED, kind = BUG_FIX), // Experimental features