[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
@@ -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( private fun createClassTypeWithAllOuterTypes(
classDescriptor: ClassDescriptor, classDescriptor: ClassDescriptor,
arguments: List<CirTypeProjection>, arguments: List<CirTypeProjection>,
@@ -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 // type alias don't needs to be commonized because it is from the standard library
fun forKnownUnderlyingType(underlyingType: CirClassOrTypeAliasType) = object : CommonizedTypeAliasTypeBuilder { fun forKnownUnderlyingType(underlyingType: CirClassOrTypeAliasType) = object : CommonizedTypeAliasTypeBuilder {
override fun build(typeAliasId: ClassId, arguments: List<CirTypeProjection>, isMarkedNullable: Boolean) = override fun build(typeAliasId: ClassId, arguments: List<CirTypeProjection>, isMarkedNullable: Boolean): CirTypeAliasType {
CirTypeFactory.createTypeAliasType( val underlyingTypeWithProperNullability = if (isMarkedNullable && !underlyingType.isMarkedNullable)
CirTypeFactory.makeNullable(underlyingType)
else
underlyingType
return CirTypeFactory.createTypeAliasType(
typeAliasId = typeAliasId, typeAliasId = typeAliasId,
underlyingType = underlyingType, // TODO replace arguments??? underlyingType = underlyingTypeWithProperNullability, // TODO replace arguments???
arguments = arguments, arguments = arguments,
isMarkedNullable = isMarkedNullable isMarkedNullable = isMarkedNullable
) )
}
} }
} }
} }