Fix false negative UPPER_BOUND_VIOLATED with typealiases in supertypes

^KT-50797 Fixed
^KT-50798 Open
This commit is contained in:
Denis.Zharkov
2022-01-14 19:29:39 +03:00
parent 8d0b511e95
commit b2543b7a26
16 changed files with 244 additions and 30 deletions
@@ -5,7 +5,10 @@
package org.jetbrains.kotlin.resolve.jvm.checkers
import org.jetbrains.kotlin.descriptors.ClassifierDescriptor
import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor
import org.jetbrains.kotlin.diagnostics.DiagnosticFactory3
import org.jetbrains.kotlin.diagnostics.Errors
import org.jetbrains.kotlin.psi.KtElement
import org.jetbrains.kotlin.psi.KtExpression
import org.jetbrains.kotlin.psi.KtTypeReference
@@ -14,7 +17,9 @@ import org.jetbrains.kotlin.resolve.UpperBoundChecker
import org.jetbrains.kotlin.resolve.UpperBoundViolatedReporter
import org.jetbrains.kotlin.resolve.jvm.diagnostics.ErrorsJvm.UPPER_BOUND_VIOLATED_BASED_ON_JAVA_ANNOTATIONS
import org.jetbrains.kotlin.resolve.jvm.diagnostics.ErrorsJvm.UPPER_BOUND_VIOLATED_IN_TYPEALIAS_EXPANSION_BASED_ON_JAVA_ANNOTATIONS
import org.jetbrains.kotlin.types.*
import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.types.TypeSubstitutor
import org.jetbrains.kotlin.types.getEnhancementDeeply
// TODO: remove this checker after removing support LV < 1.6
class WarningAwareUpperBoundChecker : UpperBoundChecker() {
@@ -35,11 +40,13 @@ class WarningAwareUpperBoundChecker : UpperBoundChecker() {
typeParameterDescriptor: TypeParameterDescriptor,
substitutor: TypeSubstitutor,
trace: BindingTrace,
typeAliasUsageElement: KtElement?
typeAliasUsageElement: KtElement?,
diagnosticForTypeAliases: DiagnosticFactory3<KtElement, KotlinType, KotlinType, ClassifierDescriptor>,
) {
checkBounds(
argumentReference, argumentType, typeParameterDescriptor, substitutor, trace, typeAliasUsageElement,
withOnlyCheckForWarning = false
withOnlyCheckForWarning = false,
diagnosticForTypeAliases = diagnosticForTypeAliases,
)
}
@@ -50,11 +57,14 @@ class WarningAwareUpperBoundChecker : UpperBoundChecker() {
substitutor: TypeSubstitutor,
trace: BindingTrace,
typeAliasUsageElement: KtElement? = null,
withOnlyCheckForWarning: Boolean = false
withOnlyCheckForWarning: Boolean = false,
diagnosticForTypeAliases: DiagnosticFactory3<KtElement, KotlinType, KotlinType, ClassifierDescriptor> =
Errors.UPPER_BOUND_VIOLATED_IN_TYPEALIAS_EXPANSION,
) {
if (typeParameterDescriptor.upperBounds.isEmpty()) return
val diagnosticsReporter = UpperBoundViolatedReporter(trace, argumentType, typeParameterDescriptor)
val diagnosticsReporter =
UpperBoundViolatedReporter(trace, argumentType, typeParameterDescriptor, diagnosticForTypeAliases = diagnosticForTypeAliases)
val diagnosticsReporterForWarnings = UpperBoundViolatedReporter(
trace, argumentType, typeParameterDescriptor,
baseDiagnostic = UPPER_BOUND_VIOLATED_BASED_ON_JAVA_ANNOTATIONS,