[FIR] Don't lose error level enhancements in warning-level-enhanced arguments

The error-level enhancement is kept as warning-level and a new
LanguageFeature is introduced to turn the warning into an error.

#KT-63208 Fixed
#KT-63209
This commit is contained in:
Kirill Rakhman
2024-01-05 15:14:29 +01:00
committed by Space Team
parent 9189154cae
commit 371b1eb3d5
22 changed files with 470 additions and 33 deletions
@@ -12,13 +12,14 @@ import kotlin.reflect.KClass
class EnhancedTypeForWarningAttribute(
override val coneType: ConeKotlinType,
val isDeprecation: Boolean,
) : ConeAttributeWithConeType<EnhancedTypeForWarningAttribute>() {
override fun union(other: EnhancedTypeForWarningAttribute?): EnhancedTypeForWarningAttribute? = null
override fun intersect(other: EnhancedTypeForWarningAttribute?): EnhancedTypeForWarningAttribute? = null
override fun add(other: EnhancedTypeForWarningAttribute?): EnhancedTypeForWarningAttribute = other ?: this
override fun isSubtypeOf(other: EnhancedTypeForWarningAttribute?): Boolean = true
override fun toString(): String = "Enhanced for warning(${coneType.renderForDebugging()})"
override fun copyWith(newType: ConeKotlinType): EnhancedTypeForWarningAttribute = EnhancedTypeForWarningAttribute(newType)
override fun copyWith(newType: ConeKotlinType): EnhancedTypeForWarningAttribute = EnhancedTypeForWarningAttribute(newType, isDeprecation)
override val key: KClass<out EnhancedTypeForWarningAttribute>
get() = EnhancedTypeForWarningAttribute::class
@@ -36,12 +37,14 @@ class EnhancedTypeForWarningAttribute(
other as EnhancedTypeForWarningAttribute
if (coneType != other.coneType) return false
if (isDeprecation != other.isDeprecation) return false
return true
}
override fun hashCode(): Int {
var result = coneType.hashCode()
result = 31 * result + isDeprecation.hashCode()
return result
}
}
@@ -51,6 +54,9 @@ val ConeAttributes.enhancedTypeForWarning: EnhancedTypeForWarningAttribute? by C
val ConeKotlinType.enhancedTypeForWarning: ConeKotlinType?
get() = attributes.enhancedTypeForWarning?.coneType
val ConeKotlinType.isEnhancedTypeForWarningDeprecation: Boolean
get() = attributes.enhancedTypeForWarning?.isDeprecation == true
/**
* Substitutor that substitutes types with their [ConeKotlinType.enhancedTypeForWarning] recursively.
*/
@@ -6,7 +6,9 @@
package org.jetbrains.kotlin.fir.java.enhancement
import org.jetbrains.kotlin.builtins.jvm.JavaToKotlinClassMap
import org.jetbrains.kotlin.config.LanguageFeature
import org.jetbrains.kotlin.fir.FirSession
import org.jetbrains.kotlin.fir.languageVersionSettings
import org.jetbrains.kotlin.fir.symbols.ConeClassLikeLookupTag
import org.jetbrains.kotlin.fir.symbols.ConeClassifierLookupTag
import org.jetbrains.kotlin.fir.types.*
@@ -15,7 +17,7 @@ import org.jetbrains.kotlin.name.ClassId
import org.jetbrains.kotlin.utils.addToStdlib.applyIf
internal fun ConeKotlinType.enhance(session: FirSession, qualifiers: IndexedJavaTypeQualifiers): ConeKotlinType? =
enhanceConeKotlinType(session, qualifiers, 0, mutableListOf<Int>().apply { computeSubtreeSizes(this) })
enhanceConeKotlinType(session, qualifiers, 0, mutableListOf<Int>().apply { computeSubtreeSizes(this) }, convertErrorsToWarnings = false)
// The index in the lambda is the position of the type component in a depth-first walk of the tree.
// Example: A<B<C, D>, E<F>> - 0<1<2, 3>, 4<5>>. For flexible types, some arguments in the lower bound
@@ -37,7 +39,8 @@ private fun ConeKotlinType.enhanceConeKotlinType(
session: FirSession,
qualifiers: IndexedJavaTypeQualifiers,
index: Int,
subtreeSizes: List<Int>
subtreeSizes: List<Int>,
convertErrorsToWarnings: Boolean,
): ConeKotlinType? {
return when (this) {
is ConeFlexibleType -> {
@@ -46,10 +49,12 @@ private fun ConeKotlinType.enhanceConeKotlinType(
// It's not totally correct, but tolerable since we would like to avoid excessive breaking changes and the warnings should be
// anyway reported.
val lowerResult = lowerBound.enhanceInflexibleType(
session, TypeComponentPosition.FLEXIBLE_LOWER, qualifiers, index, subtreeSizes
session, TypeComponentPosition.FLEXIBLE_LOWER, qualifiers, index, subtreeSizes,
isFromDefinitelyNotNullType = false, convertErrorToWarning = convertErrorsToWarnings,
)
val upperResult = upperBound.enhanceInflexibleType(
session, TypeComponentPosition.FLEXIBLE_UPPER, qualifiers, index, subtreeSizes
session, TypeComponentPosition.FLEXIBLE_UPPER, qualifiers, index, subtreeSizes,
isFromDefinitelyNotNullType = false, convertErrorToWarning = convertErrorsToWarnings,
)
when {
@@ -59,7 +64,8 @@ private fun ConeKotlinType.enhanceConeKotlinType(
}
}
is ConeSimpleKotlinType -> enhanceInflexibleType(
session, TypeComponentPosition.INFLEXIBLE, qualifiers, index, subtreeSizes
session, TypeComponentPosition.INFLEXIBLE, qualifiers, index, subtreeSizes,
isFromDefinitelyNotNullType = false, convertErrorToWarning = convertErrorsToWarnings,
)
else -> null
}
@@ -79,10 +85,11 @@ private fun ConeSimpleKotlinType.enhanceInflexibleType(
qualifiers: IndexedJavaTypeQualifiers,
index: Int,
subtreeSizes: List<Int>,
isFromDefinitelyNotNullType: Boolean = false,
isFromDefinitelyNotNullType: Boolean,
convertErrorToWarning: Boolean,
): ConeSimpleKotlinType? {
if (this is ConeDefinitelyNotNullType) {
return original.enhanceInflexibleType(session, position, qualifiers, index, subtreeSizes, isFromDefinitelyNotNullType = true)
return original.enhanceInflexibleType(session, position, qualifiers, index, subtreeSizes, isFromDefinitelyNotNullType = true, convertErrorToWarning)
}
val shouldEnhance = position.shouldEnhance()
@@ -103,19 +110,20 @@ private fun ConeSimpleKotlinType.enhanceInflexibleType(
isFromDefinitelyNotNullType,
effectiveQualifiers.definitelyNotNull,
nullabilityFromQualifiers,
enhancedTag
enhancedTag,
convertNestedErrorsToWarnings = convertErrorToWarning ||
effectiveQualifiers.isNullabilityQualifierForWarning &&
!session.languageVersionSettings.supportsFeature(LanguageFeature.SupportJavaErrorEnhancementOfArgumentsOfWarningLevelEnhanced),
)
return if (effectiveQualifiers.isNullabilityQualifierForWarning && enhanced != null) {
val newAttributes = attributes.plus(EnhancedTypeForWarningAttribute(enhanced))
return if (enhanced != null && (effectiveQualifiers.isNullabilityQualifierForWarning || convertErrorToWarning)) {
val newAttributes = attributes.plus(EnhancedTypeForWarningAttribute(enhanced, isDeprecation = convertErrorToWarning && effectiveQualifiers.enhancesSomethingForError()))
// TODO if the arguments of `enhanced` are error-level annotated, we're losing them here for compatibility with K1,
// a fix requires a deprecation cycle, see KT-63208
if (enhancedTag != lookupTag) {
// Handle case when mutability was enhanced and nullability was enhanced for warning.
enhancedTag.constructType(typeArguments, isNullable, newAttributes)
enhancedTag.constructType(enhanced.typeArguments, isNullable, newAttributes)
} else {
this.withAttributes(newAttributes)
this.withAttributes(newAttributes).withArguments(enhanced.typeArguments)
}.applyIf(isFromDefinitelyNotNullType) {
// If the original type was DNN, we need to wrap the result in a DNN type because `this` is the non-DNN part of the original.
// In the non-warning case, this happens in the nested call and so `enhanced` is already DNN.
@@ -126,6 +134,10 @@ private fun ConeSimpleKotlinType.enhanceInflexibleType(
}
}
private fun JavaTypeQualifiers.enhancesSomethingForError(): Boolean {
return !isNullabilityQualifierForWarning && (nullability != null || mutability != null || definitelyNotNull)
}
private fun ConeLookupTagBasedType.enhanceInflexibleType(
session: FirSession,
qualifiers: IndexedJavaTypeQualifiers,
@@ -135,6 +147,7 @@ private fun ConeLookupTagBasedType.enhanceInflexibleType(
isDefinitelyNotNull: Boolean,
nullabilityFromQualifiers: NullabilityQualifier?,
enhancedTag: ConeClassifierLookupTag,
convertNestedErrorsToWarnings: Boolean,
): ConeSimpleKotlinType? {
val enhancedIsNullable = when (nullabilityFromQualifiers) {
NullabilityQualifier.NULLABLE -> true
@@ -145,14 +158,15 @@ private fun ConeLookupTagBasedType.enhanceInflexibleType(
var globalArgIndex = index + 1
val enhancedArguments = typeArguments.map { arg ->
val argIndex = globalArgIndex.also { globalArgIndex += subtreeSizes[it] }
arg.type?.enhanceConeKotlinType(session, qualifiers, argIndex, subtreeSizes)?.let {
when (arg.kind) {
ProjectionKind.IN -> ConeKotlinTypeProjectionIn(it)
ProjectionKind.OUT -> ConeKotlinTypeProjectionOut(it)
ProjectionKind.STAR -> ConeStarProjection
ProjectionKind.INVARIANT -> it
arg.type?.enhanceConeKotlinType(session, qualifiers, argIndex, subtreeSizes, convertErrorsToWarnings = convertNestedErrorsToWarnings)
?.let {
when (arg.kind) {
ProjectionKind.IN -> ConeKotlinTypeProjectionIn(it)
ProjectionKind.OUT -> ConeKotlinTypeProjectionOut(it)
ProjectionKind.STAR -> ConeStarProjection
ProjectionKind.INVARIANT -> it
}
}
}
}
val shouldAddAttribute = nullabilityFromQualifiers == NullabilityQualifier.NOT_NULL && !hasEnhancedNullability