From 5d0074bda5566bdc53d428bd18c0a5ce9459ac31 Mon Sep 17 00:00:00 2001 From: Dmitriy Novozhilov Date: Mon, 18 Feb 2019 12:38:36 +0300 Subject: [PATCH] Fix bug in try/catch resolution in NI Bug was presented in a236ad5 --- .../ControlStructureTypingVisitor.java | 6 +++-- .../controlStructures/tryFinallyGeneric.kt | 25 +++++++++++++++++++ .../codegen/BlackBoxCodegenTestGenerated.java | 5 ++++ .../LightAnalysisModeTestGenerated.java | 5 ++++ .../ir/IrBlackBoxCodegenTestGenerated.java | 5 ++++ 5 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 compiler/testData/codegen/box/controlStructures/tryFinallyGeneric.kt 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 5c5a42158ba..aedd3eca422 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/types/expressions/ControlStructureTypingVisitor.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/types/expressions/ControlStructureTypingVisitor.java @@ -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); diff --git a/compiler/testData/codegen/box/controlStructures/tryFinallyGeneric.kt b/compiler/testData/codegen/box/controlStructures/tryFinallyGeneric.kt new file mode 100644 index 00000000000..e65514bbf82 --- /dev/null +++ b/compiler/testData/codegen/box/controlStructures/tryFinallyGeneric.kt @@ -0,0 +1,25 @@ +// TARGET_BACKEND: JVM +// WITH_RUNTIME +// !LANGUAGE: +NewInference + +val stack = mutableListOf() + +fun MutableList.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" + } +} \ No newline at end of file diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java index 19b0e5b1423..e82e446b0f6 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java @@ -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) diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java index 679b5d35f67..d295b2cfa4a 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java @@ -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) diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java index 11b5cc9eefd..9a1d8c3a563 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java @@ -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)