[Commonizer] ReturnTypeCommonizer: Only allow covariant nullability for covariant types
The return type of any property with a setter can be considered invariant and should enable covariant nullability commonization ^KT-48567
This commit is contained in:
committed by
Space
parent
9e34382db5
commit
2e053be703
+3
-1
@@ -7,6 +7,7 @@ package org.jetbrains.kotlin.commonizer.core
|
||||
|
||||
import org.jetbrains.kotlin.commonizer.cir.CirFunctionOrProperty
|
||||
import org.jetbrains.kotlin.commonizer.cir.CirName
|
||||
import org.jetbrains.kotlin.commonizer.cir.CirProperty
|
||||
import org.jetbrains.kotlin.commonizer.cir.CirType
|
||||
import org.jetbrains.kotlin.commonizer.core.TypeCommonizer.Options.Companion.default
|
||||
import org.jetbrains.kotlin.commonizer.mergedtree.CirKnownClassifiers
|
||||
@@ -47,7 +48,8 @@ private class ReturnTypeCommonizer(
|
||||
override fun invoke(values: List<CirFunctionOrProperty>): CirType? {
|
||||
if (values.isEmpty()) return null
|
||||
val isTopLevel = values.all { it.containingClass == null }
|
||||
return TypeCommonizer(classifiers, default.withCovariantNullabilityCommonizationEnabled(isTopLevel))
|
||||
val isCovariant = values.none { it is CirProperty && it.isVar }
|
||||
return TypeCommonizer(classifiers, default.withCovariantNullabilityCommonizationEnabled(isTopLevel && isCovariant))
|
||||
.asCommonizer().commonize(values.map { it.returnType })
|
||||
}
|
||||
}
|
||||
|
||||
+20
-1
@@ -63,14 +63,33 @@ class ReturnTypeNullabilityCommonizationTest : AbstractInlineSourcesCommonizatio
|
||||
fun `test nullable and non-nullable - property`() {
|
||||
val result = commonize {
|
||||
outputTarget("(a, b)")
|
||||
simpleSingleSourceTarget("a", "val x: Any? = Unit?")
|
||||
simpleSingleSourceTarget("a", "val x: Any? = null")
|
||||
simpleSingleSourceTarget("b", "val x: Any = Unit")
|
||||
}
|
||||
|
||||
result.assertCommonized("(a, b)", "expect val x: Any?")
|
||||
}
|
||||
|
||||
fun `test nullable and non-nullable - var - val property`() {
|
||||
val result = commonize {
|
||||
outputTarget("(a, b)")
|
||||
simpleSingleSourceTarget("a", "var x: Any? = null")
|
||||
simpleSingleSourceTarget("b", "val x: Any = Unit")
|
||||
}
|
||||
|
||||
result.assertCommonized("(a, b)", "")
|
||||
}
|
||||
|
||||
fun `test nullable and non-nullable - var var property`() {
|
||||
val result = commonize {
|
||||
outputTarget("(a, b)")
|
||||
simpleSingleSourceTarget("a", "var x: Any? = null")
|
||||
simpleSingleSourceTarget("b", "var x: Any = Unit")
|
||||
}
|
||||
|
||||
result.assertCommonized("(a, b)", "")
|
||||
}
|
||||
|
||||
fun `test different nullability typealias - function`() {
|
||||
val result = commonize {
|
||||
outputTarget("(a, b)")
|
||||
|
||||
Reference in New Issue
Block a user