diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/state/KotlinTypeMapper.java b/compiler/backend/src/org/jetbrains/kotlin/codegen/state/KotlinTypeMapper.java index e54024be702..28fa6166b49 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/state/KotlinTypeMapper.java +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/state/KotlinTypeMapper.java @@ -1,5 +1,5 @@ /* - * Copyright 2010-2015 JetBrains s.r.o. + * Copyright 2010-2016 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -71,7 +71,6 @@ import org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmMethodGenericSignature; import org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmMethodParameterKind; import org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmMethodParameterSignature; import org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmMethodSignature; -import org.jetbrains.kotlin.serialization.deserialization.DeserializedType; import org.jetbrains.kotlin.serialization.deserialization.descriptors.DeserializedCallableMemberDescriptor; import org.jetbrains.kotlin.serialization.deserialization.descriptors.DeserializedClassDescriptor; import org.jetbrains.kotlin.types.*; @@ -450,14 +449,7 @@ public class KotlinTypeMapper { PsiElement declarationElement = DescriptorToSourceUtils.descriptorToDeclaration(descriptor); if (declarationElement == null) { - String message = "Error type encountered: %s (%s)."; - if (FlexibleTypesKt.upperIfFlexible(type) instanceof DeserializedType) { - message += - " One of the possible reasons may be that this type is not directly accessible from this module. " + - "To workaround this error, try adding an explicit dependency on the module or library which contains this type " + - "to the classpath"; - } - return String.format(message, type, type.getClass().getSimpleName()); + return String.format("Error type encountered: %s (%s).", type, type.getClass().getSimpleName()); } DeclarationDescriptor containingDeclaration = descriptor.getContainingDeclaration(); diff --git a/core/descriptors/src/org/jetbrains/kotlin/types/AbstractLazyType.kt b/core/descriptors/src/org/jetbrains/kotlin/types/AbstractLazyType.kt deleted file mode 100644 index b600593bd80..00000000000 --- a/core/descriptors/src/org/jetbrains/kotlin/types/AbstractLazyType.kt +++ /dev/null @@ -1,68 +0,0 @@ -/* - * Copyright 2010-2016 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.jetbrains.kotlin.types - -import org.jetbrains.kotlin.descriptors.ClassDescriptor -import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor -import org.jetbrains.kotlin.descriptors.annotations.Annotations -import org.jetbrains.kotlin.resolve.scopes.MemberScope -import org.jetbrains.kotlin.storage.StorageManager -import org.jetbrains.kotlin.storage.getValue - -abstract class AbstractLazyType(storageManager: StorageManager) : SimpleType(), LazyType { - - private val typeConstructor = storageManager.createLazyValue { computeTypeConstructor() } - override val constructor by typeConstructor - - protected abstract fun computeTypeConstructor(): TypeConstructor - - private val _arguments = storageManager.createLazyValue { computeArguments() } - override val arguments by _arguments - - protected abstract fun computeArguments(): List - - override val memberScope by storageManager.createLazyValue { computeMemberScope(constructor, arguments) } - - override val isMarkedNullable: Boolean get() = false - - override val isError: Boolean get() = constructor.declarationDescriptor?.let { d -> ErrorUtils.isError(d) } ?: false - - override val annotations: Annotations get() = Annotations.EMPTY - - // TODO: do not force resolution - override fun replaceAnnotations(newAnnotations: Annotations): SimpleType = KotlinTypeFactory.simpleType(this, annotations = newAnnotations) - override fun makeNullableAsSpecified(newNullability: Boolean): SimpleType = KotlinTypeFactory.simpleType(this, nullable = newNullability) - - override fun toString() = when { - !typeConstructor.isComputed() -> "[Not-computed]" - !_arguments.isComputed() -> - if (constructor.parameters.isEmpty()) constructor.toString() - else "$constructor" - else -> super.toString() - } - - companion object { - fun computeMemberScope(constructor: TypeConstructor, arguments: List): MemberScope { - val descriptor = constructor.declarationDescriptor - return when (descriptor) { - is TypeParameterDescriptor -> descriptor.getDefaultType().memberScope - is ClassDescriptor -> descriptor.getMemberScope(TypeConstructorSubstitution.create(constructor, arguments)) - else -> throw IllegalStateException("Unsupported classifier: $descriptor") - } - } - } -} diff --git a/core/descriptors/src/org/jetbrains/kotlin/types/ErrorUtils.java b/core/descriptors/src/org/jetbrains/kotlin/types/ErrorUtils.java index 95d86b7cc9a..cb8a36db968 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/types/ErrorUtils.java +++ b/core/descriptors/src/org/jetbrains/kotlin/types/ErrorUtils.java @@ -464,7 +464,8 @@ public class ErrorUtils { return false; } - public static boolean isError(@NotNull DeclarationDescriptor candidate) { + public static boolean isError(@Nullable DeclarationDescriptor candidate) { + if (candidate == null) return false; return isErrorClass(candidate) || isErrorClass(candidate.getContainingDeclaration()) || candidate == ERROR_MODULE; } diff --git a/core/deserialization/src/org/jetbrains/kotlin/serialization/deserialization/DeserializedType.kt b/core/deserialization/src/org/jetbrains/kotlin/serialization/deserialization/DeserializedType.kt deleted file mode 100644 index c71d14e7934..00000000000 --- a/core/deserialization/src/org/jetbrains/kotlin/serialization/deserialization/DeserializedType.kt +++ /dev/null @@ -1,66 +0,0 @@ -/* - * Copyright 2010-2016 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.jetbrains.kotlin.serialization.deserialization - -import org.jetbrains.kotlin.descriptors.annotations.AnnotationWithTarget -import org.jetbrains.kotlin.descriptors.annotations.Annotations -import org.jetbrains.kotlin.serialization.ProtoBuf -import org.jetbrains.kotlin.serialization.deserialization.descriptors.DeserializedAnnotationsWithPossibleTargets -import org.jetbrains.kotlin.types.AbstractLazyType -import org.jetbrains.kotlin.types.SimpleType -import org.jetbrains.kotlin.types.withAbbreviation -import org.jetbrains.kotlin.utils.toReadOnlyList - -class DeserializedType private constructor( - private val c: DeserializationContext, - private val typeProto: ProtoBuf.Type, - private val additionalAnnotations: Annotations = Annotations.EMPTY -) : AbstractLazyType(c.storageManager) { - override fun computeTypeConstructor() = c.typeDeserializer.typeConstructor(typeProto) - - override fun computeArguments() = - typeProto.collectAllArguments().mapIndexed { - index, proto -> - c.typeDeserializer.typeArgument(constructor.parameters.getOrNull(index), proto) - }.toReadOnlyList() - - private fun ProtoBuf.Type.collectAllArguments(): List = - argumentList + outerType(c.typeTable)?.collectAllArguments().orEmpty() - - override val annotations = DeserializedAnnotationsWithPossibleTargets(c.storageManager) { - c.components.annotationAndConstantLoader - .loadTypeAnnotations(typeProto, c.nameResolver) - .map { AnnotationWithTarget(it, null) } + additionalAnnotations.getAllAnnotations() - } - - override val isMarkedNullable: Boolean get() = typeProto.nullable - - companion object { - fun create( - c: DeserializationContext, - typeProto: ProtoBuf.Type, - additionalAnnotations: Annotations - ): SimpleType { - val deserializedType = DeserializedType(c, typeProto, additionalAnnotations) - val abbreviatedTypeProto = typeProto.abbreviatedType(c.typeTable) ?: return deserializedType - - return deserializedType.withAbbreviation(DeserializedType(c, abbreviatedTypeProto, additionalAnnotations)) - } - } -} - - diff --git a/core/deserialization/src/org/jetbrains/kotlin/serialization/deserialization/TypeDeserializer.kt b/core/deserialization/src/org/jetbrains/kotlin/serialization/deserialization/TypeDeserializer.kt index 4e72dce98bf..85c0af3beb6 100644 --- a/core/deserialization/src/org/jetbrains/kotlin/serialization/deserialization/TypeDeserializer.kt +++ b/core/deserialization/src/org/jetbrains/kotlin/serialization/deserialization/TypeDeserializer.kt @@ -17,8 +17,10 @@ package org.jetbrains.kotlin.serialization.deserialization import org.jetbrains.kotlin.descriptors.* +import org.jetbrains.kotlin.descriptors.annotations.AnnotationWithTarget import org.jetbrains.kotlin.descriptors.annotations.Annotations import org.jetbrains.kotlin.serialization.ProtoBuf +import org.jetbrains.kotlin.serialization.deserialization.descriptors.DeserializedAnnotationsWithPossibleTargets import org.jetbrains.kotlin.serialization.deserialization.descriptors.DeserializedTypeParameterDescriptor import org.jetbrains.kotlin.types.* import org.jetbrains.kotlin.utils.toReadOnlyList @@ -27,7 +29,7 @@ import java.util.* class TypeDeserializer( private val c: DeserializationContext, private val parent: TypeDeserializer?, - private val typeParameterProtos: List, + typeParameterProtos: List, private val debugName: String ) { private val classDescriptors: (Int) -> ClassDescriptor? = c.storageManager.createMemoizedFunctionWithNullableValues { @@ -38,7 +40,7 @@ class TypeDeserializer( fqNameIndex -> computeTypeAliasDescriptor(fqNameIndex) } - private val typeParameterDescriptors = c.storageManager.createLazyValue { + private val typeParameterDescriptors = if (typeParameterProtos.isEmpty()) { mapOf() } @@ -49,10 +51,9 @@ class TypeDeserializer( } result } - } val ownTypeParameters: List - get() = typeParameterDescriptors().values.toReadOnlyList() + get() = typeParameterDescriptors.values.toReadOnlyList() // TODO: don't load identical types from TypeTable more than once fun type(proto: ProtoBuf.Type, additionalAnnotations: Annotations = Annotations.EMPTY): KotlinType { @@ -66,10 +67,32 @@ class TypeDeserializer( return simpleType(proto, additionalAnnotations) } - fun simpleType(proto: ProtoBuf.Type, additionalAnnotations: Annotations = Annotations.EMPTY) - = DeserializedType.create(c, proto, additionalAnnotations) + fun simpleType(proto: ProtoBuf.Type, additionalAnnotations: Annotations = Annotations.EMPTY): SimpleType { + val constructor = typeConstructor(proto) + if (ErrorUtils.isError(constructor.declarationDescriptor)) { + return ErrorUtils.createErrorTypeWithCustomConstructor(constructor.toString(), constructor) + } - fun typeConstructor(proto: ProtoBuf.Type): TypeConstructor = + val annotations = DeserializedAnnotationsWithPossibleTargets(c.storageManager) { + c.components.annotationAndConstantLoader + .loadTypeAnnotations(proto, c.nameResolver) + .map { AnnotationWithTarget(it, null) } + additionalAnnotations.getAllAnnotations() + } + + fun ProtoBuf.Type.collectAllArguments(): List = + argumentList + outerType(c.typeTable)?.collectAllArguments().orEmpty() + + val arguments = proto.collectAllArguments().mapIndexed { index, proto -> + typeArgument(constructor.parameters.getOrNull(index), proto) + }.toReadOnlyList() + + val simpleType = KotlinTypeFactory.simpleType(annotations, constructor, arguments, proto.nullable) + + val abbreviatedTypeProto = proto.abbreviatedType(c.typeTable) ?: return simpleType + return simpleType.withAbbreviation(simpleType(abbreviatedTypeProto, additionalAnnotations)) + } + + private fun typeConstructor(proto: ProtoBuf.Type): TypeConstructor = when { proto.hasClassName() -> { classDescriptors(proto.className)?.typeConstructor @@ -77,27 +100,22 @@ class TypeDeserializer( } proto.hasTypeParameter() -> typeParameterTypeConstructor(proto.typeParameter) - ?: ErrorUtils.createErrorType("Unknown type parameter ${proto.typeParameter}").constructor + ?: ErrorUtils.createErrorTypeConstructor("Unknown type parameter ${proto.typeParameter}") proto.hasTypeParameterName() -> { val container = c.containingDeclaration - val typeParameters = when (container) { - is ClassifierDescriptorWithTypeParameters -> container.typeConstructor.parameters - is CallableDescriptor -> container.typeParameters - else -> emptyList() - } val name = c.nameResolver.getString(proto.typeParameterName) - val parameter = typeParameters.find { it.name.asString() == name } - parameter?.typeConstructor ?: ErrorUtils.createErrorType("Deserialized type parameter $name in $container").constructor + val parameter = ownTypeParameters.find { it.name.asString() == name } + parameter?.typeConstructor ?: ErrorUtils.createErrorTypeConstructor("Deserialized type parameter $name in $container") } proto.hasTypeAliasName() -> { typeAliasDescriptors(proto.typeAliasName)?.typeConstructor ?: TODO("not found type aliases") } - else -> ErrorUtils.createErrorType("Unknown type").constructor + else -> ErrorUtils.createErrorTypeConstructor("Unknown type") } private fun typeParameterTypeConstructor(typeParameterId: Int): TypeConstructor? = - typeParameterDescriptors().get(typeParameterId)?.typeConstructor ?: + typeParameterDescriptors.get(typeParameterId)?.typeConstructor ?: parent?.typeParameterTypeConstructor(typeParameterId) private fun computeClassDescriptor(fqNameIndex: Int): ClassDescriptor? { @@ -119,7 +137,7 @@ class TypeDeserializer( } } - fun typeArgument(parameter: TypeParameterDescriptor?, typeArgumentProto: ProtoBuf.Type.Argument): TypeProjection { + private fun typeArgument(parameter: TypeParameterDescriptor?, typeArgumentProto: ProtoBuf.Type.Argument): TypeProjection { if (typeArgumentProto.projection == ProtoBuf.Type.Argument.Projection.STAR) { return if (parameter == null) TypeBasedStarProjectionImpl(c.components.moduleDescriptor.builtIns.nullableAnyType)