From 4c0572fb2f5f3cc751f4f8335892b5dcad7c8212 Mon Sep 17 00:00:00 2001 From: Stanislav Erokhin Date: Wed, 1 Jun 2016 17:53:27 +0300 Subject: [PATCH] Remove type capability CustomTypeVariable. --- .../java/typeEnhancement/typeEnhancement.kt | 42 +++++++++---------- .../java/typeEnhancement/typeQualifiers.kt | 2 +- .../org/jetbrains/kotlin/types/KotlinType.kt | 1 + .../kotlin/types/TypeCapabilities.kt | 6 +-- .../jetbrains/kotlin/types/flexibleTypes.kt | 3 -- 5 files changed, 25 insertions(+), 29 deletions(-) diff --git a/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/typeEnhancement/typeEnhancement.kt b/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/typeEnhancement/typeEnhancement.kt index 218dead893f..3aea5f7edae 100644 --- a/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/typeEnhancement/typeEnhancement.kt +++ b/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/typeEnhancement/typeEnhancement.kt @@ -124,12 +124,6 @@ private fun KotlinType.enhanceInflexible(qualifiers: (Int) -> JavaTypeQualifiers val newSubstitution = TypeConstructorSubstitution.create(typeConstructor, enhancedArguments) - val newCapabilities = - if (effectiveQualifiers.isNotNullTypeParameter) - capabilities.addCapability(CustomTypeVariable::class.java, NotNullTypeParameterTypeCapability) - else - capabilities - val enhancedType = KotlinTypeImpl.create( newAnnotations, typeConstructor, @@ -137,10 +131,11 @@ private fun KotlinType.enhanceInflexible(qualifiers: (Int) -> JavaTypeQualifiers enhancedArguments, if (enhancedClassifier is ClassDescriptor) enhancedClassifier.getMemberScope(newSubstitution) - else enhancedClassifier.getDefaultType().memberScope, - newCapabilities + else enhancedClassifier.getDefaultType().memberScope ) - return Result(enhancedType, subtreeSize, wereChanges = true) + + val result = if (effectiveQualifiers.isNotNullTypeParameter) NotNullTypeParameter(enhancedType) else enhancedType + return Result(result, subtreeSize, wereChanges = true) } private fun List.compositeAnnotationsOrSingle() = when (size) { @@ -218,28 +213,31 @@ private object EnhancedTypeAnnotationDescriptor : AnnotationDescriptor { override fun toString() = "[EnhancedType]" } -internal object NotNullTypeParameterTypeCapability : CustomTypeVariable { +internal class NotNullTypeParameter(private val delegate: SimpleType) : CustomTypeVariable, DelegatingType(), SimpleType { + override fun getDelegate(): KotlinType? = delegate + override val isTypeVariable: Boolean get() = true override fun substitutionResult(replacement: KotlinType): KotlinType { - if (!TypeUtils.isNullableType(replacement) && !replacement.isTypeParameter()) return replacement + val unwrappedType = replacement.unwrap() + if (!TypeUtils.isNullableType(unwrappedType) && !unwrappedType.isTypeParameter()) return unwrappedType - if (replacement.isFlexible()) { - with(replacement.asFlexibleType()) { - return KotlinTypeFactory.flexibleType(lowerBound.prepareReplacement().asSimpleType(), upperBound.prepareReplacement().asSimpleType()) - } + return when (unwrappedType) { + is SimpleType -> unwrappedType.prepareReplacement() + is FlexibleType -> KotlinTypeFactory.flexibleType(unwrappedType.lowerBound.prepareReplacement(), + unwrappedType.upperBound.prepareReplacement()) + else -> error("Incorrect type: $unwrappedType") } - - return replacement.prepareReplacement() } - private fun KotlinType.prepareReplacement(): KotlinType { - val result = makeNotNullable() + override val isMarkedNullable: Boolean + get() = false + + private fun SimpleType.prepareReplacement(): SimpleType { + val result = TypeUtils.makeNullableAsSpecified(this, false) if (!this.isTypeParameter()) return result - return result.replace( - newCapabilities = capabilities.addCapability( - CustomTypeVariable::class.java, NotNullTypeParameterTypeCapability)) + return NotNullTypeParameter(result) } } diff --git a/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/typeEnhancement/typeQualifiers.kt b/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/typeEnhancement/typeQualifiers.kt index f778e961781..3b53390b81e 100644 --- a/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/typeEnhancement/typeQualifiers.kt +++ b/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/typeEnhancement/typeQualifiers.kt @@ -59,7 +59,7 @@ private fun KotlinType.extractQualifiers(): JavaTypeQualifiers { return JavaTypeQualifiers( if (lower.isMarkedNullable) NULLABLE else if (!upper.isMarkedNullable) NOT_NULL else null, if (mapping.isReadOnly(lower)) READ_ONLY else if (mapping.isMutable(upper)) MUTABLE else null, - isNotNullTypeParameter = getCapability() is NotNullTypeParameterTypeCapability) + isNotNullTypeParameter = unwrap() is NotNullTypeParameter) } private fun KotlinType.extractQualifiersFromAnnotations(): JavaTypeQualifiers { diff --git a/core/descriptors/src/org/jetbrains/kotlin/types/KotlinType.kt b/core/descriptors/src/org/jetbrains/kotlin/types/KotlinType.kt index d805599915e..6958363ced8 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/types/KotlinType.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/types/KotlinType.kt @@ -133,6 +133,7 @@ private class WrappedSimpleType( override fun unwrap(): KotlinType { if (delegate.isError) return delegate // todo + if (delegate is CustomTypeVariable) return delegate // todo return KotlinTypeImpl.create(annotations, constructor, isMarkedNullable, arguments, memberScope, capabilities, abbreviatedType) } diff --git a/core/descriptors/src/org/jetbrains/kotlin/types/TypeCapabilities.kt b/core/descriptors/src/org/jetbrains/kotlin/types/TypeCapabilities.kt index b2ca5107a57..5acbe4eb0ae 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/types/TypeCapabilities.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/types/TypeCapabilities.kt @@ -53,16 +53,16 @@ inline fun KotlinType.getCapability(): T? = getCapa // To facilitate laziness, any KotlinType implementation may inherit from this trait, // even if it turns out that the type an instance represents is not actually a type variable // (i.e. it is not derived from a type parameter), see isTypeVariable -interface CustomTypeVariable : TypeCapability { +interface CustomTypeVariable { val isTypeVariable: Boolean // Throws an exception when isTypeVariable == false fun substitutionResult(replacement: KotlinType): KotlinType } -fun KotlinType.isCustomTypeVariable(): Boolean = this.getCapability(CustomTypeVariable::class.java)?.isTypeVariable ?: false +fun KotlinType.isCustomTypeVariable(): Boolean = (unwrap() as? CustomTypeVariable)?.isTypeVariable ?: false fun KotlinType.getCustomTypeVariable(): CustomTypeVariable? = - this.getCapability(CustomTypeVariable::class.java)?.let { + (unwrap() as? CustomTypeVariable)?.let { if (it.isTypeVariable) it else null } diff --git a/core/descriptors/src/org/jetbrains/kotlin/types/flexibleTypes.kt b/core/descriptors/src/org/jetbrains/kotlin/types/flexibleTypes.kt index 973f6be84ed..39d85b19bd6 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/types/flexibleTypes.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/types/flexibleTypes.kt @@ -132,9 +132,6 @@ class FlexibleTypeImpl(lowerBound: SimpleType, upperBound: SimpleType) : Delegat override val delegateType: KotlinType get() = lowerBound override fun getCapability(capabilityClass: Class): T? { - @Suppress("UNCHECKED_CAST") - if (capabilityClass == CustomTypeVariable::class.java) return this as T - return super.getCapability(capabilityClass) }