Fixes and refactors

This commit is contained in:
Irene Dea
2021-10-21 13:22:51 -07:00
committed by Dmitriy Novozhilov
parent 2e2e70fede
commit 19bfc43bee
70 changed files with 263 additions and 327 deletions
@@ -500,6 +500,8 @@ interface TypeSystemContext : TypeSystemOptimizationContext {
fun KotlinTypeMarker.getAttributes(): List<AnnotationMarker>
fun KotlinTypeMarker.getCustomAttributes(): List<AnnotationMarker>
fun substitutionSupertypePolicy(type: SimpleTypeMarker): TypeCheckerState.SupertypesPolicy
fun KotlinTypeMarker.isTypeVariableType(): Boolean
@@ -281,7 +281,7 @@ class LazyJavaClassDescriptor(
else -> return null
}
return KotlinTypeFactory.simpleNotNullType(Annotations.EMPTY, classDescriptor, parametersAsTypeProjections)
return KotlinTypeFactory.simpleNotNullType(TypeAttributes.Empty, classDescriptor, parametersAsTypeProjections)
}
private fun getPurelyImplementsFqNameFromAnnotation(): FqName? {
@@ -119,8 +119,8 @@ class JavaTypeResolver(
attr: JavaTypeAttributes,
lowerResult: SimpleType?
): SimpleType? {
val annotations =
lowerResult?.annotations ?: LazyJavaAnnotations(c, javaType)
val attributes =
lowerResult?.attributes ?: LazyJavaAnnotations(c, javaType).toDefaultAttributes()
val constructor = computeTypeConstructor(javaType, attr) ?: return null
val isNullable = attr.isNullable()
@@ -130,7 +130,7 @@ class JavaTypeResolver(
val arguments = computeArguments(javaType, attr, constructor)
return KotlinTypeFactory.simpleType(annotations, constructor, arguments, isNullable)
return KotlinTypeFactory.simpleType(attributes, constructor, arguments, isNullable)
}
private fun computeTypeConstructor(javaType: JavaClassifierType, attr: JavaTypeAttributes): TypeConstructor? {
@@ -54,9 +54,6 @@ class RawTypeImpl private constructor(lowerBound: SimpleType, upperBound: Simple
return classDescriptor.getMemberScope(RawSubstitution())
}
override fun replaceAnnotations(newAnnotations: Annotations) =
RawTypeImpl(lowerBound.replaceAnnotations(newAnnotations), upperBound.replaceAnnotations(newAnnotations))
override fun replaceAttributes(newAttributes: TypeAttributes) =
RawTypeImpl(lowerBound.replaceAttributes(newAttributes), upperBound.replaceAttributes(newAttributes))
@@ -148,7 +145,7 @@ internal class RawSubstitution(typeParameterUpperBoundEraser: TypeParameterUpper
TypeProjectionImpl(componentTypeProjection.projectionKind, eraseType(componentTypeProjection.type, attr))
)
return KotlinTypeFactory.simpleType(
type.annotations, type.constructor, arguments, type.isMarkedNullable
type.attributes, type.constructor, arguments, type.isMarkedNullable
) to false
}
@@ -156,7 +153,7 @@ internal class RawSubstitution(typeParameterUpperBoundEraser: TypeParameterUpper
val memberScope = declaration.getMemberScope(this)
return KotlinTypeFactory.simpleTypeWithNonTrivialMemberScope(
type.annotations, declaration.typeConstructor,
type.attributes, declaration.typeConstructor,
declaration.typeConstructor.parameters.map { parameter ->
computeProjection(parameter, attr)
},
@@ -150,7 +150,7 @@ class JavaTypeEnhancement(private val javaResolverSettings: JavaResolverSettings
).compositeAnnotationsOrSingle()
val enhancedType = KotlinTypeFactory.simpleType(
newAnnotations,
newAnnotations.toDefaultAttributes(),
typeConstructor,
enhancedArguments.zip(arguments) { enhanced, original -> enhanced ?: original },
enhancedNullability ?: isMarkedNullable
@@ -252,7 +252,6 @@ internal class NotNullTypeParameterImpl(override val delegate: SimpleType) : Not
return NotNullTypeParameterImpl(result)
}
override fun replaceAnnotations(newAnnotations: Annotations) = NotNullTypeParameterImpl(delegate.replaceAnnotations(newAnnotations))
override fun replaceAttributes(newAttributes: TypeAttributes) = NotNullTypeParameterImpl(delegate.replaceAttributes(newAttributes))
override fun makeNullableAsSpecified(newNullability: Boolean) =
if (newNullability) delegate.makeNullableAsSpecified(true) else this
@@ -48,7 +48,9 @@ import org.jetbrains.kotlin.resolve.sam.SamConversionResolverImpl
import org.jetbrains.kotlin.serialization.deserialization.*
import org.jetbrains.kotlin.storage.LockBasedStorageManager
import org.jetbrains.kotlin.storage.StorageManager
import org.jetbrains.kotlin.types.DefaultTypeAttributeTranslator
import org.jetbrains.kotlin.types.checker.NewKotlinTypeChecker
import org.jetbrains.kotlin.types.extensions.TypeAttributeTranslatorsForInjection
// This class is needed only for easier injection: exact types of needed components are specified in the constructor here.
// Otherwise injector generator is not smart enough to deduce, for example, which package fragment provider DeserializationComponents needs
@@ -63,7 +65,8 @@ class DeserializationComponentsForJava(
errorReporter: ErrorReporter,
lookupTracker: LookupTracker,
contractDeserializer: ContractDeserializer,
kotlinTypeChecker: NewKotlinTypeChecker
kotlinTypeChecker: NewKotlinTypeChecker,
typeAttributeTranslators: TypeAttributeTranslatorsForInjection
) {
val components: DeserializationComponents
@@ -77,7 +80,8 @@ class DeserializationComponentsForJava(
additionalClassPartsProvider = jvmBuiltIns?.customizer ?: AdditionalClassPartsProvider.None,
platformDependentDeclarationFilter = jvmBuiltIns?.customizer ?: PlatformDependentDeclarationFilter.NoPlatformDependent,
extensionRegistryLite = JvmProtoBufUtil.EXTENSION_REGISTRY,
kotlinTypeChecker = kotlinTypeChecker, samConversionResolver = SamConversionResolverImpl(storageManager, emptyList())
kotlinTypeChecker = kotlinTypeChecker, samConversionResolver = SamConversionResolverImpl(storageManager, emptyList()),
typeAttributeTranslators = typeAttributeTranslators.translators
)
}
@@ -188,6 +192,7 @@ fun makeDeserializationComponentsForJava(
return DeserializationComponentsForJava(
storageManager, module, DeserializationConfiguration.Default, javaClassDataFinder,
binaryClassAnnotationAndConstantLoader, lazyJavaPackageFragmentProvider, notFoundClasses,
errorReporter, LookupTracker.DO_NOTHING, ContractDeserializer.DEFAULT, NewKotlinTypeChecker.Default
errorReporter, LookupTracker.DO_NOTHING, ContractDeserializer.DEFAULT, NewKotlinTypeChecker.Default,
TypeAttributeTranslatorsForInjection(listOf(DefaultTypeAttributeTranslator))
)
}
-1
View File
@@ -10,7 +10,6 @@ dependencies {
api(project(":core:util.runtime"))
api(kotlinStdlib())
api(project(":kotlin-annotations-jvm"))
api(project(":compiler:util"))
compileOnly(intellijCoreDep()) { includeJars("intellij-core") }
}
@@ -665,7 +665,10 @@ public abstract class KotlinBuiltIns {
@NotNull
public SimpleType getArrayType(@NotNull Variance projectionType, @NotNull KotlinType argument, @NotNull Annotations annotations) {
List<TypeProjectionImpl> types = Collections.singletonList(new TypeProjectionImpl(projectionType, argument));
return KotlinTypeFactory.simpleNotNullType(annotations, getArray(), types);
return KotlinTypeFactory.simpleNotNullType(
TypeAttributesKt.toDefaultAttributes(annotations),
getArray(),
types);
}
@NotNull
@@ -677,7 +680,7 @@ public abstract class KotlinBuiltIns {
public SimpleType getEnumType(@NotNull SimpleType argument) {
Variance projectionType = Variance.INVARIANT;
List<TypeProjectionImpl> types = Collections.singletonList(new TypeProjectionImpl(projectionType, argument));
return KotlinTypeFactory.simpleNotNullType(Annotations.Companion.getEMPTY(), getEnum(), types);
return KotlinTypeFactory.simpleNotNullType(TypeAttributes.Companion.getEmpty(), getEnum(), types);
}
@NotNull
@@ -53,7 +53,7 @@ class ReflectionTypes(module: ModuleDescriptor, private val notFoundClasses: Not
val kMutableProperty2: ClassDescriptor by ClassLookup(3)
fun getKClassType(annotations: Annotations, type: KotlinType, variance: Variance): KotlinType =
KotlinTypeFactory.simpleNotNullType(annotations, kClass, listOf(TypeProjectionImpl(variance, type)))
KotlinTypeFactory.simpleNotNullType(annotations.toDefaultAttributes(), kClass, listOf(TypeProjectionImpl(variance, type)))
fun getKFunctionType(
annotations: Annotations,
@@ -69,7 +69,7 @@ class ReflectionTypes(module: ModuleDescriptor, private val notFoundClasses: Not
getFunctionTypeArgumentProjections(receiverType, contextReceiverTypes, parameterTypes, parameterNames, returnType, builtIns)
val classDescriptor =
if (isSuspend) getKSuspendFunction(arguments.size - 1 /* return type */) else getKFunction(arguments.size - 1 /* return type */)
return KotlinTypeFactory.simpleNotNullType(annotations, classDescriptor, arguments)
return KotlinTypeFactory.simpleNotNullType(annotations.toDefaultAttributes(), classDescriptor, arguments)
}
fun getKPropertyType(annotations: Annotations, receiverTypes: List<KotlinType>, returnType: KotlinType, mutable: Boolean): SimpleType {
@@ -90,7 +90,7 @@ class ReflectionTypes(module: ModuleDescriptor, private val notFoundClasses: Not
}
val arguments = (receiverTypes + returnType).map(::TypeProjectionImpl)
return KotlinTypeFactory.simpleNotNullType(annotations, classDescriptor, arguments)
return KotlinTypeFactory.simpleNotNullType(annotations.toDefaultAttributes(), classDescriptor, arguments)
}
companion object {
@@ -184,7 +184,7 @@ class ReflectionTypes(module: ModuleDescriptor, private val notFoundClasses: Not
fun createKPropertyStarType(module: ModuleDescriptor): KotlinType? {
val kPropertyClass = module.findClassAcrossModuleDependencies(StandardNames.FqNames.kProperty) ?: return null
return KotlinTypeFactory.simpleNotNullType(Annotations.EMPTY, kPropertyClass,
return KotlinTypeFactory.simpleNotNullType(TypeAttributes.Empty, kPropertyClass,
listOf(StarProjectionImpl(kPropertyClass.typeConstructor.parameters.single())))
}
@@ -257,7 +257,7 @@ fun createFunctionType(
if (contextReceiverTypes.isNotEmpty()) typeAnnotations =
typeAnnotations.withContextReceiversFunctionAnnotation(builtIns, contextReceiverTypes.size)
return KotlinTypeFactory.simpleNotNullType(typeAnnotations, classDescriptor, arguments)
return KotlinTypeFactory.simpleNotNullType(typeAnnotations.toDefaultAttributes(), classDescriptor, arguments)
}
fun Annotations.hasExtensionFunctionAnnotation() = hasAnnotation(StandardNames.FqNames.extensionFunctionType)
@@ -114,7 +114,7 @@ class FunctionClassDescriptor(
TypeProjectionImpl(it.defaultType)
}
KotlinTypeFactory.simpleNotNullType(Annotations.EMPTY, descriptor, arguments)
KotlinTypeFactory.simpleNotNullType(TypeAttributes.Empty, descriptor, arguments)
}.toList()
}
@@ -49,7 +49,7 @@ fun transformSuspendFunctionToRuntimeFunctionType(suspendFunType: KotlinType): S
suspendFunType.getContextReceiverTypesFromFunctionType(),
suspendFunType.getValueParameterTypesFromFunctionType().map(TypeProjection::getType) +
KotlinTypeFactory.simpleType(
Annotations.EMPTY,
TypeAttributes.Empty,
// Continuation interface is not a part of built-ins anymore, it has been moved to stdlib.
// While it must be somewhere in the dependencies, but here we don't have a reference to the module,
// and it's rather complicated to inject it by now, so we just use a fake class descriptor.
@@ -71,7 +71,7 @@ public abstract class AbstractTypeParameterDescriptor extends DeclarationDescrip
@Override
public SimpleType invoke() {
return KotlinTypeFactory.simpleTypeWithNonTrivialMemberScope(
Annotations.Companion.getEMPTY(),
TypeAttributes.Companion.getEmpty(),
getTypeConstructor(), Collections.<TypeProjection>emptyList(), false,
new LazyScopeAdapter(
new Function0<MemberScope>() {
@@ -143,7 +143,7 @@ public class LazySubstitutingClassDescriptor extends ModuleAwareClassDescriptor
public SimpleType getDefaultType() {
List<TypeProjection> typeProjections = TypeUtils.getDefaultTypeProjections(getTypeConstructor().getParameters());
return KotlinTypeFactory.simpleTypeWithNonTrivialMemberScope(
getAnnotations(),
DefaultTypeAttributeTranslator.INSTANCE.toAttributes(getAnnotations(), null, null),
getTypeConstructor(),
typeProjections,
false,
@@ -77,6 +77,10 @@ class CapturedType(
override val isMarkedNullable: Boolean = false,
override val attributes: TypeAttributes = TypeAttributes.Empty
) : SimpleType(), SubtypingRepresentatives, CapturedTypeMarker {
override val annotations: Annotations
get() = attributes.toDefaultAnnotations()
override val arguments: List<TypeProjection>
get() = listOf()
@@ -103,9 +107,6 @@ class CapturedType(
return CapturedType(typeProjection, constructor, newNullability, attributes)
}
override fun replaceAnnotations(newAnnotations: Annotations): CapturedType =
CapturedType(typeProjection, constructor, isMarkedNullable, newAnnotations.toDefaultAttributes())
override fun replaceAttributes(newAttributes: TypeAttributes): SimpleType =
CapturedType(typeProjection, constructor, isMarkedNullable, newAttributes)
@@ -21,10 +21,7 @@ import org.jetbrains.kotlin.builtins.StandardNames
import org.jetbrains.kotlin.descriptors.ModuleDescriptor
import org.jetbrains.kotlin.descriptors.annotations.Annotations
import org.jetbrains.kotlin.descriptors.findClassAcrossModuleDependencies
import org.jetbrains.kotlin.types.ErrorUtils
import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.types.KotlinTypeFactory
import org.jetbrains.kotlin.types.TypeUtils
import org.jetbrains.kotlin.types.*
interface CompileTimeConstant<out T> {
val isError: Boolean
@@ -200,7 +197,7 @@ class IntegerValueTypeConstant(
}
val unknownIntegerType = KotlinTypeFactory.simpleTypeWithNonTrivialMemberScope(
Annotations.EMPTY, typeConstructor, emptyList(), false,
TypeAttributes.Empty, typeConstructor, emptyList(), false,
ErrorUtils.createErrorScope("Scope for number value type ($typeConstructor)", true)
)
@@ -60,7 +60,7 @@ class IntegerLiteralTypeConstructor : TypeConstructor {
Mode.INTERSECTION_TYPE -> left.possibleTypes union right.possibleTypes
}
val constructor = IntegerLiteralTypeConstructor(left.value, left.module, possibleTypes)
return KotlinTypeFactory.integerLiteralType(Annotations.EMPTY, constructor, false)
return KotlinTypeFactory.integerLiteralType(TypeAttributes.Empty, constructor, false)
}
private fun fold(left: IntegerLiteralTypeConstructor, right: SimpleType): SimpleType? =
@@ -127,7 +127,7 @@ class IntegerLiteralTypeConstructor : TypeConstructor {
this.possibleTypes = possibleTypes
}
private val type = KotlinTypeFactory.integerLiteralType(Annotations.EMPTY, this, false)
private val type = KotlinTypeFactory.integerLiteralType(TypeAttributes.Empty, this, false)
private fun isContainsOnlyUnsignedTypes(): Boolean = module.allSignedLiteralTypes.all { it !in possibleTypes }
@@ -180,7 +180,7 @@ class KClassValue(value: Value) : ConstantValue<KClassValue.Value>(value) {
constructor(classId: ClassId, arrayDimensions: Int) : this(ClassLiteralValue(classId, arrayDimensions))
override fun getType(module: ModuleDescriptor): KotlinType =
KotlinTypeFactory.simpleNotNullType(Annotations.EMPTY, module.builtIns.kClass, listOf(TypeProjectionImpl(getArgumentType(module))))
KotlinTypeFactory.simpleNotNullType(TypeAttributes.Empty, module.builtIns.kClass, listOf(TypeProjectionImpl(getArgumentType(module))))
fun getArgumentType(module: ModuleDescriptor): KotlinType {
when (value) {
@@ -0,0 +1,37 @@
/*
* Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.types
import org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptor
import org.jetbrains.kotlin.descriptors.annotations.Annotations
import kotlin.reflect.KClass
class AnnotationsTypeAttribute(val annotations: Annotations) : TypeAttribute<AnnotationsTypeAttribute>() {
constructor(annotations: List<AnnotationDescriptor>) : this(Annotations.create(annotations))
override fun union(other: AnnotationsTypeAttribute?): AnnotationsTypeAttribute? = null
override fun intersect(other: AnnotationsTypeAttribute?): AnnotationsTypeAttribute? = null
override fun add(other: AnnotationsTypeAttribute?): AnnotationsTypeAttribute {
if (other == null) return this
return AnnotationsTypeAttribute(Annotations.create(annotations + other.annotations))
}
override fun isSubtypeOf(other: AnnotationsTypeAttribute?): Boolean = true
override val key: KClass<out AnnotationsTypeAttribute>
get() = AnnotationsTypeAttribute::class
override fun equals(other: Any?): Boolean {
if (other !is AnnotationsTypeAttribute) return false
return annotations == other.annotations
}
}
val TypeAttributes.annotationsAttribute: AnnotationsTypeAttribute? by TypeAttributes.attributeAccessor<AnnotationsTypeAttribute>()
val TypeAttributes.annotations: Annotations get() = annotationsAttribute?.annotations ?: Annotations.EMPTY
@@ -1,34 +0,0 @@
/*
* Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.types
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptor
import org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptorImpl
import org.jetbrains.kotlin.descriptors.annotations.Annotations
import kotlin.reflect.KClass
class CustomAnnotationTypeAttribute(val annotations: Annotations) : TypeAttribute<CustomAnnotationTypeAttribute>() {
constructor(annotations: List<AnnotationDescriptor>) : this(Annotations.create(annotations))
override fun union(other: CustomAnnotationTypeAttribute?): CustomAnnotationTypeAttribute? = null
override fun intersect(other: CustomAnnotationTypeAttribute?): CustomAnnotationTypeAttribute? = null
override fun add(other: CustomAnnotationTypeAttribute?): CustomAnnotationTypeAttribute {
if (other == null) return this
return CustomAnnotationTypeAttribute(Annotations.create(annotations + other.annotations))
}
override fun isSubtypeOf(other: CustomAnnotationTypeAttribute?): Boolean = true
override val key: KClass<out CustomAnnotationTypeAttribute>
get() = CustomAnnotationTypeAttribute::class
}
val TypeAttributes.custom: CustomAnnotationTypeAttribute? by TypeAttributes.attributeAccessor<CustomAnnotationTypeAttribute>()
val TypeAttributes.customAnnotations: Annotations get() = custom?.annotations ?: Annotations.EMPTY
@@ -36,8 +36,6 @@ open class ErrorType @JvmOverloads internal constructor(
override fun toString(): String =
constructor.toString() + if (arguments.isEmpty()) "" else arguments.joinToString(", ", "<", ">", -1, "...", null)
override fun replaceAnnotations(newAnnotations: Annotations): SimpleType = this
override fun replaceAttributes(newAttributes: TypeAttributes): SimpleType = this
override fun makeNullableAsSpecified(newNullability: Boolean): SimpleType =
@@ -77,7 +77,7 @@ class IntersectionTypeConstructor(typesToIntersect: Collection<KotlinType>) : Ty
@OptIn(TypeRefinement::class)
fun createType(): SimpleType =
KotlinTypeFactory.simpleTypeWithNonTrivialMemberScope(
Annotations.EMPTY, this, listOf(), false, this.createScopeForKotlinType()
TypeAttributes.Empty, this, listOf(), false, this.createScopeForKotlinType()
) { kotlinTypeRefiner ->
this.refine(kotlinTypeRefiner).createType()
}
@@ -52,8 +52,6 @@ sealed class KotlinType : Annotated, KotlinTypeMarker {
abstract val isMarkedNullable: Boolean
abstract val memberScope: MemberScope
abstract val attributes: TypeAttributes
override val annotations: Annotations
get() = attributes.toDefaultAnnotations()
abstract fun unwrap(): UnwrappedType
@@ -163,7 +161,6 @@ abstract class WrappedType : KotlinType() {
* todo: specify what happens with internal structure when we apply some [TypeSubstitutor]
*/
sealed class UnwrappedType : KotlinType() {
abstract fun replaceAnnotations(newAnnotations: Annotations): UnwrappedType
abstract fun replaceAttributes(newAttributes: TypeAttributes): UnwrappedType
abstract fun makeNullableAsSpecified(newNullability: Boolean): UnwrappedType
@@ -179,7 +176,6 @@ sealed class UnwrappedType : KotlinType() {
* Or more precisely, all instances are subclasses of [SimpleType] or [WrappedType] (which contains [SimpleType] inside).
*/
abstract class SimpleType : UnwrappedType(), SimpleTypeMarker, TypeArgumentListMarker {
abstract override fun replaceAnnotations(newAnnotations: Annotations): SimpleType
abstract override fun replaceAttributes(newAttributes: TypeAttributes): SimpleType
abstract override fun makeNullableAsSpecified(newNullability: Boolean): SimpleType
@@ -65,31 +65,6 @@ object KotlinTypeFactory {
}
}
@JvmStatic
@JvmOverloads
@OptIn(TypeRefinement::class)
fun simpleType(
annotations: Annotations,
constructor: TypeConstructor,
arguments: List<TypeProjection>,
nullable: Boolean,
kotlinTypeRefiner: KotlinTypeRefiner? = null
): SimpleType {
if (annotations.isEmpty() && arguments.isEmpty() && !nullable && constructor.declarationDescriptor != null) {
return constructor.declarationDescriptor!!.defaultType
}
return simpleTypeWithNonTrivialMemberScope(
annotations, constructor, arguments, nullable,
computeMemberScope(constructor, arguments, kotlinTypeRefiner)
) f@{ refiner ->
val expandedTypeOrRefinedConstructor = refineConstructor(constructor, refiner, arguments) ?: return@f null
expandedTypeOrRefinedConstructor.expandedType?.let { return@f it }
simpleType(annotations, expandedTypeOrRefinedConstructor.refinedConstructor!!, arguments, nullable, refiner)
}
}
@JvmStatic
@JvmOverloads
@OptIn(TypeRefinement::class)
@@ -118,7 +93,7 @@ object KotlinTypeFactory {
@JvmStatic
fun TypeAliasDescriptor.computeExpandedType(arguments: List<TypeProjection>): SimpleType {
return TypeAliasExpander(TypeAliasExpansionReportStrategy.DO_NOTHING, false).expand(
TypeAliasExpansion.create(null, this, arguments), Annotations.EMPTY
TypeAliasExpansion.create(null, this, arguments), TypeAttributes.Empty
)
}
@@ -141,33 +116,6 @@ object KotlinTypeFactory {
private class ExpandedTypeOrRefinedConstructor(val expandedType: SimpleType?, val refinedConstructor: TypeConstructor?)
@JvmStatic
@OptIn(TypeRefinement::class)
fun simpleTypeWithNonTrivialMemberScope(
annotations: Annotations,
constructor: TypeConstructor,
arguments: List<TypeProjection>,
nullable: Boolean,
memberScope: MemberScope
): SimpleType =
SimpleTypeImpl(constructor, arguments, nullable, memberScope) { kotlinTypeRefiner ->
val expandedTypeOrRefinedConstructor = refineConstructor(constructor, kotlinTypeRefiner, arguments) ?: return@SimpleTypeImpl null
expandedTypeOrRefinedConstructor.expandedType?.let { return@SimpleTypeImpl it }
simpleTypeWithNonTrivialMemberScope(
annotations,
expandedTypeOrRefinedConstructor.refinedConstructor!!,
arguments,
nullable,
memberScope
)
}.let {
if (annotations.isEmpty())
it
else
SimpleTypeWithAttributes(it, annotations.toDefaultAttributes())
}
@JvmStatic
@OptIn(TypeRefinement::class)
fun simpleTypeWithNonTrivialMemberScope(
@@ -195,23 +143,6 @@ object KotlinTypeFactory {
SimpleTypeWithAttributes(it, attributes)
}
@JvmStatic
fun simpleTypeWithNonTrivialMemberScope(
annotations: Annotations,
constructor: TypeConstructor,
arguments: List<TypeProjection>,
nullable: Boolean,
memberScope: MemberScope,
refinedTypeFactory: RefinedTypeFactory
): SimpleType =
SimpleTypeImpl(constructor, arguments, nullable, memberScope, refinedTypeFactory)
.let {
if (annotations.isEmpty())
it
else
SimpleTypeWithAttributes(it, annotations.toDefaultAttributes())
}
@JvmStatic
fun simpleTypeWithNonTrivialMemberScope(
attributes: TypeAttributes,
@@ -229,13 +160,6 @@ object KotlinTypeFactory {
SimpleTypeWithAttributes(it, attributes)
}
@JvmStatic
fun simpleNotNullType(
annotations: Annotations,
descriptor: ClassDescriptor,
arguments: List<TypeProjection>
): SimpleType = simpleType(annotations, descriptor.typeConstructor, arguments, nullable = false)
@JvmStatic
fun simpleNotNullType(
attributes: TypeAttributes,
@@ -246,7 +170,7 @@ object KotlinTypeFactory {
@JvmStatic
fun simpleType(
baseType: SimpleType,
annotations: Annotations = baseType.annotations,
annotations: TypeAttributes = baseType.attributes,
constructor: TypeConstructor = baseType.constructor,
arguments: List<TypeProjection> = baseType.arguments,
nullable: Boolean = baseType.isMarkedNullable
@@ -260,11 +184,11 @@ object KotlinTypeFactory {
@JvmStatic
fun integerLiteralType(
annotations: Annotations,
attributes: TypeAttributes,
constructor: IntegerLiteralTypeConstructor,
nullable: Boolean
): SimpleType = simpleTypeWithNonTrivialMemberScope(
annotations,
attributes,
constructor,
emptyList(),
nullable,
@@ -290,12 +214,6 @@ private class SimpleTypeImpl(
this
else SimpleTypeWithAttributes(this, newAttributes)
override fun replaceAnnotations(newAnnotations: Annotations) =
if (newAnnotations.isEmpty())
this
else
SimpleTypeWithAttributes(this, attributes.replaceAnnotations(newAnnotations))
override fun makeNullableAsSpecified(newNullability: Boolean) = when {
newNullability == isMarkedNullable -> this
newNullability -> NullableSimpleType(this)
@@ -322,9 +240,6 @@ class SupposititiousSimpleType(private val realType: SimpleType, val overwritten
else SupposititiousSimpleType(newType, overwrittenClass)
}
override fun replaceAnnotations(newAnnotations: Annotations): SimpleType =
maybeWrap(realType.replaceAnnotations(newAnnotations))
override fun replaceAttributes(newAttributes: TypeAttributes): SimpleType =
maybeWrap(realType.replaceAttributes(newAttributes))
@@ -350,15 +265,9 @@ abstract class DelegatingSimpleTypeImpl(override val delegate: SimpleType) : Del
else
this
override fun replaceAnnotations(newAnnotations: Annotations) =
if (newAnnotations !== annotations)
SimpleTypeWithAttributes(this, attributes.replaceAnnotations(newAnnotations))
else
this
override fun makeNullableAsSpecified(newNullability: Boolean): SimpleType {
if (newNullability == isMarkedNullable) return this
return delegate.makeNullableAsSpecified(newNullability).replaceAnnotations(annotations)
return delegate.makeNullableAsSpecified(newNullability).replaceAttributes(attributes)
}
}
@@ -366,6 +275,8 @@ private class SimpleTypeWithAttributes(
delegate: SimpleType,
override val attributes: TypeAttributes
) : DelegatingSimpleTypeImpl(delegate) {
override val annotations: Annotations
get() = attributes.toDefaultAnnotations()
@TypeRefinement
override fun replaceDelegate(delegate: SimpleType) = SimpleTypeWithAttributes(delegate, attributes)
}
@@ -48,9 +48,6 @@ abstract class DelegatingSimpleType : SimpleType() {
class AbbreviatedType(override val delegate: SimpleType, val abbreviation: SimpleType) : DelegatingSimpleType() {
val expandedType: SimpleType get() = delegate
override fun replaceAnnotations(newAnnotations: Annotations) =
AbbreviatedType(delegate.replaceAnnotations(newAnnotations), abbreviation)
override fun replaceAttributes(newAttributes: TypeAttributes): SimpleType =
AbbreviatedType(delegate.replaceAttributes(newAttributes), abbreviation)
@@ -172,9 +169,6 @@ class DefinitelyNotNullType private constructor(
override fun substitutionResult(replacement: KotlinType): KotlinType =
replacement.unwrap().makeDefinitelyNotNullOrNotNull(useCorrectedNullabilityForTypeParameters)
override fun replaceAnnotations(newAnnotations: Annotations): DefinitelyNotNullType =
DefinitelyNotNullType(delegate.replaceAnnotations(newAnnotations), useCorrectedNullabilityForTypeParameters)
override fun replaceAttributes(newAttributes: TypeAttributes): SimpleType =
DefinitelyNotNullType(delegate.replaceAttributes(newAttributes), useCorrectedNullabilityForTypeParameters)
@@ -64,8 +64,6 @@ abstract class AbstractStubType(val originalTypeVariable: TypeConstructor, overr
override val attributes: TypeAttributes
get() = TypeAttributes.Empty
override fun replaceAnnotations(newAnnotations: Annotations): SimpleType = this
override fun replaceAttributes(newAttributes: TypeAttributes): SimpleType = this
override fun makeNullableAsSpecified(newNullability: Boolean): SimpleType {
@@ -10,7 +10,6 @@ import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor
import org.jetbrains.kotlin.descriptors.annotations.Annotations
import org.jetbrains.kotlin.descriptors.annotations.composeAnnotations
import org.jetbrains.kotlin.resolve.scopes.MemberScope
import org.jetbrains.kotlin.types.checker.KotlinTypeChecker
import org.jetbrains.kotlin.types.typeUtil.containsTypeAliasParameters
import org.jetbrains.kotlin.types.typeUtil.requiresTypeAliasExpansion
@@ -19,21 +18,21 @@ class TypeAliasExpander(
private val shouldCheckBounds: Boolean
) {
fun expand(typeAliasExpansion: TypeAliasExpansion, annotations: Annotations) =
fun expand(typeAliasExpansion: TypeAliasExpansion, attributes: TypeAttributes) =
expandRecursively(
typeAliasExpansion, annotations,
typeAliasExpansion, attributes,
isNullable = false, recursionDepth = 0, withAbbreviatedType = true
)
fun expandWithoutAbbreviation(typeAliasExpansion: TypeAliasExpansion, annotations: Annotations) =
fun expandWithoutAbbreviation(typeAliasExpansion: TypeAliasExpansion, attributes: TypeAttributes) =
expandRecursively(
typeAliasExpansion, annotations,
typeAliasExpansion, attributes,
isNullable = false, recursionDepth = 0, withAbbreviatedType = false
)
private fun expandRecursively(
typeAliasExpansion: TypeAliasExpansion,
annotations: Annotations,
attributes: TypeAttributes,
isNullable: Boolean,
recursionDepth: Int,
withAbbreviatedType: Boolean
@@ -51,19 +50,19 @@ class TypeAliasExpander(
"Type alias expansion: result for ${typeAliasExpansion.descriptor} is ${expandedProjection.projectionKind}, should be invariant"
}
checkRepeatedAnnotations(expandedType.annotations, annotations)
checkRepeatedAnnotations(expandedType.annotations, attributes.annotations)
val expandedTypeWithExtraAnnotations =
expandedType.combineAnnotations(annotations).let { TypeUtils.makeNullableIfNeeded(it, isNullable) }
expandedType.combineAttributes(attributes).let { TypeUtils.makeNullableIfNeeded(it, isNullable) }
return if (withAbbreviatedType)
expandedTypeWithExtraAnnotations.withAbbreviation(typeAliasExpansion.createAbbreviation(annotations, isNullable))
expandedTypeWithExtraAnnotations.withAbbreviation(typeAliasExpansion.createAbbreviation(attributes, isNullable))
else
expandedTypeWithExtraAnnotations
}
private fun TypeAliasExpansion.createAbbreviation(annotations: Annotations, isNullable: Boolean) =
private fun TypeAliasExpansion.createAbbreviation(attributes: TypeAttributes, isNullable: Boolean) =
KotlinTypeFactory.simpleTypeWithNonTrivialMemberScope(
annotations,
attributes,
descriptor.typeConstructor,
arguments,
isNullable,
@@ -125,23 +124,23 @@ class TypeAliasExpander(
val substitutedType =
if (argumentType is DynamicType)
argumentType.combineAnnotations(underlyingType.annotations)
argumentType.combineAttributes(underlyingType.attributes)
else
argumentType.asSimpleType().combineNullabilityAndAnnotations(underlyingType)
return TypeProjectionImpl(resultingVariance, substitutedType)
}
private fun DynamicType.combineAnnotations(newAnnotations: Annotations): DynamicType =
replaceAnnotations(createCombinedAnnotations(newAnnotations))
private fun DynamicType.combineAttributes(newAttributes: TypeAttributes): DynamicType =
replaceAttributes(createdCombinedAttributes(newAttributes))
private fun SimpleType.combineAnnotations(newAnnotations: Annotations): SimpleType =
if (isError) this else replace(newAnnotations = createCombinedAnnotations(newAnnotations))
private fun SimpleType.combineAttributes(newAttributes: TypeAttributes): SimpleType =
if (isError) this else replaceAttributes(newAttributes = createdCombinedAttributes(newAttributes))
private fun KotlinType.createCombinedAnnotations(newAnnotations: Annotations): Annotations {
if (isError) return annotations
private fun KotlinType.createdCombinedAttributes(newAttributes: TypeAttributes): TypeAttributes {
if (isError) return attributes
return composeAnnotations(newAnnotations, annotations)
return newAttributes.add(attributes)
}
private fun checkRepeatedAnnotations(existingAnnotations: Annotations, newAnnotations: Annotations) {
@@ -158,7 +157,7 @@ class TypeAliasExpander(
TypeUtils.makeNullableIfNeeded(this, fromType.isMarkedNullable)
private fun SimpleType.combineNullabilityAndAnnotations(fromType: KotlinType) =
combineNullability(fromType).combineAnnotations(fromType.annotations)
combineNullability(fromType).combineAttributes(fromType.attributes)
private fun expandNonArgumentTypeProjection(
originalProjection: TypeProjection,
@@ -201,7 +200,7 @@ class TypeAliasExpander(
TypeAliasExpansion.create(typeAliasExpansion, typeDescriptor, expandedArguments)
val nestedExpandedType = expandRecursively(
nestedExpansion, type.annotations,
nestedExpansion, type.attributes,
isNullable = type.isMarkedNullable,
recursionDepth = recursionDepth + 1,
withAbbreviatedType = false
@@ -5,19 +5,29 @@
package org.jetbrains.kotlin.types
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
import org.jetbrains.kotlin.descriptors.annotations.Annotations
interface TypeAttributeTranslator {
fun toAttributes(annotations: Annotations): TypeAttributes
fun toAttributes(
annotations: Annotations,
typeConstructor: TypeConstructor? = null,
containingDeclaration: DeclarationDescriptor? = null
): TypeAttributes
fun toAnnotations(attributes: TypeAttributes): Annotations
}
object DefaultTypeAttributeTranslator : TypeAttributeTranslator {
override fun toAnnotations(attributes: TypeAttributes): Annotations {
return attributes.customAnnotations
override fun toAttributes(
annotations: Annotations,
typeConstructor: TypeConstructor?,
containingDeclaration: DeclarationDescriptor?
): TypeAttributes {
return TypeAttributes.create(listOf(AnnotationsTypeAttribute(annotations)))
}
override fun toAttributes(annotations: Annotations): TypeAttributes {
return TypeAttributes.create(listOf(CustomAnnotationTypeAttribute(annotations)))
override fun toAnnotations(attributes: TypeAttributes): Annotations {
return attributes.annotations
}
}
@@ -41,9 +41,7 @@ class TypeAttributes private constructor(attributes: List<TypeAttribute<*>>) : A
return generateNullableAccessor<TypeAttribute<*>, T>(T::class) as ReadOnlyProperty<TypeAttributes, T?>
}
val Empty: TypeAttributes = TypeAttributes(emptyList())
private fun TypeAttribute<*>.predefined(): Pair<TypeAttribute<*>, TypeAttributes> = this to TypeAttributes(this)
val Empty: TypeAttributes = TypeAttributes(listOf(AnnotationsTypeAttribute(Annotations.EMPTY)))
fun create(attributes: List<TypeAttribute<*>>): TypeAttributes {
return if (attributes.isEmpty()) {
@@ -119,6 +117,6 @@ fun TypeAttributes.toDefaultAnnotations(): Annotations =
fun Annotations.toDefaultAttributes(): TypeAttributes = DefaultTypeAttributeTranslator.toAttributes(this)
fun TypeAttributes.replaceAnnotations(newAnnotations: Annotations): TypeAttributes {
val withoutCustom = (custom?.let { this.remove(it) } ?: this)
val withoutCustom = (annotationsAttribute?.let { this.remove(it) } ?: this)
return withoutCustom.add(newAnnotations.toDefaultAttributes())
}
@@ -160,12 +160,14 @@ fun SimpleType.replace(
): SimpleType {
if (newArguments.isEmpty() && newAnnotations === annotations) return this
val newAttributes = attributes.replaceAnnotations(newAnnotations)
if (newArguments.isEmpty()) {
return replaceAnnotations(newAnnotations)
return replaceAttributes(newAttributes)
}
return KotlinTypeFactory.simpleType(
newAnnotations,
newAttributes,
constructor,
newArguments,
isMarkedNullable
@@ -46,12 +46,6 @@ public class TypeUtils {
throw new IllegalStateException(name);
}
@NotNull
@Override
public SimpleType replaceAnnotations(@NotNull Annotations newAnnotations) {
throw new IllegalStateException(name);
}
@NotNull
@Override
public SimpleType replaceAttributes(@NotNull TypeAttributes newAttributes) {
@@ -227,7 +221,7 @@ public class TypeUtils {
) {
List<TypeProjection> arguments = getDefaultTypeProjections(typeConstructor.getParameters());
return KotlinTypeFactory.simpleTypeWithNonTrivialMemberScope(
Annotations.Companion.getEMPTY(),
TypeAttributes.Companion.getEmpty(),
typeConstructor,
arguments,
false,
@@ -118,7 +118,7 @@ fun TypeProjection.substitute(doSubstitute: (KotlinType) -> KotlinType): TypePro
fun KotlinType.replaceAnnotations(newAnnotations: Annotations): KotlinType {
if (annotations.isEmpty() && newAnnotations.isEmpty()) return this
return unwrap().replaceAnnotations(newAnnotations)
return unwrap().replaceAttributes(attributes.replaceAnnotations(newAnnotations))
}
fun KotlinTypeChecker.equalTypesOrNulls(type1: KotlinType?, type2: KotlinType?): Boolean {
@@ -34,9 +34,6 @@ class SimpleTypeWithEnhancement(
override val origin get() = delegate
override fun replaceAnnotations(newAnnotations: Annotations): SimpleType =
origin.replaceAnnotations(newAnnotations).wrapEnhancement(enhancement) as SimpleType
override fun replaceAttributes(newAttributes: TypeAttributes): SimpleType =
origin.replaceAttributes(newAttributes).wrapEnhancement(enhancement) as SimpleType
@@ -64,9 +61,6 @@ class FlexibleTypeWithEnhancement(
) : FlexibleType(origin.lowerBound, origin.upperBound),
TypeWithEnhancement {
override fun replaceAnnotations(newAnnotations: Annotations): UnwrappedType =
origin.replaceAnnotations(newAnnotations).wrapEnhancement(enhancement)
override fun replaceAttributes(newAttributes: TypeAttributes): UnwrappedType =
origin.replaceAttributes(newAttributes).wrapEnhancement(enhancement)
@@ -641,17 +641,15 @@ interface ClassicTypeSystemContext : TypeSystemInferenceExtensionContext, TypeSy
override fun unionTypeAttributes(types: List<KotlinTypeMarker>): List<AnnotationMarker> {
@Suppress("UNCHECKED_CAST")
types as List<KotlinType>
return types.map { it.unwrap().attributes }.reduce { x, y -> x.union(y) }.toList()
return (types as List<KotlinType>).map { it.unwrap().attributes }.reduce { x, y -> x.union(y) }.toList()
}
override fun KotlinTypeMarker.replaceTypeAttributes(newAttributes: List<AnnotationMarker>): KotlinTypeMarker {
require(this is KotlinType)
val typeAttributes = newAttributes.filterIsInstance<TypeAttribute<*>>()
require(typeAttributes.size == newAttributes.size)
if (newAttributes.isEmpty()) return this
@Suppress("UNCHECKED_CAST")
return this.unwrap().replaceAttributes(
TypeAttributes.create(typeAttributes)
TypeAttributes.create(newAttributes as List<TypeAttribute<*>>)
)
}
@@ -675,6 +673,11 @@ interface ClassicTypeSystemContext : TypeSystemInferenceExtensionContext, TypeSy
return this.attributes.toList()
}
override fun KotlinTypeMarker.getCustomAttributes(): List<AnnotationMarker> {
require(this is KotlinType, this::errorMessage)
return this.attributes.filterNot { it is AnnotationsTypeAttribute }
}
override fun captureFromExpression(type: KotlinTypeMarker): KotlinTypeMarker? {
return captureFromExpressionInternal(type as UnwrappedType)
}
@@ -36,7 +36,7 @@ abstract class KotlinTypePreparator : AbstractTypePreparator() {
val newConstructor =
IntersectionTypeConstructor(constructor.supertypes.map { TypeUtils.makeNullableAsSpecified(it, type.isMarkedNullable) })
return KotlinTypeFactory.simpleTypeWithNonTrivialMemberScope(
type.annotations,
type.attributes,
newConstructor,
listOf(),
false,
@@ -144,7 +144,7 @@ private fun captureFromArguments(type: UnwrappedType, status: CaptureStatus): Un
}
private fun UnwrappedType.replaceArguments(arguments: List<TypeProjection>) =
KotlinTypeFactory.simpleType(annotations, constructor, arguments, isMarkedNullable)
KotlinTypeFactory.simpleType(attributes, constructor, arguments, isMarkedNullable)
private fun captureArguments(type: UnwrappedType, status: CaptureStatus): List<TypeProjection>? {
if (type.arguments.size != type.constructor.parameters.size) return null
@@ -207,14 +207,14 @@ class NewCapturedType(
captureStatus: CaptureStatus, lowerType: UnwrappedType?, projection: TypeProjection, typeParameter: TypeParameterDescriptor
) : this(captureStatus, NewCapturedTypeConstructor(projection, typeParameter = typeParameter), lowerType)
override val annotations: Annotations
get() = attributes.toDefaultAnnotations()
override val arguments: List<TypeProjection> get() = listOf()
override val memberScope: MemberScope // todo what about foo().bar() where foo() return captured type?
get() = ErrorUtils.createErrorScope("No member resolution should be done on captured type!", true)
override fun replaceAnnotations(newAnnotations: Annotations) =
NewCapturedType(captureStatus, constructor, lowerType, newAnnotations.toDefaultAttributes(), isMarkedNullable)
override fun replaceAttributes(newAttributes: TypeAttributes): SimpleType =
NewCapturedType(captureStatus, constructor, lowerType, newAttributes, isMarkedNullable)
@@ -52,9 +52,6 @@ class DynamicType(
override val isMarkedNullable: Boolean get() = false
override fun replaceAnnotations(newAnnotations: Annotations): DynamicType =
DynamicType(delegate.builtIns, attributes.replaceAnnotations(newAnnotations))
override fun replaceAttributes(newAttributes: TypeAttributes): DynamicType =
DynamicType(delegate.builtIns, newAttributes)
@@ -6,20 +6,29 @@
package org.jetbrains.kotlin.types.extensions
import com.intellij.openapi.project.Project
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
import org.jetbrains.kotlin.descriptors.annotations.Annotations
import org.jetbrains.kotlin.extensions.ProjectExtensionDescriptor
import org.jetbrains.kotlin.types.*
interface TypeAttributeTranslatorExtension : TypeAttributeTranslator
class TypeAttributeTranslatorsForInjection(project: Project) {
val translators: List<TypeAttributeTranslator> = TypeAttributeTranslators(project).translators
}
class TypeAttributeTranslators private constructor(val translators: List<TypeAttributeTranslator>) {
constructor(project: Project) : this(getInstances(project) + DefaultTypeAttributeTranslator)
fun toAttributes(annotations: Annotations): TypeAttributes {
fun toAttributes(
annotations: Annotations,
typeConstructor: TypeConstructor,
containingDeclaration: DeclarationDescriptor? = null
): TypeAttributes {
val translated = translators.map { translator ->
translator.toAttributes(annotations)
translator.toAttributes(annotations, typeConstructor, containingDeclaration)
}.flatten()
return TypeAttributes.create(translated)
}
@@ -125,9 +125,6 @@ class FlexibleTypeImpl(lowerBound: SimpleType, upperBound: SimpleType) : Flexibl
}.inheritEnhancement(unwrapped)
}
override fun replaceAnnotations(newAnnotations: Annotations): UnwrappedType =
KotlinTypeFactory.flexibleType(lowerBound.replaceAnnotations(newAnnotations), upperBound.replaceAnnotations(newAnnotations))
override fun replaceAttributes(newAttributes: TypeAttributes): UnwrappedType =
KotlinTypeFactory.flexibleType(lowerBound.replaceAttributes(newAttributes), upperBound.replaceAttributes(newAttributes))
@@ -34,7 +34,7 @@ import org.jetbrains.kotlin.resolve.scopes.MemberScope
import org.jetbrains.kotlin.serialization.deserialization.builtins.BuiltInSerializerProtocol
import org.jetbrains.kotlin.serialization.deserialization.descriptors.DeserializedPackageMemberScope
import org.jetbrains.kotlin.storage.StorageManager
import org.jetbrains.kotlin.types.TypeAttributeTranslator
import org.jetbrains.kotlin.types.extensions.TypeAttributeTranslatorsForInjection
import org.jetbrains.kotlin.types.checker.NewKotlinTypeChecker
import java.io.InputStream
@@ -47,7 +47,7 @@ class MetadataPackageFragmentProvider(
contractDeserializer: ContractDeserializer,
kotlinTypeChecker: NewKotlinTypeChecker,
samConversionResolver: SamConversionResolver,
typeAttributeTranslators: List<TypeAttributeTranslator>
typeAttributeTranslators: TypeAttributeTranslatorsForInjection
) : AbstractDeserializedPackageFragmentProvider(storageManager, finder, moduleDescriptor) {
init {
components = DeserializationComponents(
@@ -68,7 +68,7 @@ class MetadataPackageFragmentProvider(
BuiltInSerializerProtocol.extensionRegistry,
kotlinTypeChecker,
samConversionResolver,
typeAttributeTranslators = typeAttributeTranslators
typeAttributeTranslators = typeAttributeTranslators.translators
)
}
@@ -67,9 +67,13 @@ class TypeDeserializer(
return simpleType(proto, expandTypeAliases = true)
}
private fun List<TypeAttributeTranslator>.toAttributes(annotations: Annotations): TypeAttributes {
private fun List<TypeAttributeTranslator>.toAttributes(
annotations: Annotations,
constructor: TypeConstructor,
containingDeclaration: DeclarationDescriptor
): TypeAttributes {
val translated = this.map { translator ->
translator.toAttributes(annotations)
translator.toAttributes(annotations, constructor, containingDeclaration)
}.flatten()
return TypeAttributes.create(translated)
}
@@ -92,7 +96,7 @@ class TypeDeserializer(
c.components.annotationAndConstantLoader.loadTypeAnnotations(proto, c.nameResolver)
}
val attributes = c.components.typeAttributeTranslators.toAttributes(annotations)
val attributes = c.components.typeAttributeTranslators.toAttributes(annotations, constructor, c.containingDeclaration)
fun ProtoBuf.Type.collectAllArguments(): List<ProtoBuf.Type.Argument> =
argumentList + outerType(c.typeTable)?.collectAllArguments().orEmpty()
@@ -107,7 +111,9 @@ class TypeDeserializer(
expandTypeAliases && declarationDescriptor is TypeAliasDescriptor -> {
val expandedType = with(KotlinTypeFactory) { declarationDescriptor.computeExpandedType(arguments) }
val expandedAttributes = c.components.typeAttributeTranslators.toAttributes(
Annotations.create(annotations + expandedType.annotations)
Annotations.create(annotations + expandedType.annotations),
constructor,
c.containingDeclaration
)
expandedType
.makeNullableAsSpecified(expandedType.isNullable() || proto.nullable)
@@ -56,18 +56,18 @@ fun KClassifier.createType(
// TODO: throw exception if argument does not satisfy bounds
val typeAnnotations =
if (annotations.isEmpty()) Annotations.EMPTY
else Annotations.EMPTY // TODO: support type annotations
val typeAttributes =
if (annotations.isEmpty()) TypeAttributes.Empty
else TypeAttributes.Empty // TODO: support type annotations
return KTypeImpl(createKotlinType(typeAnnotations, typeConstructor, arguments, nullable))
return KTypeImpl(createKotlinType(typeAttributes, typeConstructor, arguments, nullable))
}
private fun createKotlinType(
typeAnnotations: Annotations, typeConstructor: TypeConstructor, arguments: List<KTypeProjection>, nullable: Boolean
attributes: TypeAttributes, typeConstructor: TypeConstructor, arguments: List<KTypeProjection>, nullable: Boolean
): SimpleType {
val parameters = typeConstructor.parameters
return KotlinTypeFactory.simpleType(typeAnnotations, typeConstructor, arguments.mapIndexed { index, typeProjection ->
return KotlinTypeFactory.simpleType(attributes, typeConstructor, arguments.mapIndexed { index, typeProjection ->
val type = (typeProjection.type as KTypeImpl?)?.type
when (typeProjection.variance) {
KVariance.INVARIANT -> TypeProjectionImpl(Variance.INVARIANT, type!!)