NI: Report incompatible receiver of callable reference

^KT-35535 Fixed
This commit is contained in:
Denis Zharkov
2019-12-18 15:26:00 +03:00
parent dd56c3d918
commit c486fa6189
8 changed files with 72 additions and 7 deletions
@@ -1789,6 +1789,11 @@ public class FirOldFrontendDiagnosticsTestGenerated extends AbstractFirOldFronte
runTest("compiler/testData/diagnostics/tests/callableReference/constraintFromLHSWithCorrectDirection.kt");
}
@TestMetadata("constraintFromLHSWithCorrectDirectionError.kt")
public void testConstraintFromLHSWithCorrectDirectionError() throws Exception {
runTest("compiler/testData/diagnostics/tests/callableReference/constraintFromLHSWithCorrectDirectionError.kt");
}
@TestMetadata("correctInfoAfterArrayLikeCall.kt")
public void testCorrectInfoAfterArrayLikeCall() throws Exception {
runTest("compiler/testData/diagnostics/tests/callableReference/correctInfoAfterArrayLikeCall.kt");
@@ -265,8 +265,13 @@ class DiagnosticReporterByTrackingStrategy(
NewConstraintError::class.java -> {
val constraintError = diagnostic as NewConstraintError
val position = constraintError.position.from
val argument = (position as? ArgumentConstraintPosition)?.argument
?: (position as? ReceiverConstraintPosition)?.argument
val argument =
when (position) {
is ArgumentConstraintPosition -> position.argument
is ReceiverConstraintPosition -> position.argument
is LHSArgumentConstraintPosition -> position.argument
else -> null
}
argument?.let {
it.safeAs<LambdaKotlinCallArgument>()?.let lambda@{ lambda ->
val parameterTypes = lambda.parametersTypes?.toList() ?: return@lambda
@@ -162,7 +162,7 @@ private fun preprocessCallableReference(
val lhsResult = argument.lhsResult
if (lhsResult is LHSResult.Type) {
csBuilder.addConstraintFromLHS(lhsResult, expectedType)
csBuilder.addConstraintFromLHS(argument, lhsResult, expectedType)
}
val notCallableTypeConstructor =
@@ -181,13 +181,17 @@ private fun preprocessCallableReference(
return result
}
private fun ConstraintSystemBuilder.addConstraintFromLHS(lhsResult: LHSResult.Type, expectedType: UnwrappedType) {
private fun ConstraintSystemBuilder.addConstraintFromLHS(
argument: CallableReferenceKotlinCallArgument,
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)
val constraintPosition = LHSArgumentConstraintPosition(argument, lhsResult.qualifier ?: lhsResult.unboundDetailedReceiver)
when (expectedTypeProjectionForLHS.projectionKind) {
Variance.INVARIANT -> addEqualityConstraint(lhsType, expectedTypeForLHS, constraintPosition)
@@ -63,7 +63,10 @@ class KnownTypeParameterConstraintPosition(val typeArgument: KotlinType) : Const
override fun toString() = "TypeArgument $typeArgument"
}
class LHSArgumentConstraintPosition(val receiver: DetailedReceiver) : ConstraintPosition() {
class LHSArgumentConstraintPosition(
val argument: CallableReferenceKotlinCallArgument,
val receiver: DetailedReceiver
) : ConstraintPosition() {
override fun toString(): String {
return "LHS receiver $receiver"
}
@@ -121,4 +124,4 @@ class ConstrainingTypeIsError(
val typeVariable: TypeVariableMarker,
val constraintType: KotlinTypeMarker,
val position: IncorporationConstraintPosition
) : ConstraintSystemCallDiagnostic(INAPPLICABLE)
) : ConstraintSystemCallDiagnostic(INAPPLICABLE)
@@ -0,0 +1,19 @@
// SKIP_TXT
// !LANGUAGE: +NewInference
// !DIAGNOSTICS: -UNUSED_PARAMETER
package test
import kotlin.reflect.KProperty1
interface A {
val bla: CharSequence get() = ""
}
class B<T>(val x: T)
fun <K, V> B<K>.foo(p: KProperty1<K, V>) {}
class C : A
fun <R : A> B<R>.test(){
<!INAPPLICABLE_CANDIDATE!>foo<!>(C::bla)
}
@@ -0,0 +1,19 @@
// SKIP_TXT
// !LANGUAGE: +NewInference
// !DIAGNOSTICS: -UNUSED_PARAMETER
package test
import kotlin.reflect.KProperty1
interface A {
val bla: CharSequence get() = ""
}
class B<T>(val x: T)
fun <K, V> B<K>.foo(p: KProperty1<K, V>) {}
class C : A
fun <R : A> B<R>.test(){
foo(<!TYPE_MISMATCH, TYPE_MISMATCH, TYPE_MISMATCH!>C::bla<!>)
}
@@ -1796,6 +1796,11 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest {
runTest("compiler/testData/diagnostics/tests/callableReference/constraintFromLHSWithCorrectDirection.kt");
}
@TestMetadata("constraintFromLHSWithCorrectDirectionError.kt")
public void testConstraintFromLHSWithCorrectDirectionError() throws Exception {
runTest("compiler/testData/diagnostics/tests/callableReference/constraintFromLHSWithCorrectDirectionError.kt");
}
@TestMetadata("correctInfoAfterArrayLikeCall.kt")
public void testCorrectInfoAfterArrayLikeCall() throws Exception {
runTest("compiler/testData/diagnostics/tests/callableReference/correctInfoAfterArrayLikeCall.kt");
@@ -1791,6 +1791,11 @@ public class DiagnosticsUsingJavacTestGenerated extends AbstractDiagnosticsUsing
runTest("compiler/testData/diagnostics/tests/callableReference/constraintFromLHSWithCorrectDirection.kt");
}
@TestMetadata("constraintFromLHSWithCorrectDirectionError.kt")
public void testConstraintFromLHSWithCorrectDirectionError() throws Exception {
runTest("compiler/testData/diagnostics/tests/callableReference/constraintFromLHSWithCorrectDirectionError.kt");
}
@TestMetadata("correctInfoAfterArrayLikeCall.kt")
public void testCorrectInfoAfterArrayLikeCall() throws Exception {
runTest("compiler/testData/diagnostics/tests/callableReference/correctInfoAfterArrayLikeCall.kt");