Skip to content

Commit 0111c4d

Browse files
committed
Allow references to nested class constructors in objects in LV = 1.0
#KT-16598 Fixed
1 parent 7c22113 commit 0111c4d

File tree

3 files changed

+38
-14
lines changed

3 files changed

+38
-14
lines changed

compiler/frontend/src/org/jetbrains/kotlin/types/expressions/DoubleColonExpressionResolver.kt

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ import org.jetbrains.kotlin.types.typeUtil.builtIns
5757
import org.jetbrains.kotlin.types.typeUtil.isSubtypeOf
5858
import org.jetbrains.kotlin.types.typeUtil.makeNotNullable
5959
import org.jetbrains.kotlin.types.typeUtil.makeNullable
60-
import org.jetbrains.kotlin.utils.addToStdlib.safeAs
6160
import org.jetbrains.kotlin.utils.yieldIfNotNull
6261
import java.lang.UnsupportedOperationException
6362
import java.util.*
@@ -605,27 +604,30 @@ class DoubleColonExpressionResolver(
605604
if (expression.isEmptyLHS) null
606605
else resolveDoubleColonLHS(expression, context)
607606

608-
if (lhsResult is DoubleColonLHS.Expression) {
609-
reportUnsupportedIfNeeded(expression, context)
610-
}
611-
612-
val resolutionResults =
613-
resolveCallableReferenceRHS(expression, lhsResult, context, resolveArgumentsMode)
607+
val resolutionResults = resolveCallableReferenceRHS(expression, lhsResult, context, resolveArgumentsMode)
614608

615-
reportUnsupportedReferenceToSuspendFunction(resolutionResults, expression, context)
609+
reportUnsupportedCallableReferenceIfNeeded(expression, context, lhsResult, resolutionResults)
616610

617611
return lhsResult to resolutionResults
618612
}
619613

620-
private fun reportUnsupportedReferenceToSuspendFunction(
621-
resolutionResults: OverloadResolutionResults<CallableDescriptor>?,
614+
private fun reportUnsupportedCallableReferenceIfNeeded(
622615
expression: KtCallableReferenceExpression,
623-
context: ExpressionTypingContext
616+
context: ExpressionTypingContext,
617+
lhsResult: DoubleColonLHS?,
618+
resolutionResults: OverloadResolutionResults<CallableDescriptor>?
624619
) {
625-
if (resolutionResults?.isSingleResult == true &&
626-
resolutionResults.resultingDescriptor.safeAs<FunctionDescriptor>()?.isSuspend == true) {
620+
val descriptor =
621+
if (resolutionResults?.isSingleResult == true) resolutionResults.resultingDescriptor as? FunctionDescriptor else null
622+
if (descriptor?.isSuspend == true) {
627623
context.trace.report(UNSUPPORTED.on(expression.callableReference, "Callable references to suspend functions"))
628624
}
625+
626+
val expressionResult = lhsResult as? DoubleColonLHS.Expression ?: return
627+
// "<expr>::foo" was not supported without bound callable references, except the case of a nested class constructor in an object
628+
if (!expressionResult.isObjectQualifier || descriptor !is ConstructorDescriptor) {
629+
reportUnsupportedIfNeeded(expression, context)
630+
}
629631
}
630632

631633
private class ResolutionResultsAndTraceCommitCallback(

compiler/testData/diagnostics/tests/sourceCompatibility/noBoundCallableReferences/boundCallableReference.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,14 @@ class C { companion object }
44
val ok1 = C::hashCode
55
val fail1 = <!UNSUPPORTED_FEATURE!>C.Companion<!>::hashCode
66

7-
object O
7+
object O {
8+
class Y {
9+
companion object
10+
}
11+
}
812
val fail2 = <!UNSUPPORTED_FEATURE!>O<!>::hashCode
13+
val ok2 = O::Y
14+
val ok3 = O.Y::hashCode
915

1016
fun hashCode() {}
1117

compiler/testData/diagnostics/tests/sourceCompatibility/noBoundCallableReferences/boundCallableReference.txt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ public val fail3: kotlin.reflect.KFunction0<kotlin.Int>
66
public val fail4: kotlin.reflect.KFunction0<kotlin.Int>
77
public val fail5: kotlin.reflect.KFunction0<kotlin.Int>
88
public val ok1: kotlin.reflect.KFunction1<C, kotlin.Int>
9+
public val ok2: kotlin.reflect.KFunction0<O.Y>
10+
public val ok3: kotlin.reflect.KFunction1<O.Y, kotlin.Int>
911
public fun hashCode(): kotlin.Unit
1012

1113
public final class C {
@@ -27,4 +29,18 @@ public object O {
2729
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
2830
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
2931
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
32+
33+
public final class Y {
34+
public constructor Y()
35+
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
36+
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
37+
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
38+
39+
public companion object Companion {
40+
private constructor Companion()
41+
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
42+
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
43+
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
44+
}
45+
}
3046
}

0 commit comments

Comments
 (0)