Compare commits

...

2 Commits

Author SHA1 Message Date
Dmitry Savvinov
7548f95c02 Fix too aggressive data-flow clearing of loop-assigned vars
This is effectively a revert of
447c127036, 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
2018-09-25 11:08:05 +03:00
Dmitry Savvinov
f0169b3541 Add test on smartcast from while-assigned var
Currently behavior is undesired, it will be fixed in the next commit
(see KT-27084)
2018-09-24 18:36:49 +03:00
6 changed files with 26 additions and 15 deletions

View File

@@ -286,16 +286,9 @@ public class ControlStructureTypingVisitor extends ExpressionTypingVisitor {
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 @@ public class ControlStructureTypingVisitor extends ExpressionTypingVisitor {
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(

View File

@@ -0,0 +1,9 @@
fun main(args: Array<String>) {
var result: String? = null
var i = 0
while (result == null) {
if (i == 10) result = "non null"
else i++
}
<!DEBUG_INFO_SMARTCAST!>result<!>.length
}

View File

@@ -0,0 +1,3 @@
package
public fun main(/*0*/ args: kotlin.Array<kotlin.String>): kotlin.Unit

View File

@@ -19916,6 +19916,11 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest {
runTest("compiler/testData/diagnostics/tests/smartCasts/loops/whileNull.kt");
}
@TestMetadata("whileNullAssignToSomething.kt")
public void testWhileNullAssignToSomething() throws Exception {
runTest("compiler/testData/diagnostics/tests/smartCasts/loops/whileNullAssignToSomething.kt");
}
@TestMetadata("whileNullWithBreak.kt")
public void testWhileNullWithBreak() throws Exception {
runTest("compiler/testData/diagnostics/tests/smartCasts/loops/whileNullWithBreak.kt");

View File

@@ -19916,6 +19916,11 @@ public class DiagnosticsUsingJavacTestGenerated extends AbstractDiagnosticsUsing
runTest("compiler/testData/diagnostics/tests/smartCasts/loops/whileNull.kt");
}
@TestMetadata("whileNullAssignToSomething.kt")
public void testWhileNullAssignToSomething() throws Exception {
runTest("compiler/testData/diagnostics/tests/smartCasts/loops/whileNullAssignToSomething.kt");
}
@TestMetadata("whileNullWithBreak.kt")
public void testWhileNullWithBreak() throws Exception {
runTest("compiler/testData/diagnostics/tests/smartCasts/loops/whileNullWithBreak.kt");

View File

@@ -84,7 +84,6 @@ enum class LanguageFeature(
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 @@ enum class LanguageFeature(
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