From 1acce46133b0306f0254ac2c799625beaa4de1a7 Mon Sep 17 00:00:00 2001 From: Mikhail Zarechenskiy Date: Mon, 10 Feb 2020 08:59:38 +0300 Subject: [PATCH] [NI] Fix recording info about captured local variables --- .../inference/CoroutineInferenceSession.kt | 9 ++-- ...utableLocalVariableInsideCoroutineBlock.kt | 44 +++++++++++++++++++ .../codegen/BlackBoxCodegenTestGenerated.java | 10 +++++ .../LightAnalysisModeTestGenerated.java | 10 +++++ .../ir/FirBlackBoxCodegenTestGenerated.java | 10 +++++ .../ir/IrBlackBoxCodegenTestGenerated.java | 10 +++++ .../IrJsCodegenBoxTestGenerated.java | 5 +++ .../semantics/JsCodegenBoxTestGenerated.java | 5 +++ 8 files changed, 99 insertions(+), 4 deletions(-) create mode 100644 compiler/testData/codegen/box/coroutines/captureMutableLocalVariableInsideCoroutineBlock.kt diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/inference/CoroutineInferenceSession.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/inference/CoroutineInferenceSession.kt index b01a224789c..45fd2302e07 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/inference/CoroutineInferenceSession.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/inference/CoroutineInferenceSession.kt @@ -8,6 +8,7 @@ package org.jetbrains.kotlin.resolve.calls.inference import org.jetbrains.kotlin.builtins.KotlinBuiltIns import org.jetbrains.kotlin.descriptors.CallableDescriptor import org.jetbrains.kotlin.descriptors.ModuleDescriptor +import org.jetbrains.kotlin.descriptors.impl.LocalVariableDescriptor import org.jetbrains.kotlin.resolve.BindingTrace import org.jetbrains.kotlin.resolve.MissingSupertypesResolver import org.jetbrains.kotlin.resolve.TemporaryBindingTrace @@ -90,10 +91,10 @@ class CoroutineInferenceSession( } private fun skipCall(callInfo: SingleCallResolutionResult): Boolean { - // FakeCallableDescriptorForObject can't introduce new information for inference, so it's safe to complete it fully - if (callInfo.resultCallAtom.candidateDescriptor is FakeCallableDescriptorForObject) return true - - return false + // FakeCallableDescriptorForObject and LocalVariableDescriptor can't introduce new information for inference, + // so it's safe to complete it fully + val descriptor = callInfo.resultCallAtom.candidateDescriptor + return descriptor is FakeCallableDescriptorForObject || descriptor is LocalVariableDescriptor } override fun currentConstraintSystem(): ConstraintStorage { diff --git a/compiler/testData/codegen/box/coroutines/captureMutableLocalVariableInsideCoroutineBlock.kt b/compiler/testData/codegen/box/coroutines/captureMutableLocalVariableInsideCoroutineBlock.kt new file mode 100644 index 00000000000..a6488391032 --- /dev/null +++ b/compiler/testData/codegen/box/coroutines/captureMutableLocalVariableInsideCoroutineBlock.kt @@ -0,0 +1,44 @@ +// WITH_RUNTIME +// WITH_COROUTINES +// IGNORE_BACKEND_FIR: JVM_IR + +import helpers.* +import kotlin.coroutines.* +import kotlin.coroutines.intrinsics.* +import kotlin.test.* + +fun testLaziness() { + var sharedVar = -2 + val result = sequence { + while (true) { + when (sharedVar) { + -1 -> return@sequence + -2 -> error("Invalid state: -2") + else -> yield(sharedVar) + } + } + } + + val iterator = result.iterator() + + sharedVar = 1 + assertTrue(iterator.hasNext()) + assertEquals(1, iterator.next()) + + sharedVar = 2 + assertTrue(iterator.hasNext()) + assertEquals(2, iterator.next()) + + sharedVar = 3 + assertTrue(iterator.hasNext()) + assertEquals(3, iterator.next()) + + sharedVar = -1 + assertFalse(iterator.hasNext()) + assertFailsWith { iterator.next() } +} + +fun box(): String { + testLaziness() + 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 d5fb44de169..24c48380f3e 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java @@ -6052,6 +6052,11 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { runTest("compiler/testData/codegen/box/coroutines/builderInferenceAndGenericArrayAcessCall.kt"); } + @TestMetadata("captureMutableLocalVariableInsideCoroutineBlock.kt") + public void testCaptureMutableLocalVariableInsideCoroutineBlock() throws Exception { + runTest("compiler/testData/codegen/box/coroutines/captureMutableLocalVariableInsideCoroutineBlock.kt"); + } + @TestMetadata("capturedVarInSuspendLambda.kt") public void testCapturedVarInSuspendLambda_1_2() throws Exception { runTestWithPackageReplacement("compiler/testData/codegen/box/coroutines/capturedVarInSuspendLambda.kt", "kotlin.coroutines.experimental"); @@ -12697,6 +12702,11 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { runTest("compiler/testData/codegen/box/inference/capturedStarProjection.kt"); } + @TestMetadata("coercionToUnitWithNestedLambda.kt") + public void testCoercionToUnitWithNestedLambda() throws Exception { + runTest("compiler/testData/codegen/box/inference/coercionToUnitWithNestedLambda.kt"); + } + @TestMetadata("integerLiteralTypeInLamdaReturnType.kt") public void testIntegerLiteralTypeInLamdaReturnType() throws Exception { runTest("compiler/testData/codegen/box/inference/integerLiteralTypeInLamdaReturnType.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java index 2ec5e8bbe83..d32f728f246 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java @@ -6052,6 +6052,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes runTest("compiler/testData/codegen/box/coroutines/builderInferenceAndGenericArrayAcessCall.kt"); } + @TestMetadata("captureMutableLocalVariableInsideCoroutineBlock.kt") + public void testCaptureMutableLocalVariableInsideCoroutineBlock() throws Exception { + runTest("compiler/testData/codegen/box/coroutines/captureMutableLocalVariableInsideCoroutineBlock.kt"); + } + @TestMetadata("capturedVarInSuspendLambda.kt") public void testCapturedVarInSuspendLambda_1_2() throws Exception { runTestWithPackageReplacement("compiler/testData/codegen/box/coroutines/capturedVarInSuspendLambda.kt", "kotlin.coroutines.experimental"); @@ -12697,6 +12702,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes runTest("compiler/testData/codegen/box/inference/capturedStarProjection.kt"); } + @TestMetadata("coercionToUnitWithNestedLambda.kt") + public void testCoercionToUnitWithNestedLambda() throws Exception { + runTest("compiler/testData/codegen/box/inference/coercionToUnitWithNestedLambda.kt"); + } + @TestMetadata("integerLiteralTypeInLamdaReturnType.kt") public void testIntegerLiteralTypeInLamdaReturnType() throws Exception { runTest("compiler/testData/codegen/box/inference/integerLiteralTypeInLamdaReturnType.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/ir/FirBlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/ir/FirBlackBoxCodegenTestGenerated.java index 6242791bf57..802677f3b59 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/ir/FirBlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/ir/FirBlackBoxCodegenTestGenerated.java @@ -5987,6 +5987,11 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT runTest("compiler/testData/codegen/box/coroutines/builderInferenceAndGenericArrayAcessCall.kt"); } + @TestMetadata("captureMutableLocalVariableInsideCoroutineBlock.kt") + public void testCaptureMutableLocalVariableInsideCoroutineBlock() throws Exception { + runTest("compiler/testData/codegen/box/coroutines/captureMutableLocalVariableInsideCoroutineBlock.kt"); + } + @TestMetadata("capturedVarInSuspendLambda.kt") public void testCapturedVarInSuspendLambda_1_3() throws Exception { runTestWithPackageReplacement("compiler/testData/codegen/box/coroutines/capturedVarInSuspendLambda.kt", "kotlin.coroutines"); @@ -11572,6 +11577,11 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT runTest("compiler/testData/codegen/box/inference/capturedStarProjection.kt"); } + @TestMetadata("coercionToUnitWithNestedLambda.kt") + public void testCoercionToUnitWithNestedLambda() throws Exception { + runTest("compiler/testData/codegen/box/inference/coercionToUnitWithNestedLambda.kt"); + } + @TestMetadata("integerLiteralTypeInLamdaReturnType.kt") public void testIntegerLiteralTypeInLamdaReturnType() throws Exception { runTest("compiler/testData/codegen/box/inference/integerLiteralTypeInLamdaReturnType.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java index 6bcb388ff2a..1d894327eac 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java @@ -5987,6 +5987,11 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes runTest("compiler/testData/codegen/box/coroutines/builderInferenceAndGenericArrayAcessCall.kt"); } + @TestMetadata("captureMutableLocalVariableInsideCoroutineBlock.kt") + public void testCaptureMutableLocalVariableInsideCoroutineBlock() throws Exception { + runTest("compiler/testData/codegen/box/coroutines/captureMutableLocalVariableInsideCoroutineBlock.kt"); + } + @TestMetadata("capturedVarInSuspendLambda.kt") public void testCapturedVarInSuspendLambda_1_3() throws Exception { runTestWithPackageReplacement("compiler/testData/codegen/box/coroutines/capturedVarInSuspendLambda.kt", "kotlin.coroutines"); @@ -11572,6 +11577,11 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes runTest("compiler/testData/codegen/box/inference/capturedStarProjection.kt"); } + @TestMetadata("coercionToUnitWithNestedLambda.kt") + public void testCoercionToUnitWithNestedLambda() throws Exception { + runTest("compiler/testData/codegen/box/inference/coercionToUnitWithNestedLambda.kt"); + } + @TestMetadata("integerLiteralTypeInLamdaReturnType.kt") public void testIntegerLiteralTypeInLamdaReturnType() throws Exception { runTest("compiler/testData/codegen/box/inference/integerLiteralTypeInLamdaReturnType.kt"); diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/test/ir/semantics/IrJsCodegenBoxTestGenerated.java b/js/js.tests/test/org/jetbrains/kotlin/js/test/ir/semantics/IrJsCodegenBoxTestGenerated.java index 960c970a7b1..5e077a5a494 100644 --- a/js/js.tests/test/org/jetbrains/kotlin/js/test/ir/semantics/IrJsCodegenBoxTestGenerated.java +++ b/js/js.tests/test/org/jetbrains/kotlin/js/test/ir/semantics/IrJsCodegenBoxTestGenerated.java @@ -5047,6 +5047,11 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest { runTest("compiler/testData/codegen/box/coroutines/builderInferenceAndGenericArrayAcessCall.kt"); } + @TestMetadata("captureMutableLocalVariableInsideCoroutineBlock.kt") + public void testCaptureMutableLocalVariableInsideCoroutineBlock() throws Exception { + runTest("compiler/testData/codegen/box/coroutines/captureMutableLocalVariableInsideCoroutineBlock.kt"); + } + @TestMetadata("capturedVarInSuspendLambda.kt") public void testCapturedVarInSuspendLambda_1_3() throws Exception { runTestWithPackageReplacement("compiler/testData/codegen/box/coroutines/capturedVarInSuspendLambda.kt", "kotlin.coroutines"); diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java index d745000cbed..fefd47d9a51 100644 --- a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java +++ b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java @@ -5047,6 +5047,11 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest { runTest("compiler/testData/codegen/box/coroutines/builderInferenceAndGenericArrayAcessCall.kt"); } + @TestMetadata("captureMutableLocalVariableInsideCoroutineBlock.kt") + public void testCaptureMutableLocalVariableInsideCoroutineBlock() throws Exception { + runTest("compiler/testData/codegen/box/coroutines/captureMutableLocalVariableInsideCoroutineBlock.kt"); + } + @TestMetadata("capturedVarInSuspendLambda.kt") public void testCapturedVarInSuspendLambda_1_3() throws Exception { runTestWithPackageReplacement("compiler/testData/codegen/box/coroutines/capturedVarInSuspendLambda.kt", "kotlin.coroutines");