From 08964006defbe8e81466722bddfbf9ef403d9de4 Mon Sep 17 00:00:00 2001 From: Mikhail Zarechenskiy Date: Thu, 17 Aug 2017 14:36:56 +0300 Subject: [PATCH] [NI] Avoid type capturing for types that can contain type variables --- .../kotlin/resolve/calls/tower/NewCallArguments.kt | 4 +++- .../dontCaptureTypesWithTypeVariables.kt | 14 ++++++++++++++ .../noCapturingForTypesWithTypeVariables.kt | 10 ++++++++++ .../codegen/ir/IrBlackBoxCodegenTestGenerated.java | 12 ++++++++++++ .../codegen/BlackBoxCodegenTestGenerated.java | 12 ++++++++++++ .../codegen/LightAnalysisModeTestGenerated.java | 12 ++++++++++++ .../test/semantics/JsCodegenBoxTestGenerated.java | 12 ++++++++++++ 7 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 compiler/testData/codegen/box/regressions/dontCaptureTypesWithTypeVariables.kt create mode 100644 compiler/testData/codegen/box/regressions/noCapturingForTypesWithTypeVariables.kt diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/NewCallArguments.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/NewCallArguments.kt index 74572bb6b1a..d713b3e0ed2 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/NewCallArguments.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/NewCallArguments.kt @@ -224,7 +224,9 @@ internal fun createSimplePSICallArgument( ownerDescriptor, bindingContext, typeInfoForArgument.dataFlowInfo, // dataFlowInfoBeforeThisArgument cannot be used here, because of if() { if (x != null) return; x } ExpressionReceiver.create(ktExpression, baseType, bindingContext) - ).prepareReceiverRegardingCaptureTypes() + ).let { + if (onlyResolvedCall == null) it.prepareReceiverRegardingCaptureTypes() else it + } return if (onlyResolvedCall == null) { ExpressionKotlinCallArgumentImpl(valueArgument, dataFlowInfoBeforeThisArgument, typeInfoForArgument.dataFlowInfo, receiverToCast) diff --git a/compiler/testData/codegen/box/regressions/dontCaptureTypesWithTypeVariables.kt b/compiler/testData/codegen/box/regressions/dontCaptureTypesWithTypeVariables.kt new file mode 100644 index 00000000000..f14ecd2a694 --- /dev/null +++ b/compiler/testData/codegen/box/regressions/dontCaptureTypesWithTypeVariables.kt @@ -0,0 +1,14 @@ +fun test(i: Inv) { + foo(i.superclass()) +} + +fun foo(x: T) {} + +class Inv + +fun Inv.superclass(): Inv = Inv() + +fun box(): String { + test(Inv()) + return "OK" +} \ No newline at end of file diff --git a/compiler/testData/codegen/box/regressions/noCapturingForTypesWithTypeVariables.kt b/compiler/testData/codegen/box/regressions/noCapturingForTypesWithTypeVariables.kt new file mode 100644 index 00000000000..993d8ad63cd --- /dev/null +++ b/compiler/testData/codegen/box/regressions/noCapturingForTypesWithTypeVariables.kt @@ -0,0 +1,10 @@ +fun foo(useScriptArgs: Array?) { + val constructorArgs: Array = arrayOf(useScriptArgs.orEmpty()) +} + +inline fun Array?.orEmpty(): Array = this ?: emptyArray() + +fun box(): String { + foo(arrayOf(1)) + return "OK" +} \ No newline at end of file diff --git a/compiler/tests-ir-jvm/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java b/compiler/tests-ir-jvm/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java index a53af32d8e6..f07b20c6115 100644 --- a/compiler/tests-ir-jvm/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java +++ b/compiler/tests-ir-jvm/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java @@ -16580,6 +16580,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes doTest(fileName); } + @TestMetadata("dontCaptureTypesWithTypeVariables.kt") + public void testDontCaptureTypesWithTypeVariables() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/regressions/dontCaptureTypesWithTypeVariables.kt"); + doTest(fileName); + } + @TestMetadata("doubleMerge.kt") public void testDoubleMerge() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/regressions/doubleMerge.kt"); @@ -16976,6 +16982,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes doTest(fileName); } + @TestMetadata("noCapturingForTypesWithTypeVariables.kt") + public void testNoCapturingForTypesWithTypeVariables() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/regressions/noCapturingForTypesWithTypeVariables.kt"); + doTest(fileName); + } + @TestMetadata("noResolutionRecursion.kt") public void testNoResolutionRecursion() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/regressions/noResolutionRecursion.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java index a6afaf7c587..e8501248308 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java @@ -16580,6 +16580,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { doTest(fileName); } + @TestMetadata("dontCaptureTypesWithTypeVariables.kt") + public void testDontCaptureTypesWithTypeVariables() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/regressions/dontCaptureTypesWithTypeVariables.kt"); + doTest(fileName); + } + @TestMetadata("doubleMerge.kt") public void testDoubleMerge() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/regressions/doubleMerge.kt"); @@ -16976,6 +16982,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { doTest(fileName); } + @TestMetadata("noCapturingForTypesWithTypeVariables.kt") + public void testNoCapturingForTypesWithTypeVariables() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/regressions/noCapturingForTypesWithTypeVariables.kt"); + doTest(fileName); + } + @TestMetadata("noResolutionRecursion.kt") public void testNoResolutionRecursion() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/regressions/noResolutionRecursion.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java index a3ef8ff0fa1..f35172d5d72 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java @@ -16580,6 +16580,12 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes doTest(fileName); } + @TestMetadata("dontCaptureTypesWithTypeVariables.kt") + public void testDontCaptureTypesWithTypeVariables() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/regressions/dontCaptureTypesWithTypeVariables.kt"); + doTest(fileName); + } + @TestMetadata("doubleMerge.kt") public void testDoubleMerge() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/regressions/doubleMerge.kt"); @@ -16976,6 +16982,12 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes doTest(fileName); } + @TestMetadata("noCapturingForTypesWithTypeVariables.kt") + public void testNoCapturingForTypesWithTypeVariables() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/regressions/noCapturingForTypesWithTypeVariables.kt"); + doTest(fileName); + } + @TestMetadata("noResolutionRecursion.kt") public void testNoResolutionRecursion() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/regressions/noResolutionRecursion.kt"); 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 0a43cc8b906..86dd4b7c84f 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 @@ -20228,6 +20228,12 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest { doTest(fileName); } + @TestMetadata("dontCaptureTypesWithTypeVariables.kt") + public void testDontCaptureTypesWithTypeVariables() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/regressions/dontCaptureTypesWithTypeVariables.kt"); + doTest(fileName); + } + @TestMetadata("doubleMerge.kt") public void testDoubleMerge() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/regressions/doubleMerge.kt"); @@ -20750,6 +20756,12 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest { throw new AssertionError("Looks like this test can be unmuted. Remove IGNORE_BACKEND directive for that."); } + @TestMetadata("noCapturingForTypesWithTypeVariables.kt") + public void testNoCapturingForTypesWithTypeVariables() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/regressions/noCapturingForTypesWithTypeVariables.kt"); + doTest(fileName); + } + @TestMetadata("noResolutionRecursion.kt") public void testNoResolutionRecursion() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/regressions/noResolutionRecursion.kt");