[Commonizer] Fix: Lost nullability in commonized type aliases

This commit is contained in:
Dmitriy Dolovov
2020-11-18 17:27:51 +03:00
parent e17158d961
commit 0d50ccc42d
2 changed files with 29 additions and 3 deletions

View File

@@ -115,6 +115,26 @@ object CirTypeFactory {
)
}
fun makeNullable(classOrTypeAliasType: CirClassOrTypeAliasType): CirClassOrTypeAliasType =
if (classOrTypeAliasType.isMarkedNullable)
classOrTypeAliasType
else
when (classOrTypeAliasType) {
is CirClassType -> createClassType(
classId = classOrTypeAliasType.classifierId,
outerType = classOrTypeAliasType.outerType,
visibility = classOrTypeAliasType.visibility,
arguments = classOrTypeAliasType.arguments,
isMarkedNullable = true
)
is CirTypeAliasType -> createTypeAliasType(
typeAliasId = classOrTypeAliasType.classifierId,
underlyingType = makeNullable(classOrTypeAliasType.underlyingType),
arguments = classOrTypeAliasType.arguments,
isMarkedNullable = true
)
}
private fun createClassTypeWithAllOuterTypes(
classDescriptor: ClassDescriptor,
arguments: List<CirTypeProjection>,

View File

@@ -140,13 +140,19 @@ private class TypeAliasTypeCommonizer(private val cache: CirClassifiersCache) :
// type alias don't needs to be commonized because it is from the standard library
fun forKnownUnderlyingType(underlyingType: CirClassOrTypeAliasType) = object : CommonizedTypeAliasTypeBuilder {
override fun build(typeAliasId: ClassId, arguments: List<CirTypeProjection>, isMarkedNullable: Boolean) =
CirTypeFactory.createTypeAliasType(
override fun build(typeAliasId: ClassId, arguments: List<CirTypeProjection>, isMarkedNullable: Boolean): CirTypeAliasType {
val underlyingTypeWithProperNullability = if (isMarkedNullable && !underlyingType.isMarkedNullable)
CirTypeFactory.makeNullable(underlyingType)
else
underlyingType
return CirTypeFactory.createTypeAliasType(
typeAliasId = typeAliasId,
underlyingType = underlyingType, // TODO replace arguments???
underlyingType = underlyingTypeWithProperNullability, // TODO replace arguments???
arguments = arguments,
isMarkedNullable = isMarkedNullable
)
}
}
}
}