diff --git a/compiler/fir/providers/src/org/jetbrains/kotlin/fir/types/ConeInferenceContext.kt b/compiler/fir/providers/src/org/jetbrains/kotlin/fir/types/ConeInferenceContext.kt index 9636f9805c6..bf8859163de 100644 --- a/compiler/fir/providers/src/org/jetbrains/kotlin/fir/types/ConeInferenceContext.kt +++ b/compiler/fir/providers/src/org/jetbrains/kotlin/fir/types/ConeInferenceContext.kt @@ -58,10 +58,10 @@ interface ConeInferenceContext : TypeSystemInferenceExtensionContext, ConeTypeCo arguments: List, nullable: Boolean, isExtensionFunction: Boolean, - annotations: List? + attributes: List? ): SimpleTypeMarker { - val attributesList = annotations?.filterIsInstanceTo, MutableList>>(mutableListOf()) - val attributes: ConeAttributes = if (isExtensionFunction) { + val attributesList = attributes?.filterIsInstanceTo, MutableList>>(mutableListOf()) + val coneAttributes: ConeAttributes = if (isExtensionFunction) { require(constructor is ConeClassLikeLookupTag && constructor.isBuiltinFunctionalType()) // We don't want to create new instance of ConeAttributes which // contains only CompilerConeAttributes.ExtensionFunctionType @@ -81,12 +81,12 @@ interface ConeInferenceContext : TypeSystemInferenceExtensionContext, ConeTypeCo constructor, (arguments as List).toTypedArray(), nullable, - attributes, + coneAttributes, ) is ConeTypeParameterLookupTag -> ConeTypeParameterTypeImpl( constructor, nullable, - attributes + coneAttributes ) else -> error("!") } diff --git a/compiler/fir/providers/src/org/jetbrains/kotlin/fir/types/ConeTypeContext.kt b/compiler/fir/providers/src/org/jetbrains/kotlin/fir/types/ConeTypeContext.kt index fdee372f5ad..a04392d60ef 100644 --- a/compiler/fir/providers/src/org/jetbrains/kotlin/fir/types/ConeTypeContext.kt +++ b/compiler/fir/providers/src/org/jetbrains/kotlin/fir/types/ConeTypeContext.kt @@ -420,6 +420,12 @@ interface ConeTypeContext : TypeSystemContext, TypeSystemOptimizationContext, Ty return attributes.toList() } + override fun KotlinTypeMarker.hasCustomAttributes(): Boolean { + require(this is ConeKotlinType) + val compilerAttributes = CompilerConeAttributes.classIdByCompilerAttribute + return this.attributes.any { it !in compilerAttributes && it !is CustomAnnotationTypeAttribute } + } + override fun KotlinTypeMarker.getCustomAttributes(): List { require(this is ConeKotlinType) val compilerAttributes = CompilerConeAttributes.classIdByCompilerAttribute diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/checkers/PassingProgressionAsCollectionCallChecker.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/checkers/PassingProgressionAsCollectionCallChecker.kt index c0949a8cae9..839cd737b1d 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/checkers/PassingProgressionAsCollectionCallChecker.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/checkers/PassingProgressionAsCollectionCallChecker.kt @@ -145,7 +145,7 @@ class PassingProgressionAsCollectionCallChecker(private val kotlinCallResolver: private fun makeCollectionOfAnyType(builtIns: KotlinBuiltIns): KotlinType = KotlinTypeFactory.simpleNotNullType( - Annotations.EMPTY, + TypeAttributes.Empty, builtIns.collection, listOf(TypeProjectionImpl(builtIns.nullableAnyType)) ) @@ -162,7 +162,7 @@ class PassingProgressionAsCollectionCallChecker(private val kotlinCallResolver: val newType = intersectTypes( listOf( KotlinTypeFactory.simpleNotNullType( - Annotations.EMPTY, + TypeAttributes.Empty, builtIns.collection, listOf(TypeProjectionImpl(progressionOrRangeElementType)) ), diff --git a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/types/IrTypeSystemContext.kt b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/types/IrTypeSystemContext.kt index a583cdc4d8a..56837cc4936 100644 --- a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/types/IrTypeSystemContext.kt +++ b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/types/IrTypeSystemContext.kt @@ -332,10 +332,10 @@ interface IrTypeSystemContext : TypeSystemContext, TypeSystemCommonSuperTypesCon arguments: List, nullable: Boolean, isExtensionFunction: Boolean, - annotations: List? + attributes: List? ): SimpleTypeMarker { - val ourAnnotations = annotations?.filterIsInstance() - require(ourAnnotations?.size == annotations?.size) + val ourAnnotations = attributes?.filterIsInstance() + require(ourAnnotations?.size == attributes?.size) return IrSimpleTypeImpl( constructor as IrClassifierSymbol, nullable, @@ -406,6 +406,10 @@ interface IrTypeSystemContext : TypeSystemContext, TypeSystemCommonSuperTypesCon return this.annotations.map { object : AnnotationMarker, IrElement by it {} } } + override fun KotlinTypeMarker.hasCustomAttributes(): Boolean { + return false + } + override fun KotlinTypeMarker.getCustomAttributes(): List { require(this is IrType) return emptyList() diff --git a/compiler/resolution.common/src/org/jetbrains/kotlin/resolve/calls/NewCommonSuperTypeCalculator.kt b/compiler/resolution.common/src/org/jetbrains/kotlin/resolve/calls/NewCommonSuperTypeCalculator.kt index 4a8a07a2738..1b9051e4a62 100644 --- a/compiler/resolution.common/src/org/jetbrains/kotlin/resolve/calls/NewCommonSuperTypeCalculator.kt +++ b/compiler/resolution.common/src/org/jetbrains/kotlin/resolve/calls/NewCommonSuperTypeCalculator.kt @@ -15,7 +15,7 @@ import org.jetbrains.kotlin.types.model.* object NewCommonSuperTypeCalculator { fun TypeSystemCommonSuperTypesContext.commonSuperType(types: List): KotlinTypeMarker { val maxDepth = types.maxOfOrNull { it.typeDepth() } ?: 0 - return commonSuperType(types, -maxDepth, true).replaceTypeAttributes(unionTypeAttributes(types)) + return commonSuperType(types, -maxDepth, true).let { it.replaceTypeAttributes(unionTypeAttributes(listOf(it) + types)) } } private fun TypeSystemCommonSuperTypesContext.commonSuperType( diff --git a/compiler/resolution.common/src/org/jetbrains/kotlin/resolve/calls/inference/components/PostponedArgumentInputTypesResolver.kt b/compiler/resolution.common/src/org/jetbrains/kotlin/resolve/calls/inference/components/PostponedArgumentInputTypesResolver.kt index 06d4d9142f7..3aad91c438d 100644 --- a/compiler/resolution.common/src/org/jetbrains/kotlin/resolve/calls/inference/components/PostponedArgumentInputTypesResolver.kt +++ b/compiler/resolution.common/src/org/jetbrains/kotlin/resolve/calls/inference/components/PostponedArgumentInputTypesResolver.kt @@ -411,7 +411,7 @@ class PostponedArgumentInputTypesResolver( argument.isFunctionExpressionWithReceiver() -> true else -> parameterTypesInfo.isExtensionFunction }, - annotations = parameterTypesInfo.annotations + attributes = parameterTypesInfo.annotations ) getBuilder().addSubtypeConstraint( diff --git a/compiler/resolution.common/src/org/jetbrains/kotlin/resolve/calls/inference/model/MutableConstraintStorage.kt b/compiler/resolution.common/src/org/jetbrains/kotlin/resolve/calls/inference/model/MutableConstraintStorage.kt index 54d6fd39478..5a8ce969dee 100644 --- a/compiler/resolution.common/src/org/jetbrains/kotlin/resolve/calls/inference/model/MutableConstraintStorage.kt +++ b/compiler/resolution.common/src/org/jetbrains/kotlin/resolve/calls/inference/model/MutableConstraintStorage.kt @@ -59,14 +59,18 @@ class MutableVariableWithConstraints private constructor( && previousConstraint.type == constraint.type && previousConstraint.isNullabilityConstraint == constraint.isNullabilityConstraint ) { + val noNewCustomAttributes = with(context) { + val previousType = previousConstraint.type + val type = constraint.type + (!previousType.hasCustomAttributes() && !type.hasCustomAttributes()) || + (previousType.getCustomAttributes() == type.getCustomAttributes()) + } + if (newConstraintIsUseless(previousConstraint, constraint)) { // Preserve constraints with different custom type attributes. // This allows us to union type attributes in NewCommonSuperTypeCalculator.kt - with(context) { - val prevAttributes = previousConstraint.type.getCustomAttributes() - if (prevAttributes.isEmpty() || prevAttributes == constraint.type.getCustomAttributes()) { - return previousConstraint to false - } + if (noNewCustomAttributes) { + return previousConstraint to false } } @@ -75,7 +79,7 @@ class MutableVariableWithConstraints private constructor( ConstraintKind.UPPER -> constraint.kind.isLower() ConstraintKind.EQUALITY -> true } - if (isMatchingForSimplification) { + if (isMatchingForSimplification && noNewCustomAttributes) { val actualConstraint = if (constraint.kind != ConstraintKind.EQUALITY) { Constraint( ConstraintKind.EQUALITY, diff --git a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/components/NewTypeSubstitutor.kt b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/components/NewTypeSubstitutor.kt index aafa6f9846d..0caea49d935 100644 --- a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/components/NewTypeSubstitutor.kt +++ b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/components/NewTypeSubstitutor.kt @@ -6,7 +6,6 @@ package org.jetbrains.kotlin.resolve.calls.inference.components import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor -import org.jetbrains.kotlin.descriptors.annotations.CompositeAnnotations import org.jetbrains.kotlin.resolve.calls.inference.isCaptured import org.jetbrains.kotlin.resolve.calls.inference.model.TypeVariableFromCallableDescriptor import org.jetbrains.kotlin.resolve.calls.inference.substitute diff --git a/core/compiler.common/src/org/jetbrains/kotlin/types/model/TypeSystemContext.kt b/core/compiler.common/src/org/jetbrains/kotlin/types/model/TypeSystemContext.kt index 39a455e736f..a7b929e9c7c 100644 --- a/core/compiler.common/src/org/jetbrains/kotlin/types/model/TypeSystemContext.kt +++ b/core/compiler.common/src/org/jetbrains/kotlin/types/model/TypeSystemContext.kt @@ -80,7 +80,7 @@ interface TypeSystemTypeFactoryContext: TypeSystemBuiltInsContext { arguments: List, nullable: Boolean, isExtensionFunction: Boolean = false, - annotations: List? = null + attributes: List? = null ): SimpleTypeMarker fun createTypeArgument(type: KotlinTypeMarker, variance: TypeVariance): TypeArgumentMarker @@ -500,6 +500,8 @@ interface TypeSystemContext : TypeSystemOptimizationContext { fun KotlinTypeMarker.getAttributes(): List + fun KotlinTypeMarker.hasCustomAttributes(): Boolean + fun KotlinTypeMarker.getCustomAttributes(): List fun substitutionSupertypePolicy(type: SimpleTypeMarker): TypeCheckerState.SupertypesPolicy 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 41072a385d3..3fa2a6ecef0 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 @@ -78,9 +78,6 @@ class CapturedType( override val attributes: TypeAttributes = TypeAttributes.Empty ) : SimpleType(), SubtypingRepresentatives, CapturedTypeMarker { - override val annotations: Annotations - get() = attributes.toDefaultAnnotations() - override val arguments: List get() = listOf() diff --git a/core/descriptors/src/org/jetbrains/kotlin/types/AnnotationsTypeAttribute.kt b/core/descriptors/src/org/jetbrains/kotlin/types/AnnotationsTypeAttribute.kt index 9f3a2b3ffdc..8bc7630d753 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/types/AnnotationsTypeAttribute.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/types/AnnotationsTypeAttribute.kt @@ -5,31 +5,24 @@ package org.jetbrains.kotlin.types -import org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptor import org.jetbrains.kotlin.descriptors.annotations.Annotations +import org.jetbrains.kotlin.descriptors.annotations.composeAnnotations import kotlin.reflect.KClass class AnnotationsTypeAttribute(val annotations: Annotations) : TypeAttribute() { - constructor(annotations: List) : this(Annotations.create(annotations)) + override fun union(other: AnnotationsTypeAttribute?): AnnotationsTypeAttribute = this - override fun union(other: AnnotationsTypeAttribute?): AnnotationsTypeAttribute? = null - - override fun intersect(other: AnnotationsTypeAttribute?): AnnotationsTypeAttribute? = null + override fun intersect(other: AnnotationsTypeAttribute?): AnnotationsTypeAttribute = this override fun add(other: AnnotationsTypeAttribute?): AnnotationsTypeAttribute { if (other == null) return this - return AnnotationsTypeAttribute(Annotations.create(annotations + other.annotations)) + return AnnotationsTypeAttribute(composeAnnotations(annotations, other.annotations)) } override fun isSubtypeOf(other: AnnotationsTypeAttribute?): Boolean = true override val key: KClass 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() diff --git a/core/descriptors/src/org/jetbrains/kotlin/types/ErrorType.kt b/core/descriptors/src/org/jetbrains/kotlin/types/ErrorType.kt index 2fbfe2e4b2b..c0a945ef2b9 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/types/ErrorType.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/types/ErrorType.kt @@ -27,9 +27,6 @@ open class ErrorType @JvmOverloads internal constructor( override val isMarkedNullable: Boolean = false, open val presentableName: String = "???" ) : SimpleType() { - override val annotations: Annotations - get() = Annotations.EMPTY - override val attributes: TypeAttributes get() = TypeAttributes.Empty diff --git a/core/descriptors/src/org/jetbrains/kotlin/types/KotlinType.kt b/core/descriptors/src/org/jetbrains/kotlin/types/KotlinType.kt index b84c606319d..15b0118f640 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/types/KotlinType.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/types/KotlinType.kt @@ -52,6 +52,8 @@ sealed class KotlinType : Annotated, KotlinTypeMarker { abstract val isMarkedNullable: Boolean abstract val memberScope: MemberScope abstract val attributes: TypeAttributes + override val annotations: Annotations + get() = attributes.annotations abstract fun unwrap(): UnwrappedType @@ -125,7 +127,6 @@ abstract class WrappedType : KotlinType() { open fun isComputed(): Boolean = true protected abstract val delegate: KotlinType - 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 @@ -210,12 +211,11 @@ abstract class FlexibleType(val lowerBound: SimpleType, val upperBound: SimpleTy abstract fun render(renderer: DescriptorRenderer, options: DescriptorRendererOptions): String - override val annotations: Annotations get() = delegate.annotations + override val attributes: TypeAttributes get() = delegate.attributes 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 attributes: TypeAttributes get() = delegate.attributes override fun toString(): String = DescriptorRenderer.DEBUG_TEXT.renderType(this) diff --git a/core/descriptors/src/org/jetbrains/kotlin/types/KotlinTypeFactory.kt b/core/descriptors/src/org/jetbrains/kotlin/types/KotlinTypeFactory.kt index 7efd3979202..8c97ddac9d2 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/types/KotlinTypeFactory.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/types/KotlinTypeFactory.kt @@ -206,7 +206,6 @@ private class SimpleTypeImpl( @TypeRefinement override val hasNotTrivialRefinementFactory: Boolean get() = true - override val annotations: Annotations get() = Annotations.EMPTY override val attributes: TypeAttributes get() = TypeAttributes.Empty override fun replaceAttributes(newAttributes: TypeAttributes) = @@ -254,7 +253,6 @@ class SupposititiousSimpleType(private val realType: SimpleType, val overwritten override val arguments: List = realType.arguments override val isMarkedNullable: Boolean = realType.isMarkedNullable override val memberScope: MemberScope = realType.memberScope - override val annotations: Annotations = realType.annotations override val attributes: TypeAttributes get() = realType.attributes } @@ -275,8 +273,6 @@ 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) } diff --git a/core/descriptors/src/org/jetbrains/kotlin/types/SpecialTypes.kt b/core/descriptors/src/org/jetbrains/kotlin/types/SpecialTypes.kt index aaa9037c868..9c436a7ac3e 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/types/SpecialTypes.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/types/SpecialTypes.kt @@ -30,7 +30,6 @@ import org.jetbrains.kotlin.types.model.DefinitelyNotNullTypeMarker 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 diff --git a/core/descriptors/src/org/jetbrains/kotlin/types/StubTypes.kt b/core/descriptors/src/org/jetbrains/kotlin/types/StubTypes.kt index 8e9650e358a..92098a45196 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/types/StubTypes.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/types/StubTypes.kt @@ -58,9 +58,6 @@ abstract class AbstractStubType(val originalTypeVariable: TypeConstructor, overr override val arguments: List get() = emptyList() - override val annotations: Annotations - get() = Annotations.EMPTY - override val attributes: TypeAttributes get() = TypeAttributes.Empty diff --git a/core/descriptors/src/org/jetbrains/kotlin/types/TypeAliasExpander.kt b/core/descriptors/src/org/jetbrains/kotlin/types/TypeAliasExpander.kt index 1522d9a4474..20cd671e002 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/types/TypeAliasExpander.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/types/TypeAliasExpander.kt @@ -135,7 +135,7 @@ class TypeAliasExpander( replaceAttributes(createdCombinedAttributes(newAttributes)) private fun SimpleType.combineAttributes(newAttributes: TypeAttributes): SimpleType = - if (isError) this else replaceAttributes(newAttributes = createdCombinedAttributes(newAttributes)) + if (isError) this else replace(newAttributes = createdCombinedAttributes(newAttributes)) private fun KotlinType.createdCombinedAttributes(newAttributes: TypeAttributes): TypeAttributes { if (isError) return attributes diff --git a/core/descriptors/src/org/jetbrains/kotlin/types/TypeAttributeTranslator.kt b/core/descriptors/src/org/jetbrains/kotlin/types/TypeAttributeTranslator.kt index 86f3485750f..1b26d4125c4 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/types/TypeAttributeTranslator.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/types/TypeAttributeTranslator.kt @@ -25,7 +25,9 @@ object DefaultTypeAttributeTranslator : TypeAttributeTranslator { typeConstructor: TypeConstructor?, containingDeclaration: DeclarationDescriptor? ): TypeAttributes { - return TypeAttributes.create(listOf(AnnotationsTypeAttribute(annotations))) + return if (annotations.isEmpty()) + TypeAttributes.Empty else + TypeAttributes.create(listOf(AnnotationsTypeAttribute(annotations))) } override fun toAnnotations(attributes: TypeAttributes): Annotations { diff --git a/core/descriptors/src/org/jetbrains/kotlin/types/TypeAttributes.kt b/core/descriptors/src/org/jetbrains/kotlin/types/TypeAttributes.kt index ad406aa5dfa..2ddf3c769ad 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/types/TypeAttributes.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/types/TypeAttributes.kt @@ -51,7 +51,7 @@ class TypeAttributes private constructor(attributes: List>) : A } } - val Empty: TypeAttributes = TypeAttributes(listOf(AnnotationsTypeAttribute(Annotations.EMPTY))) + val Empty: TypeAttributes = TypeAttributes(emptyList()) fun create(attributes: List>): TypeAttributes { return if (attributes.isEmpty()) { @@ -70,6 +70,8 @@ class TypeAttributes private constructor(attributes: List>) : A } } + val size: Int get() = arrayMap.size + fun union(other: TypeAttributes): TypeAttributes { return perform(other) { this.union(it) } } @@ -87,15 +89,11 @@ class TypeAttributes private constructor(attributes: List>) : A return arrayMap[index] != null } - @OptIn(ExperimentalStdlibApi::class) operator fun plus(attribute: TypeAttribute<*>): TypeAttributes { if (attribute in this) return this if (isEmpty()) return TypeAttributes(attribute) - val newAttributes = buildList { - addAll(this) - add(attribute) - } - return TypeAttributes(newAttributes) + val newAttributes = this.toList() + attribute + return create(newAttributes) } fun remove(attribute: TypeAttribute<*>): TypeAttributes { @@ -119,6 +117,7 @@ class TypeAttributes private constructor(attributes: List>) : A override val typeRegistry: TypeRegistry, TypeAttribute<*>> get() = Companion + } fun TypeAttributes.toDefaultAnnotations(): Annotations = @@ -127,6 +126,9 @@ fun TypeAttributes.toDefaultAnnotations(): Annotations = fun Annotations.toDefaultAttributes(): TypeAttributes = DefaultTypeAttributeTranslator.toAttributes(this) fun TypeAttributes.replaceAnnotations(newAnnotations: Annotations): TypeAttributes { - val withoutCustom = (annotationsAttribute?.let { this.remove(it) } ?: this) - return withoutCustom.add(newAnnotations.toDefaultAttributes()) + if (annotations === newAnnotations) return this + val withoutAnnotations = annotationsAttribute?.let { this.remove(it) } ?: this + // Check if iterator hasNext to handle FilteredAnnotations.isEmpty() with OldInference + if (!newAnnotations.iterator().hasNext() && newAnnotations.isEmpty()) return withoutAnnotations + return withoutAnnotations.plus(AnnotationsTypeAttribute(newAnnotations)) } diff --git a/core/descriptors/src/org/jetbrains/kotlin/types/TypeSubstitution.kt b/core/descriptors/src/org/jetbrains/kotlin/types/TypeSubstitution.kt index ad6943ebd16..69da4e34ae4 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/types/TypeSubstitution.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/types/TypeSubstitution.kt @@ -18,6 +18,7 @@ package org.jetbrains.kotlin.types import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor import org.jetbrains.kotlin.descriptors.annotations.Annotations +import org.jetbrains.kotlin.descriptors.annotations.FilteredAnnotations abstract class TypeSubstitution { companion object { @@ -50,6 +51,7 @@ abstract class TypeSubstitution { override fun filterAnnotations(annotations: Annotations) = this@TypeSubstitution.filterAnnotations(annotations) override fun prepareTopLevelType(topLevelType: KotlinType, position: Variance) = this@TypeSubstitution.prepareTopLevelType(topLevelType, position) + override fun isEmpty() = this@TypeSubstitution.isEmpty() } } @@ -144,23 +146,26 @@ fun KotlinType.replace( ): KotlinType { if ((newArguments.isEmpty() || newArguments === arguments) && newAnnotations === annotations) return this + val newAttributes = attributes.replaceAnnotations( + // Specially handle FilteredAnnotations here due to FilteredAnnotations.isEmpty() + if (newAnnotations is FilteredAnnotations && newAnnotations.isEmpty()) Annotations.EMPTY else newAnnotations + ) + return when (val unwrapped = unwrap()) { is FlexibleType -> KotlinTypeFactory.flexibleType( - unwrapped.lowerBound.replace(newArguments, newAnnotations), - unwrapped.upperBound.replace(newArgumentsForUpperBound, newAnnotations) + unwrapped.lowerBound.replace(newArguments, newAttributes), + unwrapped.upperBound.replace(newArgumentsForUpperBound, newAttributes) ) - is SimpleType -> unwrapped.replace(newArguments, newAnnotations) + is SimpleType -> unwrapped.replace(newArguments, newAttributes) } } @JvmOverloads fun SimpleType.replace( newArguments: List = arguments, - newAnnotations: Annotations = annotations + newAttributes: TypeAttributes = attributes ): SimpleType { - if (newArguments.isEmpty() && newAnnotations === annotations) return this - - val newAttributes = attributes.replaceAnnotations(newAnnotations) + if (newArguments.isEmpty() && newAttributes === attributes) return this if (newArguments.isEmpty()) { return replaceAttributes(newAttributes) diff --git a/core/descriptors/src/org/jetbrains/kotlin/types/checker/ClassicTypeSystemContext.kt b/core/descriptors/src/org/jetbrains/kotlin/types/checker/ClassicTypeSystemContext.kt index ad4d966a32f..1b32aad3973 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/types/checker/ClassicTypeSystemContext.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/types/checker/ClassicTypeSystemContext.kt @@ -26,6 +26,7 @@ import org.jetbrains.kotlin.resolve.unsubstitutedUnderlyingType import org.jetbrains.kotlin.types.* import org.jetbrains.kotlin.types.model.* import org.jetbrains.kotlin.types.typeUtil.* +import org.jetbrains.kotlin.utils.addToStdlib.firstIsInstanceOrNull import org.jetbrains.kotlin.utils.addToStdlib.safeAs import org.jetbrains.kotlin.types.typeUtil.isSignedOrUnsignedNumberType as classicIsSignedOrUnsignedNumberType @@ -516,12 +517,11 @@ interface ClassicTypeSystemContext : TypeSystemInferenceExtensionContext, TypeSy arguments: List, nullable: Boolean, isExtensionFunction: Boolean, - annotations: List? + attributes: List? ): SimpleTypeMarker { require(constructor is TypeConstructor, constructor::errorMessage) - val ourAnnotations = annotations?.filterIsInstance() - require(ourAnnotations?.size == annotations?.size) + val ourAnnotations = attributes?.firstIsInstanceOrNull()?.annotations?.toList() fun createExtensionFunctionAnnotation() = BuiltInAnnotationDescriptor(builtIns, FqNames.extensionFunctionType, emptyMap()) @@ -673,6 +673,11 @@ interface ClassicTypeSystemContext : TypeSystemInferenceExtensionContext, TypeSy return this.attributes.toList() } + override fun KotlinTypeMarker.hasCustomAttributes(): Boolean { + require(this is KotlinType, this::errorMessage) + return this.attributes.size > 1 + } + override fun KotlinTypeMarker.getCustomAttributes(): List { require(this is KotlinType, this::errorMessage) return this.attributes.filterNot { it is AnnotationsTypeAttribute } diff --git a/core/descriptors/src/org/jetbrains/kotlin/types/checker/NewCapturedType.kt b/core/descriptors/src/org/jetbrains/kotlin/types/checker/NewCapturedType.kt index b4c152c6124..393f8c2c73f 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/types/checker/NewCapturedType.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/types/checker/NewCapturedType.kt @@ -207,16 +207,13 @@ 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 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 replaceAttributes(newAttributes: TypeAttributes): SimpleType = - NewCapturedType(captureStatus, constructor, lowerType, newAttributes, isMarkedNullable) + NewCapturedType(captureStatus, constructor, lowerType, newAttributes, isMarkedNullable, isProjectionNotNull) override fun makeNullableAsSpecified(newNullability: Boolean) = NewCapturedType(captureStatus, constructor, lowerType, attributes, newNullability)