From d829f585f7d27cce223049e72c7cceb796510bce Mon Sep 17 00:00:00 2001 From: Valentin Kipyatkov Date: Tue, 10 Nov 2015 20:43:47 +0300 Subject: [PATCH] Changes on code review --- .../jetbrains/kotlin/diagnostics/Errors.java | 3 +-- .../rendering/DefaultErrorMessages.java | 2 +- .../calls/checkers/InvokeConventionChecker.kt | 2 +- ...ExtensionFunctionWithExplicitReceiverFix.kt | 7 +++---- .../notSimple.kt | 18 ++++++++++++++++++ .../notSimple.kt.after | 18 ++++++++++++++++++ .../simple.kt | 2 +- .../simple.kt.after | 2 +- .../idea/quickfix/QuickFixTestGenerated.java | 6 ++++++ 9 files changed, 50 insertions(+), 10 deletions(-) create mode 100644 idea/testData/quickfix/migration/invokeOnExtensionFunctionWithExplicitReceiverFix/notSimple.kt create mode 100644 idea/testData/quickfix/migration/invokeOnExtensionFunctionWithExplicitReceiverFix/notSimple.kt.after diff --git a/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/Errors.java b/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/Errors.java index bea2e019e66..d329ed0a944 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/Errors.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/Errors.java @@ -479,8 +479,7 @@ public interface Errors { DiagnosticFactory1>> CANNOT_COMPLETE_RESOLVE = DiagnosticFactory1.create(ERROR); DiagnosticFactory1>> UNRESOLVED_REFERENCE_WRONG_RECEIVER = DiagnosticFactory1.create(ERROR); DiagnosticFactory1 INVOKE_EXTENSION_ON_NOT_EXTENSION_FUNCTION = DiagnosticFactory1.create(ERROR); - DiagnosticFactory1 - INVOKE_ON_EXTENSION_FUNCTION_WITH_EXPLICIT_DISPATCH_RECEIVER = DiagnosticFactory1.create(WARNING); + DiagnosticFactory0 INVOKE_ON_EXTENSION_FUNCTION_WITH_EXPLICIT_DISPATCH_RECEIVER = DiagnosticFactory0.create(WARNING); DiagnosticFactory1 TYPE_PARAMETER_AS_REIFIED = DiagnosticFactory1.create(ERROR); DiagnosticFactory1 REIFIED_TYPE_FORBIDDEN_SUBSTITUTION = DiagnosticFactory1.create(ERROR); diff --git a/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/rendering/DefaultErrorMessages.java b/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/rendering/DefaultErrorMessages.java index df9381af0d5..5871af98710 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/rendering/DefaultErrorMessages.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/rendering/DefaultErrorMessages.java @@ -594,7 +594,7 @@ public class DefaultErrorMessages { MAP.put(CANNOT_COMPLETE_RESOLVE, "Cannot choose among the following candidates without completing type inference: {0}", AMBIGUOUS_CALLS); MAP.put(UNRESOLVED_REFERENCE_WRONG_RECEIVER, "Unresolved reference. None of the following candidates is applicable because of receiver type mismatch: {0}", AMBIGUOUS_CALLS); MAP.put(INVOKE_EXTENSION_ON_NOT_EXTENSION_FUNCTION, "Impossible call as extension because {0} is not an extension function.", ELEMENT_TEXT); - MAP.put(INVOKE_ON_EXTENSION_FUNCTION_WITH_EXPLICIT_DISPATCH_RECEIVER, "Deprecated call {0}", ELEMENT_TEXT); + MAP.put(INVOKE_ON_EXTENSION_FUNCTION_WITH_EXPLICIT_DISPATCH_RECEIVER, "Such calls are no longer supported, surround callee with parenthesis or pass receiver as first argument"); MAP.put(NO_VALUE_FOR_PARAMETER, "No value passed for parameter {0}", NAME); MAP.put(MISSING_RECEIVER, "A receiver of type {0} is required", RENDER_TYPE); diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/checkers/InvokeConventionChecker.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/checkers/InvokeConventionChecker.kt index 2bdf7e2e037..b3e9eba140f 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/checkers/InvokeConventionChecker.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/checkers/InvokeConventionChecker.kt @@ -32,7 +32,7 @@ class InvokeConventionChecker : CallChecker { if (functionCall.dispatchReceiver.exists() && functionCall.extensionReceiver.exists() && KotlinBuiltIns.isExactExtensionFunctionType(variableCall.resultingDescriptor.type)) { if (variableCall.dispatchReceiver is ExpressionReceiver || variableCall.extensionReceiver is ExpressionReceiver) { val callElement = variableCall.call.callElement - context.trace.report(Errors.INVOKE_ON_EXTENSION_FUNCTION_WITH_EXPLICIT_DISPATCH_RECEIVER.on(callElement, callElement)) + context.trace.report(Errors.INVOKE_ON_EXTENSION_FUNCTION_WITH_EXPLICIT_DISPATCH_RECEIVER.on(callElement)) } } } diff --git a/idea/src/org/jetbrains/kotlin/idea/quickfix/migration/InvokeOnExtensionFunctionWithExplicitReceiverFix.kt b/idea/src/org/jetbrains/kotlin/idea/quickfix/migration/InvokeOnExtensionFunctionWithExplicitReceiverFix.kt index dabe9666831..6b47a795dbf 100644 --- a/idea/src/org/jetbrains/kotlin/idea/quickfix/migration/InvokeOnExtensionFunctionWithExplicitReceiverFix.kt +++ b/idea/src/org/jetbrains/kotlin/idea/quickfix/migration/InvokeOnExtensionFunctionWithExplicitReceiverFix.kt @@ -29,20 +29,19 @@ import org.jetbrains.kotlin.psi.* import org.jetbrains.kotlin.psi.psiUtil.getQualifiedExpressionForSelector class InvokeOnExtensionFunctionWithExplicitReceiverFix(qualifiedExpression: KtDotQualifiedExpression) : KotlinQuickFixAction(qualifiedExpression), CleanupFix { - override fun getFamilyName() = "Fix extension function value call" + override fun getFamilyName() = "Surround callee with parenthesis" override fun getText() = familyName override fun invoke(project: Project, editor: Editor?, file: KtFile) { val callExpression = element.selectorExpression as KtCallExpression - val pattern = if (element is KtSafeQualifiedExpression)"($0?.$1)" else "($0.$1)" - val newCallee = KtPsiFactory(file).createExpressionByPattern(pattern, element.receiverExpression, callExpression.calleeExpression!!) + val newCallee = KtPsiFactory(file).createExpressionByPattern("($0.$1)", element.receiverExpression, callExpression.calleeExpression!!) val newCallExpression = element.replaced(callExpression) newCallExpression.calleeExpression!!.replace(newCallee) } companion object : KotlinSingleIntentionActionFactory() { override fun createAction(diagnostic: Diagnostic): IntentionAction? { - val callee = Errors.INVOKE_ON_EXTENSION_FUNCTION_WITH_EXPLICIT_DISPATCH_RECEIVER.cast(diagnostic).a as? KtNameReferenceExpression ?: return null + val callee = Errors.INVOKE_ON_EXTENSION_FUNCTION_WITH_EXPLICIT_DISPATCH_RECEIVER.cast(diagnostic).psiElement as? KtNameReferenceExpression ?: return null val callExpression = callee.parent as? KtCallExpression ?: return null val qualifiedExpression = callExpression.getQualifiedExpressionForSelector() as? KtDotQualifiedExpression ?: return null return InvokeOnExtensionFunctionWithExplicitReceiverFix(qualifiedExpression) diff --git a/idea/testData/quickfix/migration/invokeOnExtensionFunctionWithExplicitReceiverFix/notSimple.kt b/idea/testData/quickfix/migration/invokeOnExtensionFunctionWithExplicitReceiverFix/notSimple.kt new file mode 100644 index 00000000000..ea3455d5c53 --- /dev/null +++ b/idea/testData/quickfix/migration/invokeOnExtensionFunctionWithExplicitReceiverFix/notSimple.kt @@ -0,0 +1,18 @@ +// "Surround callee with parenthesis" "true" + +class A { + val foo: B.(String, Int) -> Unit get() = null!! +} + +class B { + fun getA() = A() +} + + +fun test(b: B) { + with(b) { + b.getA().foo("", 1) + } +} + +public inline fun with(receiver: T, f: T.() -> R): R = receiver.f() \ No newline at end of file diff --git a/idea/testData/quickfix/migration/invokeOnExtensionFunctionWithExplicitReceiverFix/notSimple.kt.after b/idea/testData/quickfix/migration/invokeOnExtensionFunctionWithExplicitReceiverFix/notSimple.kt.after new file mode 100644 index 00000000000..e633d174a7e --- /dev/null +++ b/idea/testData/quickfix/migration/invokeOnExtensionFunctionWithExplicitReceiverFix/notSimple.kt.after @@ -0,0 +1,18 @@ +// "Surround callee with parenthesis" "true" + +class A { + val foo: B.(String, Int) -> Unit get() = null!! +} + +class B { + fun getA() = A() +} + + +fun test(b: B) { + with(b) { + (b.getA().foo)("", 1) + } +} + +public inline fun with(receiver: T, f: T.() -> R): R = receiver.f() \ No newline at end of file diff --git a/idea/testData/quickfix/migration/invokeOnExtensionFunctionWithExplicitReceiverFix/simple.kt b/idea/testData/quickfix/migration/invokeOnExtensionFunctionWithExplicitReceiverFix/simple.kt index ef7faf9a683..187c8c91bb0 100644 --- a/idea/testData/quickfix/migration/invokeOnExtensionFunctionWithExplicitReceiverFix/simple.kt +++ b/idea/testData/quickfix/migration/invokeOnExtensionFunctionWithExplicitReceiverFix/simple.kt @@ -1,4 +1,4 @@ -// "Fix extension function value call" "true" +// "Surround callee with parenthesis" "true" class A { val foo: B.() -> Unit get() = null!! diff --git a/idea/testData/quickfix/migration/invokeOnExtensionFunctionWithExplicitReceiverFix/simple.kt.after b/idea/testData/quickfix/migration/invokeOnExtensionFunctionWithExplicitReceiverFix/simple.kt.after index 704a2406717..6b2ecc7acf0 100644 --- a/idea/testData/quickfix/migration/invokeOnExtensionFunctionWithExplicitReceiverFix/simple.kt.after +++ b/idea/testData/quickfix/migration/invokeOnExtensionFunctionWithExplicitReceiverFix/simple.kt.after @@ -1,4 +1,4 @@ -// "Fix extension function value call" "true" +// "Surround callee with parenthesis" "true" class A { val foo: B.() -> Unit get() = null!! diff --git a/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java index 22763a223ea..8d65fa05962 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java @@ -4369,6 +4369,12 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest { KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/quickfix/migration/invokeOnExtensionFunctionWithExplicitReceiverFix"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), true); } + @TestMetadata("notSimple.kt") + public void testNotSimple() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/migration/invokeOnExtensionFunctionWithExplicitReceiverFix/notSimple.kt"); + doTest(fileName); + } + @TestMetadata("simple.kt") public void testSimple() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/migration/invokeOnExtensionFunctionWithExplicitReceiverFix/simple.kt");