diff --git a/compiler/fir/resolve/tests/org/jetbrains/kotlin/fir/FirDiagnosticsSmokeTestGenerated.java b/compiler/fir/resolve/tests/org/jetbrains/kotlin/fir/FirDiagnosticsSmokeTestGenerated.java index deb62c9cd40..26d37186128 100644 --- a/compiler/fir/resolve/tests/org/jetbrains/kotlin/fir/FirDiagnosticsSmokeTestGenerated.java +++ b/compiler/fir/resolve/tests/org/jetbrains/kotlin/fir/FirDiagnosticsSmokeTestGenerated.java @@ -1687,6 +1687,11 @@ public class FirDiagnosticsSmokeTestGenerated extends AbstractFirDiagnosticsSmok runTest("compiler/testData/diagnostics/tests/callableReference/classVsPackage.kt"); } + @TestMetadata("constraintFromLHSWithCorrectDirection.kt") + public void testConstraintFromLHSWithCorrectDirection() throws Exception { + runTest("compiler/testData/diagnostics/tests/callableReference/constraintFromLHSWithCorrectDirection.kt"); + } + @TestMetadata("correctInfoAfterArrayLikeCall.kt") public void testCorrectInfoAfterArrayLikeCall() throws Exception { runTest("compiler/testData/diagnostics/tests/callableReference/correctInfoAfterArrayLikeCall.kt"); diff --git a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/components/PostponeArgumentsChecks.kt b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/components/PostponeArgumentsChecks.kt index 958660cffd3..b378e512fdf 100644 --- a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/components/PostponeArgumentsChecks.kt +++ b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/components/PostponeArgumentsChecks.kt @@ -25,6 +25,7 @@ import org.jetbrains.kotlin.resolve.calls.inference.model.LHSArgumentConstraintP import org.jetbrains.kotlin.resolve.calls.inference.model.TypeVariableForLambdaReturnType import org.jetbrains.kotlin.resolve.calls.model.* import org.jetbrains.kotlin.types.UnwrappedType +import org.jetbrains.kotlin.types.Variance import org.jetbrains.kotlin.types.typeUtil.builtIns import org.jetbrains.kotlin.utils.addToStdlib.safeAs @@ -159,21 +160,15 @@ private fun preprocessCallableReference( if (expectedType == null) return result + val lhsResult = argument.lhsResult + if (lhsResult is LHSResult.Type) { + csBuilder.addConstraintFromLHS(lhsResult, expectedType) + } + val notCallableTypeConstructor = csBuilder.getProperSuperTypeConstructors(expectedType) .firstOrNull { !ReflectionTypes.isPossibleExpectedCallableType(it.requireIs()) } - argument.lhsResult.safeAs()?.let { - val lhsType = it.unboundDetailedReceiver.stableType - if (ReflectionTypes.isNumberedTypeWithOneOrMoreNumber(expectedType)) { - val lhsTypeVariable = expectedType.arguments.first().type.unwrap() - csBuilder.addSubtypeConstraint( - lhsType, - lhsTypeVariable, - LHSArgumentConstraintPosition(it.qualifier ?: it.unboundDetailedReceiver) - ) - } - } if (notCallableTypeConstructor != null) { diagnosticsHolder.addDiagnostic( NotCallableExpectedType( @@ -186,6 +181,21 @@ private fun preprocessCallableReference( return result } +private fun ConstraintSystemBuilder.addConstraintFromLHS(lhsResult: LHSResult.Type, expectedType: UnwrappedType) { + if (!ReflectionTypes.isNumberedTypeWithOneOrMoreNumber(expectedType)) return + + val lhsType = lhsResult.unboundDetailedReceiver.stableType + val expectedTypeProjectionForLHS = expectedType.arguments.first() + val expectedTypeForLHS = expectedTypeProjectionForLHS.type + val constraintPosition = LHSArgumentConstraintPosition(lhsResult.qualifier ?: lhsResult.unboundDetailedReceiver) + + when (expectedTypeProjectionForLHS.projectionKind) { + Variance.INVARIANT -> addEqualityConstraint(lhsType, expectedTypeForLHS, constraintPosition) + Variance.IN_VARIANCE -> addSubtypeConstraint(expectedType, lhsType, constraintPosition) + Variance.OUT_VARIANCE -> addSubtypeConstraint(lhsType, expectedType, constraintPosition) + } +} + private fun preprocessCollectionLiteralArgument( collectionLiteralArgument: CollectionLiteralKotlinCallArgument, expectedType: UnwrappedType? diff --git a/compiler/testData/diagnostics/tests/callableReference/constraintFromLHSWithCorrectDirection.kt b/compiler/testData/diagnostics/tests/callableReference/constraintFromLHSWithCorrectDirection.kt new file mode 100644 index 00000000000..a46c6cc7cd7 --- /dev/null +++ b/compiler/testData/diagnostics/tests/callableReference/constraintFromLHSWithCorrectDirection.kt @@ -0,0 +1,24 @@ +// !WITH_NEW_INFERENCE +// !DIAGNOSTICS: -UNUSED_PARAMETER + +import kotlin.reflect.KProperty1 + +interface A +inline val T.bla get() = 1 + +class B +fun B.foo(p: KProperty1): B = TODO() + +fun B.bar(p: KProperty1): B = TODO() + +fun B.baz(p: KProperty1): B = TODO() + +fun B.star(p: KProperty1<*, V>): B = TODO() + + +fun B.test(){ + foo(A::bla) + bar(A::bla) + baz(A::bla) + star(A::bla) +} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/callableReference/constraintFromLHSWithCorrectDirection.txt b/compiler/testData/diagnostics/tests/callableReference/constraintFromLHSWithCorrectDirection.txt new file mode 100644 index 00000000000..80b79c95713 --- /dev/null +++ b/compiler/testData/diagnostics/tests/callableReference/constraintFromLHSWithCorrectDirection.txt @@ -0,0 +1,21 @@ +package + +public val T.bla: kotlin.Int +public fun B.bar(/*0*/ p: kotlin.reflect.KProperty1): B +public fun B.baz(/*0*/ p: kotlin.reflect.KProperty1): B +public fun B.foo(/*0*/ p: kotlin.reflect.KProperty1): B +public fun B.star(/*0*/ p: kotlin.reflect.KProperty1<*, V>): B +public fun B.test(): kotlin.Unit + +public interface A { + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String +} + +public final class B { + public constructor B() + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String +} diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java index 209d26bb10e..73181bd82fb 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java @@ -1694,6 +1694,11 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest { runTest("compiler/testData/diagnostics/tests/callableReference/classVsPackage.kt"); } + @TestMetadata("constraintFromLHSWithCorrectDirection.kt") + public void testConstraintFromLHSWithCorrectDirection() throws Exception { + runTest("compiler/testData/diagnostics/tests/callableReference/constraintFromLHSWithCorrectDirection.kt"); + } + @TestMetadata("correctInfoAfterArrayLikeCall.kt") public void testCorrectInfoAfterArrayLikeCall() throws Exception { runTest("compiler/testData/diagnostics/tests/callableReference/correctInfoAfterArrayLikeCall.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java index d2e391a285a..79ef04507dd 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java @@ -1689,6 +1689,11 @@ public class DiagnosticsUsingJavacTestGenerated extends AbstractDiagnosticsUsing runTest("compiler/testData/diagnostics/tests/callableReference/classVsPackage.kt"); } + @TestMetadata("constraintFromLHSWithCorrectDirection.kt") + public void testConstraintFromLHSWithCorrectDirection() throws Exception { + runTest("compiler/testData/diagnostics/tests/callableReference/constraintFromLHSWithCorrectDirection.kt"); + } + @TestMetadata("correctInfoAfterArrayLikeCall.kt") public void testCorrectInfoAfterArrayLikeCall() throws Exception { runTest("compiler/testData/diagnostics/tests/callableReference/correctInfoAfterArrayLikeCall.kt");