Fix compatibility resolve for references with multiple outer candidates

#KT-39533 Fixed
This commit is contained in:
Mikhail Zarechenskiy
2020-06-18 16:24:09 +03:00
parent 58183b774d
commit 9c8e979308
15 changed files with 280 additions and 4 deletions
@@ -1794,6 +1794,11 @@ public class FirOldFrontendDiagnosticsTestGenerated extends AbstractFirOldFronte
runTest("compiler/testData/diagnostics/tests/callableReference/classVsPackage.kt");
}
@TestMetadata("compatibilityResolveWithVarargAndOperatorCall.kt")
public void testCompatibilityResolveWithVarargAndOperatorCall() throws Exception {
runTest("compiler/testData/diagnostics/tests/callableReference/compatibilityResolveWithVarargAndOperatorCall.kt");
}
@TestMetadata("constraintFromLHSWithCorrectDirection.kt")
public void testConstraintFromLHSWithCorrectDirection() throws Exception {
runTest("compiler/testData/diagnostics/tests/callableReference/constraintFromLHSWithCorrectDirection.kt");
@@ -1804,6 +1809,11 @@ public class FirOldFrontendDiagnosticsTestGenerated extends AbstractFirOldFronte
runTest("compiler/testData/diagnostics/tests/callableReference/constraintFromLHSWithCorrectDirectionError.kt");
}
@TestMetadata("correctCandidateWithCompatibilityForSeveralCandidates.kt")
public void testCorrectCandidateWithCompatibilityForSeveralCandidates() throws Exception {
runTest("compiler/testData/diagnostics/tests/callableReference/correctCandidateWithCompatibilityForSeveralCandidates.kt");
}
@TestMetadata("correctInfoAfterArrayLikeCall.kt")
public void testCorrectInfoAfterArrayLikeCall() throws Exception {
runTest("compiler/testData/diagnostics/tests/callableReference/correctInfoAfterArrayLikeCall.kt");
@@ -1974,6 +1984,11 @@ public class FirOldFrontendDiagnosticsTestGenerated extends AbstractFirOldFronte
runTest("compiler/testData/diagnostics/tests/callableReference/typealiases.kt");
}
@TestMetadata("unitAdaptationForReferenceCompatibility.kt")
public void testUnitAdaptationForReferenceCompatibility() throws Exception {
runTest("compiler/testData/diagnostics/tests/callableReference/unitAdaptationForReferenceCompatibility.kt");
}
@TestMetadata("unused.kt")
public void testUnused() throws Exception {
runTest("compiler/testData/diagnostics/tests/callableReference/unused.kt");
@@ -287,9 +287,12 @@ class KotlinToResolvedCallTransformer(
is ArgumentMatch -> {
parameter = argumentMapping.valueParameter
// We should take expected type from the last used conversion
// TODO: move this logic into ParameterTypeConversion
val expectedType =
resolvedCall.getExpectedTypeForSamConvertedArgument(valueArgument)
resolvedCall.getExpectedTypeForUnitConvertedArgument(valueArgument)
?: resolvedCall.getExpectedTypeForSuspendConvertedArgument(valueArgument)
?: resolvedCall.getExpectedTypeForSamConvertedArgument(valueArgument)
?: getEffectiveExpectedType(argumentMapping.valueParameter, valueArgument, context)
Pair(
expectedType,
@@ -667,6 +670,7 @@ class NewResolvedCallImpl<D : CallableDescriptor>(
private var smartCastDispatchReceiverType: KotlinType? = null
private var expectedTypeForSamConvertedArgumentMap: MutableMap<ValueArgument, UnwrappedType>? = null
private var expectedTypeForSuspendConvertedArgumentMap: MutableMap<ValueArgument, UnwrappedType>? = null
private var expectedTypeForUnitConvertedArgumentMap: MutableMap<ValueArgument, UnwrappedType>? = null
private var argumentTypeForConstantConvertedMap: MutableMap<KtExpression, IntegerValueTypeConstant>? = null
@@ -749,6 +753,7 @@ class NewResolvedCallImpl<D : CallableDescriptor>(
calculateExpectedTypeForSamConvertedArgumentMap(substitutor)
calculateExpectedTypeForSuspendConvertedArgumentMap(substitutor)
calculateExpectedTypeForUnitConvertedArgumentMap(substitutor)
calculateExpectedTypeForConstantConvertedArgumentMap()
}
@@ -820,6 +825,9 @@ class NewResolvedCallImpl<D : CallableDescriptor>(
fun getExpectedTypeForSuspendConvertedArgument(valueArgument: ValueArgument): UnwrappedType? =
expectedTypeForSuspendConvertedArgumentMap?.get(valueArgument)
fun getExpectedTypeForUnitConvertedArgument(valueArgument: ValueArgument): UnwrappedType? =
expectedTypeForUnitConvertedArgumentMap?.get(valueArgument)
private fun calculateExpectedTypeForConstantConvertedArgumentMap() {
if (resolvedCallAtom.argumentsWithConstantConversion.isEmpty()) return
@@ -853,6 +861,18 @@ class NewResolvedCallImpl<D : CallableDescriptor>(
}
}
private fun calculateExpectedTypeForUnitConvertedArgumentMap(substitutor: NewTypeSubstitutor?) {
if (resolvedCallAtom.argumentsWithUnitConversion.isEmpty()) return
expectedTypeForUnitConvertedArgumentMap = hashMapOf()
for ((argument, convertedType) in resolvedCallAtom.argumentsWithUnitConversion) {
val typeWithFreshVariables = resolvedCallAtom.freshVariablesSubstitutor.safeSubstitute(convertedType)
val expectedType = substitutor?.safeSubstitute(typeWithFreshVariables) ?: typeWithFreshVariables
expectedTypeForUnitConvertedArgumentMap!![argument.psiCallArgument.valueArgument] = expectedType
}
}
override fun argumentToParameterMap(
resultingDescriptor: CallableDescriptor,
valueArguments: Map<ValueParameterDescriptor, ResolvedValueArgument>,
@@ -905,7 +925,8 @@ class NewResolvedCallImpl<D : CallableDescriptor>(
fun ResolutionCandidateApplicability.toResolutionStatus(): ResolutionStatus = when (this) {
ResolutionCandidateApplicability.RESOLVED,
ResolutionCandidateApplicability.RESOLVED_LOW_PRIORITY,
ResolutionCandidateApplicability.RESOLVED_WITH_ERROR -> ResolutionStatus.SUCCESS
ResolutionCandidateApplicability.RESOLVED_WITH_ERROR,
ResolutionCandidateApplicability.RESOLVED_NEED_PRESERVE_COMPATIBILITY -> ResolutionStatus.SUCCESS
ResolutionCandidateApplicability.INAPPLICABLE_WRONG_RECEIVER -> ResolutionStatus.RECEIVER_TYPE_ERROR
else -> ResolutionStatus.OTHER_ERROR
}
@@ -110,7 +110,6 @@ class CallableReferenceResolver(
chosenCandidate.diagnostics.forEach {
val transformedDiagnostic = when (it) {
is CompatibilityWarning -> CompatibilityWarningOnArgument(argument, it.candidate)
is LowerPriorityToPreserveCompatibility -> return@forEach
else -> it
}
diagnosticsHolder.addDiagnostic(transformedDiagnostic)
@@ -0,0 +1,18 @@
// !DIAGNOSTICS: -UNUSED_PARAMETER
fun interface IFoo {
fun foo(i: Int)
}
fun interface IFoo2 : IFoo
object A
operator fun A.get(i: IFoo) = 1
operator fun A.set(i: IFoo, newValue: Int) {}
fun withVararg(vararg xs: Int) = 42
fun test1() {
<!UNRESOLVED_REFERENCE!><!INAPPLICABLE_CANDIDATE!>A[::withVararg]<!> += 1<!>
}
@@ -0,0 +1,18 @@
// !DIAGNOSTICS: -UNUSED_PARAMETER
fun interface IFoo {
fun foo(i: Int)
}
fun interface IFoo2 : IFoo
object A
operator fun A.get(i: IFoo) = 1
operator fun A.set(i: IFoo, newValue: Int) {}
fun withVararg(vararg xs: Int) = 42
fun test1() {
A[::withVararg] += 1
}
@@ -0,0 +1,27 @@
package
public fun test1(): kotlin.Unit
public fun withVararg(/*0*/ vararg xs: kotlin.Int /*kotlin.IntArray*/): kotlin.Int
public operator fun A.get(/*0*/ i: IFoo): kotlin.Int
public operator fun A.set(/*0*/ i: IFoo, /*1*/ newValue: kotlin.Int): kotlin.Unit
public object A {
private constructor 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 fun interface IFoo {
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public abstract fun foo(/*0*/ i: kotlin.Int): kotlin.Unit
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
public fun interface IFoo2 : IFoo {
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public abstract override /*1*/ /*fake_override*/ fun foo(/*0*/ i: kotlin.Int): kotlin.Unit
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
@@ -0,0 +1,21 @@
// !DIAGNOSTICS: -UNUSED_PARAMETER -UNUSED_EXPRESSION
fun <T> foo(f: () -> T): T = f()
fun bar(): Unit {}
object Scope {
fun bar(s: String = ""): Double = 0.0
fun <T> foo(f: () -> T): T = f()
object NestedScope {
fun bar(a: Int = 0): String = ""
fun test() {
// Despite the fact ::bar is resolved with compatibility warning, it's important not to propagate it to the outer call
val result = <!DEBUG_INFO_CALL("fqName: Scope.foo; typeCall: function")!>foo(::bar)<!>
<!DEBUG_INFO_EXPRESSION_TYPE("kotlin.String")!>result<!>
}
}
}
@@ -0,0 +1,21 @@
// !DIAGNOSTICS: -UNUSED_PARAMETER -UNUSED_EXPRESSION
fun <T> foo(f: () -> T): T = f()
fun bar(): Unit {}
object Scope {
fun bar(s: String = ""): Double = 0.0
fun <T> foo(f: () -> T): T = f()
object NestedScope {
fun bar(a: Int = 0): String = ""
fun test() {
// Despite the fact ::bar is resolved with compatibility warning, it's important not to propagate it to the outer call
val result = <!DEBUG_INFO_CALL("fqName: Scope.foo; typeCall: function")!>foo(<!COMPATIBILITY_WARNING!>::bar<!>)<!>
<!DEBUG_INFO_EXPRESSION_TYPE("kotlin.Unit")!>result<!>
}
}
}
@@ -0,0 +1,22 @@
package
public fun bar(): kotlin.Unit
public fun </*0*/ T> foo(/*0*/ f: () -> T): T
public object Scope {
private constructor Scope()
public final fun bar(/*0*/ s: kotlin.String = ...): kotlin.Double
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public final fun </*0*/ T> foo(/*0*/ f: () -> T): T
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
public object NestedScope {
private constructor NestedScope()
public final fun bar(/*0*/ a: kotlin.Int = ...): kotlin.String
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 final fun test(): kotlin.Unit
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
}
@@ -0,0 +1,25 @@
// FILE: Callable.java
public interface Callable<V> {
V call() throws Exception;
}
// FILE: Future.java
public class Future<T> {}
// FILE: Executor.java
public interface Executor {
<T> Future<T> submit(Callable<T> task);
Future<?> submit(Runnable task);
}
// FILE: test.kt
fun f(): String = "test"
class A {
fun schedule1(e: Executor): Future<String> = e.submit(::f)
fun schedule2(e: Executor): Future<String> = e.submit { f() }
}
@@ -0,0 +1,25 @@
// FILE: Callable.java
public interface Callable<V> {
V call() throws Exception;
}
// FILE: Future.java
public class Future<T> {}
// FILE: Executor.java
public interface Executor {
<T> Future<T> submit(Callable<T> task);
Future<?> submit(Runnable task);
}
// FILE: test.kt
fun f(): String = "test"
class A {
fun schedule1(e: Executor): Future<String> = e.submit(::f)
fun schedule2(e: Executor): Future<String> = <!TYPE_MISMATCH!>e.submit { f() }<!>
}
@@ -0,0 +1,34 @@
package
public fun f(): kotlin.String
public final class A {
public constructor 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 final fun schedule1(/*0*/ e: Executor): Future<kotlin.String>
public final fun schedule2(/*0*/ e: Executor): Future<kotlin.String>
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
public interface Callable</*0*/ V : kotlin.Any!> {
public abstract fun call(): V!
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 interface Executor {
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 abstract fun </*0*/ T : kotlin.Any!> submit(/*0*/ task: Callable<T!>!): Future<T!>!
public abstract fun submit(/*0*/ task: java.lang.Runnable!): Future<*>!
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
public open class Future</*0*/ T : kotlin.Any!> {
public constructor Future</*0*/ T : kotlin.Any!>()
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
}
@@ -14,7 +14,7 @@ fun test1(s: SubInt, sWrong: SubIntWrong) {
foo(<!TYPE_MISMATCH!>a<!>)
a <!CAST_NEVER_SUCCEEDS!>as<!> (Int, String) -> String
foo(a)
foo(<!DEBUG_INFO_SMARTCAST!>a<!>)
}
fun <T> test2(x: T) where T : (Int, String) -> Int, T : (Double) -> Int {
@@ -1801,6 +1801,11 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTestWithFirVali
runTest("compiler/testData/diagnostics/tests/callableReference/classVsPackage.kt");
}
@TestMetadata("compatibilityResolveWithVarargAndOperatorCall.kt")
public void testCompatibilityResolveWithVarargAndOperatorCall() throws Exception {
runTest("compiler/testData/diagnostics/tests/callableReference/compatibilityResolveWithVarargAndOperatorCall.kt");
}
@TestMetadata("constraintFromLHSWithCorrectDirection.kt")
public void testConstraintFromLHSWithCorrectDirection() throws Exception {
runTest("compiler/testData/diagnostics/tests/callableReference/constraintFromLHSWithCorrectDirection.kt");
@@ -1811,6 +1816,11 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTestWithFirVali
runTest("compiler/testData/diagnostics/tests/callableReference/constraintFromLHSWithCorrectDirectionError.kt");
}
@TestMetadata("correctCandidateWithCompatibilityForSeveralCandidates.kt")
public void testCorrectCandidateWithCompatibilityForSeveralCandidates() throws Exception {
runTest("compiler/testData/diagnostics/tests/callableReference/correctCandidateWithCompatibilityForSeveralCandidates.kt");
}
@TestMetadata("correctInfoAfterArrayLikeCall.kt")
public void testCorrectInfoAfterArrayLikeCall() throws Exception {
runTest("compiler/testData/diagnostics/tests/callableReference/correctInfoAfterArrayLikeCall.kt");
@@ -1981,6 +1991,11 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTestWithFirVali
runTest("compiler/testData/diagnostics/tests/callableReference/typealiases.kt");
}
@TestMetadata("unitAdaptationForReferenceCompatibility.kt")
public void testUnitAdaptationForReferenceCompatibility() throws Exception {
runTest("compiler/testData/diagnostics/tests/callableReference/unitAdaptationForReferenceCompatibility.kt");
}
@TestMetadata("unused.kt")
public void testUnused() throws Exception {
runTest("compiler/testData/diagnostics/tests/callableReference/unused.kt");
@@ -1796,6 +1796,11 @@ public class DiagnosticsUsingJavacTestGenerated extends AbstractDiagnosticsUsing
runTest("compiler/testData/diagnostics/tests/callableReference/classVsPackage.kt");
}
@TestMetadata("compatibilityResolveWithVarargAndOperatorCall.kt")
public void testCompatibilityResolveWithVarargAndOperatorCall() throws Exception {
runTest("compiler/testData/diagnostics/tests/callableReference/compatibilityResolveWithVarargAndOperatorCall.kt");
}
@TestMetadata("constraintFromLHSWithCorrectDirection.kt")
public void testConstraintFromLHSWithCorrectDirection() throws Exception {
runTest("compiler/testData/diagnostics/tests/callableReference/constraintFromLHSWithCorrectDirection.kt");
@@ -1806,6 +1811,11 @@ public class DiagnosticsUsingJavacTestGenerated extends AbstractDiagnosticsUsing
runTest("compiler/testData/diagnostics/tests/callableReference/constraintFromLHSWithCorrectDirectionError.kt");
}
@TestMetadata("correctCandidateWithCompatibilityForSeveralCandidates.kt")
public void testCorrectCandidateWithCompatibilityForSeveralCandidates() throws Exception {
runTest("compiler/testData/diagnostics/tests/callableReference/correctCandidateWithCompatibilityForSeveralCandidates.kt");
}
@TestMetadata("correctInfoAfterArrayLikeCall.kt")
public void testCorrectInfoAfterArrayLikeCall() throws Exception {
runTest("compiler/testData/diagnostics/tests/callableReference/correctInfoAfterArrayLikeCall.kt");
@@ -1976,6 +1986,11 @@ public class DiagnosticsUsingJavacTestGenerated extends AbstractDiagnosticsUsing
runTest("compiler/testData/diagnostics/tests/callableReference/typealiases.kt");
}
@TestMetadata("unitAdaptationForReferenceCompatibility.kt")
public void testUnitAdaptationForReferenceCompatibility() throws Exception {
runTest("compiler/testData/diagnostics/tests/callableReference/unitAdaptationForReferenceCompatibility.kt");
}
@TestMetadata("unused.kt")
public void testUnused() throws Exception {
runTest("compiler/testData/diagnostics/tests/callableReference/unused.kt");