Add compatibility warning for SAM conversions

This commit is contained in:
Mikhail Zarechenskiy
2020-05-31 19:04:38 +03:00
parent 01de789c76
commit 03358c61d4
11 changed files with 85 additions and 7 deletions
@@ -761,6 +761,7 @@ public interface Errors {
DiagnosticFactory1<PsiElement, KotlinType> REIFIED_TYPE_UNSAFE_SUBSTITUTION = DiagnosticFactory1.create(WARNING);
DiagnosticFactory0<KtElement> CANDIDATE_CHOSEN_USING_OVERLOAD_RESOLUTION_BY_LAMBDA_ANNOTATION = DiagnosticFactory0.create(WARNING);
DiagnosticFactory0<KtElement> COMPATIBILITY_WARNING = DiagnosticFactory0.create(WARNING);
DiagnosticFactory3<KtReferenceExpression, ClassifierDescriptor, WrongResolutionToClassifier, String> RESOLUTION_TO_CLASSIFIER =
DiagnosticFactory3.create(ERROR);
@@ -898,6 +898,8 @@ public class DefaultErrorMessages {
MAP.put(TYPE_PARAMETERS_NOT_ALLOWED, "Type parameters are not allowed here");
MAP.put(CANDIDATE_CHOSEN_USING_OVERLOAD_RESOLUTION_BY_LAMBDA_ANNOTATION, "Candidate was choosen only by @OverloadResolutionByLambdaReturnType annotation");
MAP.put(COMPATIBILITY_WARNING, "Candidate was chosen to preserve compatibility");
MAP.put(TYPE_PARAMETER_OF_PROPERTY_NOT_USED_IN_RECEIVER, "Type parameter of a property must be used in its receiver type");
MAP.put(SUPERTYPES_FOR_ANNOTATION_CLASS, "Annotation class cannot have supertypes");
@@ -80,6 +80,9 @@ class DiagnosticReporterByTrackingStrategy(
CandidateChosenUsingOverloadResolutionByLambdaAnnotation::class.java -> {
trace.report(CANDIDATE_CHOSEN_USING_OVERLOAD_RESOLUTION_BY_LAMBDA_ANNOTATION.on(psiKotlinCall.psiCall.callElement))
}
CompatibilityWarning::class.java -> {
trace.report(COMPATIBILITY_WARNING.on(psiKotlinCall.psiCall.callElement))
}
}
}
@@ -422,6 +422,10 @@ class NewResolutionOldInference(
getResultApplicability(diagnostics)
}
override fun addCompatibilityWarning(other: Candidate) {
// Only applicable for new inference
}
override val isSuccessful = getResultApplicability(eagerDiagnostics).isSuccess
}
@@ -63,6 +63,11 @@ class CallableReferenceCandidate(
val diagnostics: List<KotlinCallDiagnostic>
) : Candidate {
override val resultingApplicability = getResultApplicability(diagnostics)
override fun addCompatibilityWarning(other: Candidate) {
// TODO: now only arguments can be converted, so CR candidates shouldn't be affected
}
override val isSuccessful get() = resultingApplicability.isSuccess
var freshSubstitutor: FreshVariableNewTypeSubstitutor? = null
@@ -249,3 +249,9 @@ class CandidateChosenUsingOverloadResolutionByLambdaAnnotation : KotlinCallDiagn
reporter.onCall(this)
}
}
class CompatibilityWarning : KotlinCallDiagnostic(RESOLVED) {
override fun report(reporter: DiagnosticReporter) {
reporter.onCall(this)
}
}
@@ -165,6 +165,12 @@ class KotlinResolutionCandidate(
return maxOf(currentApplicability, systemApplicability, variableApplicability)
}
override fun addCompatibilityWarning(other: Candidate) {
if (this !== other) {
addDiagnostic(CompatibilityWarning())
}
}
override fun toString(): String {
val descriptor = DescriptorRenderer.COMPACT.render(resolvedCall.candidateDescriptor)
val okOrFail = if (currentApplicability.isSuccess) "OK" else "FAIL"
@@ -35,6 +35,8 @@ interface Candidate {
val isSuccessful: Boolean
val resultingApplicability: ResolutionCandidateApplicability
fun addCompatibilityWarning(other: Candidate)
}
interface CandidateFactory<out C : Candidate> {
@@ -310,9 +312,25 @@ class TowerResolver {
override fun getSuccessfulCandidates(): Collection<C>? {
if (!isSuccessful) return null
val firstGroupWithResolved = candidateGroups.firstOrNull {
it.any(::isSuccessfulCandidate)
} ?: return null
var compatibilityCandidate: C? = null
var firstGroupWithResolved: Collection<C>? = null
outer@ for (group in candidateGroups) {
for (candidate in group) {
if (isSuccessfulCandidate(candidate)) {
firstGroupWithResolved = group
break@outer
}
if (compatibilityCandidate == null && isSuccessfulPreserveCompatibility(candidate)) {
compatibilityCandidate = candidate
}
}
}
if (firstGroupWithResolved == null) return null
if (compatibilityCandidate != null) {
firstGroupWithResolved.forEach { it.addCompatibilityWarning(compatibilityCandidate) }
}
return firstGroupWithResolved.filter(::isSuccessfulCandidate)
}
@@ -322,6 +340,9 @@ class TowerResolver {
|| candidate.resultingApplicability == ResolutionCandidateApplicability.RESOLVED_WITH_ERROR
}
private fun isSuccessfulPreserveCompatibility(candidate: C): Boolean =
candidate.resultingApplicability == ResolutionCandidateApplicability.RESOLVED_NEED_PRESERVE_COMPATIBILITY
override fun pushCandidates(candidates: Collection<C>) {
val thereIsSuccessful = candidates.any { it.isSuccessful }
if (!isSuccessful && !thereIsSuccessful) {
@@ -1,5 +1,15 @@
// !DIAGNOSTICS: -UNUSED_PARAMETER -UNUSED_EXPRESSION
object Test0 {
fun foo(f: Runnable): Int = 0
fun foo(x: () -> String): String = ""
fun test(f: () -> Unit) {
val result = foo(f)
<!DEBUG_INFO_EXPRESSION_TYPE("kotlin.Int")!>result<!>
}
}
object Test1 {
fun foo(x: Any) {}
fun foo(f: () -> Unit) {}
@@ -1,5 +1,15 @@
// !DIAGNOSTICS: -UNUSED_PARAMETER -UNUSED_EXPRESSION
object Test0 {
fun foo(f: Runnable): Int = 0
fun foo(x: () -> String): String = ""
fun test(f: () -> Unit) {
val result = foo(f)
<!DEBUG_INFO_EXPRESSION_TYPE("kotlin.Int")!>result<!>
}
}
object Test1 {
fun foo(x: Any) {}
fun foo(f: () -> Unit) {}
@@ -8,7 +18,7 @@ object Test1 {
fun foo(r: Runnable) {}
fun test(f: () -> Unit) {
<!DEBUG_INFO_CALL("fqName: Test1.foo; typeCall: function")!>foo(f)<!>
<!COMPATIBILITY_WARNING, DEBUG_INFO_CALL("fqName: Test1.foo; typeCall: function")!>foo(f)<!>
}
}
}
@@ -30,7 +40,7 @@ object Test3 {
fun foo(n: Number, f: () -> Unit): String = ""
fun test(f: () -> Unit) {
val result = foo(1, f)
val result = <!COMPATIBILITY_WARNING!>foo(1, f)<!>
<!DEBUG_INFO_EXPRESSION_TYPE("kotlin.String")!>result<!>
}
}
@@ -42,7 +52,7 @@ object Test4 {
fun bar() {}
fun test() {
val result = foo(1, ::bar)
val result = <!COMPATIBILITY_WARNING!>foo(1, ::bar)<!>
<!DEBUG_INFO_EXPRESSION_TYPE("kotlin.String")!>result<!>
}
}
@@ -55,7 +65,7 @@ object Test5 {
fun foo(r: Runnable) {}
fun test() {
<!DEBUG_INFO_CALL("fqName: Test5.foo; typeCall: function")!>foo { }<!>
<!COMPATIBILITY_WARNING, DEBUG_INFO_CALL("fqName: Test5.foo; typeCall: function")!>foo { }<!>
}
}
}
@@ -1,5 +1,15 @@
package
public object Test0 {
private constructor Test0()
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public final fun foo(/*0*/ x: () -> kotlin.String): kotlin.String
public final fun foo(/*0*/ f: java.lang.Runnable): kotlin.Int
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public final fun test(/*0*/ f: () -> kotlin.Unit): kotlin.Unit
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
public object Test1 {
private constructor Test1()
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean