Skip to content

Commit e19c1b5

Browse files
committed
Drop MockTypeAliasDescriptor, use MockClassDescriptor instead
It's irrelevant whether the non-found classifier is a class or a typealias
1 parent 12b48f8 commit e19c1b5

File tree

2 files changed

+29
-106
lines changed

2 files changed

+29
-106
lines changed

core/deserialization/src/org/jetbrains/kotlin/serialization/deserialization/NotFoundClasses.kt

Lines changed: 7 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,16 @@ package org.jetbrains.kotlin.serialization.deserialization
1818

1919
import org.jetbrains.kotlin.descriptors.*
2020
import org.jetbrains.kotlin.descriptors.annotations.Annotations
21-
import org.jetbrains.kotlin.descriptors.impl.AbstractTypeAliasDescriptor
2221
import org.jetbrains.kotlin.descriptors.impl.ClassDescriptorBase
2322
import org.jetbrains.kotlin.descriptors.impl.EmptyPackageFragmentDescriptor
2423
import org.jetbrains.kotlin.descriptors.impl.TypeParameterDescriptorImpl
2524
import org.jetbrains.kotlin.name.ClassId
2625
import org.jetbrains.kotlin.name.FqName
2726
import org.jetbrains.kotlin.name.Name
28-
import org.jetbrains.kotlin.resolve.descriptorUtil.builtIns
29-
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameUnsafe
3027
import org.jetbrains.kotlin.resolve.descriptorUtil.module
3128
import org.jetbrains.kotlin.resolve.scopes.MemberScope
3229
import org.jetbrains.kotlin.storage.StorageManager
33-
import org.jetbrains.kotlin.storage.getValue
3430
import org.jetbrains.kotlin.types.ClassTypeConstructorImpl
35-
import org.jetbrains.kotlin.types.SimpleType
36-
import org.jetbrains.kotlin.types.TypeSubstitutor
3731
import org.jetbrains.kotlin.types.Variance
3832

3933
class NotFoundClasses(private val storageManager: StorageManager, private val module: ModuleDescriptor) {
@@ -46,28 +40,7 @@ class NotFoundClasses(private val storageManager: StorageManager, private val mo
4640
EmptyPackageFragmentDescriptor(module, fqName)
4741
}
4842

49-
private val classes = storageManager.createMemoizedFunction<ClassRequest, ClassDescriptor> { request ->
50-
computeClassifier(request, {
51-
owner, name, isInner, numberOfTypeParametersCount ->
52-
MockClassDescriptor(storageManager, owner, name, isInner, numberOfTypeParametersCount)
53-
})
54-
}
55-
56-
private val typeAliases = storageManager.createMemoizedFunction<ClassRequest, TypeAliasDescriptor> { request ->
57-
computeClassifier(request, {
58-
owner, name, isInner, numberOfTypeParametersCount ->
59-
MockTypeAliasDescriptor(storageManager, owner, name, isInner, numberOfTypeParametersCount)
60-
})
61-
}
62-
63-
// TODO: Uncomment this when KT-12871 is fixed
64-
// private typealias ConstructorFunction<D> = (DeclarationDescriptor, Name, isInner: Boolean, numberOfTypeParametersCount: Int) -> D
65-
private fun <D> computeClassifier(
66-
request: ClassRequest,
67-
constructor: (DeclarationDescriptor, Name, isInner: Boolean, numberOfTypeParametersCount: Int) -> D
68-
): D {
69-
val (classId, typeParametersCount) = request
70-
43+
private val classes = storageManager.createMemoizedFunction<ClassRequest, ClassDescriptor> { (classId, typeParametersCount) ->
7144
if (classId.isLocal) {
7245
throw UnsupportedOperationException("Unresolved local class: $classId")
7346
}
@@ -79,7 +52,7 @@ class NotFoundClasses(private val storageManager: StorageManager, private val mo
7952
// Treat a class with a nested ClassId as inner for simplicity, otherwise the outer type cannot have generic arguments
8053
val isInner = classId.isNestedClass
8154

82-
return constructor(container, classId.shortClassName, isInner, typeParametersCount.firstOrNull() ?: 0)
55+
MockClassDescriptor(storageManager, container, classId.shortClassName, isInner, typeParametersCount.firstOrNull() ?: 0)
8356
}
8457

8558
class MockClassDescriptor internal constructor(
@@ -89,7 +62,11 @@ class NotFoundClasses(private val storageManager: StorageManager, private val mo
8962
private val isInner: Boolean,
9063
numberOfDeclaredTypeParameters: Int
9164
) : ClassDescriptorBase(storageManager, container, name, SourceElement.NO_SOURCE, /* isExternal = */ false) {
92-
private val typeParameters = createTypeParameters(this, numberOfDeclaredTypeParameters)
65+
private val typeParameters = (1..numberOfDeclaredTypeParameters).map { index ->
66+
TypeParameterDescriptorImpl.createWithDefaultBound(
67+
this, Annotations.EMPTY, false, Variance.INVARIANT, Name.identifier("T$index"), index
68+
)
69+
}
9370

9471
private val typeConstructor = ClassTypeConstructorImpl(this, /* isFinal = */ true, typeParameters, setOf(module.builtIns.anyType))
9572

@@ -117,57 +94,11 @@ class NotFoundClasses(private val storageManager: StorageManager, private val mo
11794
override fun toString() = "class $name (not found)"
11895
}
11996

120-
private class MockTypeAliasDescriptor(
121-
storageManager: StorageManager,
122-
containingDeclaration: DeclarationDescriptor,
123-
name: Name,
124-
private val isInner: Boolean,
125-
numberOfDeclaredTypeParameters: Int
126-
) : AbstractTypeAliasDescriptor(containingDeclaration, Annotations.EMPTY, name, SourceElement.NO_SOURCE, Visibilities.PUBLIC) {
127-
init {
128-
initialize(createTypeParameters(this, numberOfDeclaredTypeParameters))
129-
}
130-
131-
private val constructorTypeParameters by storageManager.createLazyValue { computeConstructorTypeParameters() }
132-
133-
override fun getTypeConstructorTypeParameters() = constructorTypeParameters
134-
135-
// We don't have enough information about underlying type, so just take nullable Any?
136-
// Anyway it should not used extensively, because not found type aliases are only used for type abbreviations
137-
override val underlyingType: SimpleType
138-
get() = builtIns.nullableAnyType
139-
override val expandedType: SimpleType
140-
get() = builtIns.nullableAnyType
141-
override fun getDefaultType(): SimpleType =
142-
builtIns.nullableAnyType
143-
override val classDescriptor: ClassDescriptor?
144-
get() = expandedType.constructor.declarationDescriptor as? ClassDescriptor
145-
146-
override fun isInner(): Boolean = isInner
147-
148-
override fun substitute(substitutor: TypeSubstitutor) = this
149-
150-
override fun toString() = "MockTypeAliasDescriptor[$fqNameUnsafe]"
151-
}
152-
15397
// We create different ClassDescriptor instances for types with the same ClassId but different number of type arguments.
15498
// (This may happen when a class with the same FQ name is instantiated with different type arguments in different modules.)
15599
// It's better than creating just one descriptor because otherwise would fail in multiple places where it's asserted that
156100
// the number of type arguments in a type must be equal to the number of the type parameters of the class
157101
fun getClass(classId: ClassId, typeParametersCount: List<Int>): ClassDescriptor {
158102
return classes(ClassRequest(classId, typeParametersCount))
159103
}
160-
161-
fun getTypeAlias(classId: ClassId, typeParametersCount: List<Int>): TypeAliasDescriptor {
162-
return typeAliases(ClassRequest(classId, typeParametersCount))
163-
}
164-
}
165-
166-
private fun createTypeParameters(
167-
classifierDescriptor: ClassifierDescriptor,
168-
numberOfDeclaredTypeParameters: Int
169-
) = (1..numberOfDeclaredTypeParameters).map { index ->
170-
TypeParameterDescriptorImpl.createWithDefaultBound(
171-
classifierDescriptor, Annotations.EMPTY, false, Variance.INVARIANT, Name.identifier("T$index"), index
172-
)
173104
}

core/deserialization/src/org/jetbrains/kotlin/serialization/deserialization/TypeDeserializer.kt

Lines changed: 22 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -112,39 +112,31 @@ class TypeDeserializer(
112112
return simpleType.withAbbreviation(simpleType(abbreviatedTypeProto, additionalAnnotations))
113113
}
114114

115-
private fun typeConstructor(proto: ProtoBuf.Type): TypeConstructor =
116-
when {
117-
proto.hasClassName() -> {
118-
classDescriptors(proto.className)?.typeConstructor
119-
?: c.nameResolver.getClassId(proto.className).let { classId ->
120-
c.components.notFoundClasses.getClass(classId, computeTypeParametersCount(classId, proto, c.typeTable)).typeConstructor
121-
}
122-
}
123-
proto.hasTypeParameter() ->
124-
typeParameterTypeConstructor(proto.typeParameter)
125-
?: ErrorUtils.createErrorTypeConstructor("Unknown type parameter ${proto.typeParameter}")
126-
proto.hasTypeParameterName() -> {
127-
val container = c.containingDeclaration
128-
val name = c.nameResolver.getString(proto.typeParameterName)
129-
val parameter = ownTypeParameters.find { it.name.asString() == name }
130-
parameter?.typeConstructor ?: ErrorUtils.createErrorTypeConstructor("Deserialized type parameter $name in $container")
131-
}
132-
proto.hasTypeAliasName() -> {
133-
typeAliasDescriptors(proto.typeAliasName)?.typeConstructor
134-
?: c.nameResolver.getClassId(proto.typeAliasName).let { classId ->
135-
c.components.notFoundClasses.getTypeAlias(classId, computeTypeParametersCount(classId, proto, c.typeTable)).typeConstructor
136-
}
137-
}
138-
else -> ErrorUtils.createErrorTypeConstructor("Unknown type")
115+
private fun typeConstructor(proto: ProtoBuf.Type): TypeConstructor {
116+
fun notFoundClass(classIdIndex: Int): ClassDescriptor {
117+
val classId = c.nameResolver.getClassId(classIdIndex)
118+
val typeParametersCount = generateSequence(proto) { it.outerType(c.typeTable) }.map { it.argumentCount }.toMutableList()
119+
val classNestingLevel = generateSequence(classId, ClassId::getOuterClassId).count()
120+
while (typeParametersCount.size < classNestingLevel) {
121+
typeParametersCount.add(0)
139122
}
123+
return c.components.notFoundClasses.getClass(classId, typeParametersCount)
124+
}
140125

141-
private fun computeTypeParametersCount(classId: ClassId, proto: ProtoBuf.Type, typeTable: TypeTable): List<Int> {
142-
val typeParametersCount = generateSequence(proto) { it.outerType(typeTable) }.map { it.argumentCount }.toMutableList()
143-
val classNestingLevel = generateSequence(classId, ClassId::getOuterClassId).count()
144-
while (typeParametersCount.size < classNestingLevel) {
145-
typeParametersCount.add(0)
126+
return when {
127+
proto.hasClassName() -> (classDescriptors(proto.className) ?: notFoundClass(proto.className)).typeConstructor
128+
proto.hasTypeParameter() ->
129+
typeParameterTypeConstructor(proto.typeParameter)
130+
?: ErrorUtils.createErrorTypeConstructor("Unknown type parameter ${proto.typeParameter}")
131+
proto.hasTypeParameterName() -> {
132+
val container = c.containingDeclaration
133+
val name = c.nameResolver.getString(proto.typeParameterName)
134+
val parameter = ownTypeParameters.find { it.name.asString() == name }
135+
parameter?.typeConstructor ?: ErrorUtils.createErrorTypeConstructor("Deserialized type parameter $name in $container")
136+
}
137+
proto.hasTypeAliasName() -> (typeAliasDescriptors(proto.typeAliasName) ?: notFoundClass(proto.typeAliasName)).typeConstructor
138+
else -> ErrorUtils.createErrorTypeConstructor("Unknown type")
146139
}
147-
return typeParametersCount
148140
}
149141

150142
private fun createSuspendFunctionType(

0 commit comments

Comments
 (0)