FIR Java: simplify flexible nullability manipulations

This commit is contained in:
Mikhail Glukhikh
2020-11-18 10:55:34 +03:00
committed by teamcityserver
parent fc7f589caa
commit b658e99f91
4 changed files with 15 additions and 13 deletions
@@ -34,7 +34,7 @@ class ConeAttributes private constructor(attributes: List<ConeAttribute<*>>) : A
} }
val Empty: ConeAttributes = ConeAttributes(emptyList()) val Empty: ConeAttributes = ConeAttributes(emptyList())
val WithFlexibleNullability: ConeAttributes = ConeAttributes(listOf(CompilerConeAttributes.FlexibleNullability)) internal val WithFlexibleNullability: ConeAttributes = ConeAttributes(listOf(CompilerConeAttributes.FlexibleNullability))
fun create(attributes: List<ConeAttribute<*>>): ConeAttributes { fun create(attributes: List<ConeAttribute<*>>): ConeAttributes {
return if (attributes.isEmpty()) { return if (attributes.isEmpty()) {
@@ -68,13 +68,6 @@ class ConeAttributes private constructor(attributes: List<ConeAttribute<*>>) : A
return perform(other) { this.intersect(it) } return perform(other) { this.intersect(it) }
} }
fun intersectUnless(other: ConeAttributes, predicate: (ConeAttributes) -> Boolean): ConeAttributes {
return if (predicate.invoke(this))
this
else
perform(other) { this.intersect(it) }
}
override fun iterator(): Iterator<ConeAttribute<*>> { override fun iterator(): Iterator<ConeAttribute<*>> {
return arrayMap.iterator() return arrayMap.iterator()
} }
@@ -101,3 +94,12 @@ class ConeAttributes private constructor(attributes: List<ConeAttribute<*>>) : A
return arrayMap.isEmpty() return arrayMap.isEmpty()
} }
} }
private fun ConeAttributes.intersectUnless(other: ConeAttributes, predicate: (ConeAttributes) -> Boolean): ConeAttributes =
if (predicate.invoke(this)) this else intersect(other)
fun ConeAttributes.withFlexible(): ConeAttributes =
intersect(ConeAttributes.WithFlexibleNullability)
fun ConeAttributes.withFlexibleUnless(predicate: (ConeAttributes) -> Boolean): ConeAttributes =
intersectUnless(ConeAttributes.WithFlexibleNullability, predicate)
@@ -200,7 +200,7 @@ private fun ClassId.toConeFlexibleType(
toConeKotlinType( toConeKotlinType(
typeArguments, typeArguments,
isNullable = false, isNullable = false,
attributes.intersectUnless(ConeAttributes.WithFlexibleNullability) { it.hasEnhancedNullability } attributes.withFlexibleUnless { it.hasEnhancedNullability }
), ),
toConeKotlinType(typeArgumentsForUpper, isNullable = true, attributes) toConeKotlinType(typeArgumentsForUpper, isNullable = true, attributes)
) )
@@ -242,7 +242,7 @@ private fun JavaClassifierType.toConeKotlinTypeWithoutEnhancement(
else else
ConeFlexibleType( ConeFlexibleType(
lowerBound.withAttributes( lowerBound.withAttributes(
lowerBound.attributes.intersectUnless(ConeAttributes.WithFlexibleNullability) { it.hasEnhancedNullability } lowerBound.attributes.withFlexibleUnless { it.hasEnhancedNullability }
), ),
upperBound upperBound
) )
@@ -316,7 +316,7 @@ private fun getErasedVersionOfFirstUpperBound(
// Avoid exponential complexity // Avoid exponential complexity
ConeFlexibleType( ConeFlexibleType(
lowerBound.withAttributes( lowerBound.withAttributes(
lowerBound.attributes.intersectUnless(ConeAttributes.WithFlexibleNullability) { it.hasEnhancedNullability } lowerBound.attributes.withFlexibleUnless { it.hasEnhancedNullability }
), ),
lowerBound.withNullability(ConeNullability.NULLABLE) lowerBound.withNullability(ConeNullability.NULLABLE)
) )
@@ -92,7 +92,7 @@ private fun ConeKotlinType.enhanceConeKotlinType(
val lowerResult = lowerBound.enhanceInflexibleType( val lowerResult = lowerBound.enhanceInflexibleType(
session, TypeComponentPosition.FLEXIBLE_LOWER, qualifiers, index, session, TypeComponentPosition.FLEXIBLE_LOWER, qualifiers, index,
attributes = if (needsFlexibleNullabilityAttribute) attributes = if (needsFlexibleNullabilityAttribute)
lowerBound.attributes.intersect(ConeAttributes.WithFlexibleNullability) lowerBound.attributes.withFlexible()
else else
lowerBound.attributes lowerBound.attributes
) )
@@ -106,7 +106,7 @@ class JavaClassMembersEnhancementScope(
if (valueParameter.returnTypeRef.coneType is ConeFlexibleType) { if (valueParameter.returnTypeRef.coneType is ConeFlexibleType) {
ConeFlexibleType( ConeFlexibleType(
type.withAttributes( type.withAttributes(
type.attributes.intersectUnless(ConeAttributes.WithFlexibleNullability) { type.attributes.withFlexibleUnless {
it.hasEnhancedNullability it.hasEnhancedNullability
} }
), ),