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
@@ -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) {