From bcefee0105b9197cbb050266c6bab691167e7608 Mon Sep 17 00:00:00 2001 From: Toshiaki Kameyama Date: Sun, 13 Oct 2019 13:28:19 +0900 Subject: [PATCH] Replace deprecated symbol usage: do not add 'this' receiver for imported object function #KT-26361 Fixed --- .../idea/codeInliner/CodeToInlineBuilder.kt | 31 +++++++++++-------- .../importedObjectFunction.kt | 21 +++++++++++++ .../importedObjectFunction.kt.after | 22 +++++++++++++ .../importedObjectProperty.kt | 21 +++++++++++++ .../importedObjectProperty.kt.after | 22 +++++++++++++ .../idea/quickfix/QuickFixTestGenerated.java | 10 ++++++ 6 files changed, 114 insertions(+), 13 deletions(-) create mode 100644 idea/testData/quickfix/deprecatedSymbolUsage/importedObjectFunction.kt create mode 100644 idea/testData/quickfix/deprecatedSymbolUsage/importedObjectFunction.kt.after create mode 100644 idea/testData/quickfix/deprecatedSymbolUsage/importedObjectProperty.kt create mode 100644 idea/testData/quickfix/deprecatedSymbolUsage/importedObjectProperty.kt.after diff --git a/idea/src/org/jetbrains/kotlin/idea/codeInliner/CodeToInlineBuilder.kt b/idea/src/org/jetbrains/kotlin/idea/codeInliner/CodeToInlineBuilder.kt index a52c1793b71..d21f44a2b20 100644 --- a/idea/src/org/jetbrains/kotlin/idea/codeInliner/CodeToInlineBuilder.kt +++ b/idea/src/org/jetbrains/kotlin/idea/codeInliner/CodeToInlineBuilder.kt @@ -16,7 +16,10 @@ package org.jetbrains.kotlin.idea.codeInliner -import org.jetbrains.kotlin.descriptors.* +import org.jetbrains.kotlin.descriptors.CallableDescriptor +import org.jetbrains.kotlin.descriptors.PropertyDescriptor +import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor +import org.jetbrains.kotlin.descriptors.ValueParameterDescriptor import org.jetbrains.kotlin.diagnostics.Errors import org.jetbrains.kotlin.idea.caches.resolve.getResolutionFacade import org.jetbrains.kotlin.idea.core.asExpression @@ -33,6 +36,7 @@ import org.jetbrains.kotlin.psi.psiUtil.getReceiverExpression import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType import org.jetbrains.kotlin.renderer.render import org.jetbrains.kotlin.resolve.BindingContext +import org.jetbrains.kotlin.resolve.ImportedFromObjectCallableDescriptor import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall import org.jetbrains.kotlin.resolve.calls.model.isReallySuccess import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameOrNull @@ -185,18 +189,19 @@ class CodeToInlineBuilder( } else if (target is TypeParameterDescriptor && target.containingDeclaration == targetCallable) { expression.putCopyableUserData(CodeToInline.TYPE_PARAMETER_USAGE_KEY, target.name) } - - val resolvedCall = expression.getResolvedCall(bindingContext) - if (resolvedCall != null && resolvedCall.isReallySuccess()) { - val receiver = if (resolvedCall.resultingDescriptor.isExtension) - resolvedCall.extensionReceiver - else - resolvedCall.dispatchReceiver - if (receiver is ImplicitReceiver) { - val resolutionScope = expression.getResolutionScope(bindingContext, resolutionFacade) - val receiverExpression = receiver.asExpression(resolutionScope, psiFactory) - if (receiverExpression != null) { - receiversToAdd.add(Triple(expression, receiverExpression, receiver.type)) + if (targetCallable !is ImportedFromObjectCallableDescriptor<*>) { + val resolvedCall = expression.getResolvedCall(bindingContext) + if (resolvedCall != null && resolvedCall.isReallySuccess()) { + val receiver = if (resolvedCall.resultingDescriptor.isExtension) + resolvedCall.extensionReceiver + else + resolvedCall.dispatchReceiver + if (receiver is ImplicitReceiver) { + val resolutionScope = expression.getResolutionScope(bindingContext, resolutionFacade) + val receiverExpression = receiver.asExpression(resolutionScope, psiFactory) + if (receiverExpression != null) { + receiversToAdd.add(Triple(expression, receiverExpression, receiver.type)) + } } } } diff --git a/idea/testData/quickfix/deprecatedSymbolUsage/importedObjectFunction.kt b/idea/testData/quickfix/deprecatedSymbolUsage/importedObjectFunction.kt new file mode 100644 index 00000000000..e5da46f9e5c --- /dev/null +++ b/idea/testData/quickfix/deprecatedSymbolUsage/importedObjectFunction.kt @@ -0,0 +1,21 @@ +// "Replace with 'stopKoin()'" "true" +// WITH_RUNTIME + +package com.example.pkg + +import com.example.pkg.StandAloneContext.closeKoin + +object StandAloneContext { + @Deprecated( + "Renamed, use stopKoin() instead.", + ReplaceWith("stopKoin()", "com.example.pkg.StandAloneContext.stopKoin") + ) + fun closeKoin() = stopKoin() + + fun stopKoin() {} +} + + +fun koin() { + closeKoin() +} \ No newline at end of file diff --git a/idea/testData/quickfix/deprecatedSymbolUsage/importedObjectFunction.kt.after b/idea/testData/quickfix/deprecatedSymbolUsage/importedObjectFunction.kt.after new file mode 100644 index 00000000000..3b1621839ba --- /dev/null +++ b/idea/testData/quickfix/deprecatedSymbolUsage/importedObjectFunction.kt.after @@ -0,0 +1,22 @@ +// "Replace with 'stopKoin()'" "true" +// WITH_RUNTIME + +package com.example.pkg + +import com.example.pkg.StandAloneContext.closeKoin +import com.example.pkg.StandAloneContext.stopKoin + +object StandAloneContext { + @Deprecated( + "Renamed, use stopKoin() instead.", + ReplaceWith("stopKoin()", "com.example.pkg.StandAloneContext.stopKoin") + ) + fun closeKoin() = stopKoin() + + fun stopKoin() {} +} + + +fun koin() { + stopKoin() +} \ No newline at end of file diff --git a/idea/testData/quickfix/deprecatedSymbolUsage/importedObjectProperty.kt b/idea/testData/quickfix/deprecatedSymbolUsage/importedObjectProperty.kt new file mode 100644 index 00000000000..330f9f78c08 --- /dev/null +++ b/idea/testData/quickfix/deprecatedSymbolUsage/importedObjectProperty.kt @@ -0,0 +1,21 @@ +// "Replace with 'stopKoin'" "true" +// WITH_RUNTIME + +package com.example.pkg + +import com.example.pkg.StandAloneContext.closeKoin + +object StandAloneContext { + @Deprecated( + "Renamed, use stopKoin() instead.", + ReplaceWith("stopKoin", "com.example.pkg.StandAloneContext.stopKoin") + ) + val closeKoin = 1 + + val stopKoin = 2 +} + + +fun koin() { + val i = closeKoin +} \ No newline at end of file diff --git a/idea/testData/quickfix/deprecatedSymbolUsage/importedObjectProperty.kt.after b/idea/testData/quickfix/deprecatedSymbolUsage/importedObjectProperty.kt.after new file mode 100644 index 00000000000..3dcd9787075 --- /dev/null +++ b/idea/testData/quickfix/deprecatedSymbolUsage/importedObjectProperty.kt.after @@ -0,0 +1,22 @@ +// "Replace with 'stopKoin'" "true" +// WITH_RUNTIME + +package com.example.pkg + +import com.example.pkg.StandAloneContext.closeKoin +import com.example.pkg.StandAloneContext.stopKoin + +object StandAloneContext { + @Deprecated( + "Renamed, use stopKoin() instead.", + ReplaceWith("stopKoin", "com.example.pkg.StandAloneContext.stopKoin") + ) + val closeKoin = 1 + + val stopKoin = 2 +} + + +fun koin() { + val i = stopKoin +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java index a1a021070a0..88fdc8612f3 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java @@ -6145,6 +6145,16 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest { runTest("idea/testData/quickfix/deprecatedSymbolUsage/implicitQualifiedThisRuntime.kt"); } + @TestMetadata("importedObjectFunction.kt") + public void testImportedObjectFunction() throws Exception { + runTest("idea/testData/quickfix/deprecatedSymbolUsage/importedObjectFunction.kt"); + } + + @TestMetadata("importedObjectProperty.kt") + public void testImportedObjectProperty() throws Exception { + runTest("idea/testData/quickfix/deprecatedSymbolUsage/importedObjectProperty.kt"); + } + @TestMetadata("incorrectArgs.kt") public void testIncorrectArgs() throws Exception { runTest("idea/testData/quickfix/deprecatedSymbolUsage/incorrectArgs.kt");