From ff516cac73c89d7cf41338641e38658ba68c2ff7 Mon Sep 17 00:00:00 2001 From: Valentin Kipyatkov Date: Mon, 10 Feb 2020 08:02:14 +0200 Subject: [PATCH] Do not include overriders when analyzing parameter usages unless invoked on parameter --- .../jetbrains/kotlin/idea/slicer/Slicer.kt | 7 +++-- idea/testData/slicer/inflow/openFun.kt | 28 +++++++++++++++++++ .../slicer/inflow/openFun.leafGroups.txt | 5 ++++ .../slicer/inflow/openFun.nullnessGroups.txt | 4 +++ .../slicer/inflow/openFun.results.txt | 3 ++ .../slicer/inflow/openFunInvokeOnParameter.kt | 28 +++++++++++++++++++ .../openFunInvokeOnParameter.leafGroups.txt | 7 +++++ ...penFunInvokeOnParameter.nullnessGroups.txt | 3 ++ .../openFunInvokeOnParameter.results.txt | 3 ++ .../SlicerLeafGroupingTestGenerated.java | 12 +++++++- .../SlicerNullnessGroupingTestGenerated.java | 12 +++++++- .../idea/slicer/SlicerTreeTestGenerated.java | 12 +++++++- 12 files changed, 118 insertions(+), 6 deletions(-) create mode 100644 idea/testData/slicer/inflow/openFun.kt create mode 100644 idea/testData/slicer/inflow/openFun.leafGroups.txt create mode 100644 idea/testData/slicer/inflow/openFun.nullnessGroups.txt create mode 100644 idea/testData/slicer/inflow/openFun.results.txt create mode 100644 idea/testData/slicer/inflow/openFunInvokeOnParameter.kt create mode 100644 idea/testData/slicer/inflow/openFunInvokeOnParameter.leafGroups.txt create mode 100644 idea/testData/slicer/inflow/openFunInvokeOnParameter.nullnessGroups.txt create mode 100644 idea/testData/slicer/inflow/openFunInvokeOnParameter.results.txt diff --git a/idea/src/org/jetbrains/kotlin/idea/slicer/Slicer.kt b/idea/src/org/jetbrains/kotlin/idea/slicer/Slicer.kt index 0c85a0cb786..0cce5619533 100644 --- a/idea/src/org/jetbrains/kotlin/idea/slicer/Slicer.kt +++ b/idea/src/org/jetbrains/kotlin/idea/slicer/Slicer.kt @@ -249,7 +249,7 @@ class InflowSlicer( } } - private fun KtParameter.processParameter() { + private fun KtParameter.processParameter(includeOverriders: Boolean) { if (!canProcess()) return val function = ownerFunction ?: return @@ -272,7 +272,7 @@ class InflowSlicer( val parameterDescriptor = resolveToParameterDescriptorIfAny(BodyResolveMode.FULL) ?: return - (function as? KtFunction)?.processCalls(parentUsage.scope.toSearchScope(), includeOverriders = true) body@{ + (function as? KtFunction)?.processCalls(parentUsage.scope.toSearchScope(), includeOverriders) body@{ val refElement = it.element ?: return@body val refParent = refElement.parent @@ -394,7 +394,8 @@ class InflowSlicer( when (element) { is KtProperty -> element.processProperty() - is KtParameter -> element.processParameter() + // for parameter, we include overriders only when the feature is invoked on parameter itself + is KtParameter -> element.processParameter(includeOverriders = parentUsage.parent == null) is KtDeclarationWithBody -> element.processFunction() else -> element.processExpression() } diff --git a/idea/testData/slicer/inflow/openFun.kt b/idea/testData/slicer/inflow/openFun.kt new file mode 100644 index 00000000000..3bde0ba15f4 --- /dev/null +++ b/idea/testData/slicer/inflow/openFun.kt @@ -0,0 +1,28 @@ +// FLOW: IN + +open class C { + var other: C? = null + + fun foo(p: Int) { + println(p + 1) + other?.bar(p + 1) + } + + fun bar(p: Int) { + println(p + 2) + } +} + +class D : C() { + var other: D? = null + + override fun foo(p: Int) { + println(p + 3) + other?.bar(p + 3) + } + + override fun bar(p: Int) { + println(p + 4) + } +} + diff --git a/idea/testData/slicer/inflow/openFun.leafGroups.txt b/idea/testData/slicer/inflow/openFun.leafGroups.txt new file mode 100644 index 00000000000..763e319b1f9 --- /dev/null +++ b/idea/testData/slicer/inflow/openFun.leafGroups.txt @@ -0,0 +1,5 @@ +8 other?.bar(p + 1) +12 println(p + 2) +11 fun bar(p: Int) { +8 other?.bar(p + 1) + diff --git a/idea/testData/slicer/inflow/openFun.nullnessGroups.txt b/idea/testData/slicer/inflow/openFun.nullnessGroups.txt new file mode 100644 index 00000000000..0df649738ad --- /dev/null +++ b/idea/testData/slicer/inflow/openFun.nullnessGroups.txt @@ -0,0 +1,4 @@ +[NotNull Values] +12 println(p + 2) +12 println(p + 2) + diff --git a/idea/testData/slicer/inflow/openFun.results.txt b/idea/testData/slicer/inflow/openFun.results.txt new file mode 100644 index 00000000000..757d3e8ceda --- /dev/null +++ b/idea/testData/slicer/inflow/openFun.results.txt @@ -0,0 +1,3 @@ +12 println(p + 2) +11 fun bar(p: Int) { +8 other?.bar(p + 1) diff --git a/idea/testData/slicer/inflow/openFunInvokeOnParameter.kt b/idea/testData/slicer/inflow/openFunInvokeOnParameter.kt new file mode 100644 index 00000000000..4827b38f873 --- /dev/null +++ b/idea/testData/slicer/inflow/openFunInvokeOnParameter.kt @@ -0,0 +1,28 @@ +// FLOW: IN + +open class C { + var other: C? = null + + fun foo(p: Int) { + println(p + 1) + other?.bar(p + 1) + } + + fun bar(p: Int) { + println(p + 2) + } +} + +class D : C() { + var other: D? = null + + override fun foo(p: Int) { + println(p + 3) + other?.bar(p + 3) + } + + override fun bar(p: Int) { + println(p + 4) + } +} + diff --git a/idea/testData/slicer/inflow/openFunInvokeOnParameter.leafGroups.txt b/idea/testData/slicer/inflow/openFunInvokeOnParameter.leafGroups.txt new file mode 100644 index 00000000000..0a70b613117 --- /dev/null +++ b/idea/testData/slicer/inflow/openFunInvokeOnParameter.leafGroups.txt @@ -0,0 +1,7 @@ +8 other?.bar(p + 1) +11 fun bar(p: Int) { +8 other?.bar(p + 1) + +21 other?.bar(p + 3) +11 fun bar(p: Int) { +21 other?.bar(p + 3) diff --git a/idea/testData/slicer/inflow/openFunInvokeOnParameter.nullnessGroups.txt b/idea/testData/slicer/inflow/openFunInvokeOnParameter.nullnessGroups.txt new file mode 100644 index 00000000000..16f576f218e --- /dev/null +++ b/idea/testData/slicer/inflow/openFunInvokeOnParameter.nullnessGroups.txt @@ -0,0 +1,3 @@ +[NotNull Values] +11 fun bar(p: Int) { +11 fun bar(p: Int) { diff --git a/idea/testData/slicer/inflow/openFunInvokeOnParameter.results.txt b/idea/testData/slicer/inflow/openFunInvokeOnParameter.results.txt new file mode 100644 index 00000000000..918b91d0b46 --- /dev/null +++ b/idea/testData/slicer/inflow/openFunInvokeOnParameter.results.txt @@ -0,0 +1,3 @@ +11 fun bar(p: Int) { +8 other?.bar(p + 1) +21 other?.bar(p + 3) diff --git a/idea/tests/org/jetbrains/kotlin/idea/slicer/SlicerLeafGroupingTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/slicer/SlicerLeafGroupingTestGenerated.java index 84cae3e84a8..981188e13b1 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/slicer/SlicerLeafGroupingTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/slicer/SlicerLeafGroupingTestGenerated.java @@ -1,5 +1,5 @@ /* - * Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors. * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. */ @@ -228,6 +228,16 @@ public class SlicerLeafGroupingTestGenerated extends AbstractSlicerLeafGroupingT runTest("idea/testData/slicer/inflow/nullsAndNotNulls.kt"); } + @TestMetadata("openFun.kt") + public void testOpenFun() throws Exception { + runTest("idea/testData/slicer/inflow/openFun.kt"); + } + + @TestMetadata("openFunInvokeOnParameter.kt") + public void testOpenFunInvokeOnParameter() throws Exception { + runTest("idea/testData/slicer/inflow/openFunInvokeOnParameter.kt"); + } + @TestMetadata("overrideFun.kt") public void testOverrideFun() throws Exception { runTest("idea/testData/slicer/inflow/overrideFun.kt"); diff --git a/idea/tests/org/jetbrains/kotlin/idea/slicer/SlicerNullnessGroupingTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/slicer/SlicerNullnessGroupingTestGenerated.java index e8d8d17ec09..e7a54679fe4 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/slicer/SlicerNullnessGroupingTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/slicer/SlicerNullnessGroupingTestGenerated.java @@ -1,5 +1,5 @@ /* - * Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors. * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. */ @@ -228,6 +228,16 @@ public class SlicerNullnessGroupingTestGenerated extends AbstractSlicerNullnessG runTest("idea/testData/slicer/inflow/nullsAndNotNulls.kt"); } + @TestMetadata("openFun.kt") + public void testOpenFun() throws Exception { + runTest("idea/testData/slicer/inflow/openFun.kt"); + } + + @TestMetadata("openFunInvokeOnParameter.kt") + public void testOpenFunInvokeOnParameter() throws Exception { + runTest("idea/testData/slicer/inflow/openFunInvokeOnParameter.kt"); + } + @TestMetadata("overrideFun.kt") public void testOverrideFun() throws Exception { runTest("idea/testData/slicer/inflow/overrideFun.kt"); diff --git a/idea/tests/org/jetbrains/kotlin/idea/slicer/SlicerTreeTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/slicer/SlicerTreeTestGenerated.java index 54b3022322b..9f296edb804 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/slicer/SlicerTreeTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/slicer/SlicerTreeTestGenerated.java @@ -1,5 +1,5 @@ /* - * Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors. * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. */ @@ -228,6 +228,16 @@ public class SlicerTreeTestGenerated extends AbstractSlicerTreeTest { runTest("idea/testData/slicer/inflow/nullsAndNotNulls.kt"); } + @TestMetadata("inflow/openFun.kt") + public void testInflow_OpenFun() throws Exception { + runTest("idea/testData/slicer/inflow/openFun.kt"); + } + + @TestMetadata("inflow/openFunInvokeOnParameter.kt") + public void testInflow_OpenFunInvokeOnParameter() throws Exception { + runTest("idea/testData/slicer/inflow/openFunInvokeOnParameter.kt"); + } + @TestMetadata("inflow/overrideFun.kt") public void testInflow_OverrideFun() throws Exception { runTest("idea/testData/slicer/inflow/overrideFun.kt");