Add compatibility warning for reference adaptation

This commit is contained in:
Mikhail Zarechenskiy
2020-05-31 23:50:19 +03:00
parent 684dfb959c
commit 47e6805186
10 changed files with 240 additions and 3 deletions
@@ -1934,6 +1934,11 @@ public class FirOldFrontendDiagnosticsTestGenerated extends AbstractFirOldFronte
runTest("compiler/testData/diagnostics/tests/callableReference/propertyOfNestedGenericClass.kt");
}
@TestMetadata("referenceAdaptationCompatibility.kt")
public void testReferenceAdaptationCompatibility() throws Exception {
runTest("compiler/testData/diagnostics/tests/callableReference/referenceAdaptationCompatibility.kt");
}
@TestMetadata("rewriteAtSliceOnGetOperator.kt")
public void testRewriteAtSliceOnGetOperator() throws Exception {
runTest("compiler/testData/diagnostics/tests/callableReference/rewriteAtSliceOnGetOperator.kt");
@@ -218,6 +218,10 @@ class DiagnosticReporterByTrackingStrategy(
trace.report(CANNOT_INFER_PARAMETER_TYPE.on(parameter))
}
}
CompatibilityWarningOnArgument::class.java -> {
trace.report(COMPATIBILITY_WARNING.on(callArgument.psiCallArgument.valueArgument.asElement()))
}
}
}
@@ -13,6 +13,7 @@ import org.jetbrains.kotlin.resolve.calls.components.CreateFreshVariablesSubstit
import org.jetbrains.kotlin.resolve.calls.inference.ConstraintSystemOperation
import org.jetbrains.kotlin.resolve.calls.inference.components.FreshVariableNewTypeSubstitutor
import org.jetbrains.kotlin.resolve.calls.inference.model.ArgumentConstraintPosition
import org.jetbrains.kotlin.resolve.calls.inference.model.LowerPriorityToPreserveCompatibility
import org.jetbrains.kotlin.resolve.calls.model.*
import org.jetbrains.kotlin.resolve.calls.tasks.ExplicitReceiverKind
import org.jetbrains.kotlin.resolve.calls.tasks.ExplicitReceiverKind.DISPATCH_RECEIVER
@@ -60,12 +61,17 @@ class CallableReferenceCandidate(
val explicitReceiverKind: ExplicitReceiverKind,
val reflectionCandidateType: UnwrappedType,
val callableReferenceAdaptation: CallableReferenceAdaptation?,
val diagnostics: List<KotlinCallDiagnostic>
initialDiagnostics: List<KotlinCallDiagnostic>
) : Candidate {
private val mutableDiagnostics = initialDiagnostics.toMutableList()
val diagnostics: List<KotlinCallDiagnostic> = mutableDiagnostics
override val resultingApplicability = getResultApplicability(diagnostics)
override fun addCompatibilityWarning(other: Candidate) {
// TODO: now only arguments can be converted, so CR candidates shouldn't be affected
if (this !== other) {
mutableDiagnostics.add(CompatibilityWarning())
}
}
override val isSuccessful get() = resultingApplicability.isSuccess
@@ -207,6 +213,10 @@ class CallableReferencesCandidateFactory(
callComponents.builtIns
)
if (needCompatibilityWarning(callableReferenceAdaptation)) {
diagnostics.add(LowerPriorityToPreserveCompatibility)
}
if (callableReferenceAdaptation != null &&
callableReferenceAdaptation.defaults != 0 &&
!callComponents.languageVersionSettings.supportsFeature(LanguageFeature.FunctionReferenceWithDefaultValueAsOtherType)
@@ -251,6 +261,15 @@ class CallableReferencesCandidateFactory(
)
}
private fun needCompatibilityWarning(callableReferenceAdaptation: CallableReferenceAdaptation?): Boolean {
if (callableReferenceAdaptation == null) return false
return callableReferenceAdaptation.defaults != 0 ||
callableReferenceAdaptation.suspendConversionStrategy != SuspendConversionStrategy.NO_CONVERSION ||
callableReferenceAdaptation.coercionStrategy != CoercionStrategy.NO_COERCION ||
callableReferenceAdaptation.mappedArguments.values.any { it is ResolvedCallArgument.VarargArgument }
}
private enum class VarargMappingState {
UNMAPPED, MAPPED_WITH_PLAIN_ARGS, MAPPED_WITH_ARRAY
}
@@ -106,7 +106,13 @@ class CallableReferenceResolver(
)
}
diagnosticsHolder.addDiagnosticIfNotNull(diagnostic)
chosenCandidate.diagnostics.forEach { diagnosticsHolder.addDiagnostic(it) }
chosenCandidate.diagnostics.forEach {
val transformedDiagnostic = when (it) {
is CompatibilityWarning -> CompatibilityWarningOnArgument(argument)
else -> it
}
diagnosticsHolder.addDiagnostic(transformedDiagnostic)
}
chosenCandidate.freshSubstitutor = toFreshSubstitutor
} else {
if (candidates.isEmpty()) {
@@ -254,4 +254,10 @@ class CompatibilityWarning : KotlinCallDiagnostic(RESOLVED) {
override fun report(reporter: DiagnosticReporter) {
reporter.onCall(this)
}
}
class CompatibilityWarningOnArgument(val argument: CallableReferenceKotlinCallArgument) : KotlinCallDiagnostic(RESOLVED) {
override fun report(reporter: DiagnosticReporter) {
reporter.onCallArgument(argument, this)
}
}
@@ -0,0 +1,57 @@
// !DIAGNOSTICS: -UNUSED_PARAMETER -UNUSED_EXPRESSION
object Test1 {
fun <T> foo(f: () -> T): T = f()
fun bar(): Int = 0
object Scope {
fun bar(x: Int = 0): String = ""
fun test() {
val result = foo(::bar)
<!DEBUG_INFO_EXPRESSION_TYPE("kotlin.String")!>result<!>
}
}
}
object Test2 {
fun foo(f: () -> Unit) {}
fun bar() {}
object Scope {
fun bar(): String = ""
fun test() {
foo(::bar)
}
}
}
object Test3 {
fun <T> foo(f: (Int, Int) -> T): T = TODO()
fun bar(x: Int, y: Int): Int = 0
object Scope {
fun bar(vararg ints: Int): String = ""
fun test() {
val result = foo(::bar)
<!DEBUG_INFO_EXPRESSION_TYPE("kotlin.Int")!>result<!>
}
}
}
object Test4 {
fun <T> foo(f: (Array<String>) -> T): T = TODO()
fun bar(g: Array<String>): Int = 0
object Scope {
// Works before 1.4
fun bar(vararg g: String): String = ""
fun test() {
val result = foo(::bar)
<!DEBUG_INFO_EXPRESSION_TYPE("kotlin.String")!>result<!>
}
}
}
@@ -0,0 +1,57 @@
// !DIAGNOSTICS: -UNUSED_PARAMETER -UNUSED_EXPRESSION
object Test1 {
fun <T> foo(f: () -> T): T = f()
fun bar(): Int = 0
object Scope {
fun bar(x: Int = 0): String = ""
fun test() {
val result = foo(<!COMPATIBILITY_WARNING!>::bar<!>)
<!DEBUG_INFO_EXPRESSION_TYPE("kotlin.Int")!>result<!>
}
}
}
object Test2 {
fun foo(f: () -> Unit) {}
fun bar() {}
object Scope {
fun bar(): String = ""
fun test() {
foo(<!COMPATIBILITY_WARNING!>::bar<!>)
}
}
}
object Test3 {
fun <T> foo(f: (Int, Int) -> T): T = TODO()
fun bar(x: Int, y: Int): Int = 0
object Scope {
fun bar(vararg ints: Int): String = ""
fun test() {
val result = foo(<!COMPATIBILITY_WARNING!>::bar<!>)
<!DEBUG_INFO_EXPRESSION_TYPE("kotlin.Int")!>result<!>
}
}
}
object Test4 {
fun <T> foo(f: (Array<String>) -> T): T = TODO()
fun bar(g: Array<String>): Int = 0
object Scope {
// Works before 1.4
fun bar(vararg g: String): String = ""
fun test() {
val result = foo(::bar)
<!DEBUG_INFO_EXPRESSION_TYPE("kotlin.String")!>result<!>
}
}
}
@@ -0,0 +1,73 @@
package
public object Test1 {
private constructor Test1()
public final fun bar(): kotlin.Int
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 Scope {
private constructor Scope()
public final fun bar(/*0*/ x: 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
}
}
public object Test2 {
private constructor Test2()
public final fun bar(): kotlin.Unit
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public final fun foo(/*0*/ f: () -> kotlin.Unit): kotlin.Unit
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
public object Scope {
private constructor Scope()
public final fun bar(): 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
}
}
public object Test3 {
private constructor Test3()
public final fun bar(/*0*/ x: kotlin.Int, /*1*/ y: kotlin.Int): kotlin.Int
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public final fun </*0*/ T> foo(/*0*/ f: (kotlin.Int, kotlin.Int) -> T): T
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
public object Scope {
private constructor Scope()
public final fun bar(/*0*/ vararg ints: kotlin.Int /*kotlin.IntArray*/): 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
}
}
public object Test4 {
private constructor Test4()
public final fun bar(/*0*/ g: kotlin.Array<kotlin.String>): kotlin.Int
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public final fun </*0*/ T> foo(/*0*/ f: (kotlin.Array<kotlin.String>) -> T): T
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
public object Scope {
private constructor Scope()
public final fun bar(/*0*/ vararg g: kotlin.String /*kotlin.Array<out 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 final fun test(): kotlin.Unit
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
}
@@ -1941,6 +1941,11 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTestWithFirVali
runTest("compiler/testData/diagnostics/tests/callableReference/propertyOfNestedGenericClass.kt");
}
@TestMetadata("referenceAdaptationCompatibility.kt")
public void testReferenceAdaptationCompatibility() throws Exception {
runTest("compiler/testData/diagnostics/tests/callableReference/referenceAdaptationCompatibility.kt");
}
@TestMetadata("rewriteAtSliceOnGetOperator.kt")
public void testRewriteAtSliceOnGetOperator() throws Exception {
runTest("compiler/testData/diagnostics/tests/callableReference/rewriteAtSliceOnGetOperator.kt");
@@ -1936,6 +1936,11 @@ public class DiagnosticsUsingJavacTestGenerated extends AbstractDiagnosticsUsing
runTest("compiler/testData/diagnostics/tests/callableReference/propertyOfNestedGenericClass.kt");
}
@TestMetadata("referenceAdaptationCompatibility.kt")
public void testReferenceAdaptationCompatibility() throws Exception {
runTest("compiler/testData/diagnostics/tests/callableReference/referenceAdaptationCompatibility.kt");
}
@TestMetadata("rewriteAtSliceOnGetOperator.kt")
public void testRewriteAtSliceOnGetOperator() throws Exception {
runTest("compiler/testData/diagnostics/tests/callableReference/rewriteAtSliceOnGetOperator.kt");