Fix nested typealiases expansion and as a result fix serialization

Consider the following situation:
 ```
 class Inv<T>
 typealias A<K> = Inv<K>
 typealias B<V> = Inv<A<K>>

 fun <U> materialize(): B<U> = TODO()
 ```

 Type `B<U>` is expanding to `Inv<Inv<U>>` and for this type `B<U>` and
 `Inv<A<U>>` are abbreviated types, but due to a bug we forgot to make
 substitution for `Inv<A<U>>` and were getting abbreviated type
 `Inv<A<K>>` where `K` is a type parameter from the typealias declaration.

 This bug didn't affect subtyping anyhow but the incorrect type was
 serialized and caused problems during deserialization as there wasn't
 `K` in deserialization context.

 #KT-24964 Fixed
 #KT-20780 Fixed
 #KT-20065 Fixed
 #KT-28236 Fixed
 #KT-21775 Fixed
This commit is contained in:
Mikhail Zarechenskiy
2019-02-22 17:39:40 +03:00
parent f1d5912f05
commit 617bed1bf1
5 changed files with 115 additions and 13 deletions
@@ -214,25 +214,16 @@ class TypeAliasExpander(
withAbbreviatedType = false
)
val substitutedType = type.substituteArguments(typeAliasExpansion, recursionDepth)
// 'dynamic' type can't be abbreviated - will be reported separately
val typeWithAbbreviation =
if (nestedExpandedType.isDynamic()) nestedExpandedType else nestedExpandedType.withAbbreviation(type)
if (nestedExpandedType.isDynamic()) nestedExpandedType else nestedExpandedType.withAbbreviation(substitutedType)
TypeProjectionImpl(originalProjection.projectionKind, typeWithAbbreviation)
}
else -> {
val substitutedArguments = type.arguments.mapIndexed { i, originalArgument ->
val projection = expandTypeProjection(
originalArgument, typeAliasExpansion, typeConstructor.parameters[i], recursionDepth + 1
)
if (projection.isStarProjection) projection
else TypeProjectionImpl(
projection.projectionKind,
TypeUtils.makeNullableIfNeeded(projection.type, originalArgument.type.isMarkedNullable)
)
}
val substitutedType = type.replace(newArguments = substitutedArguments)
val substitutedType = type.substituteArguments(typeAliasExpansion, recursionDepth)
checkTypeArgumentsSubstitution(type, substitutedType)
@@ -241,6 +232,23 @@ class TypeAliasExpander(
}
}
private fun SimpleType.substituteArguments(typeAliasExpansion: TypeAliasExpansion, recursionDepth: Int): SimpleType {
val typeConstructor = this.constructor
val substitutedArguments = this.arguments.mapIndexed { i, originalArgument ->
val projection = expandTypeProjection(
originalArgument, typeAliasExpansion, typeConstructor.parameters[i], recursionDepth + 1
)
if (projection.isStarProjection) projection
else TypeProjectionImpl(
projection.projectionKind,
TypeUtils.makeNullableIfNeeded(projection.type, originalArgument.type.isMarkedNullable)
)
}
return this.replace(newArguments = substitutedArguments)
}
private fun checkTypeArgumentsSubstitution(unsubstitutedType: KotlinType, substitutedType: KotlinType) {
val typeSubstitutor = TypeSubstitutor.create(substitutedType)
+43
View File
@@ -0,0 +1,43 @@
// FILE: lib.kt
package lib
class TestObserver<T> {
fun assertValue(valuePredicate: (T) -> Boolean): Unit = TODO()
}
class Single<T> {
fun test(): TestObserver<T> = TODO()
}
class Employee
class Either<T>
typealias DomainEither<T> = Either<T>
typealias DomainSingle<T> = Single<DomainEither<T>>
fun provideDomainSingle(): DomainSingle<Employee> = TODO()
class CreateEmployeeUseCaseAccessor {
fun testNormalName() {
val testObs = provideDomainSingle().test()
testObs.assertValue { true }
}
}
// FILE: main.kt
import lib.*
class CreateEmployeeUseCaseTest {
fun testNormalName() {
val testObs = provideDomainSingle().test()
testObs.assertValue { true }
}
}
fun box(): String {
return "OK"
}
@@ -0,0 +1,17 @@
// FILE: lib.kt
package lib
typealias Dispatch<Msg> = (Msg) -> Unit
typealias Effect<Msg> = (Dispatch<Msg>) -> Unit
fun <Msg> noEffect(): Effect<Msg> = TODO()
// FILE: main.kt
import lib.*
fun box(): String {
val s = { noEffect<Unit>() }
return "OK"
}
@@ -0,0 +1,19 @@
// FILE: lib.kt
package lib
class Inv<K>
typealias A<V> = Inv<V>
typealias B<T> = Inv<A<T>>
fun <U> materialize(): B<U>? = null
// FILE: main.kt
import lib.*
fun box(): String {
val s = { materialize<Unit>() }
return "OK"
}
@@ -213,6 +213,11 @@ public class CompileKotlinAgainstKotlinTestGenerated extends AbstractCompileKotl
runTest("compiler/testData/compileKotlinAgainstKotlin/kt14012_multi.kt");
}
@TestMetadata("kt21775.kt")
public void testKt21775() throws Exception {
runTest("compiler/testData/compileKotlinAgainstKotlin/kt21775.kt");
}
@TestMetadata("multifileClassInlineFunctionAccessingProperty.kt")
public void testMultifileClassInlineFunctionAccessingProperty() throws Exception {
runTest("compiler/testData/compileKotlinAgainstKotlin/multifileClassInlineFunctionAccessingProperty.kt");
@@ -233,11 +238,21 @@ public class CompileKotlinAgainstKotlinTestGenerated extends AbstractCompileKotl
runTest("compiler/testData/compileKotlinAgainstKotlin/nestedEnum.kt");
}
@TestMetadata("nestedFunctionTypeAliasExpansion.kt")
public void testNestedFunctionTypeAliasExpansion() throws Exception {
runTest("compiler/testData/compileKotlinAgainstKotlin/nestedFunctionTypeAliasExpansion.kt");
}
@TestMetadata("nestedObject.kt")
public void testNestedObject() throws Exception {
runTest("compiler/testData/compileKotlinAgainstKotlin/nestedObject.kt");
}
@TestMetadata("nestedTypeAliasExpansion.kt")
public void testNestedTypeAliasExpansion() throws Exception {
runTest("compiler/testData/compileKotlinAgainstKotlin/nestedTypeAliasExpansion.kt");
}
@TestMetadata("optionalAnnotation.kt")
public void testOptionalAnnotation() throws Exception {
runTest("compiler/testData/compileKotlinAgainstKotlin/optionalAnnotation.kt");