[NI] Take into account use-site variance for constraint from LHS of CR

#KT-32851 Fixed
This commit is contained in:
Mikhail Zarechenskiy
2019-08-12 17:49:39 +03:00
parent 0620762b97
commit 1969ad6e9d
6 changed files with 81 additions and 11 deletions
@@ -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");
@@ -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<LHSResult.Type>()?.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?
@@ -0,0 +1,24 @@
// !WITH_NEW_INFERENCE
// !DIAGNOSTICS: -UNUSED_PARAMETER
import kotlin.reflect.KProperty1
interface A
inline val <T : A> T.bla get() = 1
class B<T>
fun <K, V> B<K>.foo(p: KProperty1<in K, V>): B<V> = TODO()
fun <K, V> B<out K>.bar(p: KProperty1<out K, V>): B<V> = TODO()
fun <K, V> B<K>.baz(p: KProperty1<out K, V>): B<V> = TODO()
fun <K, V> B<K>.star(p: KProperty1<*, V>): B<V> = TODO()
fun <R : A> B<R>.test(){
foo(A::bla)
bar(A::bla)
<!OI;TYPE_INFERENCE_CONFLICTING_SUBSTITUTIONS!>baz<!>(<!NI;TYPE_MISMATCH!>A::bla<!>)
star(A::bla)
}
@@ -0,0 +1,21 @@
package
public val </*0*/ T : A> T.bla: kotlin.Int
public fun </*0*/ K, /*1*/ V> B<out K>.bar(/*0*/ p: kotlin.reflect.KProperty1<out K, V>): B<V>
public fun </*0*/ K, /*1*/ V> B<K>.baz(/*0*/ p: kotlin.reflect.KProperty1<out K, V>): B<V>
public fun </*0*/ K, /*1*/ V> B<K>.foo(/*0*/ p: kotlin.reflect.KProperty1<in K, V>): B<V>
public fun </*0*/ K, /*1*/ V> B<K>.star(/*0*/ p: kotlin.reflect.KProperty1<*, V>): B<V>
public fun </*0*/ R : A> B<R>.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</*0*/ T> {
public constructor B</*0*/ T>()
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
}
@@ -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");
@@ -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");