Support bound references to objects and companion objects
Tweak member extension detection a little bit to prohibit extensions imported from objects (they don't have one of the receiver parameters)
This commit is contained in:
@@ -549,7 +549,6 @@ public interface Errors {
|
||||
DiagnosticFactory1<KtExpression, CallableMemberDescriptor> EXTENSION_IN_CLASS_REFERENCE_NOT_ALLOWED = DiagnosticFactory1.create(ERROR);
|
||||
DiagnosticFactory0<KtExpression> CALLABLE_REFERENCE_LHS_NOT_A_CLASS = DiagnosticFactory0.create(ERROR);
|
||||
DiagnosticFactory0<KtExpression> CALLABLE_REFERENCE_TO_MEMBER_OR_EXTENSION_WITH_EMPTY_LHS = DiagnosticFactory0.create(ERROR);
|
||||
DiagnosticFactory0<KtExpression> CALLABLE_REFERENCE_TO_OBJECT_MEMBER = DiagnosticFactory0.create(ERROR);
|
||||
DiagnosticFactory0<KtExpression> CALLABLE_REFERENCE_TO_ANNOTATION_CONSTRUCTOR = DiagnosticFactory0.create(ERROR);
|
||||
|
||||
DiagnosticFactory0<KtExpression> CLASS_LITERAL_LHS_NOT_A_CLASS = DiagnosticFactory0.create(ERROR);
|
||||
|
||||
+1
-2
@@ -40,7 +40,7 @@ import java.util.List;
|
||||
|
||||
import static org.jetbrains.kotlin.diagnostics.Errors.*;
|
||||
import static org.jetbrains.kotlin.diagnostics.rendering.Renderers.*;
|
||||
import static org.jetbrains.kotlin.diagnostics.rendering.RenderingContext.*;
|
||||
import static org.jetbrains.kotlin.diagnostics.rendering.RenderingContext.of;
|
||||
|
||||
public class DefaultErrorMessages {
|
||||
|
||||
@@ -733,7 +733,6 @@ public class DefaultErrorMessages {
|
||||
MAP.put(CALLABLE_REFERENCE_TO_MEMBER_OR_EXTENSION_WITH_EMPTY_LHS,
|
||||
"Left-hand side of a callable reference with a receiver parameter cannot be empty. " +
|
||||
"Please specify the type of the receiver before '::' explicitly");
|
||||
MAP.put(CALLABLE_REFERENCE_TO_OBJECT_MEMBER, "Callable references to object members are not supported");
|
||||
MAP.put(CALLABLE_REFERENCE_TO_ANNOTATION_CONSTRUCTOR, "Annotation class cannot be instantiated");
|
||||
|
||||
MAP.put(CLASS_LITERAL_LHS_NOT_A_CLASS, "Only classes are allowed on the left hand side of a class literal");
|
||||
|
||||
+12
-7
@@ -184,6 +184,13 @@ private fun createReflectionTypeForCallableDescriptor(
|
||||
reportOn: KtExpression?,
|
||||
ignoreReceiver: Boolean
|
||||
): KotlinType? {
|
||||
if (descriptor is CallableMemberDescriptor && isMemberExtension(descriptor)) {
|
||||
if (reportOn != null) {
|
||||
trace?.report(EXTENSION_IN_CLASS_REFERENCE_NOT_ALLOWED.on(reportOn, descriptor))
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
val extensionReceiver = descriptor.extensionReceiverParameter
|
||||
val dispatchReceiver = descriptor.dispatchReceiverParameter?.let { dispatchReceiver ->
|
||||
// See CallableDescriptor#getOwnerForEffectiveDispatchReceiverParameter
|
||||
@@ -192,13 +199,6 @@ private fun createReflectionTypeForCallableDescriptor(
|
||||
else dispatchReceiver
|
||||
}
|
||||
|
||||
if (extensionReceiver != null && dispatchReceiver != null && descriptor is CallableMemberDescriptor) {
|
||||
if (reportOn != null) {
|
||||
trace?.report(EXTENSION_IN_CLASS_REFERENCE_NOT_ALLOWED.on(reportOn, descriptor))
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
val receiverType =
|
||||
if ((extensionReceiver != null || dispatchReceiver != null) && !ignoreReceiver)
|
||||
lhsType ?: extensionReceiver?.type ?: dispatchReceiver?.type
|
||||
@@ -224,6 +224,11 @@ private fun createReflectionTypeForCallableDescriptor(
|
||||
}
|
||||
}
|
||||
|
||||
private fun isMemberExtension(descriptor: CallableMemberDescriptor): Boolean {
|
||||
val original = (descriptor as? ImportedFromObjectCallableDescriptor<*>)?.callableFromObject ?: descriptor
|
||||
return original.extensionReceiverParameter != null && original.dispatchReceiverParameter != null
|
||||
}
|
||||
|
||||
fun getReflectionTypeForCandidateDescriptor(
|
||||
descriptor: CallableDescriptor,
|
||||
reflectionTypes: ReflectionTypes,
|
||||
|
||||
+20
-10
@@ -27,14 +27,17 @@ import org.jetbrains.kotlin.descriptors.annotations.Annotations
|
||||
import org.jetbrains.kotlin.diagnostics.Errors.*
|
||||
import org.jetbrains.kotlin.psi.*
|
||||
import org.jetbrains.kotlin.psi.codeFragmentUtil.suppressDiagnosticsInDebugMode
|
||||
import org.jetbrains.kotlin.psi.psiUtil.getQualifiedElementSelector
|
||||
import org.jetbrains.kotlin.resolve.*
|
||||
import org.jetbrains.kotlin.resolve.callableReferences.createReflectionTypeForResolvedCallableReference
|
||||
import org.jetbrains.kotlin.resolve.callableReferences.resolvePossiblyAmbiguousCallableReference
|
||||
import org.jetbrains.kotlin.resolve.calls.CallResolver
|
||||
import org.jetbrains.kotlin.resolve.calls.callResolverUtil.ResolveArgumentsMode
|
||||
import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall
|
||||
import org.jetbrains.kotlin.resolve.calls.context.TemporaryTraceAndCache
|
||||
import org.jetbrains.kotlin.resolve.calls.results.OverloadResolutionResults
|
||||
import org.jetbrains.kotlin.resolve.calls.results.OverloadResolutionResultsUtil
|
||||
import org.jetbrains.kotlin.resolve.calls.util.FakeCallableDescriptorForObject
|
||||
import org.jetbrains.kotlin.types.ErrorUtils
|
||||
import org.jetbrains.kotlin.types.KotlinType
|
||||
import org.jetbrains.kotlin.types.KotlinTypeImpl
|
||||
@@ -138,11 +141,22 @@ class DoubleColonExpressionResolver(
|
||||
val contextForExpr = c.replaceTraceAndCache(traceForExpr)
|
||||
val typeInfo = expressionTypingServices.getTypeInfo(expression, contextForExpr)
|
||||
val type = typeInfo.type
|
||||
// TODO (!!!): it's wrong to only check type, should check that there's a companion qualifier
|
||||
if (type != null && !DescriptorUtils.isCompanionObject(type.constructor.declarationDescriptor)) {
|
||||
traceForExpr.commit()
|
||||
return DoubleColonLHS.Expression(typeInfo).apply {
|
||||
c.trace.record(BindingContext.DOUBLE_COLON_LHS, expression, this)
|
||||
|
||||
if (type != null) {
|
||||
val call = traceForExpr.trace.bindingContext[BindingContext.CALL, expression.getQualifiedElementSelector()]
|
||||
val resolvedCall = call.getResolvedCall(traceForExpr.trace.bindingContext)
|
||||
val implicitReferenceToCompanion =
|
||||
resolvedCall != null &&
|
||||
resolvedCall.resultingDescriptor.let { result ->
|
||||
result is FakeCallableDescriptorForObject &&
|
||||
result.classDescriptor.companionObjectDescriptor != null
|
||||
}
|
||||
|
||||
if (!implicitReferenceToCompanion) {
|
||||
traceForExpr.commit()
|
||||
return DoubleColonLHS.Expression(typeInfo).apply {
|
||||
c.trace.record(BindingContext.DOUBLE_COLON_LHS, expression, this)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -248,11 +262,7 @@ class DoubleColonExpressionResolver(
|
||||
context.trace.report(CALLABLE_REFERENCE_TO_MEMBER_OR_EXTENSION_WITH_EMPTY_LHS.on(reference))
|
||||
}
|
||||
|
||||
val containingDeclaration = descriptor.containingDeclaration
|
||||
if (DescriptorUtils.isObject(containingDeclaration)) {
|
||||
context.trace.report(CALLABLE_REFERENCE_TO_OBJECT_MEMBER.on(reference))
|
||||
}
|
||||
if (descriptor is ConstructorDescriptor && DescriptorUtils.isAnnotationClass(containingDeclaration)) {
|
||||
if (descriptor is ConstructorDescriptor && DescriptorUtils.isAnnotationClass(descriptor.containingDeclaration)) {
|
||||
context.trace.report(CALLABLE_REFERENCE_TO_ANNOTATION_CONSTRUCTOR.on(reference))
|
||||
}
|
||||
|
||||
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
// !CHECK_TYPE
|
||||
// !DIAGNOSTICS: -UNUSED_EXPRESSION
|
||||
|
||||
package test
|
||||
|
||||
class C {
|
||||
companion object {
|
||||
fun foo(): String = "companion"
|
||||
fun bar() {}
|
||||
}
|
||||
|
||||
fun foo(): Int = 0
|
||||
}
|
||||
|
||||
fun test() {
|
||||
val r1 = C::foo
|
||||
checkSubtype<(C) -> Int>(r1)
|
||||
|
||||
val r2 = test.C::foo
|
||||
checkSubtype<(C) -> Int>(r2)
|
||||
|
||||
val r3 = C.Companion::foo
|
||||
checkSubtype<() -> String>(r3)
|
||||
|
||||
val r4 = test.C.Companion::foo
|
||||
checkSubtype<() -> String>(r4)
|
||||
|
||||
val r5 = (C)::foo
|
||||
checkSubtype<() -> String>(r5)
|
||||
|
||||
val r6 = (test.C)::foo
|
||||
checkSubtype<() -> String>(r6)
|
||||
|
||||
val c = C.Companion
|
||||
val r7 = c::foo
|
||||
checkSubtype<() -> String>(r7)
|
||||
|
||||
C::<!UNRESOLVED_REFERENCE!>bar<!>
|
||||
}
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
package
|
||||
|
||||
package test {
|
||||
public fun test(): kotlin.Unit
|
||||
|
||||
public final class C {
|
||||
public constructor C()
|
||||
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
|
||||
public final fun foo(): kotlin.Int
|
||||
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
|
||||
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
|
||||
|
||||
public companion object Companion {
|
||||
private constructor Companion()
|
||||
public final fun bar(): kotlin.Unit
|
||||
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
|
||||
public final fun foo(): kotlin.String
|
||||
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
|
||||
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
// !CHECK_TYPE
|
||||
|
||||
object Obj {
|
||||
fun foo() {}
|
||||
val bar = 2
|
||||
}
|
||||
|
||||
fun test() {
|
||||
checkSubtype<() -> Unit>(Obj::foo)
|
||||
checkSubtype<() -> Int>(Obj::bar)
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package
|
||||
|
||||
public fun test(): kotlin.Unit
|
||||
|
||||
public object Obj {
|
||||
private constructor Obj()
|
||||
public final val bar: kotlin.Int = 2
|
||||
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
|
||||
public final fun foo(): kotlin.Unit
|
||||
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
|
||||
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
|
||||
}
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
// !DIAGNOSTICS: -UNUSED_EXPRESSION
|
||||
|
||||
import Obj.ext
|
||||
import A.Companion.ext2
|
||||
|
||||
object Obj {
|
||||
val String.ext: String get() = this
|
||||
}
|
||||
|
||||
class A {
|
||||
companion object {
|
||||
val String.ext2: String get() = this
|
||||
}
|
||||
}
|
||||
|
||||
fun test() {
|
||||
String::<!EXTENSION_IN_CLASS_REFERENCE_NOT_ALLOWED!>ext<!>
|
||||
Obj::<!UNRESOLVED_REFERENCE_WRONG_RECEIVER!>ext<!>
|
||||
|
||||
String::<!EXTENSION_IN_CLASS_REFERENCE_NOT_ALLOWED!>ext2<!>
|
||||
A.Companion::<!UNRESOLVED_REFERENCE_WRONG_RECEIVER!>ext2<!>
|
||||
A::<!UNRESOLVED_REFERENCE_WRONG_RECEIVER!>ext2<!>
|
||||
|
||||
A::<!UNRESOLVED_REFERENCE!>foo<!>
|
||||
A::<!UNRESOLVED_REFERENCE!>bar<!>
|
||||
}
|
||||
-4
@@ -10,10 +10,8 @@ public final class A {
|
||||
|
||||
public companion object Companion {
|
||||
private constructor Companion()
|
||||
public final val bar: kotlin.Int = 2
|
||||
public final val kotlin.String.ext2: kotlin.String
|
||||
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
|
||||
public final fun foo(): kotlin.Unit
|
||||
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
|
||||
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
|
||||
}
|
||||
@@ -21,10 +19,8 @@ public final class A {
|
||||
|
||||
public object Obj {
|
||||
private constructor Obj()
|
||||
public final val bar: kotlin.Int = 2
|
||||
public final val kotlin.String.ext: kotlin.String
|
||||
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
|
||||
public final fun foo(): kotlin.Unit
|
||||
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
|
||||
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
|
||||
}
|
||||
-31
@@ -1,31 +0,0 @@
|
||||
// !DIAGNOSTICS: -UNUSED_EXPRESSION
|
||||
|
||||
import Obj.ext
|
||||
import A.Companion.ext2
|
||||
|
||||
object Obj {
|
||||
fun foo() {}
|
||||
val bar = 2
|
||||
val String.ext: String get() = this
|
||||
}
|
||||
|
||||
class A {
|
||||
companion object {
|
||||
fun foo() {}
|
||||
val bar = 2
|
||||
val String.ext2: String get() = this
|
||||
}
|
||||
}
|
||||
|
||||
fun test() {
|
||||
Obj::<!CALLABLE_REFERENCE_TO_OBJECT_MEMBER!>foo<!>
|
||||
Obj::<!CALLABLE_REFERENCE_TO_OBJECT_MEMBER!>bar<!>
|
||||
String::<!CALLABLE_REFERENCE_TO_OBJECT_MEMBER!>ext<!>
|
||||
|
||||
A.Companion::<!CALLABLE_REFERENCE_TO_OBJECT_MEMBER!>foo<!>
|
||||
A.Companion::<!CALLABLE_REFERENCE_TO_OBJECT_MEMBER!>bar<!>
|
||||
String::<!CALLABLE_REFERENCE_TO_OBJECT_MEMBER!>ext2<!>
|
||||
|
||||
A::<!UNRESOLVED_REFERENCE!>foo<!>
|
||||
A::<!UNRESOLVED_REFERENCE!>bar<!>
|
||||
}
|
||||
@@ -1683,9 +1683,9 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest {
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("objectMembersUnsupported.kt")
|
||||
public void testObjectMembersUnsupported() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/callableReference/objectMembersUnsupported.kt");
|
||||
@TestMetadata("memberExtensionsImportedFromObjectsUnsupported.kt")
|
||||
public void testMemberExtensionsImportedFromObjectsUnsupported() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/callableReference/memberExtensionsImportedFromObjectsUnsupported.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@@ -1713,6 +1713,27 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest {
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("compiler/testData/diagnostics/tests/callableReference/bound")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
public static class Bound extends AbstractDiagnosticsTest {
|
||||
public void testAllFilesPresentInBound() throws Exception {
|
||||
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/diagnostics/tests/callableReference/bound"), Pattern.compile("^(.+)\\.kt$"), true);
|
||||
}
|
||||
|
||||
@TestMetadata("companionObject.kt")
|
||||
public void testCompanionObject() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/callableReference/bound/companionObject.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("object.kt")
|
||||
public void testObject() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/callableReference/bound/object.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("compiler/testData/diagnostics/tests/callableReference/function")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
|
||||
Reference in New Issue
Block a user