From e82c909f750968787f3496cc6a5b3735608d9684 Mon Sep 17 00:00:00 2001 From: Mikhail Zarechenskiy Date: Mon, 10 Jul 2017 10:58:36 +0300 Subject: [PATCH] Use more specific status to report diagnostics #KT-12737 Fixed --- .../calls/tower/NewResolutionOldInference.kt | 1 + .../resolve/calls/tower/ImplicitScopeTower.kt | 3 ++- .../kotlin/resolve/calls/tower/TowerUtils.kt | 7 +++++- .../extensions/ExtensionsCalledOnSuper.kt | 3 ++- ...gArgumentPassedToLocalExtensionFunction.kt | 20 +++++++++++++++++ ...ArgumentPassedToLocalExtensionFunction.txt | 5 +++++ .../typealias/typeAliasObjectWithInvoke.kt | 4 ++-- .../typealias/typeAliasObjectWithInvoke.txt | 4 ++-- .../checkers/DiagnosticsTestGenerated.java | 6 +++++ .../mismatchingArgs/checkArgumentTypes.test | 22 ++++++++++++++++++- .../methodCallExpression/stringMethods.kt | 8 ++----- 11 files changed, 69 insertions(+), 14 deletions(-) create mode 100644 compiler/testData/diagnostics/tests/inference/reportingImprovements/wrongArgumentPassedToLocalExtensionFunction.kt create mode 100644 compiler/testData/diagnostics/tests/inference/reportingImprovements/wrongArgumentPassedToLocalExtensionFunction.txt diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/NewResolutionOldInference.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/NewResolutionOldInference.kt index 4a1052664f3..08edeeecb52 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/NewResolutionOldInference.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/NewResolutionOldInference.kt @@ -469,6 +469,7 @@ internal fun createPreviousResolveError(status: ResolutionStatus): PreviousResol val level = when (status) { ResolutionStatus.SUCCESS, ResolutionStatus.INCOMPLETE_TYPE_INFERENCE -> return null ResolutionStatus.UNSAFE_CALL_ERROR -> ResolutionCandidateApplicability.MAY_THROW_RUNTIME_ERROR + ResolutionStatus.ARGUMENTS_MAPPING_ERROR -> ResolutionCandidateApplicability.INAPPLICABLE_ARGUMENTS_MAPPING_ERROR ResolutionStatus.RECEIVER_TYPE_ERROR -> ResolutionCandidateApplicability.INAPPLICABLE_WRONG_RECEIVER else -> ResolutionCandidateApplicability.INAPPLICABLE } diff --git a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/tower/ImplicitScopeTower.kt b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/tower/ImplicitScopeTower.kt index 0fc50f5ed27..cd308fb9373 100644 --- a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/tower/ImplicitScopeTower.kt +++ b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/tower/ImplicitScopeTower.kt @@ -71,7 +71,8 @@ enum class ResolutionCandidateApplicability { MAY_THROW_RUNTIME_ERROR, // unsafe call or unstable smart cast RUNTIME_ERROR, // problems with visibility IMPOSSIBLE_TO_GENERATE, // access to outer class from nested - INAPPLICABLE, // arguments not matched + INAPPLICABLE, // arguments have wrong types + INAPPLICABLE_ARGUMENTS_MAPPING_ERROR, // arguments not mapped to parameters (i.e. different size of arguments and parameters) INAPPLICABLE_WRONG_RECEIVER, // receiver not matched HIDDEN, // removed from resolve } diff --git a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/tower/TowerUtils.kt b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/tower/TowerUtils.kt index 55744cc41d5..9c737236348 100644 --- a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/tower/TowerUtils.kt +++ b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/tower/TowerUtils.kt @@ -20,6 +20,11 @@ import org.jetbrains.kotlin.descriptors.CallableDescriptor import org.jetbrains.kotlin.descriptors.CallableMemberDescriptor import org.jetbrains.kotlin.resolve.scopes.receivers.ReceiverValueWithSmartCastInfo +private val INAPPLICABLE_STATUSES = setOf( + ResolutionCandidateApplicability.INAPPLICABLE, + ResolutionCandidateApplicability.INAPPLICABLE_ARGUMENTS_MAPPING_ERROR, + ResolutionCandidateApplicability.INAPPLICABLE_WRONG_RECEIVER) + val ResolutionCandidateApplicability.isSuccess: Boolean get() = this <= ResolutionCandidateApplicability.RESOLVED_LOW_PRIORITY @@ -30,7 +35,7 @@ val CandidateWithBoundDispatchReceiver.requiresExtensionReceiver: Boolean get() = descriptor.extensionReceiverParameter != null val ResolutionCandidateApplicability.isInapplicable: Boolean - get() = this == ResolutionCandidateApplicability.INAPPLICABLE || this == ResolutionCandidateApplicability.INAPPLICABLE_WRONG_RECEIVER + get() = this in INAPPLICABLE_STATUSES internal class CandidateWithBoundDispatchReceiverImpl( override val dispatchReceiver: ReceiverValueWithSmartCastInfo?, diff --git a/compiler/testData/diagnostics/tests/extensions/ExtensionsCalledOnSuper.kt b/compiler/testData/diagnostics/tests/extensions/ExtensionsCalledOnSuper.kt index 220d2cbeb2a..a8edcb712d7 100644 --- a/compiler/testData/diagnostics/tests/extensions/ExtensionsCalledOnSuper.kt +++ b/compiler/testData/diagnostics/tests/extensions/ExtensionsCalledOnSuper.kt @@ -16,6 +16,7 @@ class C : T { super.foo() // OK super.bar() // Error super.buzz() // OK, resolved to a member - super.buzz1() // Resolved to a member, but error: no parameter passed where required + super.buzz1() // Resolved to an extension + super.buzz1("") // Resolved to a member } } \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/inference/reportingImprovements/wrongArgumentPassedToLocalExtensionFunction.kt b/compiler/testData/diagnostics/tests/inference/reportingImprovements/wrongArgumentPassedToLocalExtensionFunction.kt new file mode 100644 index 00000000000..b38f78d7d05 --- /dev/null +++ b/compiler/testData/diagnostics/tests/inference/reportingImprovements/wrongArgumentPassedToLocalExtensionFunction.kt @@ -0,0 +1,20 @@ +// WITH_RUNTIME + +fun Runnable.test(f: Runnable.(Int) -> Unit) { + f("") +} + +fun test(f: Runnable.(Int) -> Unit, runnable: Runnable) { + with (runnable) { + f("") + } +} + +fun Int.test(f: String.(Int) -> Unit) { + f("", 0) + f("") + with("") { + f(0) + f(0.0) + } +} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/inference/reportingImprovements/wrongArgumentPassedToLocalExtensionFunction.txt b/compiler/testData/diagnostics/tests/inference/reportingImprovements/wrongArgumentPassedToLocalExtensionFunction.txt new file mode 100644 index 00000000000..f74fd0fa870 --- /dev/null +++ b/compiler/testData/diagnostics/tests/inference/reportingImprovements/wrongArgumentPassedToLocalExtensionFunction.txt @@ -0,0 +1,5 @@ +package + +public fun test(/*0*/ f: java.lang.Runnable.(kotlin.Int) -> kotlin.Unit, /*1*/ runnable: java.lang.Runnable): kotlin.Unit +public fun java.lang.Runnable.test(/*0*/ f: java.lang.Runnable.(kotlin.Int) -> kotlin.Unit): kotlin.Unit +public fun kotlin.Int.test(/*0*/ f: kotlin.String.(kotlin.Int) -> kotlin.Unit): kotlin.Unit diff --git a/compiler/testData/diagnostics/tests/typealias/typeAliasObjectWithInvoke.kt b/compiler/testData/diagnostics/tests/typealias/typeAliasObjectWithInvoke.kt index 98dd13918c5..6e4059292de 100644 --- a/compiler/testData/diagnostics/tests/typealias/typeAliasObjectWithInvoke.kt +++ b/compiler/testData/diagnostics/tests/typealias/typeAliasObjectWithInvoke.kt @@ -17,5 +17,5 @@ val test2 = WI(null) val test3 = CWI() val test4 = CWI("") -val test5 = CWI(null) -val test5a = ClassWithCompanionObjectWithInvoke(null) +val test5 = CWI(null) +val test5a = ClassWithCompanionObjectWithInvoke(null) diff --git a/compiler/testData/diagnostics/tests/typealias/typeAliasObjectWithInvoke.txt b/compiler/testData/diagnostics/tests/typealias/typeAliasObjectWithInvoke.txt index dc71f767a36..809fa64eb98 100644 --- a/compiler/testData/diagnostics/tests/typealias/typeAliasObjectWithInvoke.txt +++ b/compiler/testData/diagnostics/tests/typealias/typeAliasObjectWithInvoke.txt @@ -4,8 +4,8 @@ public val test1: ObjectWithInvoke public val test2: ObjectWithInvoke public val test3: CWI /* = ClassWithCompanionObjectWithInvoke */ public val test4: kotlin.Any -public val test5: CWI /* = ClassWithCompanionObjectWithInvoke */ -public val test5a: ClassWithCompanionObjectWithInvoke +public val test5: kotlin.Any +public val test5a: kotlin.Any public final class ClassWithCompanionObjectWithInvoke { public constructor ClassWithCompanionObjectWithInvoke() diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java index f489adab3a8..aaf5f32c788 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java @@ -10931,6 +10931,12 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/inference/reportingImprovements/wrongArgumentExtensionFunction.kt"); doTest(fileName); } + + @TestMetadata("wrongArgumentPassedToLocalExtensionFunction.kt") + public void testWrongArgumentPassedToLocalExtensionFunction() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/inference/reportingImprovements/wrongArgumentPassedToLocalExtensionFunction.kt"); + doTest(fileName); + } } @TestMetadata("compiler/testData/diagnostics/tests/inference/substitutions") diff --git a/idea/testData/quickfix/autoImports/mismatchingArgs/checkArgumentTypes.test b/idea/testData/quickfix/autoImports/mismatchingArgs/checkArgumentTypes.test index 90de3554324..4cd0d14bf60 100644 --- a/idea/testData/quickfix/autoImports/mismatchingArgs/checkArgumentTypes.test +++ b/idea/testData/quickfix/autoImports/mismatchingArgs/checkArgumentTypes.test @@ -1,5 +1,5 @@ // FILE: first.before.kt -// "Import" "false" +// "Import" "true" // ERROR: Too many arguments for public final fun foo(): Unit defined in main.X // ACTION: Add parameter to function 'foo' // ACTION: Create extension function 'X.foo' @@ -23,3 +23,23 @@ import main.X fun X.foo(p: String) { } + +// FILE: first.after.kt +// "Import" "true" +// ERROR: Too many arguments for public final fun foo(): Unit defined in main.X +// ACTION: Add parameter to function 'foo' +// ACTION: Create extension function 'X.foo' +// ACTION: Create member function 'X.foo' + +package main + +import other.foo + +class X { + fun foo() { + } + + fun f() { + this.foo(1) + } +} diff --git a/j2k/testData/fileOrElement/methodCallExpression/stringMethods.kt b/j2k/testData/fileOrElement/methodCallExpression/stringMethods.kt index b3a8f3ef732..f5d8407b6d3 100644 --- a/j2k/testData/fileOrElement/methodCallExpression/stringMethods.kt +++ b/j2k/testData/fileOrElement/methodCallExpression/stringMethods.kt @@ -1,9 +1,5 @@ -// ERROR: Too many arguments for public constructor String() defined in kotlin.String -// ERROR: Too many arguments for public constructor String() defined in kotlin.String -// ERROR: Too many arguments for public constructor String() defined in kotlin.String -// ERROR: Too many arguments for public constructor String() defined in kotlin.String -// ERROR: Too many arguments for public constructor String() defined in kotlin.String -// ERROR: Too many arguments for public constructor String() defined in kotlin.String +// ERROR: Type mismatch: inferred type is String but Charset was expected +// ERROR: Type mismatch: inferred type is String but Charset was expected import java.nio.charset.Charset import java.util.*