From 6571a573f1419688fd56db3c3b2442850eda70b8 Mon Sep 17 00:00:00 2001 From: Mikhail Glukhikh Date: Tue, 11 Apr 2017 15:06:03 +0300 Subject: [PATCH] Use correct callElement for variable invoke in replacer Check only 'status.isSuccess' before inlining, no descriptor check Add two tests and fixes two other --- .../CallableUsageReplacementStrategy.kt | 15 +++++++++++---- .../addParenthesis/CallCorrected.kt | 10 ++++++++++ .../addParenthesis/CallCorrected.kt.after | 9 +++++++++ .../addParenthesis/IndexedCorrected.kt | 10 ++++++++++ .../addParenthesis/IndexedCorrected.kt.after | 9 +++++++++ .../refactoring/inline/InlineTestGenerated.java | 12 ++++++++++++ 6 files changed, 61 insertions(+), 4 deletions(-) create mode 100644 idea/testData/refactoring/inline/inlineVariableOrProperty/addParenthesis/CallCorrected.kt create mode 100644 idea/testData/refactoring/inline/inlineVariableOrProperty/addParenthesis/CallCorrected.kt.after create mode 100644 idea/testData/refactoring/inline/inlineVariableOrProperty/addParenthesis/IndexedCorrected.kt create mode 100644 idea/testData/refactoring/inline/inlineVariableOrProperty/addParenthesis/IndexedCorrected.kt.after diff --git a/idea/src/org/jetbrains/kotlin/idea/codeInliner/CallableUsageReplacementStrategy.kt b/idea/src/org/jetbrains/kotlin/idea/codeInliner/CallableUsageReplacementStrategy.kt index fc7f02147c9..060d81de3d0 100644 --- a/idea/src/org/jetbrains/kotlin/idea/codeInliner/CallableUsageReplacementStrategy.kt +++ b/idea/src/org/jetbrains/kotlin/idea/codeInliner/CallableUsageReplacementStrategy.kt @@ -21,7 +21,7 @@ import org.jetbrains.kotlin.idea.intentions.OperatorToFunctionIntention import org.jetbrains.kotlin.lexer.KtTokens import org.jetbrains.kotlin.psi.* import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall -import org.jetbrains.kotlin.resolve.calls.model.isReallySuccess +import org.jetbrains.kotlin.resolve.calls.model.VariableAsFunctionResolvedCall import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode class CallableUsageReplacementStrategy( @@ -31,10 +31,17 @@ class CallableUsageReplacementStrategy( override fun createReplacer(usage: KtSimpleNameExpression): (() -> KtElement?)? { val bindingContext = usage.analyze(BodyResolveMode.PARTIAL) val resolvedCall = usage.getResolvedCall(bindingContext) ?: return null - if (!resolvedCall.isReallySuccess()) return null + if (!resolvedCall.status.isSuccess) return null - val callElement = resolvedCall.call.callElement - if (callElement !is KtExpression && callElement !is KtAnnotationEntry) return null + val callElement = when (resolvedCall) { + is VariableAsFunctionResolvedCall -> resolvedCall.variableCall.call.callElement + else -> resolvedCall.call.callElement + + } + + if (callElement !is KtExpression && callElement !is KtAnnotationEntry) { + return null + } //TODO: precheck pattern correctness for annotation entry diff --git a/idea/testData/refactoring/inline/inlineVariableOrProperty/addParenthesis/CallCorrected.kt b/idea/testData/refactoring/inline/inlineVariableOrProperty/addParenthesis/CallCorrected.kt new file mode 100644 index 00000000000..d2f8661b3e9 --- /dev/null +++ b/idea/testData/refactoring/inline/inlineVariableOrProperty/addParenthesis/CallCorrected.kt @@ -0,0 +1,10 @@ +class Predicate(val x: Int) { + operator fun invoke() = x + + operator fun unaryMinus() = Predicate(-x) +} + +fun test(p: Predicate) { + val x = -p + println(x()) +} \ No newline at end of file diff --git a/idea/testData/refactoring/inline/inlineVariableOrProperty/addParenthesis/CallCorrected.kt.after b/idea/testData/refactoring/inline/inlineVariableOrProperty/addParenthesis/CallCorrected.kt.after new file mode 100644 index 00000000000..bceb4b33a5b --- /dev/null +++ b/idea/testData/refactoring/inline/inlineVariableOrProperty/addParenthesis/CallCorrected.kt.after @@ -0,0 +1,9 @@ +class Predicate(val x: Int) { + operator fun invoke() = x + + operator fun unaryMinus() = Predicate(-x) +} + +fun test(p: Predicate) { + println((-p)()) +} \ No newline at end of file diff --git a/idea/testData/refactoring/inline/inlineVariableOrProperty/addParenthesis/IndexedCorrected.kt b/idea/testData/refactoring/inline/inlineVariableOrProperty/addParenthesis/IndexedCorrected.kt new file mode 100644 index 00000000000..f18fef6c2a4 --- /dev/null +++ b/idea/testData/refactoring/inline/inlineVariableOrProperty/addParenthesis/IndexedCorrected.kt @@ -0,0 +1,10 @@ +class Predicate(val x: Int) { + operator fun set(i: Int, j: Int) {} + + operator fun unaryMinus() = Predicate(-x) +} + +fun test(p: Predicate) { + val x = -p + x[13] = 42 +} \ No newline at end of file diff --git a/idea/testData/refactoring/inline/inlineVariableOrProperty/addParenthesis/IndexedCorrected.kt.after b/idea/testData/refactoring/inline/inlineVariableOrProperty/addParenthesis/IndexedCorrected.kt.after new file mode 100644 index 00000000000..cd139d4aee3 --- /dev/null +++ b/idea/testData/refactoring/inline/inlineVariableOrProperty/addParenthesis/IndexedCorrected.kt.after @@ -0,0 +1,9 @@ +class Predicate(val x: Int) { + operator fun set(i: Int, j: Int) {} + + operator fun unaryMinus() = Predicate(-x) +} + +fun test(p: Predicate) { + (-p)[13] = 42 +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/refactoring/inline/InlineTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/refactoring/inline/InlineTestGenerated.java index acc03bdaa8e..1a26b513964 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/refactoring/inline/InlineTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/refactoring/inline/InlineTestGenerated.java @@ -679,6 +679,12 @@ public class InlineTestGenerated extends AbstractInlineTest { doTest(fileName); } + @TestMetadata("CallCorrected.kt") + public void testCallCorrected() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/inline/inlineVariableOrProperty/addParenthesis/CallCorrected.kt"); + doTest(fileName); + } + @TestMetadata("CallDontAdd.kt") public void testCallDontAdd() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/inline/inlineVariableOrProperty/addParenthesis/CallDontAdd.kt"); @@ -751,6 +757,12 @@ public class InlineTestGenerated extends AbstractInlineTest { doTest(fileName); } + @TestMetadata("IndexedCorrected.kt") + public void testIndexedCorrected() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/inline/inlineVariableOrProperty/addParenthesis/IndexedCorrected.kt"); + doTest(fileName); + } + @TestMetadata("IsDontAdd.kt") public void testIsDontAdd() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/inline/inlineVariableOrProperty/addParenthesis/IsDontAdd.kt");