From 87a41293e8af81d98fb4d97e6a871f082c6b17b5 Mon Sep 17 00:00:00 2001 From: Mikhail Zarechenskiy Date: Thu, 22 Jun 2017 02:50:07 +0300 Subject: [PATCH] Improve diagnostics on callable reference of unresolved class #KT-10839 Fixed --- .../DoubleColonExpressionResolver.kt | 13 +++++++---- .../tests/callableReference/bound/kt12843.kt | 4 ++-- .../tests/callableReference/bound/kt12843.txt | 2 +- .../bound/reservedExpressionSyntax.kt | 4 ++-- .../bound/reservedExpressionSyntax3.kt | 4 ++-- .../callableReference/function/unresolved.kt | 22 ++++++++++++++----- .../callableReference/function/unresolved.txt | 3 ++- .../tests/callableReference/packageInLhs.kt | 4 ++-- .../illegalSelectorCallableReference.kt | 4 ++-- .../calleeExpressionAsCallExpression.kt | 2 +- .../nullCalleeExpression.kt | 2 +- .../diagnostics/tests/regressions/kt13685.kt | 2 +- 12 files changed, 41 insertions(+), 25 deletions(-) diff --git a/compiler/frontend/src/org/jetbrains/kotlin/types/expressions/DoubleColonExpressionResolver.kt b/compiler/frontend/src/org/jetbrains/kotlin/types/expressions/DoubleColonExpressionResolver.kt index 4bf73a9f0e8..6681ee43d62 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/types/expressions/DoubleColonExpressionResolver.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/types/expressions/DoubleColonExpressionResolver.kt @@ -528,7 +528,9 @@ class DoubleColonExpressionResolver( resolvedCall?.resultingDescriptor ?: return null } else { - context.trace.report(UNRESOLVED_REFERENCE.on(expression.callableReference, expression.callableReference)) + if (lhs != null || expression.isEmptyLHS) { + context.trace.report(UNRESOLVED_REFERENCE.on(expression.callableReference, expression.callableReference)) + } return null } @@ -680,10 +682,13 @@ class DoubleColonExpressionResolver( ): OverloadResolutionResults? { val reference = expression.callableReference - val lhsType = - lhs?.type ?: - return tryResolveRHSWithReceiver("resolve callable reference with empty LHS", null, reference, c, mode) + val lhsType = lhs?.type + if (lhsType == null) { + if (!expression.isEmptyLHS) return null + + return tryResolveRHSWithReceiver("resolve callable reference with empty LHS", null, reference, c, mode) ?.apply { commitTrace() }?.results + } val resultSequence = buildSequence { when (lhs) { diff --git a/compiler/testData/diagnostics/tests/callableReference/bound/kt12843.kt b/compiler/testData/diagnostics/tests/callableReference/bound/kt12843.kt index 13eea2248bc..ef876a1d587 100644 --- a/compiler/testData/diagnostics/tests/callableReference/bound/kt12843.kt +++ b/compiler/testData/diagnostics/tests/callableReference/bound/kt12843.kt @@ -1,6 +1,6 @@ class Foo { fun bar() {} - fun f() = Unresolved()::bar + fun f() = Unresolved()::bar } -val f: () -> Unit = Unresolved()::foo +val f: () -> Unit = Unresolved()::foo diff --git a/compiler/testData/diagnostics/tests/callableReference/bound/kt12843.txt b/compiler/testData/diagnostics/tests/callableReference/bound/kt12843.txt index d6f29028676..356c2834d58 100644 --- a/compiler/testData/diagnostics/tests/callableReference/bound/kt12843.txt +++ b/compiler/testData/diagnostics/tests/callableReference/bound/kt12843.txt @@ -6,7 +6,7 @@ public final class Foo { public constructor Foo() public final fun bar(): kotlin.Unit public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean - public final fun f(): kotlin.reflect.KFunction0 + public final fun f(): [ERROR : Error function type] public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String } diff --git a/compiler/testData/diagnostics/tests/callableReference/bound/reservedExpressionSyntax.kt b/compiler/testData/diagnostics/tests/callableReference/bound/reservedExpressionSyntax.kt index b16c984c0f8..a4a3f87a651 100644 --- a/compiler/testData/diagnostics/tests/callableReference/bound/reservedExpressionSyntax.kt +++ b/compiler/testData/diagnostics/tests/callableReference/bound/reservedExpressionSyntax.kt @@ -26,8 +26,8 @@ class Test { fun List.testClassLiteral2() = b?::class fun List.testClassLiteral3() = b::class - fun List.testUnresolved1() = unresolved::foo + fun List.testUnresolved1() = unresolved::foo fun List.testUnresolved2() = a<unresolved>::foo fun List.testUnresolved3() = a<>::foo - fun List.testUnresolved4() = unresolved?::foo + fun List.testUnresolved4() = unresolved?::foo } \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/callableReference/bound/reservedExpressionSyntax3.kt b/compiler/testData/diagnostics/tests/callableReference/bound/reservedExpressionSyntax3.kt index a8865eae1d6..0ad7a50a451 100644 --- a/compiler/testData/diagnostics/tests/callableReference/bound/reservedExpressionSyntax3.kt +++ b/compiler/testData/diagnostics/tests/callableReference/bound/reservedExpressionSyntax3.kt @@ -21,8 +21,8 @@ class Test { val Int.c: Int get() = 42 - val test1: () -> Right = a.b<Int>.c::foo + val test1: () -> Right = a.b<Int>.c::foo val test1a: () -> Right = a.b.c::foo - val test2: () -> Right = a.b<Int>.c?::foo + val test2: () -> Right = a.b<Int>.c?::foo } \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/callableReference/function/unresolved.kt b/compiler/testData/diagnostics/tests/callableReference/function/unresolved.kt index 6e048684c12..722e7c87f4a 100644 --- a/compiler/testData/diagnostics/tests/callableReference/function/unresolved.kt +++ b/compiler/testData/diagnostics/tests/callableReference/function/unresolved.kt @@ -1,12 +1,22 @@ -// !DIAGNOSTICS: -UNUSED_EXPRESSION +// !DIAGNOSTICS: -UNUSED_EXPRESSION, -UNUSED_PARAMETER class A -fun main() { +fun test1() { val foo = ::foo - + ::bar - + A::bar - - B::bar + + B::bar } + +fun test2() { + fun foo(x: Any) {} + fun foo() {} + + Unresolved::foo + foo(Unresolved::foo) + foo(Unresolved::unresolved) + ::unresolved +} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/callableReference/function/unresolved.txt b/compiler/testData/diagnostics/tests/callableReference/function/unresolved.txt index f7f8de0be43..f9e93a70eaa 100644 --- a/compiler/testData/diagnostics/tests/callableReference/function/unresolved.txt +++ b/compiler/testData/diagnostics/tests/callableReference/function/unresolved.txt @@ -1,6 +1,7 @@ package -public fun main(): kotlin.Unit +public fun test1(): kotlin.Unit +public fun test2(): kotlin.Unit public final class A { public constructor A() diff --git a/compiler/testData/diagnostics/tests/callableReference/packageInLhs.kt b/compiler/testData/diagnostics/tests/callableReference/packageInLhs.kt index 43086090ff1..69c0ccba288 100644 --- a/compiler/testData/diagnostics/tests/callableReference/packageInLhs.kt +++ b/compiler/testData/diagnostics/tests/callableReference/packageInLhs.kt @@ -4,7 +4,7 @@ package foo fun test() { - foo::test + foo::test } // FILE: qualifiedName.kt @@ -12,5 +12,5 @@ fun test() { package foo.bar fun test() { - foo.bar::test + foo.bar::test } diff --git a/compiler/testData/diagnostics/tests/incompleteCode/illegalSelectorCallableReference.kt b/compiler/testData/diagnostics/tests/incompleteCode/illegalSelectorCallableReference.kt index 36a67ca024b..18a41f6d0ee 100644 --- a/compiler/testData/diagnostics/tests/incompleteCode/illegalSelectorCallableReference.kt +++ b/compiler/testData/diagnostics/tests/incompleteCode/illegalSelectorCallableReference.kt @@ -1,8 +1,8 @@ // !DIAGNOSTICS: -UNUSED_EXPRESSION fun test() { - "a"."b"::foo + "a"."b"::foo "a"."b"::class - "a"."b"."c"::foo + "a"."b"."c"::foo "a"."b"."c"::class } diff --git a/compiler/testData/diagnostics/tests/qualifiedExpression/calleeExpressionAsCallExpression.kt b/compiler/testData/diagnostics/tests/qualifiedExpression/calleeExpressionAsCallExpression.kt index 0a1bead57c9..f7f0bc31ac3 100644 --- a/compiler/testData/diagnostics/tests/qualifiedExpression/calleeExpressionAsCallExpression.kt +++ b/compiler/testData/diagnostics/tests/qualifiedExpression/calleeExpressionAsCallExpression.kt @@ -1 +1 @@ -val unwrapped = some<sdf()()Any>::unwrap \ No newline at end of file +val unwrapped = some<sdf()()Any>::unwrap \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/qualifiedExpression/nullCalleeExpression.kt b/compiler/testData/diagnostics/tests/qualifiedExpression/nullCalleeExpression.kt index fbabb3e8b49..45a670c731e 100644 --- a/compiler/testData/diagnostics/tests/qualifiedExpression/nullCalleeExpression.kt +++ b/compiler/testData/diagnostics/tests/qualifiedExpression/nullCalleeExpression.kt @@ -1 +1 @@ -val unwrapped = some.<cabc$WrapperAny>::unwrap \ No newline at end of file +val unwrapped = some.<cabc$WrapperAny>::unwrap \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/regressions/kt13685.kt b/compiler/testData/diagnostics/tests/regressions/kt13685.kt index d578e24185a..19b3b9a51cc 100644 --- a/compiler/testData/diagnostics/tests/regressions/kt13685.kt +++ b/compiler/testData/diagnostics/tests/regressions/kt13685.kt @@ -2,5 +2,5 @@ fun foo() { val text: List = null!! - text.map Any?::toString + text.map Any?::toString }