Fix bug in try/catch resolution in NI

Bug was presented in a236ad5
This commit is contained in:
Dmitriy Novozhilov
2019-02-18 12:38:36 +03:00
parent 1e4b7e1ef1
commit 5d0074bda5
5 changed files with 44 additions and 2 deletions
@@ -646,7 +646,9 @@ public class ControlStructureTypingVisitor extends ExpressionTypingVisitor {
// it is not actually correct way (#KT-28370) of computing context, but it's how was in OI
// Fix of it is breaking change and allowed with NewDataFlowForTryExpressions language feature.
// See [processTryBranchesWithNewDataFlowAlgorithm] function
ExpressionTypingContext tryOutputContext = getCleanedContextFromTryWithAssignmentsToVar(tryExpression, nothingInAllCatchBranches, context);
ExpressionTypingContext tryOutputContext = getCleanedContextFromTryWithAssignmentsToVar(tryExpression, nothingInAllCatchBranches, context)
.replaceExpectedType(NO_EXPECTED_TYPE)
.replaceContextDependency(INDEPENDENT);
KotlinTypeInfo result = TypeInfoFactoryKt.createTypeInfo(resultType, tryOutputContext);
@@ -713,7 +715,7 @@ public class ControlStructureTypingVisitor extends ExpressionTypingVisitor {
PreliminaryLoopVisitor catchVisitor = PreliminaryLoopVisitor.visitCatchBlocks(tryExpression);
ExpressionTypingContext catchOutputContextFromAllBranches = tryOutputContext.replaceDataFlowInfo(
catchVisitor.clearDataFlowInfoForAssignedLocalVariables(dataFlowInfoAfterTry, components.languageVersionSettings)
);
).replaceContextDependency(INDEPENDENT).replaceExpectedType(NO_EXPECTED_TYPE);
KotlinTypeInfo finallyTypeInfo = facade.getTypeInfo(finallyBlock, catchOutputContextFromAllBranches);
DataFlowInfo finallyDataFlowInfo = finallyTypeInfo.getDataFlowInfo();
resultDataFlowInfo = finallyDataFlowInfo.and(nonExceptionalTryCatchesOutputInfo);
@@ -0,0 +1,25 @@
// TARGET_BACKEND: JVM
// WITH_RUNTIME
// !LANGUAGE: +NewInference
val stack = mutableListOf<Int>()
fun <E> MutableList<E>.pop() = this.removeAt(size - 1)
fun foo() {}
fun getBoolean(): Boolean = true
fun box(): String {
val b = getBoolean()
if (b) {
stack.add(1)
try {
return "OK"
} finally {
stack.pop()
}
} else {
return "OK"
}
}
@@ -4888,6 +4888,11 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
runTest("compiler/testData/codegen/box/controlStructures/tryCatchFinallyChain.kt");
}
@TestMetadata("tryFinallyGeneric.kt")
public void testTryFinallyGeneric() throws Exception {
runTest("compiler/testData/codegen/box/controlStructures/tryFinallyGeneric.kt");
}
@TestMetadata("compiler/testData/codegen/box/controlStructures/breakContinueInExpressions")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
@@ -4888,6 +4888,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes
runTest("compiler/testData/codegen/box/controlStructures/tryCatchFinallyChain.kt");
}
@TestMetadata("tryFinallyGeneric.kt")
public void testTryFinallyGeneric() throws Exception {
runTest("compiler/testData/codegen/box/controlStructures/tryFinallyGeneric.kt");
}
@TestMetadata("compiler/testData/codegen/box/controlStructures/breakContinueInExpressions")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
@@ -4888,6 +4888,11 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
runTest("compiler/testData/codegen/box/controlStructures/tryCatchFinallyChain.kt");
}
@TestMetadata("tryFinallyGeneric.kt")
public void testTryFinallyGeneric() throws Exception {
runTest("compiler/testData/codegen/box/controlStructures/tryFinallyGeneric.kt");
}
@TestMetadata("compiler/testData/codegen/box/controlStructures/breakContinueInExpressions")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)