diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/serialization/JvmSerializerExtension.java b/compiler/backend/src/org/jetbrains/kotlin/codegen/serialization/JvmSerializerExtension.java index a8cda2a47c2..d82ec83a0ed 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/serialization/JvmSerializerExtension.java +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/serialization/JvmSerializerExtension.java @@ -33,7 +33,7 @@ import org.jetbrains.kotlin.serialization.SerializerExtension; import org.jetbrains.kotlin.serialization.StringTable; import org.jetbrains.kotlin.serialization.jvm.ClassMapperLite; import org.jetbrains.kotlin.serialization.jvm.JvmProtoBuf; -import org.jetbrains.kotlin.types.DelegatingFlexibleType; +import org.jetbrains.kotlin.types.FlexibleType; import org.jetbrains.kotlin.types.KotlinType; import org.jetbrains.org.objectweb.asm.Type; import org.jetbrains.org.objectweb.asm.commons.Method; @@ -84,7 +84,7 @@ public class JvmSerializerExtension extends SerializerExtension { @Override public void serializeFlexibleType( - @NotNull DelegatingFlexibleType flexibleType, + @NotNull FlexibleType flexibleType, @NotNull ProtoBuf.Type.Builder lowerProto, @NotNull ProtoBuf.Type.Builder upperProto ) { diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/PossiblyBareType.java b/compiler/frontend/src/org/jetbrains/kotlin/resolve/PossiblyBareType.java index db162e41e60..b01194a0bbd 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/PossiblyBareType.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/PossiblyBareType.java @@ -94,12 +94,12 @@ public class PossiblyBareType { KotlinType nullableActualType = TypeUtils.makeNullable(getActualType()); - KotlinType abbreviatedType = KotlinTypeKt.getAbbreviatedType(getActualType()); + KotlinType abbreviatedType = SpecialTypesKt.getAbbreviatedType(getActualType()); if (abbreviatedType == null) { return type(nullableActualType); } else { - return type(KotlinTypeKt.withAbbreviatedType(KotlinTypeKt.asSimpleType(nullableActualType), KotlinTypeKt.asSimpleType(TypeUtils.makeNullable(abbreviatedType)))); + return type(SpecialTypesKt.withAbbreviatedType(KotlinTypeKt.asSimpleType(nullableActualType), KotlinTypeKt.asSimpleType(TypeUtils.makeNullable(abbreviatedType)))); } } diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/TypeResolver.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/TypeResolver.kt index 778310355b6..1c2270c48e4 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/TypeResolver.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/TypeResolver.kt @@ -44,6 +44,7 @@ import org.jetbrains.kotlin.resolve.scopes.utils.findClassifier import org.jetbrains.kotlin.resolve.source.toSourceElement import org.jetbrains.kotlin.storage.LockBasedStorageManager import org.jetbrains.kotlin.storage.StorageManager +import org.jetbrains.kotlin.storage.getValue import org.jetbrains.kotlin.types.* import org.jetbrains.kotlin.types.Variance.* import org.jetbrains.kotlin.types.typeUtil.isArrayOfNothing @@ -102,7 +103,7 @@ class TypeResolver( class LazyKotlinType : WrappedType(), LazyEntity { private val _delegate = storageManager.createLazyValue { doResolvePossiblyBareType(c, typeReference).getActualType() } - override fun unwrap() = _delegate() + override val delegate: KotlinType by _delegate override fun isComputed() = _delegate.isComputed() override fun forceResolveAllContents() { diff --git a/compiler/frontend/src/org/jetbrains/kotlin/types/DeferredType.java b/compiler/frontend/src/org/jetbrains/kotlin/types/DeferredType.java index a4af4eefd57..8e686867d99 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/types/DeferredType.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/types/DeferredType.java @@ -88,7 +88,7 @@ public class DeferredType extends WrappedType implements LazyType { @NotNull @Override - public KotlinType unwrap() { + public KotlinType getDelegate() { return lazyValue.invoke(); } diff --git a/compiler/serialization/src/org/jetbrains/kotlin/serialization/DescriptorSerializer.java b/compiler/serialization/src/org/jetbrains/kotlin/serialization/DescriptorSerializer.java index 4d71bcb0690..af3d5843329 100644 --- a/compiler/serialization/src/org/jetbrains/kotlin/serialization/DescriptorSerializer.java +++ b/compiler/serialization/src/org/jetbrains/kotlin/serialization/DescriptorSerializer.java @@ -506,7 +506,7 @@ public class DescriptorSerializer { } if (FlexibleTypesKt.isFlexible(type)) { - DelegatingFlexibleType flexibleType = (DelegatingFlexibleType) FlexibleTypesKt.asFlexibleType(type); + FlexibleType flexibleType = FlexibleTypesKt.asFlexibleType(type); ProtoBuf.Type.Builder lowerBound = type(flexibleType.getLowerBound()); ProtoBuf.Type.Builder upperBound = type(flexibleType.getUpperBound()); @@ -548,7 +548,7 @@ public class DescriptorSerializer { builder.setNullable(type.isMarkedNullable()); } - KotlinType abbreviatedType = KotlinTypeKt.getAbbreviatedType(type); + KotlinType abbreviatedType = SpecialTypesKt.getAbbreviatedType(type); if (abbreviatedType != null) { if (useTypeTable()) { builder.setAbbreviatedTypeId(typeId(abbreviatedType)); diff --git a/compiler/serialization/src/org/jetbrains/kotlin/serialization/SerializerExtension.java b/compiler/serialization/src/org/jetbrains/kotlin/serialization/SerializerExtension.java index efadb0b1d0a..1ccf992856f 100644 --- a/compiler/serialization/src/org/jetbrains/kotlin/serialization/SerializerExtension.java +++ b/compiler/serialization/src/org/jetbrains/kotlin/serialization/SerializerExtension.java @@ -18,7 +18,7 @@ package org.jetbrains.kotlin.serialization; import org.jetbrains.annotations.NotNull; import org.jetbrains.kotlin.descriptors.*; -import org.jetbrains.kotlin.types.DelegatingFlexibleType; +import org.jetbrains.kotlin.types.FlexibleType; import org.jetbrains.kotlin.types.KotlinType; public abstract class SerializerExtension { @@ -50,7 +50,7 @@ public abstract class SerializerExtension { public void serializeValueParameter(@NotNull ValueParameterDescriptor descriptor, @NotNull ProtoBuf.ValueParameter.Builder proto) { } - public void serializeFlexibleType(@NotNull DelegatingFlexibleType flexibleType, @NotNull ProtoBuf.Type.Builder lowerProto, @NotNull ProtoBuf.Type.Builder upperProto) { + public void serializeFlexibleType(@NotNull FlexibleType flexibleType, @NotNull ProtoBuf.Type.Builder lowerProto, @NotNull ProtoBuf.Type.Builder upperProto) { } public void serializeType(@NotNull KotlinType type, @NotNull ProtoBuf.Type.Builder proto) { diff --git a/compiler/testData/resolvedCalls/dynamic/explicitReceiverIsDispatchReceiver.txt b/compiler/testData/resolvedCalls/dynamic/explicitReceiverIsDispatchReceiver.txt index 0910e990c41..5ac23df5069 100644 --- a/compiler/testData/resolvedCalls/dynamic/explicitReceiverIsDispatchReceiver.txt +++ b/compiler/testData/resolvedCalls/dynamic/explicitReceiverIsDispatchReceiver.txt @@ -9,5 +9,5 @@ Resolved call: Resulting descriptor: fun foo(): dynamic defined in bar Explicit receiver kind = DISPATCH_RECEIVER -Dispatch receiver = a {('Nothing'..'Any?')} +Dispatch receiver = a {dynamic} Extension receiver = NO_RECEIVER diff --git a/compiler/testData/resolvedCalls/dynamic/explicitReceiverIsExtensionReceiver.txt b/compiler/testData/resolvedCalls/dynamic/explicitReceiverIsExtensionReceiver.txt index 17e222edc57..10366bd49f4 100644 --- a/compiler/testData/resolvedCalls/dynamic/explicitReceiverIsExtensionReceiver.txt +++ b/compiler/testData/resolvedCalls/dynamic/explicitReceiverIsExtensionReceiver.txt @@ -12,4 +12,4 @@ Resulting descriptor: fun dynamic.foo(): Unit defined in root package Explicit receiver kind = EXTENSION_RECEIVER Dispatch receiver = NO_RECEIVER -Extension receiver = a {('Nothing'..'Any?')} +Extension receiver = a {dynamic} diff --git a/compiler/testData/resolvedCalls/dynamic/hasBothDispatchAndExtensionReceivers.txt b/compiler/testData/resolvedCalls/dynamic/hasBothDispatchAndExtensionReceivers.txt index 780095acccb..36f5469aee0 100644 --- a/compiler/testData/resolvedCalls/dynamic/hasBothDispatchAndExtensionReceivers.txt +++ b/compiler/testData/resolvedCalls/dynamic/hasBothDispatchAndExtensionReceivers.txt @@ -16,4 +16,4 @@ Resulting descriptor: fun dynamic.foo(): Unit defined in A Explicit receiver kind = EXTENSION_RECEIVER Dispatch receiver = AExt{fun A.(): Unit defined in bar} -Extension receiver = b {('Nothing'..'Any?')} +Extension receiver = b {dynamic} diff --git a/compiler/testData/resolvedCalls/dynamic/hasBothDispatchAndExtensionReceiversWithoutExplicitReceiver.txt b/compiler/testData/resolvedCalls/dynamic/hasBothDispatchAndExtensionReceiversWithoutExplicitReceiver.txt index d7d24928d83..a876a235e3a 100644 --- a/compiler/testData/resolvedCalls/dynamic/hasBothDispatchAndExtensionReceiversWithoutExplicitReceiver.txt +++ b/compiler/testData/resolvedCalls/dynamic/hasBothDispatchAndExtensionReceiversWithoutExplicitReceiver.txt @@ -18,4 +18,4 @@ Resulting descriptor: fun dynamic.foo(): Unit defined in A Explicit receiver kind = NO_EXPLICIT_RECEIVER Dispatch receiver = AExt{fun A.(): Unit defined in bar} -Extension receiver = ('Nothing'..'Any?')Ext{fun dynamic.(): Unit defined in bar.} +Extension receiver = dynamicExt{fun dynamic.(): Unit defined in bar.} diff --git a/compiler/testData/resolvedCalls/dynamic/implicitReceiverIsDispatchReceiver.txt b/compiler/testData/resolvedCalls/dynamic/implicitReceiverIsDispatchReceiver.txt index 578d82819e2..f17e715739f 100644 --- a/compiler/testData/resolvedCalls/dynamic/implicitReceiverIsDispatchReceiver.txt +++ b/compiler/testData/resolvedCalls/dynamic/implicitReceiverIsDispatchReceiver.txt @@ -9,5 +9,5 @@ Resolved call: Resulting descriptor: fun foo(): dynamic defined in bar Explicit receiver kind = NO_EXPLICIT_RECEIVER -Dispatch receiver = ('Nothing'..'Any?')Ext{fun dynamic.bar(): Unit defined in root package} +Dispatch receiver = dynamicExt{fun dynamic.bar(): Unit defined in root package} Extension receiver = NO_RECEIVER diff --git a/compiler/testData/resolvedCalls/dynamic/implicitReceiverIsExtensionReceiver.txt b/compiler/testData/resolvedCalls/dynamic/implicitReceiverIsExtensionReceiver.txt index 2114195c710..eb22771ca84 100644 --- a/compiler/testData/resolvedCalls/dynamic/implicitReceiverIsExtensionReceiver.txt +++ b/compiler/testData/resolvedCalls/dynamic/implicitReceiverIsExtensionReceiver.txt @@ -12,4 +12,4 @@ Resulting descriptor: fun dynamic.foo(): Unit defined in root package Explicit receiver kind = NO_EXPLICIT_RECEIVER Dispatch receiver = NO_RECEIVER -Extension receiver = ('Nothing'..'Any?')Ext{fun dynamic.bar(): Unit defined in root package} +Extension receiver = dynamicExt{fun dynamic.bar(): Unit defined in root package} diff --git a/compiler/tests-common/org/jetbrains/kotlin/test/KotlinTestWithEnvironmentManagement.java b/compiler/tests-common/org/jetbrains/kotlin/test/KotlinTestWithEnvironmentManagement.java index c74c0a5aa6c..57dc5f3a193 100644 --- a/compiler/tests-common/org/jetbrains/kotlin/test/KotlinTestWithEnvironmentManagement.java +++ b/compiler/tests-common/org/jetbrains/kotlin/test/KotlinTestWithEnvironmentManagement.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. @@ -19,12 +19,12 @@ package org.jetbrains.kotlin.test; import org.jetbrains.annotations.NotNull; import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment; import org.jetbrains.kotlin.test.testFramework.KtUsefulTestCase; -import org.jetbrains.kotlin.types.DelegatingFlexibleType; +import org.jetbrains.kotlin.types.FlexibleTypeImpl; public abstract class KotlinTestWithEnvironmentManagement extends KtUsefulTestCase { static { System.setProperty("java.awt.headless", "true"); - DelegatingFlexibleType.RUN_SLOW_ASSERTIONS = true; + FlexibleTypeImpl.RUN_SLOW_ASSERTIONS = true; } @NotNull diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/LazyOperationsLog.kt b/compiler/tests/org/jetbrains/kotlin/checkers/LazyOperationsLog.kt index 41de5df36a9..6a37671fa73 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/LazyOperationsLog.kt +++ b/compiler/tests/org/jetbrains/kotlin/checkers/LazyOperationsLog.kt @@ -30,7 +30,6 @@ import org.jetbrains.kotlin.serialization.ProtoBuf import org.jetbrains.kotlin.serialization.deserialization.DeserializationContext import org.jetbrains.kotlin.serialization.deserialization.TypeDeserializer import org.jetbrains.kotlin.types.KotlinType -import org.jetbrains.kotlin.types.KotlinTypeImpl import org.jetbrains.kotlin.utils.Printer import java.lang.reflect.Constructor import java.lang.reflect.GenericDeclaration @@ -177,7 +176,7 @@ class LazyOperationsLog( o.joinTo(sb, ", ", prefix = "{", postfix = "}", limit = 3) { render(it) } } } - o is KotlinTypeImpl -> { + o is KotlinType -> { StringBuilder().apply { append(o.constructor) if (!o.arguments.isEmpty()) { diff --git a/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/ErasedOverridabilityCondition.kt b/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/ErasedOverridabilityCondition.kt index 43144a4278e..10cef6f8953 100644 --- a/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/ErasedOverridabilityCondition.kt +++ b/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/ErasedOverridabilityCondition.kt @@ -25,7 +25,6 @@ import org.jetbrains.kotlin.load.java.lazy.types.RawTypeImpl import org.jetbrains.kotlin.resolve.ExternalOverridabilityCondition import org.jetbrains.kotlin.resolve.ExternalOverridabilityCondition.Result import org.jetbrains.kotlin.resolve.OverridingUtil -import org.jetbrains.kotlin.types.unwrap import org.jetbrains.kotlin.utils.singletonOrEmptyList class ErasedOverridabilityCondition : ExternalOverridabilityCondition { diff --git a/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/lazy/types/LazyJavaTypeResolver.kt b/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/lazy/types/LazyJavaTypeResolver.kt index 8f8ef550a2d..79e25d316d9 100644 --- a/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/lazy/types/LazyJavaTypeResolver.kt +++ b/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/lazy/types/LazyJavaTypeResolver.kt @@ -115,10 +115,10 @@ class LazyJavaTypeResolver( ) : AbstractLazyType(c.storageManager) { override val annotations = CompositeAnnotations(listOf(LazyJavaAnnotations(c, javaType), attr.typeAnnotations)) - private val classifier = c.storageManager.createNullableLazyValue { javaType.classifier } + private val classifier: JavaClassifier? get() = javaType.classifier override fun computeTypeConstructor(): TypeConstructor { - val classifier = classifier() ?: return createNotFoundClass() + val classifier = classifier ?: return createNotFoundClass() return when (classifier) { is JavaClass -> { val fqName = classifier.fqName.sure { "Class type should have a FQ name: $classifier" } @@ -279,13 +279,13 @@ class LazyJavaTypeResolver( !attr.isMarkedNotNull && // 'L extends List' in Java is a List in Kotlin, not a List // nullability will be taken care of in individual member signatures - when (classifier()) { + when (classifier) { is JavaTypeParameter -> { attr.howThisTypeIsUsed !in setOf(TYPE_ARGUMENT, UPPER_BOUND, SUPERTYPE_ARGUMENT, SUPERTYPE) } is JavaClass, null -> attr.howThisTypeIsUsed !in setOf(TYPE_ARGUMENT, SUPERTYPE_ARGUMENT, SUPERTYPE) - else -> error("Unknown classifier: ${classifier()}") + else -> error("Unknown classifier: ${classifier}") } } diff --git a/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/lazy/types/RawType.kt b/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/lazy/types/RawType.kt index 733508b2a0d..bb095473dc3 100644 --- a/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/lazy/types/RawType.kt +++ b/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/lazy/types/RawType.kt @@ -27,17 +27,15 @@ import org.jetbrains.kotlin.resolve.descriptorUtil.builtIns import org.jetbrains.kotlin.resolve.scopes.MemberScope import org.jetbrains.kotlin.types.* import org.jetbrains.kotlin.types.checker.KotlinTypeChecker -import org.jetbrains.kotlin.types.typeUtil.replaceAnnotations -class RawTypeImpl(lowerBound: SimpleType, upperBound: SimpleType) : DelegatingFlexibleType(lowerBound, upperBound), RawType { +class RawTypeImpl(lowerBound: SimpleType, upperBound: SimpleType) : FlexibleType(lowerBound, upperBound), RawType { init { assert (KotlinTypeChecker.DEFAULT.isSubtypeOf(lowerBound, upperBound)) { "Lower bound $lowerBound of a flexible type must be a subtype of the upper bound $upperBound" } } - override fun getDelegate(): KotlinType = delegateType - override val delegateType: KotlinType get() = lowerBound + override val delegate: SimpleType get() = lowerBound override val memberScope: MemberScope get() { @@ -46,11 +44,11 @@ class RawTypeImpl(lowerBound: SimpleType, upperBound: SimpleType) : DelegatingFl return classDescriptor.getMemberScope(RawSubstitution) } - override fun replaceAnnotations(newAnnotations: Annotations): KotlinType - = RawTypeImpl(lowerBound.replaceAnnotations(newAnnotations).asSimpleType(), upperBound) + override fun replaceAnnotations(newAnnotations: Annotations) + = RawTypeImpl(lowerBound.replaceAnnotations(newAnnotations), upperBound.replaceAnnotations(newAnnotations)) - override fun makeNullableAsSpecified(nullable: Boolean): KotlinType - = RawTypeImpl(TypeUtils.makeNullableAsSpecified(lowerBound, nullable), TypeUtils.makeNullableAsSpecified(upperBound, nullable)) + override fun makeNullableAsSpecified(newNullability: Boolean) + = RawTypeImpl(lowerBound.makeNullableAsSpecified(newNullability), upperBound.makeNullableAsSpecified(newNullability)) override fun render(renderer: DescriptorRenderer, options: DescriptorRendererOptions): String { fun onlyOutDiffers(first: String, second: String) = first == second.removePrefix("out ") || second == "*" 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 3aea5f7edae..07ffb533ae0 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,14 +124,14 @@ private fun KotlinType.enhanceInflexible(qualifiers: (Int) -> JavaTypeQualifiers val newSubstitution = TypeConstructorSubstitution.create(typeConstructor, enhancedArguments) - val enhancedType = KotlinTypeImpl.create( + val enhancedType = KotlinTypeFactory.simpleType( newAnnotations, typeConstructor, - enhancedNullability, enhancedArguments, + enhancedNullability, if (enhancedClassifier is ClassDescriptor) enhancedClassifier.getMemberScope(newSubstitution) - else enhancedClassifier.getDefaultType().memberScope + else enhancedClassifier.defaultType.memberScope ) val result = if (effectiveQualifiers.isNotNullTypeParameter) NotNullTypeParameter(enhancedType) else enhancedType @@ -213,8 +213,7 @@ private object EnhancedTypeAnnotationDescriptor : AnnotationDescriptor { override fun toString() = "[EnhancedType]" } -internal class NotNullTypeParameter(private val delegate: SimpleType) : CustomTypeVariable, DelegatingType(), SimpleType { - override fun getDelegate(): KotlinType? = delegate +internal class NotNullTypeParameter(override val delegate: SimpleType) : CustomTypeVariable, DelegatingSimpleType() { override val isTypeVariable: Boolean get() = true @@ -240,4 +239,10 @@ internal class NotNullTypeParameter(private val delegate: SimpleType) : CustomTy return NotNullTypeParameter(result) } + + override fun replaceAnnotations(newAnnotations: Annotations) = NotNullTypeParameter(delegate.replaceAnnotations(newAnnotations)) + override fun makeNullableAsSpecified(newNullability: Boolean) = this + + override val isError: Boolean + get() = false } diff --git a/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/kotlin/JvmBuiltInsSettings.kt b/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/kotlin/JvmBuiltInsSettings.kt index b8274f19545..49f9f57d54c 100644 --- a/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/kotlin/JvmBuiltInsSettings.kt +++ b/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/kotlin/JvmBuiltInsSettings.kt @@ -78,7 +78,7 @@ open class JvmBuiltInsSettings( //NOTE: can't reference anyType right away, because this is sometimes called when JvmBuiltIns are initializing val superTypes = listOf(object : WrappedType() { - override fun unwrap() = moduleDescriptor.builtIns.anyType + override val delegate: KotlinType get() = moduleDescriptor.builtIns.anyType }) val mockSerializableClass = ClassDescriptorImpl( diff --git a/core/descriptors/src/org/jetbrains/kotlin/resolve/calls/inference/CapturedTypeConstructor.kt b/core/descriptors/src/org/jetbrains/kotlin/resolve/calls/inference/CapturedTypeConstructor.kt index 0b3b86b9f50..e241cf826af 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/resolve/calls/inference/CapturedTypeConstructor.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/resolve/calls/inference/CapturedTypeConstructor.kt @@ -62,7 +62,7 @@ class CapturedType( override val constructor: TypeConstructor = CapturedTypeConstructor(typeProjection), override val isMarkedNullable: Boolean = false, override val annotations: Annotations = Annotations.EMPTY -): AbstractKotlinType(), SubtypingRepresentatives, TypeWithCustomReplacement { +): SimpleType(), SubtypingRepresentatives { override val arguments: List get() = listOf() @@ -83,14 +83,14 @@ class CapturedType( override fun sameTypeConstructor(type: KotlinType) = constructor === type.constructor - override fun toString() = "Captured($typeProjection)${if (isMarkedNullable) '?' else ""}" + override fun toString() = "Captured($typeProjection)" + if (isMarkedNullable) "?" else "" - override fun makeNullableAsSpecified(nullable: Boolean): KotlinType { - if (nullable == isMarkedNullable) return this - return CapturedType(typeProjection, constructor, nullable, annotations) + override fun makeNullableAsSpecified(newNullability: Boolean): CapturedType { + if (newNullability == isMarkedNullable) return this + return CapturedType(typeProjection, constructor, newNullability, annotations) } - override fun replaceAnnotations(newAnnotations: Annotations): KotlinType = CapturedType(typeProjection, constructor, isMarkedNullable, newAnnotations) + override fun replaceAnnotations(newAnnotations: Annotations): CapturedType = CapturedType(typeProjection, constructor, isMarkedNullable, newAnnotations) } fun createCapturedType(typeProjection: TypeProjection): KotlinType @@ -120,7 +120,8 @@ private fun TypeProjection.createCapturedIfNeeded(typeParameterDescriptor: TypeP // TODO: Make star projection type lazy return if (isStarProjection) TypeProjectionImpl(object : WrappedType() { - override fun unwrap(): KotlinType = this@createCapturedIfNeeded.type + override val delegate: KotlinType + get() = this@createCapturedIfNeeded.type }) else TypeProjectionImpl(this@createCapturedIfNeeded.type) diff --git a/core/descriptors/src/org/jetbrains/kotlin/types/AbstractKotlinType.java b/core/descriptors/src/org/jetbrains/kotlin/types/AbstractKotlinType.java deleted file mode 100644 index e11a0eff64e..00000000000 --- a/core/descriptors/src/org/jetbrains/kotlin/types/AbstractKotlinType.java +++ /dev/null @@ -1,84 +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.annotations.Nullable; -import org.jetbrains.kotlin.descriptors.annotations.AnnotationWithTarget; -import org.jetbrains.kotlin.renderer.DescriptorRenderer; -import org.jetbrains.kotlin.types.checker.KotlinTypeChecker; - -import java.util.Iterator; -import java.util.List; - -public abstract class AbstractKotlinType implements KotlinType, SimpleType { // TODO temporary upper bound - - @Override - public final int hashCode() { - int result = getConstructor().hashCode(); - result = 31 * result + getArguments().hashCode(); - result = 31 * result + (isMarkedNullable() ? 1 : 0); - return result; - } - - @Override - public final boolean equals(Object obj) { - if (this == obj) return true; - if (!(obj instanceof KotlinType)) return false; - - KotlinType type = (KotlinType) obj; - - return isMarkedNullable() == type.isMarkedNullable() && KotlinTypeChecker.FLEXIBLE_UNEQUAL_TO_INFLEXIBLE.equalTypes(this, type); - } - - @Override - public String toString() { - StringBuilder sb = new StringBuilder(); - - for (AnnotationWithTarget annotationWithTarget : getAnnotations().getAllAnnotations()) { - sb.append("["); - sb.append(DescriptorRenderer.DEBUG_TEXT.renderAnnotation( - annotationWithTarget.getAnnotation(), annotationWithTarget.getTarget())); - sb.append("] "); - } - - sb.append(getConstructor()); - - List arguments = getArguments(); - if (!arguments.isEmpty()) { - sb.append("<"); - for (Iterator i = arguments.iterator(); i.hasNext(); ) { - sb.append(i.next()); - if (i.hasNext()) { - sb.append(", "); - } - } - sb.append(">"); - } - - if (isMarkedNullable()) { - sb.append("?"); - } - - return sb.toString(); - } - - @Nullable - @Override - public SimpleType getAbbreviatedType() { - return null; - } -} diff --git a/core/descriptors/src/org/jetbrains/kotlin/types/AbstractLazyType.kt b/core/descriptors/src/org/jetbrains/kotlin/types/AbstractLazyType.kt index 8e57b6fb41b..fa28741b8d2 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/types/AbstractLazyType.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/types/AbstractLazyType.kt @@ -23,7 +23,7 @@ import org.jetbrains.kotlin.resolve.scopes.MemberScope import org.jetbrains.kotlin.storage.StorageManager import org.jetbrains.kotlin.storage.getValue -abstract class AbstractLazyType(storageManager: StorageManager) : AbstractKotlinType(), SimpleType, LazyType { +abstract class AbstractLazyType(storageManager: StorageManager) : SimpleType(), LazyType { private val typeConstructor = storageManager.createLazyValue { computeTypeConstructor() } override val constructor by typeConstructor @@ -52,6 +52,10 @@ abstract class AbstractLazyType(storageManager: StorageManager) : AbstractKotlin 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() -> diff --git a/core/descriptors/src/org/jetbrains/kotlin/types/DelegatingType.java b/core/descriptors/src/org/jetbrains/kotlin/types/DelegatingType.java deleted file mode 100644 index 29b3082be70..00000000000 --- a/core/descriptors/src/org/jetbrains/kotlin/types/DelegatingType.java +++ /dev/null @@ -1,88 +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.annotations.NotNull; -import org.jetbrains.annotations.Nullable; -import org.jetbrains.kotlin.descriptors.annotations.Annotations; -import org.jetbrains.kotlin.resolve.scopes.MemberScope; -import org.jetbrains.kotlin.types.checker.KotlinTypeChecker; - -import java.util.List; - -public abstract class DelegatingType implements KotlinType, SimpleType { // TODO temporary upper bound - protected abstract KotlinType getDelegate(); - - @NotNull - @Override - public TypeConstructor getConstructor() { - return getDelegate().getConstructor(); - } - - @NotNull - @Override - public List getArguments() { - return getDelegate().getArguments(); - } - - @Override - public boolean isMarkedNullable() { - return getDelegate().isMarkedNullable(); - } - - @NotNull - @Override - public MemberScope getMemberScope() { - return getDelegate().getMemberScope(); - } - - @Override - public boolean isError() { - return getDelegate().isError(); - } - - @NotNull - @Override - public Annotations getAnnotations() { - return getDelegate().getAnnotations(); - } - - @Override - public int hashCode() { - return getDelegate().hashCode(); - } - - @Override - public boolean equals(Object obj) { - if (this == obj) return true; - if (!(obj instanceof KotlinType)) return false; - - KotlinType type = (KotlinType) obj; - return KotlinTypeChecker.FLEXIBLE_UNEQUAL_TO_INFLEXIBLE.equalTypes(this, type); - } - - @Override - public String toString() { - return getDelegate().toString(); - } - - @Nullable - @Override - public SimpleType getAbbreviatedType() { - return null; - } -} diff --git a/core/descriptors/src/org/jetbrains/kotlin/types/ErrorUtils.java b/core/descriptors/src/org/jetbrains/kotlin/types/ErrorUtils.java index 28188dc24df..95d86b7cc9a 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/types/ErrorUtils.java +++ b/core/descriptors/src/org/jetbrains/kotlin/types/ErrorUtils.java @@ -390,7 +390,7 @@ public class ErrorUtils { @NotNull public static SimpleType createErrorTypeWithArguments(@NotNull String debugMessage, @NotNull List arguments) { - return new ErrorTypeImpl(createErrorTypeConstructor(debugMessage), createErrorScope(debugMessage), arguments); + return new ErrorTypeImpl(createErrorTypeConstructor(debugMessage), createErrorScope(debugMessage), arguments, false); } @NotNull @@ -472,35 +472,26 @@ public class ErrorUtils { return candidate instanceof ErrorClassDescriptor; } - @NotNull - public static TypeParameterDescriptor createErrorTypeParameter(int index, @NotNull String debugMessage) { - return TypeParameterDescriptorImpl.createWithDefaultBound( - ERROR_CLASS, - Annotations.Companion.getEMPTY(), - false, - Variance.INVARIANT, - Name.special(""), - index - ); - } - - private static class ErrorTypeImpl implements SimpleType { + private static class ErrorTypeImpl extends SimpleType { private final TypeConstructor constructor; private final MemberScope memberScope; private final List arguments; + private final boolean nullability; private ErrorTypeImpl( @NotNull TypeConstructor constructor, @NotNull MemberScope memberScope, - @NotNull List arguments + @NotNull List arguments, + boolean nullability ) { this.constructor = constructor; this.memberScope = memberScope; this.arguments = arguments; + this.nullability = nullability; } private ErrorTypeImpl(@NotNull TypeConstructor constructor, @NotNull MemberScope memberScope) { - this(constructor, memberScope, Collections.emptyList()); + this(constructor, memberScope, Collections.emptyList(), false); } @NotNull @@ -517,7 +508,7 @@ public class ErrorUtils { @Override public boolean isMarkedNullable() { - return false; + return nullability; } @NotNull @@ -542,10 +533,16 @@ public class ErrorUtils { return constructor.toString() + (arguments.isEmpty() ? "" : joinToString(arguments, ", ", "<", ">", -1, "...", null)); } - @Nullable + @NotNull @Override - public SimpleType getAbbreviatedType() { - return null; + public SimpleType replaceAnnotations(@NotNull Annotations newAnnotations) { + return this; + } + + @NotNull + @Override + public SimpleType makeNullableAsSpecified(boolean newNullability) { + return new ErrorTypeImpl(constructor, memberScope, arguments, newNullability); } } diff --git a/core/descriptors/src/org/jetbrains/kotlin/types/KotlinType.kt b/core/descriptors/src/org/jetbrains/kotlin/types/KotlinType.kt index e45fc3ad371..12e705ee59b 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/types/KotlinType.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/types/KotlinType.kt @@ -19,59 +19,59 @@ package org.jetbrains.kotlin.types import org.jetbrains.kotlin.descriptors.annotations.Annotated import org.jetbrains.kotlin.descriptors.annotations.Annotations import org.jetbrains.kotlin.renderer.DescriptorRenderer +import org.jetbrains.kotlin.renderer.DescriptorRendererOptions import org.jetbrains.kotlin.resolve.scopes.MemberScope import org.jetbrains.kotlin.types.checker.KotlinTypeChecker /** * @see KotlinTypeChecker.isSubtypeOf */ -interface KotlinType : Annotated { - val constructor: TypeConstructor +sealed class KotlinType : Annotated { - val arguments: List + abstract val constructor: TypeConstructor + abstract val arguments: List + abstract val isMarkedNullable: Boolean + abstract val memberScope: MemberScope + abstract val isError: Boolean - val isMarkedNullable: Boolean + abstract fun unwrap(): UnwrappedType - val memberScope: MemberScope + // ------- internal staff ------ - val isError: Boolean + final override fun hashCode(): Int { + if (isError) return super.hashCode() - override fun equals(other: Any?): Boolean + var result = constructor.hashCode() + result = 31 * result + arguments.hashCode() + result = 31 * result + if (isMarkedNullable) 1 else 0 + return result + } + + final override fun equals(other: Any?): Boolean { + if (this === other) return true + if (other !is KotlinType) return false + + return isMarkedNullable == other.isMarkedNullable && KotlinTypeChecker.FLEXIBLE_UNEQUAL_TO_INFLEXIBLE.equalTypes(this, other) + } } -@Deprecated("Temporary marker method for refactoring") -fun KotlinType.asSimpleType(): SimpleType { - return unwrap() as SimpleType -} +abstract class WrappedType() : KotlinType(), LazyType { + open fun isComputed(): Boolean = true + protected abstract val delegate: KotlinType -fun KotlinType.unwrap(): KotlinType { - if (this is WrappedType) return unwrap().unwrap() - return this -} - -interface SimpleType : KotlinType { - val abbreviatedType : SimpleType? get() = null -} - -interface TypeWithCustomReplacement : KotlinType { - fun makeNullableAsSpecified(nullable: Boolean): KotlinType - - fun replaceAnnotations(newAnnotations: Annotations): KotlinType -} - -abstract class WrappedType() : KotlinType, LazyType { override val annotations: Annotations get() = delegate.annotations override val constructor: TypeConstructor get() = delegate.constructor override val arguments: List get() = delegate.arguments override val isMarkedNullable: Boolean get() = delegate.isMarkedNullable override val memberScope: MemberScope get() = delegate.memberScope - - open fun isComputed(): Boolean = true - open val delegate: KotlinType - get() = unwrap() - - abstract fun unwrap(): KotlinType + override final fun unwrap(): UnwrappedType { + var result = delegate + while (result is WrappedType) { + result = result.delegate + } + return result as UnwrappedType + } override fun toString(): String { if (isComputed()) { @@ -84,58 +84,26 @@ abstract class WrappedType() : KotlinType, LazyType { // todo: remove this later override val isError: Boolean get() = delegate.isError - override fun equals(other: Any?): Boolean = unwrap().equals(other) - override fun hashCode(): Int = unwrap().hashCode() } -fun SimpleType.lazyReplaceNullability(newNullable: Boolean): SimpleType { - if (this is WrappedSimpleType) { - return WrappedSimpleType(delegate, newAnnotations, newNullable) - } - else { - return WrappedSimpleType(this, newNullable = newNullable) - } +sealed class UnwrappedType: KotlinType() { + abstract fun replaceAnnotations(newAnnotations: Annotations): UnwrappedType + abstract fun makeNullableAsSpecified(newNullability: Boolean): UnwrappedType + + override final fun unwrap(): UnwrappedType = this } -fun SimpleType.lazyReplaceAnnotations(newAnnotations: Annotations): SimpleType { - if (this is WrappedSimpleType) { - return WrappedSimpleType(delegate, newAnnotations, newNullable) - } - else { - return WrappedSimpleType(this, newAnnotations) - } -} - -fun KotlinType.getAbbreviatedType(): SimpleType? = (unwrap() as? SimpleType)?.abbreviatedType - -fun SimpleType.withAbbreviatedType(abbreviatedType: SimpleType): SimpleType { - if (isError) return this - return KotlinTypeImpl.create(annotations, constructor, isMarkedNullable, arguments, memberScope, abbreviatedType) -} - -private class WrappedSimpleType( - override val delegate: SimpleType, - val newAnnotations: Annotations? = null, - val newNullable: Boolean? = null -): WrappedType(), SimpleType { - override val annotations: Annotations - get() = newAnnotations ?: delegate.annotations - - override val isMarkedNullable: Boolean - get() = newNullable ?: delegate.isMarkedNullable - - 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, abbreviatedType) - } +abstract class SimpleType : UnwrappedType() { + abstract override fun replaceAnnotations(newAnnotations: Annotations): SimpleType + abstract override fun makeNullableAsSpecified(newNullability: Boolean): SimpleType override fun toString(): String { - if (isError) return delegate.toString() + // for error types this method should be overridden + if (isError) return "ErrorType" return buildString { - for (annotation in annotations.getAllAnnotations()) { - append("[", DescriptorRenderer.DEBUG_TEXT.renderAnnotation(annotation.annotation, annotation.target), "] ") + for ((annotation, target) in annotations.getAllAnnotations()) { + append("[", DescriptorRenderer.DEBUG_TEXT.renderAnnotation(annotation, target), "] ") } append(constructor) @@ -143,4 +111,34 @@ private class WrappedSimpleType( if (isMarkedNullable) append("?") } } -} \ No newline at end of file +} + +// lowerBound is a subtype of upperBound +abstract class FlexibleType(val lowerBound: SimpleType, val upperBound: SimpleType) : + UnwrappedType(), SubtypingRepresentatives { + + abstract val delegate: SimpleType + + override val subTypeRepresentative: KotlinType + get() = lowerBound + override val superTypeRepresentative: KotlinType + get() = upperBound + + override fun sameTypeConstructor(type: KotlinType) = false + + abstract fun render(renderer: DescriptorRenderer, options: DescriptorRendererOptions): String + + override val annotations: Annotations get() = delegate.annotations + override val constructor: TypeConstructor get() = delegate.constructor + override val arguments: List get() = delegate.arguments + override val isMarkedNullable: Boolean get() = delegate.isMarkedNullable + override val memberScope: MemberScope get() = delegate.memberScope + override val isError: Boolean get() = false + + override fun toString(): String = DescriptorRenderer.DEBUG_TEXT.renderType(this) +} + +@Deprecated("Temporary marker method for refactoring") +fun KotlinType.asSimpleType(): SimpleType { + return unwrap() as SimpleType +} diff --git a/core/descriptors/src/org/jetbrains/kotlin/types/KotlinTypeFactory.kt b/core/descriptors/src/org/jetbrains/kotlin/types/KotlinTypeFactory.kt index 2bc772662f4..05026b795fe 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/types/KotlinTypeFactory.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/types/KotlinTypeFactory.kt @@ -29,14 +29,14 @@ object KotlinTypeFactory { arguments: List, nullable: Boolean, memberScope: MemberScope - ): SimpleType = KotlinTypeImpl.create(annotations, constructor, nullable, arguments, memberScope) + ): SimpleType = SimpleTypeImpl(annotations, constructor, arguments, nullable, memberScope) @JvmStatic fun simpleNotNullType( annotations: Annotations, descriptor: ClassDescriptor, arguments: List - ): SimpleType = KotlinTypeImpl.create(annotations, descriptor.typeConstructor, false, arguments, descriptor.getMemberScope(arguments)) + ): SimpleType = SimpleTypeImpl(annotations, descriptor.typeConstructor, arguments, false, descriptor.getMemberScope(arguments)) @JvmStatic fun simpleType( @@ -49,8 +49,28 @@ object KotlinTypeFactory { ): SimpleType = simpleType(annotations, constructor, arguments, nullable, memberScope) @JvmStatic - fun flexibleType(lowerBound: SimpleType, upperBound: SimpleType): KotlinType { + fun flexibleType(lowerBound: SimpleType, upperBound: SimpleType): UnwrappedType { if (lowerBound == upperBound) return lowerBound return FlexibleTypeImpl(lowerBound, upperBound) } +} + +private class SimpleTypeImpl( + override val annotations: Annotations, + override val constructor: TypeConstructor, + override val arguments: List, + override val isMarkedNullable: Boolean, + override val memberScope: MemberScope +) : SimpleType() { + override fun replaceAnnotations(newAnnotations: Annotations) = SimpleTypeImpl(newAnnotations, constructor, arguments, isMarkedNullable, memberScope) + override fun makeNullableAsSpecified(newNullability: Boolean) = SimpleTypeImpl(annotations, constructor, arguments, newNullability, memberScope) + + override val isError: Boolean + get() = false + + init { + if (memberScope is ErrorUtils.ErrorScope) { + throw IllegalStateException("SimpleTypeImpl should not be created for error type: $memberScope\n$constructor") + } + } } \ No newline at end of file diff --git a/core/descriptors/src/org/jetbrains/kotlin/types/KotlinTypeImpl.kt b/core/descriptors/src/org/jetbrains/kotlin/types/KotlinTypeImpl.kt deleted file mode 100644 index fb8884ff1d9..00000000000 --- a/core/descriptors/src/org/jetbrains/kotlin/types/KotlinTypeImpl.kt +++ /dev/null @@ -1,83 +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.annotations.Annotations -import org.jetbrains.kotlin.resolve.scopes.MemberScope - -open class KotlinTypeImpl -internal constructor( - override val annotations: Annotations, - final override val constructor: TypeConstructor, - override val isMarkedNullable: Boolean, - override val arguments: List, - final override val memberScope: MemberScope -) : AbstractKotlinType(), SimpleType { - - companion object { - - @Deprecated("", ReplaceWith("KotlinTypeFactory.simpleType(annotations, constructor, arguments, nullable, memberScope)", "org.jetbrains.kotlin.types.KotlinTypeFactory")) - @JvmStatic fun create(annotations: Annotations, - constructor: TypeConstructor, - nullable: Boolean, - arguments: List, - memberScope: MemberScope): KotlinTypeImpl - - = KotlinTypeImpl(annotations, constructor, nullable, arguments, memberScope) - - @JvmStatic fun create(annotations: Annotations, - constructor: TypeConstructor, - nullable: Boolean, - arguments: List, - memberScope: MemberScope, - abbreviatedType: SimpleType? = null - ): KotlinTypeImpl { - if (abbreviatedType != null) { - return WithCapabilities(annotations, constructor, nullable, arguments, memberScope, abbreviatedType) - } - return KotlinTypeImpl(annotations, constructor, nullable, arguments, memberScope) - } - - @Deprecated("", ReplaceWith("KotlinTypeFactory.simpleType(annotations, descriptor, arguments, nullable)", "org.jetbrains.kotlin.types.KotlinTypeFactory")) - @JvmStatic fun create(annotations: Annotations, - descriptor: ClassDescriptor, - nullable: Boolean, - arguments: List): KotlinTypeImpl - - = KotlinTypeImpl( - annotations, descriptor.typeConstructor, nullable, arguments, descriptor.getMemberScope(arguments) - ) - } - - private class WithCapabilities( - annotations: Annotations, - constructor: TypeConstructor, - nullable: Boolean, - arguments: List, - memberScope: MemberScope, - override val abbreviatedType: SimpleType? - ) : KotlinTypeImpl(annotations, constructor, nullable, arguments, memberScope) - - init { - if (memberScope is ErrorUtils.ErrorScope) { - throw IllegalStateException("JetTypeImpl should not be created for error type: $memberScope\n$constructor") - } - } - - override val isError: Boolean get() = false -} diff --git a/core/descriptors/src/org/jetbrains/kotlin/types/LazyType.java b/core/descriptors/src/org/jetbrains/kotlin/types/LazyType.java index c02915cc75b..3a4d19fdc31 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/types/LazyType.java +++ b/core/descriptors/src/org/jetbrains/kotlin/types/LazyType.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. @@ -16,5 +16,5 @@ package org.jetbrains.kotlin.types; -public interface LazyType extends KotlinType { +public interface LazyType { } diff --git a/core/descriptors/src/org/jetbrains/kotlin/types/SpecialTypes.kt b/core/descriptors/src/org/jetbrains/kotlin/types/SpecialTypes.kt new file mode 100644 index 00000000000..81dd26125b4 --- /dev/null +++ b/core/descriptors/src/org/jetbrains/kotlin/types/SpecialTypes.kt @@ -0,0 +1,47 @@ +/* + * 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.annotations.Annotations +import org.jetbrains.kotlin.resolve.scopes.MemberScope + +abstract class DelegatingSimpleType : SimpleType() { + protected abstract val delegate: SimpleType + + override val annotations: Annotations get() = delegate.annotations + override val constructor: TypeConstructor get() = delegate.constructor + override val arguments: List get() = delegate.arguments + override val isMarkedNullable: Boolean get() = delegate.isMarkedNullable + override val memberScope: MemberScope get() = delegate.memberScope +} + +private class AbbreviatedType(override val delegate: SimpleType, val abbreviatedType: SimpleType) : DelegatingSimpleType() { + override fun replaceAnnotations(newAnnotations: Annotations) + = AbbreviatedType(delegate.replaceAnnotations(newAnnotations), abbreviatedType) + + override fun makeNullableAsSpecified(newNullability: Boolean) + = AbbreviatedType(delegate.makeNullableAsSpecified(newNullability), abbreviatedType.makeNullableAsSpecified(newNullability)) + + override val isError: Boolean get() = false +} + +fun KotlinType.getAbbreviatedType(): SimpleType? = (unwrap() as? AbbreviatedType)?.abbreviatedType + +fun SimpleType.withAbbreviatedType(abbreviatedType: SimpleType): SimpleType { + if (isError) return this + return AbbreviatedType(this, abbreviatedType) +} diff --git a/core/descriptors/src/org/jetbrains/kotlin/types/TypeSubstitution.kt b/core/descriptors/src/org/jetbrains/kotlin/types/TypeSubstitution.kt index bef01b8436c..7bb18c25aaf 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/types/TypeSubstitution.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/types/TypeSubstitution.kt @@ -121,11 +121,11 @@ fun KotlinType.replace( if (newArguments.isEmpty() && newAnnotations === annotations) return this if (newArguments.isEmpty()) { - return KotlinTypeImpl.create( + return KotlinTypeFactory.simpleType( newAnnotations, constructor, - isMarkedNullable, arguments, + isMarkedNullable, memberScope ) } @@ -138,11 +138,11 @@ fun KotlinType.replace( declarationDescriptor.getMemberScope(newSubstitution) else ErrorUtils.createErrorScope("Unexpected declaration descriptor for type constructor: $constructor") - return KotlinTypeImpl.create( + return KotlinTypeFactory.simpleType( newAnnotations, constructor, - isMarkedNullable, newArguments, + isMarkedNullable, newScope ) } diff --git a/core/descriptors/src/org/jetbrains/kotlin/types/TypeSubstitutor.java b/core/descriptors/src/org/jetbrains/kotlin/types/TypeSubstitutor.java index 5e48c3d7154..2ef166be718 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/types/TypeSubstitutor.java +++ b/core/descriptors/src/org/jetbrains/kotlin/types/TypeSubstitutor.java @@ -133,7 +133,7 @@ public class TypeSubstitutor { // The type is within the substitution range, i.e. T or T? KotlinType type = originalProjection.getType(); - if (DynamicTypesKt.isDynamic(type) || KotlinTypeKt.unwrap(type) instanceof RawType) { + if (DynamicTypesKt.isDynamic(type) || type.unwrap() instanceof RawType) { return originalProjection; // todo investigate } diff --git a/core/descriptors/src/org/jetbrains/kotlin/types/TypeUtils.java b/core/descriptors/src/org/jetbrains/kotlin/types/TypeUtils.java index 84545606a88..7aa3b21f4d0 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/types/TypeUtils.java +++ b/core/descriptors/src/org/jetbrains/kotlin/types/TypeUtils.java @@ -36,7 +36,7 @@ public class TypeUtils { public static final SimpleType DONT_CARE = ErrorUtils.createErrorTypeWithCustomDebugName("DONT_CARE"); public static final SimpleType CANT_INFER_FUNCTION_PARAM_TYPE = ErrorUtils.createErrorType("Cannot be inferred"); - public static class SpecialType implements SimpleType { + public static class SpecialType extends DelegatingSimpleType { private final String name; public SpecialType(String name) { @@ -45,24 +45,7 @@ public class TypeUtils { @NotNull @Override - public TypeConstructor getConstructor() { - throw new IllegalStateException(name); - } - - @NotNull - @Override - public List getArguments() { - throw new IllegalStateException(name); - } - - @Override - public boolean isMarkedNullable() { - throw new IllegalStateException(name); - } - - @NotNull - @Override - public MemberScope getMemberScope() { + protected SimpleType getDelegate() { throw new IllegalStateException(name); } @@ -73,7 +56,13 @@ public class TypeUtils { @NotNull @Override - public Annotations getAnnotations() { + public SimpleType replaceAnnotations(@NotNull Annotations newAnnotations) { + throw new IllegalStateException(name); + } + + @NotNull + @Override + public SimpleType makeNullableAsSpecified(boolean newNullability) { throw new IllegalStateException(name); } @@ -81,12 +70,6 @@ public class TypeUtils { public String toString() { return name; } - - @Nullable - @Override - public SimpleType getAbbreviatedType() { - return null; - } } @NotNull @@ -129,17 +112,7 @@ public class TypeUtils { @NotNull public static KotlinType makeNullableAsSpecified(@NotNull KotlinType type, boolean nullable) { - KotlinType unwrappedType = KotlinTypeKt.unwrap(type); - if (unwrappedType instanceof TypeWithCustomReplacement) { - return ((TypeWithCustomReplacement) unwrappedType).makeNullableAsSpecified(nullable); - } - else { - SimpleType simpleType = (SimpleType) unwrappedType; - - if (!(simpleType instanceof LazyType) && simpleType.isMarkedNullable() == nullable) return simpleType; - - return KotlinTypeKt.lazyReplaceNullability(simpleType, nullable); - } + return type.unwrap().makeNullableAsSpecified(nullable); } @NotNull @@ -431,7 +404,7 @@ public class TypeUtils { if (type == null) return false; if (isSpecialType.invoke(type)) return true; - KotlinType unwrappedType = KotlinTypeKt.unwrap(type); + UnwrappedType unwrappedType = type.unwrap(); FlexibleType flexibleType = unwrappedType instanceof FlexibleType ? (FlexibleType) unwrappedType : null; if (flexibleType != null && (contains(flexibleType.getLowerBound(), isSpecialType) || contains(flexibleType.getUpperBound(), isSpecialType))) { diff --git a/core/descriptors/src/org/jetbrains/kotlin/types/TypeUtils.kt b/core/descriptors/src/org/jetbrains/kotlin/types/TypeUtils.kt index b6a7a62252a..a0d7f969f4c 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/types/TypeUtils.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/types/TypeUtils.kt @@ -85,10 +85,7 @@ fun TypeProjection.substitute(doSubstitute: (KotlinType) -> KotlinType): TypePro fun KotlinType.replaceAnnotations(newAnnotations: Annotations): KotlinType { if (annotations.isEmpty() && newAnnotations.isEmpty()) return this - val unwrappedType = unwrap() - if (unwrappedType is TypeWithCustomReplacement) return unwrappedType.replaceAnnotations(newAnnotations) - - return asSimpleType().lazyReplaceAnnotations(newAnnotations) + return unwrap().replaceAnnotations(newAnnotations) } fun KotlinTypeChecker.equalTypesOrNulls(type1: KotlinType?, type2: KotlinType?): Boolean { @@ -165,7 +162,7 @@ fun KotlinType.replaceArgumentsWithStarProjections(): KotlinType { ) } else { - return asSimpleType().replaceArgumentsWithStarProjections() + return (unwrapped as SimpleType).replaceArgumentsWithStarProjections() } } diff --git a/core/descriptors/src/org/jetbrains/kotlin/types/dynamicTypes.kt b/core/descriptors/src/org/jetbrains/kotlin/types/dynamicTypes.kt index d5f3d1b8a14..db6a6189b88 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/types/dynamicTypes.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/types/dynamicTypes.kt @@ -36,17 +36,15 @@ fun KotlinType.isDynamic(): Boolean = unwrap() is DynamicType fun createDynamicType(builtIns: KotlinBuiltIns) = DynamicType(builtIns, Annotations.EMPTY) -class DynamicType(builtIns: KotlinBuiltIns, override val annotations: Annotations) : DelegatingFlexibleType(builtIns.nothingType, builtIns.nullableAnyType) { - override val delegateType: KotlinType get() = upperBound +class DynamicType(builtIns: KotlinBuiltIns, override val annotations: Annotations) : FlexibleType(builtIns.nothingType, builtIns.nullableAnyType) { + override val delegate: SimpleType get() = upperBound - override fun makeNullableAsSpecified(nullable: Boolean): KotlinType { - // Nullability has no effect on dynamics - return createDynamicType(delegateType.builtIns) - } + // Nullability has no effect on dynamics + override fun makeNullableAsSpecified(newNullability: Boolean): DynamicType = this override val isMarkedNullable: Boolean get() = false - override fun replaceAnnotations(newAnnotations: Annotations): KotlinType = DynamicType(delegateType.builtIns, annotations) + override fun replaceAnnotations(newAnnotations: Annotations): DynamicType = DynamicType(delegate.builtIns, annotations) override fun render(renderer: DescriptorRenderer, options: DescriptorRendererOptions): String = "dynamic" } diff --git a/core/descriptors/src/org/jetbrains/kotlin/types/flexibleTypes.kt b/core/descriptors/src/org/jetbrains/kotlin/types/flexibleTypes.kt index 51cd02385a8..8737e9fb019 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/types/flexibleTypes.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/types/flexibleTypes.kt @@ -23,21 +23,6 @@ import org.jetbrains.kotlin.renderer.DescriptorRendererOptions import org.jetbrains.kotlin.types.checker.KotlinTypeChecker import org.jetbrains.kotlin.types.typeUtil.replaceAnnotations -interface FlexibleType : SubtypingRepresentatives, TypeWithCustomReplacement { - // lowerBound is a subtype of upperBound - val lowerBound: SimpleType - val upperBound: SimpleType - - override val subTypeRepresentative: KotlinType - get() = lowerBound - - override val superTypeRepresentative: KotlinType - get() = upperBound - - override fun sameTypeConstructor(type: KotlinType) = false - - fun render(renderer: DescriptorRenderer, options: DescriptorRendererOptions): String -} fun KotlinType.isFlexible(): Boolean = unwrap() is FlexibleType fun KotlinType.asFlexibleType(): FlexibleType = unwrap() as FlexibleType @@ -84,21 +69,18 @@ fun Collection.singleBestRepresentative(): TypeProjection? { fun KotlinType.lowerIfFlexible(): SimpleType = (if (this.isFlexible()) this.asFlexibleType().lowerBound else this).asSimpleType() fun KotlinType.upperIfFlexible(): SimpleType = (if (this.isFlexible()) this.asFlexibleType().upperBound else this).asSimpleType() -abstract class DelegatingFlexibleType protected constructor( - override val lowerBound: SimpleType, - override val upperBound: SimpleType -) : DelegatingType(), FlexibleType { +class FlexibleTypeImpl(lowerBound: SimpleType, upperBound: SimpleType) : FlexibleType(lowerBound, upperBound), CustomTypeVariable { companion object { @JvmField var RUN_SLOW_ASSERTIONS = false } - // These assertions are needed for checking invariants of flexible types. - // - // Unfortunately isSubtypeOf is running resolve for lazy types. - // Because of this we can't run these assertions when we are creating this type. See EA-74904 - // - // Also isSubtypeOf is not a very fast operation, so we are running assertions only if ASSERTIONS_ENABLED. See KT-7540 + // These assertions are needed for checking invariants of flexible types. + // + // Unfortunately isSubtypeOf is running resolve for lazy types. + // Because of this we can't run these assertions when we are creating this type. See EA-74904 + // + // Also isSubtypeOf is not a very fast operation, so we are running assertions only if ASSERTIONS_ENABLED. See KT-7540 private var assertionsDone = false private fun runAssertions() { @@ -113,39 +95,25 @@ abstract class DelegatingFlexibleType protected constructor( } } - protected abstract val delegateType: KotlinType - - override fun makeNullableAsSpecified(nullable: Boolean): KotlinType { - return KotlinTypeFactory.flexibleType(TypeUtils.makeNullableAsSpecified(lowerBound, nullable), TypeUtils.makeNullableAsSpecified(upperBound, nullable)) - } - - override fun getDelegate(): KotlinType { - runAssertions() - return delegateType - } - - override fun toString() = "('$lowerBound'..'$upperBound')" -} - -class FlexibleTypeImpl(lowerBound: SimpleType, upperBound: SimpleType) : DelegatingFlexibleType(lowerBound, upperBound), CustomTypeVariable { - - override val delegateType: KotlinType get() = lowerBound + override val delegate: SimpleType + get() { + runAssertions() + return lowerBound + } override val isTypeVariable: Boolean get() = lowerBound.constructor.declarationDescriptor is TypeParameterDescriptor && lowerBound.constructor == upperBound.constructor override fun substitutionResult(replacement: KotlinType): KotlinType { - if (replacement.isFlexible()) { - return replacement - } - else { - val simpleType = replacement.asSimpleType() - return KotlinTypeFactory.flexibleType(simpleType, TypeUtils.makeNullable(simpleType)) + val unwrapped = replacement.unwrap() + return when(unwrapped) { + is FlexibleType -> unwrapped + is SimpleType -> KotlinTypeFactory.flexibleType(unwrapped, TypeUtils.makeNullable(unwrapped)) } } - override fun replaceAnnotations(newAnnotations: Annotations): KotlinType - = KotlinTypeFactory.flexibleType(lowerBound.replaceAnnotations(newAnnotations).asSimpleType(), upperBound) + override fun replaceAnnotations(newAnnotations: Annotations): UnwrappedType + = KotlinTypeFactory.flexibleType(lowerBound.replaceAnnotations(newAnnotations), upperBound.replaceAnnotations(newAnnotations)) override fun render(renderer: DescriptorRenderer, options: DescriptorRendererOptions): String { if (options.debugMode) { @@ -153,4 +121,7 @@ class FlexibleTypeImpl(lowerBound: SimpleType, upperBound: SimpleType) : Delegat } return renderer.renderFlexibleType(renderer.renderType(lowerBound), renderer.renderType(upperBound)) } + + override fun makeNullableAsSpecified(newNullability: Boolean): UnwrappedType + = KotlinTypeFactory.flexibleType(lowerBound.makeNullableAsSpecified(newNullability), upperBound.makeNullableAsSpecified(newNullability)) } diff --git a/core/deserialization/src/org/jetbrains/kotlin/serialization/deserialization/DeserializedType.kt b/core/deserialization/src/org/jetbrains/kotlin/serialization/deserialization/DeserializedType.kt index 7b24e017c6e..fd0508937b5 100644 --- a/core/deserialization/src/org/jetbrains/kotlin/serialization/deserialization/DeserializedType.kt +++ b/core/deserialization/src/org/jetbrains/kotlin/serialization/deserialization/DeserializedType.kt @@ -24,7 +24,7 @@ import org.jetbrains.kotlin.storage.getValue import org.jetbrains.kotlin.types.* import org.jetbrains.kotlin.utils.toReadOnlyList -class DeserializedType( +class DeserializedType private constructor( private val c: DeserializationContext, private val typeProto: ProtoBuf.Type, private val additionalAnnotations: Annotations = Annotations.EMPTY @@ -54,9 +54,18 @@ class DeserializedType( return descriptor != null && ErrorUtils.isError(descriptor) } - override val abbreviatedType by c.storageManager.createNullableLazyValue { - val abbreviatedTypeProto = typeProto.abbreviatedType(c.typeTable) ?: return@createNullableLazyValue null + 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 - c.typeDeserializer.type(abbreviatedTypeProto, additionalAnnotations).asSimpleType() + return deserializedType.withAbbreviatedType(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 262b1163ade..6d3ac528683 100644 --- a/core/deserialization/src/org/jetbrains/kotlin/serialization/deserialization/TypeDeserializer.kt +++ b/core/deserialization/src/org/jetbrains/kotlin/serialization/deserialization/TypeDeserializer.kt @@ -58,12 +58,12 @@ class TypeDeserializer( fun type(proto: ProtoBuf.Type, additionalAnnotations: Annotations = Annotations.EMPTY): KotlinType { if (proto.hasFlexibleTypeCapabilitiesId()) { val id = c.nameResolver.getString(proto.flexibleTypeCapabilitiesId) - val lowerBound = DeserializedType(c, proto, additionalAnnotations) - val upperBound = DeserializedType(c, proto.flexibleUpperBound(c.typeTable)!!, additionalAnnotations) + val lowerBound = DeserializedType.create(c, proto, additionalAnnotations) + val upperBound = DeserializedType.create(c, proto.flexibleUpperBound(c.typeTable)!!, additionalAnnotations) return c.components.flexibleTypeDeserializer.create(proto, id, lowerBound, upperBound) } - return DeserializedType(c, proto, additionalAnnotations) + return DeserializedType.create(c, proto, additionalAnnotations) } fun typeConstructor(proto: ProtoBuf.Type): TypeConstructor = diff --git a/idea/ide-common/src/org/jetbrains/kotlin/idea/util/Utils.kt b/idea/ide-common/src/org/jetbrains/kotlin/idea/util/Utils.kt index 80572e98ae0..db2ed864a3a 100644 --- a/idea/ide-common/src/org/jetbrains/kotlin/idea/util/Utils.kt +++ b/idea/ide-common/src/org/jetbrains/kotlin/idea/util/Utils.kt @@ -26,7 +26,6 @@ import org.jetbrains.kotlin.resolve.scopes.receivers.ReceiverValue import org.jetbrains.kotlin.types.FlexibleType import org.jetbrains.kotlin.types.KotlinType import org.jetbrains.kotlin.types.checker.KotlinTypeChecker -import org.jetbrains.kotlin.types.unwrap fun KtFunctionLiteral.findLabelAndCall(): Pair { val literalParent = (this.parent as KtLambdaExpression).parent diff --git a/idea/src/org/jetbrains/kotlin/idea/refactoring/changeSignature/changeSignatureUtils.kt b/idea/src/org/jetbrains/kotlin/idea/refactoring/changeSignature/changeSignatureUtils.kt index 627fed8244b..b503773cfe3 100644 --- a/idea/src/org/jetbrains/kotlin/idea/refactoring/changeSignature/changeSignatureUtils.kt +++ b/idea/src/org/jetbrains/kotlin/idea/refactoring/changeSignature/changeSignatureUtils.kt @@ -89,8 +89,7 @@ private object ForceTypeCopySubstitution : TypeSubstitution() { override fun get(key: KotlinType) = with(key) { if (isError) return@with asTypeProjection() - KotlinTypeImpl.create( - annotations, constructor, isMarkedNullable, arguments, memberScope).asTypeProjection() + KotlinTypeFactory.simpleType(annotations, constructor, arguments, isMarkedNullable, memberScope).asTypeProjection() } override fun isEmpty() = false diff --git a/js/js.serializer/src/org/jetbrains/kotlin/serialization/js/KotlinJavascriptSerializerExtension.kt b/js/js.serializer/src/org/jetbrains/kotlin/serialization/js/KotlinJavascriptSerializerExtension.kt index dcf865b2788..a9564f20034 100644 --- a/js/js.serializer/src/org/jetbrains/kotlin/serialization/js/KotlinJavascriptSerializerExtension.kt +++ b/js/js.serializer/src/org/jetbrains/kotlin/serialization/js/KotlinJavascriptSerializerExtension.kt @@ -20,10 +20,10 @@ import com.google.protobuf.ExtensionRegistryLite import org.jetbrains.kotlin.serialization.KotlinSerializerExtensionBase import org.jetbrains.kotlin.serialization.ProtoBuf import org.jetbrains.kotlin.serialization.SerializerExtensionProtocol -import org.jetbrains.kotlin.types.DelegatingFlexibleType +import org.jetbrains.kotlin.types.FlexibleType class KotlinJavascriptSerializerExtension : KotlinSerializerExtensionBase(JsSerializerProtocol) { - override fun serializeFlexibleType(flexibleType: DelegatingFlexibleType, lowerProto: ProtoBuf.Type.Builder, upperProto: ProtoBuf.Type.Builder) { + override fun serializeFlexibleType(flexibleType: FlexibleType, lowerProto: ProtoBuf.Type.Builder, upperProto: ProtoBuf.Type.Builder) { lowerProto.flexibleTypeCapabilitiesId = stringTable.getStringIndex(DynamicTypeDeserializer.id) } }