[Commonizer] typeAliasUtils: computeSuitableUnderlyingType: Consider commonized type-aliases

This commit is contained in:
sebastian.sellmair
2021-06-30 17:10:02 +02:00
committed by Space
parent 5ca81b01f0
commit 9794068f22
6 changed files with 122 additions and 22 deletions
@@ -65,6 +65,41 @@ class CommonizeNativeDistributionTest {
)
}
@Test
fun `commonize - apple platforms`() {
val iosTarget = CommonizerTarget(IOS_ARM64, IOS_X64, IOS_SIMULATOR_ARM64)
val watchosTarget = CommonizerTarget(WATCHOS_ARM64, WATCHOS_X64, WATCHOS_SIMULATOR_ARM64)
val macosTarget = CommonizerTarget(MACOS_X64, MACOS_ARM64)
val appleTarget = SharedCommonizerTarget(iosTarget.konanTargets + watchosTarget.konanTargets + macosTarget.konanTargets)
CliCommonizer(this::class.java.classLoader).commonizeNativeDistribution(
konanHome = konanHome,
outputTargets = setOf(iosTarget, watchosTarget, macosTarget, appleTarget),
outputDirectory = temporaryOutputDirectory.root,
logLevel = CommonizerLogLevel.Info
)
assertTrue(
resolveCommonizedDirectory(temporaryOutputDirectory.root, iosTarget).isDirectory,
"Expected directory for $iosTarget"
)
assertTrue(
resolveCommonizedDirectory(temporaryOutputDirectory.root, watchosTarget).isDirectory,
"Expected directory for $watchosTarget"
)
assertTrue(
resolveCommonizedDirectory(temporaryOutputDirectory.root, macosTarget).isDirectory,
"Expected directory for $macosTarget"
)
assertTrue(
resolveCommonizedDirectory(temporaryOutputDirectory.root, appleTarget).isDirectory,
"Expected directory for $appleTarget"
)
}
@Test
fun `commonize - no outputTargets specified`() {
CliCommonizer(this::class.java.classLoader).commonizeNativeDistribution(
@@ -17,9 +17,11 @@ internal tailrec fun computeSuitableUnderlyingType(
): CirClassOrTypeAliasType? {
return when (underlyingType) {
is CirClassType -> underlyingType.withCommonizedArguments(classifiers)
is CirTypeAliasType -> if (classifiers.commonDependencies.hasClassifier(underlyingType.classifierId))
underlyingType.withCommonizedArguments(classifiers) else
computeSuitableUnderlyingType(classifiers, underlyingType.underlyingType)
is CirTypeAliasType ->
if (classifiers.commonDependencies.hasClassifier(underlyingType.classifierId) ||
classifiers.commonizedNodes.typeAliasNode(underlyingType.classifierId)?.commonDeclaration?.invoke() != null
) underlyingType.withCommonizedArguments(classifiers)
else computeSuitableUnderlyingType(classifiers, underlyingType.underlyingType)
}
}
@@ -1,26 +1,26 @@
expect class A()
// Lifted up type aliases:
typealias B = A // class at the RHS
typealias C = A // TA at the RHS, expanded to the same class
typealias C2 = A // 2x TA at the RHS, expanded to the same class
typealias C3 = A // 3x TA at the RHS, expanded to the same class
typealias C = B // TA lifted up as is
typealias C2 = C // TA lifted up as is
typealias C3 = C2 // TA lifted up as is
typealias D = A // class/TA expanded to the same class at the RHS
typealias E = A // different TAs expanded to the same class at the RHS
typealias E = B // different TAs use common type from TA-chain
typealias F = List<String> // parameterized type at the RHS
typealias H<T> = List<T> // TA with own parameters
typealias I2<T> = List<T>
typealias I3<R> = List<R>
typealias I4 = List<String>
typealias I3<R> = I2<R>
typealias I4 = I2<String>
typealias I5<V, K> = Map<K, V>
typealias I6<T, R> = Map<R, T>
typealias I6<T, R> = I5<T, R>
typealias I7<K, V> = Map<K, V>
typealias I8<T, R> = Map<R, T>
typealias I9<Q, W> = Map<W, Q>
typealias I8<T, R> = I7<R, T>
typealias I9<Q, W> = I8<Q, W>
typealias J<T> = Function<T> // function type at the RHS
typealias M = () -> Unit // same return type
@@ -38,14 +38,14 @@ expect class T
typealias U = A // same nullability of the RHS class
expect class V // different nullability of the RHS class
typealias W = A // same nullability of the RHS TA
typealias W = U // same nullability of the RHS TA
expect class X // different nullability of the RHS TA
expect class Y // TA at the RHS with the different nullability of own RHS
// Supertypes:
expect class FILE expect constructor(): kotlinx.cinterop.CStructVar
typealias uuid_t = kotlinx.cinterop.CArrayPointer<kotlinx.cinterop.UByteVar>
typealias uuid_t = __darwin_uuid_t
typealias __darwin_uuid_t = kotlinx.cinterop.CArrayPointer<kotlinx.cinterop.UByteVar>
expect val uuid: uuid_t
@@ -2,12 +2,12 @@ class A
// Lifted up type aliases:
typealias B = A // class at the RHS
typealias C = B // TA at the RHS, expanded to the same class
typealias C2 = C // 2x TA at the RHS, expanded to the same class
typealias C3 = C2 // 3x TA at the RHS, expanded to the same class
typealias C = B // TA lifted up as is
typealias C2 = C // TA lifted up as is
typealias C3 = C2 // TA lifted up as is
typealias D = B // class/TA expanded to the same class at the RHS
typealias E = C // different TAs expanded to the same class at the RHS
typealias E = C // different TAs use common type from TA-chain
typealias F = List<String> // parameterized type at the RHS
typealias G = List<Int> // different parameterized types at the RHS
@@ -2,12 +2,12 @@ class A
// Lifted up type aliases:
typealias B = A // class at the RHS
typealias C = B // TA at the RHS, expanded to the same class
typealias C2 = C // 2x TA at the RHS, expanded to the same class
typealias C3 = C2 // 3x TA at the RHS, expanded to the same class
typealias C = B // TA lifted up as is
typealias C2 = C // TA lifted up as is
typealias C3 = C2 // TA lifted up as is
typealias D = A // class/TA expanded to the same class at the RHS
typealias E = B // different TAs expanded to the same class at the RHS
typealias E = B // different TAs use common type from TA-chain
typealias F = List<String> // parameterized type at the RHS
typealias G = List<String> // different parameterized types at the RHS
@@ -548,6 +548,69 @@ class HierarchicalClassAndTypeAliasCommonizationTest : AbstractInlineSourcesComm
)
}
@Test
fun `test typealias to numbers`() {
val result = commonize {
outputTarget("(a, b)", "(c, d)", "(a, b, c, d)")
simpleSingleSourceTarget(
"a", """
typealias Proxy = Long
typealias X = Proxy
val x: X
""".trimIndent()
)
simpleSingleSourceTarget(
"b", """
typealias Proxy = Long
typealias X = Proxy
val x: X
""".trimIndent()
)
simpleSingleSourceTarget(
"c", """
typealias Proxy = Int
typealias X = Proxy
val x: X
""".trimIndent()
)
simpleSingleSourceTarget(
"d", """
typealias Proxy = Short
typealias X = Proxy
val x: X
""".trimIndent()
)
}
result.assertCommonized(
"(a, b)", """
typealias Proxy = Long
typealias X = Proxy
expect val x: X
""".trimIndent()
)
result.assertCommonized(
"(c, d)", """
expect class Proxy
typealias X = Proxy
expect val x: X
""".trimIndent()
)
result.assertCommonized(
"(a, b, c, d)", """
expect class Proxy
typealias X = Proxy
expect val x: X
""".trimIndent()
)
}
fun `todo - test boxed function using TA and expanded type`() {
val result = commonize {
outputTarget("(a, b)")