NI: don't do substitution for unsupported callable descriptors to use as callable references

^KT-41729 Fixed
This commit is contained in:
Victor Petukhov
2020-09-09 11:55:18 +03:00
parent 668098f3a8
commit 685d16ec68
27 changed files with 247 additions and 55 deletions
@@ -1784,11 +1784,6 @@ public class FirOldFrontendDiagnosticsTestGenerated extends AbstractFirOldFronte
runTest("compiler/testData/diagnostics/tests/callableReference/callableReferenceAsLastExpressionInBlock.kt");
}
@TestMetadata("callableReferenceToLocalVariable.kt")
public void testCallableReferenceToLocalVariable() throws Exception {
runTest("compiler/testData/diagnostics/tests/callableReference/callableReferenceToLocalVariable.kt");
}
@TestMetadata("classVsPackage.kt")
public void testClassVsPackage() throws Exception {
runTest("compiler/testData/diagnostics/tests/callableReference/classVsPackage.kt");
@@ -2555,11 +2550,6 @@ public class FirOldFrontendDiagnosticsTestGenerated extends AbstractFirOldFronte
runTest("compiler/testData/diagnostics/tests/callableReference/property/kt7945_unrelatedClass.kt");
}
@TestMetadata("localVariable.kt")
public void testLocalVariable() throws Exception {
runTest("compiler/testData/diagnostics/tests/callableReference/property/localVariable.kt");
}
@TestMetadata("memberFromTopLevel.kt")
public void testMemberFromTopLevel() throws Exception {
runTest("compiler/testData/diagnostics/tests/callableReference/property/memberFromTopLevel.kt");
@@ -2580,11 +2570,6 @@ public class FirOldFrontendDiagnosticsTestGenerated extends AbstractFirOldFronte
runTest("compiler/testData/diagnostics/tests/callableReference/property/samePriorityForFunctionsAndProperties.kt");
}
@TestMetadata("syntheticProperties.kt")
public void testSyntheticProperties() throws Exception {
runTest("compiler/testData/diagnostics/tests/callableReference/property/syntheticProperties.kt");
}
@TestMetadata("topLevelFromTopLevel.kt")
public void testTopLevelFromTopLevel() throws Exception {
runTest("compiler/testData/diagnostics/tests/callableReference/property/topLevelFromTopLevel.kt");
@@ -2853,6 +2838,44 @@ public class FirOldFrontendDiagnosticsTestGenerated extends AbstractFirOldFronte
runTest("compiler/testData/diagnostics/tests/callableReference/resolve/withVararg.kt");
}
}
@TestMetadata("compiler/testData/diagnostics/tests/callableReference/unsupported")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class Unsupported extends AbstractFirOldFrontendDiagnosticsTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doTest, this, testDataFilePath);
}
public void testAllFilesPresentInUnsupported() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/diagnostics/tests/callableReference/unsupported"), Pattern.compile("^(.+)\\.kt$"), Pattern.compile("^(.+)\\.fir\\.kts?$"), true);
}
@TestMetadata("callableReferenceToLocalVariable.kt")
public void testCallableReferenceToLocalVariable() throws Exception {
runTest("compiler/testData/diagnostics/tests/callableReference/unsupported/callableReferenceToLocalVariable.kt");
}
@TestMetadata("localVariable.kt")
public void testLocalVariable() throws Exception {
runTest("compiler/testData/diagnostics/tests/callableReference/unsupported/localVariable.kt");
}
@TestMetadata("localVariableWithSubstitution.kt")
public void testLocalVariableWithSubstitution() throws Exception {
runTest("compiler/testData/diagnostics/tests/callableReference/unsupported/localVariableWithSubstitution.kt");
}
@TestMetadata("parameterWithSubstitution.kt")
public void testParameterWithSubstitution() throws Exception {
runTest("compiler/testData/diagnostics/tests/callableReference/unsupported/parameterWithSubstitution.kt");
}
@TestMetadata("syntheticProperties.kt")
public void testSyntheticProperties() throws Exception {
runTest("compiler/testData/diagnostics/tests/callableReference/unsupported/syntheticProperties.kt");
}
}
}
@TestMetadata("compiler/testData/diagnostics/tests/cast")
@@ -2908,6 +2908,11 @@ public class FirOldFrontendDiagnosticsTestWithStdlibGenerated extends AbstractFi
runTest("compiler/testData/diagnostics/testsWithStdLib/inference/arrayConstructor.kt");
}
@TestMetadata("callableReferenceOnParameter.kt")
public void testCallableReferenceOnParameter() throws Exception {
runTest("compiler/testData/diagnostics/testsWithStdLib/inference/callableReferenceOnParameter.kt");
}
@TestMetadata("integerLiterals.kt")
public void testIntegerLiterals() throws Exception {
runTest("compiler/testData/diagnostics/testsWithStdLib/inference/integerLiterals.kt");
@@ -345,12 +345,11 @@ class ResolvedAtomCompleter(
(callableCandidate.candidate.typeParameters.map { it.typeConstructor } zip resultTypeParameters).toMap()
)
val firstSubstitution = typeParametersSubstitutor.toOldSubstitution()
val secondSubstitution = resultSubstitutor.toOldSubstitution()
val resultSubstitutor = TypeSubstitutor.createChainedSubstitutor(
firstSubstitution,
secondSubstitution
)
val resultSubstitutor = if (callableCandidate.candidate.isSupportedForCallableReference()) {
val firstSubstitution = typeParametersSubstitutor.toOldSubstitution()
val secondSubstitution = resultSubstitutor.toOldSubstitution()
TypeSubstitutor.createChainedSubstitutor(firstSubstitution, secondSubstitution)
} else TypeSubstitutor.EMPTY
val psiCallArgument = resolvedAtom.atom.psiCallArgument as CallableReferenceKotlinCallArgumentImpl
val callableReferenceExpression = psiCallArgument.ktCallableReferenceExpression
@@ -491,7 +491,7 @@ class CallableReferencesCandidateFactory(
val descriptorReturnType = descriptor.returnType
?: ErrorUtils.createErrorType("Error return type for descriptor: $descriptor")
when (descriptor) {
return when (descriptor) {
is PropertyDescriptor -> {
val mutable = descriptor.isVar && run {
val setter = descriptor.setter
@@ -501,7 +501,7 @@ class CallableReferencesCandidateFactory(
)
}
return callComponents.reflectionTypes.getKPropertyType(
callComponents.reflectionTypes.getKPropertyType(
Annotations.EMPTY,
argumentsAndReceivers,
descriptorReturnType,
@@ -532,12 +532,15 @@ class CallableReferencesCandidateFactory(
val suspendConversionStrategy = callableReferenceAdaptation?.suspendConversionStrategy
val isSuspend = descriptor.isSuspend || suspendConversionStrategy == SuspendConversionStrategy.SUSPEND_CONVERSION
return callComponents.reflectionTypes.getKFunctionType(
callComponents.reflectionTypes.getKFunctionType(
Annotations.EMPTY, null, argumentsAndReceivers, null,
returnType, descriptor.builtIns, isSuspend
) to callableReferenceAdaptation
}
else -> return ErrorUtils.createErrorType("Unsupported descriptor type: $descriptor") to null
else -> {
assert(!descriptor.isSupportedForCallableReference()) { "${descriptor::class} isn't supported to use in callable references actually, but it's listed in `isSupportedForCallableReference` method" }
ErrorUtils.createErrorType("Unsupported descriptor type: $descriptor") to null
}
}
}
@@ -0,0 +1,14 @@
// Issue: KT-41729
import kotlin.reflect.KProperty
class Foo {
operator fun <T> getValue(thisRef: Any?, property: KProperty<*>) = 1
}
fun main() {
val f = Foo()
val a: Int
<!VARIABLE_EXPECTED!>get()<!> = f.getValue(null, ::a) // no exception after fix
<!UNRESOLVED_REFERENCE!>print<!>(<!UNINITIALIZED_VARIABLE!>a<!>)
}
@@ -0,0 +1,14 @@
// Issue: KT-41729
import kotlin.reflect.KProperty
class Foo {
operator fun <T> getValue(thisRef: Any?, property: KProperty<*>) = 1
}
fun main() {
val f = Foo()
val a: Int
<!UNRESOLVED_REFERENCE!>get<!>() = f.<!NEW_INFERENCE_NO_INFORMATION_FOR_PARAMETER!>getValue<!>(null, ::<!UNSUPPORTED!>a<!>) // no exception after fix
<!UNRESOLVED_REFERENCE!>print<!>(<!UNINITIALIZED_VARIABLE!>a<!>)
}
@@ -0,0 +1,11 @@
package
public fun main(): kotlin.Unit
public final class Foo {
public constructor Foo()
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public final operator fun </*0*/ T> getValue(/*0*/ thisRef: kotlin.Any?, /*1*/ property: kotlin.reflect.KProperty<*>): kotlin.Int
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
@@ -0,0 +1,14 @@
// Issue: KT-41729
import kotlin.reflect.KProperty
class Foo {
operator fun <T> getValue(thisRef: Any?, property: KProperty<*>) = 1
}
fun main(x: Int) {
val f = Foo()
val a: Int
<!VARIABLE_EXPECTED!>get()<!> = f.getValue(null, ::x) // no exception after fix
<!UNRESOLVED_REFERENCE!>print<!>(<!UNINITIALIZED_VARIABLE!>a<!>)
}
@@ -0,0 +1,14 @@
// Issue: KT-41729
import kotlin.reflect.KProperty
class Foo {
operator fun <T> getValue(thisRef: Any?, property: KProperty<*>) = 1
}
fun main(<!UNUSED_PARAMETER!>x<!>: Int) {
val f = Foo()
val a: Int
<!UNRESOLVED_REFERENCE!>get<!>() = f.<!NEW_INFERENCE_NO_INFORMATION_FOR_PARAMETER!>getValue<!>(null, ::<!UNSUPPORTED!>x<!>) // no exception after fix
<!UNRESOLVED_REFERENCE!>print<!>(<!UNINITIALIZED_VARIABLE!>a<!>)
}
@@ -0,0 +1,11 @@
package
public fun main(/*0*/ x: kotlin.Int): kotlin.Unit
public final class Foo {
public constructor Foo()
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public final operator fun </*0*/ T> getValue(/*0*/ thisRef: kotlin.Any?, /*1*/ property: kotlin.reflect.KProperty<*>): kotlin.Int
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
@@ -0,0 +1,8 @@
// Issue: KT-37736
internal class Z<K> {
val map = HashMap<String, String>()
inline fun compute(key: String, producer: () -> String): String {
return map.getOrPut(key, ::producer)
}
}
@@ -0,0 +1,8 @@
// Issue: KT-37736
internal class Z<K> {
val map = HashMap<String, String>()
inline fun compute(key: String, <!UNUSED_PARAMETER!>producer<!>: () -> String): String {
return map.getOrPut(key, ::<!UNSUPPORTED, USAGE_IS_NOT_INLINABLE!>producer<!>)
}
}
@@ -0,0 +1,10 @@
package
internal final class Z</*0*/ K> {
public constructor Z</*0*/ K>()
public final val map: kotlin.collections.HashMap<kotlin.String, kotlin.String> /* = java.util.HashMap<kotlin.String, kotlin.String> */
public final inline fun compute(/*0*/ key: kotlin.String, /*1*/ producer: () -> kotlin.String): 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 open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
@@ -1791,11 +1791,6 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTestWithFirVali
runTest("compiler/testData/diagnostics/tests/callableReference/callableReferenceAsLastExpressionInBlock.kt");
}
@TestMetadata("callableReferenceToLocalVariable.kt")
public void testCallableReferenceToLocalVariable() throws Exception {
runTest("compiler/testData/diagnostics/tests/callableReference/callableReferenceToLocalVariable.kt");
}
@TestMetadata("classVsPackage.kt")
public void testClassVsPackage() throws Exception {
runTest("compiler/testData/diagnostics/tests/callableReference/classVsPackage.kt");
@@ -2562,11 +2557,6 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTestWithFirVali
runTest("compiler/testData/diagnostics/tests/callableReference/property/kt7945_unrelatedClass.kt");
}
@TestMetadata("localVariable.kt")
public void testLocalVariable() throws Exception {
runTest("compiler/testData/diagnostics/tests/callableReference/property/localVariable.kt");
}
@TestMetadata("memberFromTopLevel.kt")
public void testMemberFromTopLevel() throws Exception {
runTest("compiler/testData/diagnostics/tests/callableReference/property/memberFromTopLevel.kt");
@@ -2587,11 +2577,6 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTestWithFirVali
runTest("compiler/testData/diagnostics/tests/callableReference/property/samePriorityForFunctionsAndProperties.kt");
}
@TestMetadata("syntheticProperties.kt")
public void testSyntheticProperties() throws Exception {
runTest("compiler/testData/diagnostics/tests/callableReference/property/syntheticProperties.kt");
}
@TestMetadata("topLevelFromTopLevel.kt")
public void testTopLevelFromTopLevel() throws Exception {
runTest("compiler/testData/diagnostics/tests/callableReference/property/topLevelFromTopLevel.kt");
@@ -2860,6 +2845,44 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTestWithFirVali
runTest("compiler/testData/diagnostics/tests/callableReference/resolve/withVararg.kt");
}
}
@TestMetadata("compiler/testData/diagnostics/tests/callableReference/unsupported")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class Unsupported extends AbstractDiagnosticsTestWithFirValidation {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doTest, this, testDataFilePath);
}
public void testAllFilesPresentInUnsupported() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/diagnostics/tests/callableReference/unsupported"), Pattern.compile("^(.*)\\.kts?$"), Pattern.compile("^(.+)\\.fir\\.kts?$"), true);
}
@TestMetadata("callableReferenceToLocalVariable.kt")
public void testCallableReferenceToLocalVariable() throws Exception {
runTest("compiler/testData/diagnostics/tests/callableReference/unsupported/callableReferenceToLocalVariable.kt");
}
@TestMetadata("localVariable.kt")
public void testLocalVariable() throws Exception {
runTest("compiler/testData/diagnostics/tests/callableReference/unsupported/localVariable.kt");
}
@TestMetadata("localVariableWithSubstitution.kt")
public void testLocalVariableWithSubstitution() throws Exception {
runTest("compiler/testData/diagnostics/tests/callableReference/unsupported/localVariableWithSubstitution.kt");
}
@TestMetadata("parameterWithSubstitution.kt")
public void testParameterWithSubstitution() throws Exception {
runTest("compiler/testData/diagnostics/tests/callableReference/unsupported/parameterWithSubstitution.kt");
}
@TestMetadata("syntheticProperties.kt")
public void testSyntheticProperties() throws Exception {
runTest("compiler/testData/diagnostics/tests/callableReference/unsupported/syntheticProperties.kt");
}
}
}
@TestMetadata("compiler/testData/diagnostics/tests/cast")
@@ -3058,6 +3058,11 @@ public class DiagnosticsTestWithStdLibGenerated extends AbstractDiagnosticsTestW
runTest("compiler/testData/diagnostics/testsWithStdLib/inference/arrayConstructor.kt");
}
@TestMetadata("callableReferenceOnParameter.kt")
public void testCallableReferenceOnParameter() throws Exception {
runTest("compiler/testData/diagnostics/testsWithStdLib/inference/callableReferenceOnParameter.kt");
}
@TestMetadata("integerLiterals.kt")
public void testIntegerLiterals() throws Exception {
runTest("compiler/testData/diagnostics/testsWithStdLib/inference/integerLiterals.kt");
@@ -3058,6 +3058,11 @@ public class DiagnosticsTestWithStdLibUsingJavacGenerated extends AbstractDiagno
runTest("compiler/testData/diagnostics/testsWithStdLib/inference/arrayConstructor.kt");
}
@TestMetadata("callableReferenceOnParameter.kt")
public void testCallableReferenceOnParameter() throws Exception {
runTest("compiler/testData/diagnostics/testsWithStdLib/inference/callableReferenceOnParameter.kt");
}
@TestMetadata("integerLiterals.kt")
public void testIntegerLiterals() throws Exception {
runTest("compiler/testData/diagnostics/testsWithStdLib/inference/integerLiterals.kt");
@@ -1786,11 +1786,6 @@ public class DiagnosticsUsingJavacTestGenerated extends AbstractDiagnosticsUsing
runTest("compiler/testData/diagnostics/tests/callableReference/callableReferenceAsLastExpressionInBlock.kt");
}
@TestMetadata("callableReferenceToLocalVariable.kt")
public void testCallableReferenceToLocalVariable() throws Exception {
runTest("compiler/testData/diagnostics/tests/callableReference/callableReferenceToLocalVariable.kt");
}
@TestMetadata("classVsPackage.kt")
public void testClassVsPackage() throws Exception {
runTest("compiler/testData/diagnostics/tests/callableReference/classVsPackage.kt");
@@ -2557,11 +2552,6 @@ public class DiagnosticsUsingJavacTestGenerated extends AbstractDiagnosticsUsing
runTest("compiler/testData/diagnostics/tests/callableReference/property/kt7945_unrelatedClass.kt");
}
@TestMetadata("localVariable.kt")
public void testLocalVariable() throws Exception {
runTest("compiler/testData/diagnostics/tests/callableReference/property/localVariable.kt");
}
@TestMetadata("memberFromTopLevel.kt")
public void testMemberFromTopLevel() throws Exception {
runTest("compiler/testData/diagnostics/tests/callableReference/property/memberFromTopLevel.kt");
@@ -2582,11 +2572,6 @@ public class DiagnosticsUsingJavacTestGenerated extends AbstractDiagnosticsUsing
runTest("compiler/testData/diagnostics/tests/callableReference/property/samePriorityForFunctionsAndProperties.kt");
}
@TestMetadata("syntheticProperties.kt")
public void testSyntheticProperties() throws Exception {
runTest("compiler/testData/diagnostics/tests/callableReference/property/syntheticProperties.kt");
}
@TestMetadata("topLevelFromTopLevel.kt")
public void testTopLevelFromTopLevel() throws Exception {
runTest("compiler/testData/diagnostics/tests/callableReference/property/topLevelFromTopLevel.kt");
@@ -2855,6 +2840,44 @@ public class DiagnosticsUsingJavacTestGenerated extends AbstractDiagnosticsUsing
runTest("compiler/testData/diagnostics/tests/callableReference/resolve/withVararg.kt");
}
}
@TestMetadata("compiler/testData/diagnostics/tests/callableReference/unsupported")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class Unsupported extends AbstractDiagnosticsUsingJavacTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doTest, this, testDataFilePath);
}
public void testAllFilesPresentInUnsupported() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/diagnostics/tests/callableReference/unsupported"), Pattern.compile("^(.+)\\.kt$"), Pattern.compile("^(.+)\\.fir\\.kts?$"), true);
}
@TestMetadata("callableReferenceToLocalVariable.kt")
public void testCallableReferenceToLocalVariable() throws Exception {
runTest("compiler/testData/diagnostics/tests/callableReference/unsupported/callableReferenceToLocalVariable.kt");
}
@TestMetadata("localVariable.kt")
public void testLocalVariable() throws Exception {
runTest("compiler/testData/diagnostics/tests/callableReference/unsupported/localVariable.kt");
}
@TestMetadata("localVariableWithSubstitution.kt")
public void testLocalVariableWithSubstitution() throws Exception {
runTest("compiler/testData/diagnostics/tests/callableReference/unsupported/localVariableWithSubstitution.kt");
}
@TestMetadata("parameterWithSubstitution.kt")
public void testParameterWithSubstitution() throws Exception {
runTest("compiler/testData/diagnostics/tests/callableReference/unsupported/parameterWithSubstitution.kt");
}
@TestMetadata("syntheticProperties.kt")
public void testSyntheticProperties() throws Exception {
runTest("compiler/testData/diagnostics/tests/callableReference/unsupported/syntheticProperties.kt");
}
}
}
@TestMetadata("compiler/testData/diagnostics/tests/cast")
@@ -54,3 +54,5 @@ fun DeclarationDescriptor.isTopLevelInPackage(name: String, packageName: String)
val packageFqName = containingDeclaration.fqName.asString()
return packageName == packageFqName
}
fun CallableDescriptor.isSupportedForCallableReference() = this is PropertyDescriptor || this is FunctionDescriptor