From 19bfc43beeab56f597519fe8eb3a5392305f08b9 Mon Sep 17 00:00:00 2001 From: Irene Dea Date: Thu, 21 Oct 2021 13:22:51 -0700 Subject: [PATCH] Fixes and refactors --- .../codegen/CollectionStubMethodGenerator.kt | 2 +- .../kotlin/fir/types/ConeInferenceContext.kt | 14 ++- .../kotlin/fir/types/ConeTypeContext.kt | 6 + .../jvm/JvmDeclarationReturnTypeSanitizer.kt | 22 ++-- .../checkers/JavaClassOnCompanionChecker.kt | 7 +- .../jetbrains/kotlin/frontend/di/injection.kt | 3 +- .../kotlin/resolve/DeclarationsChecker.kt | 2 +- .../jetbrains/kotlin/resolve/TypeResolver.kt | 16 ++- .../kotlin/resolve/calls/CallCompleter.kt | 2 +- .../calls/inference/BuilderInferenceUtil.kt | 2 - .../tower/KotlinToResolvedCallTransformer.kt | 2 +- .../resolve/calls/util/CallResolverUtil.kt | 4 +- .../lazy/descriptors/LazyAnnotations.kt | 1 + .../kotlin/types/CommonSupertypes.java | 2 +- .../DoubleColonExpressionResolver.kt | 2 +- .../ir/descriptors/IrBasedDescriptors.kt | 2 +- .../descriptors/IrBuiltInsOverDescriptors.kt | 4 +- .../kotlin/ir/types/IrTypeSystemContext.kt | 5 + .../model/MutableConstraintStorage.kt | 15 ++- .../KotlinConstraintSystemCompleter.kt | 2 +- .../components/NewTypeSubstitutor.kt | 4 +- .../calls/inference/model/TypeVariable.kt | 8 +- .../ConstraintSystemTestData.kt | 3 +- .../kotlin/types/model/TypeSystemContext.kt | 2 + .../descriptors/LazyJavaClassDescriptor.kt | 2 +- .../load/java/lazy/types/JavaTypeResolver.kt | 6 +- .../kotlin/load/java/lazy/types/RawType.kt | 7 +- .../java/typeEnhancement/typeEnhancement.kt | 3 +- .../DeserializationComponentsForJava.kt | 11 +- core/descriptors/build.gradle.kts | 1 - .../kotlin/builtins/KotlinBuiltIns.java | 7 +- .../kotlin/builtins/ReflectionTypes.kt | 8 +- .../kotlin/builtins/functionTypes.kt | 2 +- .../functions/FunctionClassDescriptor.kt | 2 +- .../kotlin/builtins/suspendFunctionTypes.kt | 2 +- .../impl/AbstractTypeParameterDescriptor.java | 2 +- .../impl/LazySubstitutingClassDescriptor.java | 2 +- .../inference/CapturedTypeConstructor.kt | 7 +- .../resolve/constants/CompileTimeConstant.kt | 7 +- .../IntegerLiteralTypeConstructor.kt | 4 +- .../resolve/constants/constantValues.kt | 2 +- .../kotlin/types/AnnotationsTypeAttribute.kt | 37 +++++++ .../types/CustomAnnotationTypeAttribute.kt | 34 ------ .../org/jetbrains/kotlin/types/ErrorType.kt | 2 - .../types/IntersectionTypeConstructor.kt | 2 +- .../org/jetbrains/kotlin/types/KotlinType.kt | 4 - .../kotlin/types/KotlinTypeFactory.kt | 103 ++---------------- .../jetbrains/kotlin/types/SpecialTypes.kt | 6 - .../org/jetbrains/kotlin/types/StubTypes.kt | 2 - .../kotlin/types/TypeAliasExpander.kt | 41 ++++--- .../kotlin/types/TypeAttributeTranslator.kt | 20 +++- .../jetbrains/kotlin/types/TypeAttributes.kt | 6 +- .../kotlin/types/TypeSubstitution.kt | 6 +- .../org/jetbrains/kotlin/types/TypeUtils.java | 8 +- .../org/jetbrains/kotlin/types/TypeUtils.kt | 2 +- .../kotlin/types/TypeWithEnhancement.kt | 6 - .../types/checker/ClassicTypeSystemContext.kt | 13 ++- .../types/checker/KotlinTypePreparator.kt | 2 +- .../kotlin/types/checker/NewCapturedType.kt | 8 +- .../jetbrains/kotlin/types/dynamicTypes.kt | 3 - .../extensions/TypeAttributeTranslators.kt | 13 ++- .../jetbrains/kotlin/types/flexibleTypes.kt | 3 - .../MetadataPackageFragmentProvider.kt | 6 +- .../deserialization/TypeDeserializer.kt | 14 ++- .../src/kotlin/reflect/full/KClassifiers.kt | 12 +- .../res/syntheticDescriptorGeneration.kt | 7 +- .../compiler/resolve/KSerializationUtil.kt | 2 +- .../resolve/KSerializerDescriptorResolver.kt | 16 +-- .../compiler/resolve/SearchUtils.kt | 3 +- .../ide_common/idea/util/TypeUtils.kt | 2 +- 70 files changed, 263 insertions(+), 327 deletions(-) create mode 100644 core/descriptors/src/org/jetbrains/kotlin/types/AnnotationsTypeAttribute.kt delete mode 100644 core/descriptors/src/org/jetbrains/kotlin/types/CustomAnnotationTypeAttribute.kt diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/CollectionStubMethodGenerator.kt b/compiler/backend/src/org/jetbrains/kotlin/codegen/CollectionStubMethodGenerator.kt index 5547d539b92..487dd8abd7e 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/CollectionStubMethodGenerator.kt +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/CollectionStubMethodGenerator.kt @@ -341,7 +341,7 @@ class CollectionStubMethodGenerator( } private fun newType(classDescriptor: ClassDescriptor, typeArguments: List): KotlinType { - return KotlinTypeFactory.simpleNotNullType(Annotations.EMPTY, classDescriptor, typeArguments) + return KotlinTypeFactory.simpleNotNullType(TypeAttributes.Empty, classDescriptor, typeArguments) } private fun FunctionDescriptor.signature(): JvmMethodGenericSignature = 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 6474c49afa1..9636f9805c6 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 @@ -402,16 +402,20 @@ interface ConeInferenceContext : TypeSystemInferenceExtensionContext, ConeTypeCo override fun unionTypeAttributes(types: List): List { @Suppress("UNCHECKED_CAST") - types as List - return types.map { it.attributes }.reduce { x, y -> x.union(y) }.toList() + return (types as List).map { it.attributes }.reduce { x, y -> x.union(y) }.toList() } override fun KotlinTypeMarker.replaceTypeAttributes(newAttributes: List): KotlinTypeMarker { require(this is ConeKotlinType) - val typeAttributes = newAttributes.filterIsInstance>() - require(typeAttributes.size == newAttributes.size) if (newAttributes.isEmpty()) return this - return createSimpleType(this.typeConstructor(), this.getArguments(), this.isNullable, this.isExtensionFunctionType, typeAttributes) + @Suppress("UNCHECKED_CAST") + return createSimpleType( + this.typeConstructor(), + this.getArguments(), + this.isNullable, + this.isExtensionFunctionType, + newAttributes as List> + ) } override fun TypeConstructorMarker.getApproximatedIntegerLiteralType(): KotlinTypeMarker { 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 f423b1aa213..fdee372f5ad 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.getCustomAttributes(): List { + require(this is ConeKotlinType) + val compilerAttributes = CompilerConeAttributes.classIdByCompilerAttribute + return attributes.filterNot { it in compilerAttributes || it is CustomAnnotationTypeAttribute } + } + override fun SimpleTypeMarker.isStubType(): Boolean { return this is ConeStubType } diff --git a/compiler/frontend.java/src/org/jetbrains/kotlin/resolve/jvm/JvmDeclarationReturnTypeSanitizer.kt b/compiler/frontend.java/src/org/jetbrains/kotlin/resolve/jvm/JvmDeclarationReturnTypeSanitizer.kt index 08a1e800d7c..4d485497c58 100644 --- a/compiler/frontend.java/src/org/jetbrains/kotlin/resolve/jvm/JvmDeclarationReturnTypeSanitizer.kt +++ b/compiler/frontend.java/src/org/jetbrains/kotlin/resolve/jvm/JvmDeclarationReturnTypeSanitizer.kt @@ -24,22 +24,24 @@ import org.jetbrains.kotlin.resolve.BindingTrace import org.jetbrains.kotlin.resolve.DeclarationReturnTypeSanitizer import org.jetbrains.kotlin.types.UnwrappedType import org.jetbrains.kotlin.types.WrappedTypeFactory +import org.jetbrains.kotlin.types.replaceAnnotations object JvmDeclarationReturnTypeSanitizer : DeclarationReturnTypeSanitizer { override fun sanitizeReturnType( - inferred: UnwrappedType, - wrappedTypeFactory: WrappedTypeFactory, - trace: BindingTrace, - languageVersionSettings: LanguageVersionSettings + inferred: UnwrappedType, + wrappedTypeFactory: WrappedTypeFactory, + trace: BindingTrace, + languageVersionSettings: LanguageVersionSettings ): UnwrappedType = - if (languageVersionSettings.supportsFeature(LanguageFeature.StrictJavaNullabilityAssertions)) { - // NB can't check for presence of EnhancedNullability here, - // because it will also cause recursion in declaration type resolution. - inferred.replaceAnnotations( + if (languageVersionSettings.supportsFeature(LanguageFeature.StrictJavaNullabilityAssertions)) { + // NB can't check for presence of EnhancedNullability here, + // because it will also cause recursion in declaration type resolution. + inferred.replaceAttributes( + inferred.attributes.replaceAnnotations( FilteredAnnotations(inferred.annotations, languageVersionSettings.supportsFeature(LanguageFeature.NewInference)) { it != JvmAnnotationNames.ENHANCED_NULLABILITY_ANNOTATION } ) - } - else inferred + ) + } else inferred } \ No newline at end of file diff --git a/compiler/frontend.java/src/org/jetbrains/kotlin/resolve/jvm/checkers/JavaClassOnCompanionChecker.kt b/compiler/frontend.java/src/org/jetbrains/kotlin/resolve/jvm/checkers/JavaClassOnCompanionChecker.kt index b4bc473cc26..99d032a57ae 100644 --- a/compiler/frontend.java/src/org/jetbrains/kotlin/resolve/jvm/checkers/JavaClassOnCompanionChecker.kt +++ b/compiler/frontend.java/src/org/jetbrains/kotlin/resolve/jvm/checkers/JavaClassOnCompanionChecker.kt @@ -20,12 +20,12 @@ import com.intellij.psi.PsiElement import org.jetbrains.kotlin.descriptors.ClassDescriptor import org.jetbrains.kotlin.descriptors.PackageFragmentDescriptor import org.jetbrains.kotlin.descriptors.PropertyDescriptor -import org.jetbrains.kotlin.descriptors.annotations.Annotations import org.jetbrains.kotlin.resolve.calls.checkers.CallChecker import org.jetbrains.kotlin.resolve.calls.checkers.CallCheckerContext import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall import org.jetbrains.kotlin.resolve.jvm.diagnostics.ErrorsJvm import org.jetbrains.kotlin.types.KotlinTypeFactory +import org.jetbrains.kotlin.types.TypeAttributes import org.jetbrains.kotlin.types.TypeProjectionImpl class JavaClassOnCompanionChecker : CallChecker { @@ -44,8 +44,9 @@ class JavaClassOnCompanionChecker : CallChecker { val javaLangClass = actualType.constructor.declarationDescriptor as? ClassDescriptor ?: return val arguments = listOf(TypeProjectionImpl(containingClass.defaultType)) - val expectedType = KotlinTypeFactory.simpleType(Annotations.EMPTY, javaLangClass.typeConstructor, arguments, - actualType.isMarkedNullable) + val expectedType = KotlinTypeFactory.simpleType( + TypeAttributes.Empty, javaLangClass.typeConstructor, arguments, + actualType.isMarkedNullable) context.trace.report(ErrorsJvm.JAVA_CLASS_ON_COMPANION.on(reportOn, actualType, expectedType)) } } diff --git a/compiler/frontend/src/org/jetbrains/kotlin/frontend/di/injection.kt b/compiler/frontend/src/org/jetbrains/kotlin/frontend/di/injection.kt index 9c51a1abe24..98ba080998f 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/frontend/di/injection.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/frontend/di/injection.kt @@ -49,6 +49,7 @@ import org.jetbrains.kotlin.types.expressions.DeclarationScopeProviderForLocalCl import org.jetbrains.kotlin.types.expressions.LocalClassDescriptorHolder import org.jetbrains.kotlin.types.expressions.LocalLazyDeclarationResolver import org.jetbrains.kotlin.types.extensions.TypeAttributeTranslators +import org.jetbrains.kotlin.types.extensions.TypeAttributeTranslatorsForInjection import org.jetbrains.kotlin.util.ProgressManagerBasedCancellationChecker fun StorageComponentContainer.configureModule( @@ -80,7 +81,7 @@ fun StorageComponentContainer.configureModule( analyzerServices.platformConfigurator.configureModuleComponents(this) analyzerServices.platformConfigurator.configureModuleDependentCheckers(this) - useInstance(TypeAttributeTranslators(moduleContext.project).translators) + useImpl() for (extension in StorageComponentContainerContributor.getInstances(moduleContext.project)) { extension.registerModuleComponents(this, platform, moduleContext.module) diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/DeclarationsChecker.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/DeclarationsChecker.kt index 7890245cd3a..823fe63de5e 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/DeclarationsChecker.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/DeclarationsChecker.kt @@ -242,7 +242,7 @@ class DeclarationsChecker( private fun checkTypeAliasExpansion(declaration: KtTypeAlias, typeAliasDescriptor: TypeAliasDescriptor) { val typeAliasExpansion = TypeAliasExpansion.createWithFormalArguments(typeAliasDescriptor) val reportStrategy = TypeAliasDeclarationCheckingReportStrategy(trace, typeAliasDescriptor, declaration, upperBoundChecker) - TypeAliasExpander(reportStrategy, true).expandWithoutAbbreviation(typeAliasExpansion, Annotations.EMPTY) + TypeAliasExpander(reportStrategy, true).expandWithoutAbbreviation(typeAliasExpansion, TypeAttributes.Empty) } private fun checkConstructorDeclaration(constructorDescriptor: ClassConstructorDescriptor, declaration: KtConstructor<*>) { diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/TypeResolver.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/TypeResolver.kt index cd6bbd448a4..79d4ea2398d 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/TypeResolver.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/TypeResolver.kt @@ -105,7 +105,7 @@ class TypeResolver( fun resolveExpandedTypeForTypeAlias(typeAliasDescriptor: TypeAliasDescriptor): SimpleType { val typeAliasExpansion = TypeAliasExpansion.createWithFormalArguments(typeAliasDescriptor) - val expandedType = TypeAliasExpander.NON_REPORTING.expandWithoutAbbreviation(typeAliasExpansion, Annotations.EMPTY) + val expandedType = TypeAliasExpander.NON_REPORTING.expandWithoutAbbreviation(typeAliasExpansion, TypeAttributes.Empty) return expandedType } @@ -523,7 +523,7 @@ class TypeResolver( ErrorUtils.createErrorType("?") else KotlinTypeFactory.simpleTypeWithNonTrivialMemberScope( - typeAttributeTranslators.toAttributes(annotations), + typeAttributeTranslators.toAttributes(annotations, typeParameter.typeConstructor, containing), typeParameter.typeConstructor, listOf(), false, @@ -605,7 +605,11 @@ class TypeResolver( } val resultingType = - KotlinTypeFactory.simpleNotNullType(typeAttributeTranslators.toAttributes(annotations), classDescriptor, arguments) + KotlinTypeFactory.simpleNotNullType( + typeAttributeTranslators.toAttributes(annotations, classDescriptor.typeConstructor, c.scope.ownerDescriptor), + classDescriptor, + arguments + ) // We create flexible types by convention here // This is not intended to be used in normal users' environments, only for tests and debugger etc @@ -701,9 +705,11 @@ class TypeResolver( return createErrorTypeForTypeConstructor(c, projectionFromAllQualifierParts, typeConstructor) } + val attributes = typeAttributeTranslators.toAttributes(annotations, descriptor.typeConstructor, c.scope.ownerDescriptor) + return if (c.abbreviated) { val abbreviatedType = KotlinTypeFactory.simpleType( - typeAttributeTranslators.toAttributes(annotations), + attributes, descriptor.typeConstructor, arguments, false @@ -711,7 +717,7 @@ class TypeResolver( type(abbreviatedType) } else { val typeAliasExpansion = TypeAliasExpansion.create(null, descriptor, arguments) - val expandedType = TypeAliasExpander(reportStrategy, c.checkBounds).expand(typeAliasExpansion, annotations) + val expandedType = TypeAliasExpander(reportStrategy, c.checkBounds).expand(typeAliasExpansion, attributes) type(expandedType) } } diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/CallCompleter.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/CallCompleter.kt index 896fc05a40f..ee71d5b7bf6 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/CallCompleter.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/CallCompleter.kt @@ -331,7 +331,7 @@ class CallCompleter( val value = constant.getValue(TypeUtils.NO_EXPECTED_TYPE).safeAs()?.toLong() ?: return null val typeConstructor = IntegerValueTypeConstructor(value, moduleDescriptor, constant.parameters) return KotlinTypeFactory.simpleTypeWithNonTrivialMemberScope( - Annotations.EMPTY, typeConstructor, emptyList(), false, + TypeAttributes.Empty, typeConstructor, emptyList(), false, ErrorUtils.createErrorScope("Scope for number value type ($typeConstructor)", true) ) } diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/inference/BuilderInferenceUtil.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/inference/BuilderInferenceUtil.kt index 28f37b957c2..cbbb5abb2fc 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/inference/BuilderInferenceUtil.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/inference/BuilderInferenceUtil.kt @@ -54,8 +54,6 @@ class TypeTemplate( typeVariable.originalTypeParameter.builtIns.nothingType, typeVariable.originalTypeParameter.builtIns.anyType.makeNullableAsSpecified(nullable) ) { - override fun replaceAnnotations(newAnnotations: Annotations) = this - override fun replaceAttributes(newAttributes: TypeAttributes) = this override fun makeNullableAsSpecified(newNullability: Boolean) = TypeTemplate(typeVariable, builderInferenceData, newNullability) diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/KotlinToResolvedCallTransformer.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/KotlinToResolvedCallTransformer.kt index 00b6e84590a..259fae1b6f4 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/KotlinToResolvedCallTransformer.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/KotlinToResolvedCallTransformer.kt @@ -394,7 +394,7 @@ class KotlinToResolvedCallTransformer( val value = constant.getValue(TypeUtils.NO_EXPECTED_TYPE).safeAs()?.toLong() ?: return null val typeConstructor = IntegerLiteralTypeConstructor(value, moduleDescriptor, constant.parameters) return KotlinTypeFactory.simpleTypeWithNonTrivialMemberScope( - Annotations.EMPTY, typeConstructor, emptyList(), false, + TypeAttributes.Empty, typeConstructor, emptyList(), false, ErrorUtils.createErrorScope("Scope for number value type ($typeConstructor)", true), ) } diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/util/CallResolverUtil.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/util/CallResolverUtil.kt index b608ce792f6..c282307897d 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/util/CallResolverUtil.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/util/CallResolverUtil.kt @@ -70,7 +70,7 @@ fun replaceReturnTypeForCallable(type: KotlinType, given: KotlinType): KotlinTyp fun replaceReturnTypeByUnknown(type: KotlinType) = replaceReturnTypeForCallable(type, DONT_CARE) private fun replaceTypeArguments(type: KotlinType, newArguments: List) = - KotlinTypeFactory.simpleType(type.annotations, type.constructor, newArguments, type.isMarkedNullable) + KotlinTypeFactory.simpleType(type.attributes, type.constructor, newArguments, type.isMarkedNullable) private fun getParameterArgumentsOfCallableType(type: KotlinType) = type.arguments.dropLast(1) @@ -126,7 +126,7 @@ fun getErasedReceiverType(receiverParameterDescriptor: ReceiverParameterDescript } return KotlinTypeFactory.simpleTypeWithNonTrivialMemberScope( - receiverType.annotations, receiverTypeConstructor, fakeTypeArguments, + receiverType.attributes, receiverTypeConstructor, fakeTypeArguments, receiverType.isMarkedNullable, ErrorUtils.createErrorScope("Error scope for erased receiver type", /*throwExceptions=*/true) ) } diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/lazy/descriptors/LazyAnnotations.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/lazy/descriptors/LazyAnnotations.kt index 4e37ce2acb5..4abe30f439b 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/lazy/descriptors/LazyAnnotations.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/lazy/descriptors/LazyAnnotations.kt @@ -36,6 +36,7 @@ import org.jetbrains.kotlin.storage.StorageManager import org.jetbrains.kotlin.storage.getValue import org.jetbrains.kotlin.types.AbbreviatedType import org.jetbrains.kotlin.types.ErrorUtils +import org.jetbrains.kotlin.types.typeUtil.replaceAnnotations abstract class LazyAnnotationsContext( val annotationResolver: AnnotationResolver, diff --git a/compiler/frontend/src/org/jetbrains/kotlin/types/CommonSupertypes.java b/compiler/frontend/src/org/jetbrains/kotlin/types/CommonSupertypes.java index f49e6d55a19..7de642c858b 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/types/CommonSupertypes.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/types/CommonSupertypes.java @@ -272,7 +272,7 @@ public class CommonSupertypes { else { newScope = ErrorUtils.createErrorScope("A scope for common supertype which is not a normal classifier", true); } - return KotlinTypeFactory.simpleTypeWithNonTrivialMemberScope(Annotations.Companion.getEMPTY(), constructor, newProjections, nullable, newScope); + return KotlinTypeFactory.simpleTypeWithNonTrivialMemberScope(TypeAttributes.Companion.getEmpty(), constructor, newProjections, nullable, newScope); } @NotNull diff --git a/compiler/frontend/src/org/jetbrains/kotlin/types/expressions/DoubleColonExpressionResolver.kt b/compiler/frontend/src/org/jetbrains/kotlin/types/expressions/DoubleColonExpressionResolver.kt index a1b71ac0c3d..def5e862eba 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/types/expressions/DoubleColonExpressionResolver.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/types/expressions/DoubleColonExpressionResolver.kt @@ -498,7 +498,7 @@ class DoubleColonExpressionResolver( val arguments = descriptor.typeConstructor.parameters.map(TypeUtils::makeStarProjection) KotlinTypeFactory.simpleType( - Annotations.EMPTY, descriptor.typeConstructor, arguments, + TypeAttributes.Empty, descriptor.typeConstructor, arguments, possiblyBareType.isNullable || doubleColonExpression.hasQuestionMarks ) } else { diff --git a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/descriptors/IrBasedDescriptors.kt b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/descriptors/IrBasedDescriptors.kt index 9bb985d66ec..a835ef511a1 100644 --- a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/descriptors/IrBasedDescriptors.kt +++ b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/descriptors/IrBasedDescriptors.kt @@ -286,7 +286,7 @@ open class IrBasedTypeParameterDescriptor(owner: IrTypeParameter) : TypeParamete private val _defaultType: SimpleType by lazy { KotlinTypeFactory.simpleTypeWithNonTrivialMemberScope( - Annotations.EMPTY, typeConstructor, emptyList(), false, + TypeAttributes.Empty, typeConstructor, emptyList(), false, LazyScopeAdapter { TypeIntersectionScope.create( "Scope for type parameter " + name.asString(), diff --git a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/descriptors/IrBuiltInsOverDescriptors.kt b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/descriptors/IrBuiltInsOverDescriptors.kt index cc12d382f73..44e78696e5d 100644 --- a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/descriptors/IrBuiltInsOverDescriptors.kt +++ b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/descriptors/IrBuiltInsOverDescriptors.kt @@ -405,8 +405,8 @@ class IrBuiltInsOverDescriptors( override val checkNotNullSymbol = defineCheckNotNullOperator() - private fun TypeConstructor.makeNonNullType() = KotlinTypeFactory.simpleType(Annotations.EMPTY, this, listOf(), false) - private fun TypeConstructor.makeNullableType() = KotlinTypeFactory.simpleType(Annotations.EMPTY, this, listOf(), true) + private fun TypeConstructor.makeNonNullType() = KotlinTypeFactory.simpleType(TypeAttributes.Empty, this, listOf(), false) + private fun TypeConstructor.makeNullableType() = KotlinTypeFactory.simpleType(TypeAttributes.Empty, this, listOf(), true) override val dataClassArrayMemberHashCodeSymbol = defineOperator("dataClassArrayMemberHashCode", intType, listOf(anyType)) 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 9be8078bfb0..a583cdc4d8a 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 @@ -406,6 +406,11 @@ interface IrTypeSystemContext : TypeSystemContext, TypeSystemCommonSuperTypesCon return this.annotations.map { object : AnnotationMarker, IrElement by it {} } } + override fun KotlinTypeMarker.getCustomAttributes(): List { + require(this is IrType) + return emptyList() + } + override fun createErrorType(debugName: String): SimpleTypeMarker { TODO("IrTypeSystemContext doesn't support constraint system resolution") } 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 082aeeca589..54d6fd39478 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 @@ -60,7 +60,14 @@ class MutableVariableWithConstraints private constructor( && previousConstraint.isNullabilityConstraint == constraint.isNullabilityConstraint ) { if (newConstraintIsUseless(previousConstraint, constraint)) { - return previousConstraint to false + // 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 + } + } } val isMatchingForSimplification = when (previousConstraint.kind) { @@ -139,11 +146,7 @@ class MutableVariableWithConstraints private constructor( return false return when (old.kind) { - ConstraintKind.EQUALITY -> { - with(context) { - old.type.getAttributes() == new.type.getAttributes() - } - } + ConstraintKind.EQUALITY -> true ConstraintKind.LOWER -> new.kind.isLower() ConstraintKind.UPPER -> new.kind.isUpper() } diff --git a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/components/KotlinConstraintSystemCompleter.kt b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/components/KotlinConstraintSystemCompleter.kt index 3263c80502e..f6e4abce67e 100644 --- a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/components/KotlinConstraintSystemCompleter.kt +++ b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/components/KotlinConstraintSystemCompleter.kt @@ -291,7 +291,7 @@ class KotlinConstraintSystemCompleter( csBuilder.registerVariable(returnVariable) val functionalType: KotlinType = csBuilder.buildCurrentSubstitutor().safeSubstitute(candidate.getSystem().asConstraintSystemCompleterContext(), atom.expectedType!!) as KotlinType val expectedType = KotlinTypeFactory.simpleType( - functionalType.annotations, + functionalType.attributes, functionalType.constructor, functionalType.arguments.dropLast(1) + returnVariable.defaultType.asTypeProjection(), functionalType.isMarkedNullable 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 3bab1500451..aafa6f9846d 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 @@ -148,7 +148,9 @@ interface NewTypeSubstitutor : TypeSubstitutorMarker { // simple classifier type var replacement = substituteNotNullTypeWithConstructor(typeConstructor) ?: return null if (keepAnnotation) { - replacement = replacement.replaceAnnotations(CompositeAnnotations(replacement.annotations, type.annotations)) + replacement = replacement.replaceAttributes( + replacement.attributes.add(type.attributes) + ) } if (type.isMarkedNullable) { replacement = replacement.makeNullableAsSpecified(true) diff --git a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/model/TypeVariable.kt b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/model/TypeVariable.kt index 1587ae71aa2..e81a98dc10b 100644 --- a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/model/TypeVariable.kt +++ b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/model/TypeVariable.kt @@ -23,15 +23,11 @@ import org.jetbrains.kotlin.descriptors.annotations.Annotations import org.jetbrains.kotlin.resolve.calls.model.PostponableKotlinCallArgument import org.jetbrains.kotlin.resolve.descriptorUtil.builtIns import org.jetbrains.kotlin.resolve.descriptorUtil.hasOnlyInputTypesAnnotation -import org.jetbrains.kotlin.types.KotlinType -import org.jetbrains.kotlin.types.KotlinTypeFactory -import org.jetbrains.kotlin.types.SimpleType -import org.jetbrains.kotlin.types.TypeConstructor +import org.jetbrains.kotlin.types.* import org.jetbrains.kotlin.types.checker.KotlinTypeRefiner import org.jetbrains.kotlin.types.checker.NewTypeVariableConstructor import org.jetbrains.kotlin.types.model.TypeVariableMarker import org.jetbrains.kotlin.types.model.TypeVariableTypeConstructorMarker -import org.jetbrains.kotlin.types.TypeRefinement class TypeVariableTypeConstructor( @@ -74,7 +70,7 @@ sealed class NewTypeVariable( fun TypeConstructor.typeForTypeVariable(): SimpleType { require(this is TypeVariableTypeConstructor) return KotlinTypeFactory.simpleTypeWithNonTrivialMemberScope( - Annotations.EMPTY, this, arguments = emptyList(), + TypeAttributes.Empty, this, arguments = emptyList(), nullable = false, memberScope = builtIns.any.unsubstitutedMemberScope ) } diff --git a/compiler/tests-common/tests/org/jetbrains/kotlin/resolve/constraintSystem/ConstraintSystemTestData.kt b/compiler/tests-common/tests/org/jetbrains/kotlin/resolve/constraintSystem/ConstraintSystemTestData.kt index 409264e2516..8467b67cea5 100644 --- a/compiler/tests-common/tests/org/jetbrains/kotlin/resolve/constraintSystem/ConstraintSystemTestData.kt +++ b/compiler/tests-common/tests/org/jetbrains/kotlin/resolve/constraintSystem/ConstraintSystemTestData.kt @@ -33,6 +33,7 @@ import org.jetbrains.kotlin.resolve.scopes.MemberScope import org.jetbrains.kotlin.test.DummyTraces import org.jetbrains.kotlin.types.KotlinType import org.jetbrains.kotlin.types.KotlinTypeFactory +import org.jetbrains.kotlin.types.TypeAttributes import java.util.regex.Pattern class ConstraintSystemTestData( @@ -67,7 +68,7 @@ class ConstraintSystemTestData( val number = matcher.group(1)!! val parameters = CompileTimeConstant.Parameters(false, false, false, false, false, false, false, false) return KotlinTypeFactory.simpleTypeWithNonTrivialMemberScope( - Annotations.EMPTY, + TypeAttributes.Empty, IntegerValueTypeConstructor(number.toLong(), functionFoo.module, parameters), listOf(), false, 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 f313877d7dd..39a455e736f 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 @@ -500,6 +500,8 @@ interface TypeSystemContext : TypeSystemOptimizationContext { fun KotlinTypeMarker.getAttributes(): List + fun KotlinTypeMarker.getCustomAttributes(): List + fun substitutionSupertypePolicy(type: SimpleTypeMarker): TypeCheckerState.SupertypesPolicy fun KotlinTypeMarker.isTypeVariableType(): Boolean diff --git a/core/descriptors.jvm/src/org/jetbrains/kotlin/load/java/lazy/descriptors/LazyJavaClassDescriptor.kt b/core/descriptors.jvm/src/org/jetbrains/kotlin/load/java/lazy/descriptors/LazyJavaClassDescriptor.kt index 3d8ae3468c1..e2dfaef725e 100644 --- a/core/descriptors.jvm/src/org/jetbrains/kotlin/load/java/lazy/descriptors/LazyJavaClassDescriptor.kt +++ b/core/descriptors.jvm/src/org/jetbrains/kotlin/load/java/lazy/descriptors/LazyJavaClassDescriptor.kt @@ -281,7 +281,7 @@ class LazyJavaClassDescriptor( else -> return null } - return KotlinTypeFactory.simpleNotNullType(Annotations.EMPTY, classDescriptor, parametersAsTypeProjections) + return KotlinTypeFactory.simpleNotNullType(TypeAttributes.Empty, classDescriptor, parametersAsTypeProjections) } private fun getPurelyImplementsFqNameFromAnnotation(): FqName? { diff --git a/core/descriptors.jvm/src/org/jetbrains/kotlin/load/java/lazy/types/JavaTypeResolver.kt b/core/descriptors.jvm/src/org/jetbrains/kotlin/load/java/lazy/types/JavaTypeResolver.kt index e1309066fe6..fba789bc422 100644 --- a/core/descriptors.jvm/src/org/jetbrains/kotlin/load/java/lazy/types/JavaTypeResolver.kt +++ b/core/descriptors.jvm/src/org/jetbrains/kotlin/load/java/lazy/types/JavaTypeResolver.kt @@ -119,8 +119,8 @@ class JavaTypeResolver( attr: JavaTypeAttributes, lowerResult: SimpleType? ): SimpleType? { - val annotations = - lowerResult?.annotations ?: LazyJavaAnnotations(c, javaType) + val attributes = + lowerResult?.attributes ?: LazyJavaAnnotations(c, javaType).toDefaultAttributes() val constructor = computeTypeConstructor(javaType, attr) ?: return null val isNullable = attr.isNullable() @@ -130,7 +130,7 @@ class JavaTypeResolver( val arguments = computeArguments(javaType, attr, constructor) - return KotlinTypeFactory.simpleType(annotations, constructor, arguments, isNullable) + return KotlinTypeFactory.simpleType(attributes, constructor, arguments, isNullable) } private fun computeTypeConstructor(javaType: JavaClassifierType, attr: JavaTypeAttributes): TypeConstructor? { diff --git a/core/descriptors.jvm/src/org/jetbrains/kotlin/load/java/lazy/types/RawType.kt b/core/descriptors.jvm/src/org/jetbrains/kotlin/load/java/lazy/types/RawType.kt index 77a5add3e59..091bd86291a 100644 --- a/core/descriptors.jvm/src/org/jetbrains/kotlin/load/java/lazy/types/RawType.kt +++ b/core/descriptors.jvm/src/org/jetbrains/kotlin/load/java/lazy/types/RawType.kt @@ -54,9 +54,6 @@ class RawTypeImpl private constructor(lowerBound: SimpleType, upperBound: Simple return classDescriptor.getMemberScope(RawSubstitution()) } - override fun replaceAnnotations(newAnnotations: Annotations) = - RawTypeImpl(lowerBound.replaceAnnotations(newAnnotations), upperBound.replaceAnnotations(newAnnotations)) - override fun replaceAttributes(newAttributes: TypeAttributes) = RawTypeImpl(lowerBound.replaceAttributes(newAttributes), upperBound.replaceAttributes(newAttributes)) @@ -148,7 +145,7 @@ internal class RawSubstitution(typeParameterUpperBoundEraser: TypeParameterUpper TypeProjectionImpl(componentTypeProjection.projectionKind, eraseType(componentTypeProjection.type, attr)) ) return KotlinTypeFactory.simpleType( - type.annotations, type.constructor, arguments, type.isMarkedNullable + type.attributes, type.constructor, arguments, type.isMarkedNullable ) to false } @@ -156,7 +153,7 @@ internal class RawSubstitution(typeParameterUpperBoundEraser: TypeParameterUpper val memberScope = declaration.getMemberScope(this) return KotlinTypeFactory.simpleTypeWithNonTrivialMemberScope( - type.annotations, declaration.typeConstructor, + type.attributes, declaration.typeConstructor, declaration.typeConstructor.parameters.map { parameter -> computeProjection(parameter, attr) }, diff --git a/core/descriptors.jvm/src/org/jetbrains/kotlin/load/java/typeEnhancement/typeEnhancement.kt b/core/descriptors.jvm/src/org/jetbrains/kotlin/load/java/typeEnhancement/typeEnhancement.kt index f2c877ba8f2..f79a4a4f92a 100644 --- a/core/descriptors.jvm/src/org/jetbrains/kotlin/load/java/typeEnhancement/typeEnhancement.kt +++ b/core/descriptors.jvm/src/org/jetbrains/kotlin/load/java/typeEnhancement/typeEnhancement.kt @@ -150,7 +150,7 @@ class JavaTypeEnhancement(private val javaResolverSettings: JavaResolverSettings ).compositeAnnotationsOrSingle() val enhancedType = KotlinTypeFactory.simpleType( - newAnnotations, + newAnnotations.toDefaultAttributes(), typeConstructor, enhancedArguments.zip(arguments) { enhanced, original -> enhanced ?: original }, enhancedNullability ?: isMarkedNullable @@ -252,7 +252,6 @@ internal class NotNullTypeParameterImpl(override val delegate: SimpleType) : Not return NotNullTypeParameterImpl(result) } - override fun replaceAnnotations(newAnnotations: Annotations) = NotNullTypeParameterImpl(delegate.replaceAnnotations(newAnnotations)) override fun replaceAttributes(newAttributes: TypeAttributes) = NotNullTypeParameterImpl(delegate.replaceAttributes(newAttributes)) override fun makeNullableAsSpecified(newNullability: Boolean) = if (newNullability) delegate.makeNullableAsSpecified(true) else this diff --git a/core/descriptors.jvm/src/org/jetbrains/kotlin/load/kotlin/DeserializationComponentsForJava.kt b/core/descriptors.jvm/src/org/jetbrains/kotlin/load/kotlin/DeserializationComponentsForJava.kt index ac88c331077..847d8b12b33 100644 --- a/core/descriptors.jvm/src/org/jetbrains/kotlin/load/kotlin/DeserializationComponentsForJava.kt +++ b/core/descriptors.jvm/src/org/jetbrains/kotlin/load/kotlin/DeserializationComponentsForJava.kt @@ -48,7 +48,9 @@ import org.jetbrains.kotlin.resolve.sam.SamConversionResolverImpl import org.jetbrains.kotlin.serialization.deserialization.* import org.jetbrains.kotlin.storage.LockBasedStorageManager import org.jetbrains.kotlin.storage.StorageManager +import org.jetbrains.kotlin.types.DefaultTypeAttributeTranslator import org.jetbrains.kotlin.types.checker.NewKotlinTypeChecker +import org.jetbrains.kotlin.types.extensions.TypeAttributeTranslatorsForInjection // This class is needed only for easier injection: exact types of needed components are specified in the constructor here. // Otherwise injector generator is not smart enough to deduce, for example, which package fragment provider DeserializationComponents needs @@ -63,7 +65,8 @@ class DeserializationComponentsForJava( errorReporter: ErrorReporter, lookupTracker: LookupTracker, contractDeserializer: ContractDeserializer, - kotlinTypeChecker: NewKotlinTypeChecker + kotlinTypeChecker: NewKotlinTypeChecker, + typeAttributeTranslators: TypeAttributeTranslatorsForInjection ) { val components: DeserializationComponents @@ -77,7 +80,8 @@ class DeserializationComponentsForJava( additionalClassPartsProvider = jvmBuiltIns?.customizer ?: AdditionalClassPartsProvider.None, platformDependentDeclarationFilter = jvmBuiltIns?.customizer ?: PlatformDependentDeclarationFilter.NoPlatformDependent, extensionRegistryLite = JvmProtoBufUtil.EXTENSION_REGISTRY, - kotlinTypeChecker = kotlinTypeChecker, samConversionResolver = SamConversionResolverImpl(storageManager, emptyList()) + kotlinTypeChecker = kotlinTypeChecker, samConversionResolver = SamConversionResolverImpl(storageManager, emptyList()), + typeAttributeTranslators = typeAttributeTranslators.translators ) } @@ -188,6 +192,7 @@ fun makeDeserializationComponentsForJava( return DeserializationComponentsForJava( storageManager, module, DeserializationConfiguration.Default, javaClassDataFinder, binaryClassAnnotationAndConstantLoader, lazyJavaPackageFragmentProvider, notFoundClasses, - errorReporter, LookupTracker.DO_NOTHING, ContractDeserializer.DEFAULT, NewKotlinTypeChecker.Default + errorReporter, LookupTracker.DO_NOTHING, ContractDeserializer.DEFAULT, NewKotlinTypeChecker.Default, + TypeAttributeTranslatorsForInjection(listOf(DefaultTypeAttributeTranslator)) ) } diff --git a/core/descriptors/build.gradle.kts b/core/descriptors/build.gradle.kts index 947981536be..e7487f99031 100644 --- a/core/descriptors/build.gradle.kts +++ b/core/descriptors/build.gradle.kts @@ -10,7 +10,6 @@ dependencies { api(project(":core:util.runtime")) api(kotlinStdlib()) api(project(":kotlin-annotations-jvm")) - api(project(":compiler:util")) compileOnly(intellijCoreDep()) { includeJars("intellij-core") } } diff --git a/core/descriptors/src/org/jetbrains/kotlin/builtins/KotlinBuiltIns.java b/core/descriptors/src/org/jetbrains/kotlin/builtins/KotlinBuiltIns.java index 10a7a68316f..a55ea132cde 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/builtins/KotlinBuiltIns.java +++ b/core/descriptors/src/org/jetbrains/kotlin/builtins/KotlinBuiltIns.java @@ -665,7 +665,10 @@ public abstract class KotlinBuiltIns { @NotNull public SimpleType getArrayType(@NotNull Variance projectionType, @NotNull KotlinType argument, @NotNull Annotations annotations) { List types = Collections.singletonList(new TypeProjectionImpl(projectionType, argument)); - return KotlinTypeFactory.simpleNotNullType(annotations, getArray(), types); + return KotlinTypeFactory.simpleNotNullType( + TypeAttributesKt.toDefaultAttributes(annotations), + getArray(), + types); } @NotNull @@ -677,7 +680,7 @@ public abstract class KotlinBuiltIns { public SimpleType getEnumType(@NotNull SimpleType argument) { Variance projectionType = Variance.INVARIANT; List types = Collections.singletonList(new TypeProjectionImpl(projectionType, argument)); - return KotlinTypeFactory.simpleNotNullType(Annotations.Companion.getEMPTY(), getEnum(), types); + return KotlinTypeFactory.simpleNotNullType(TypeAttributes.Companion.getEmpty(), getEnum(), types); } @NotNull diff --git a/core/descriptors/src/org/jetbrains/kotlin/builtins/ReflectionTypes.kt b/core/descriptors/src/org/jetbrains/kotlin/builtins/ReflectionTypes.kt index 69873d23ad2..e3ebee3d3d4 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/builtins/ReflectionTypes.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/builtins/ReflectionTypes.kt @@ -53,7 +53,7 @@ class ReflectionTypes(module: ModuleDescriptor, private val notFoundClasses: Not val kMutableProperty2: ClassDescriptor by ClassLookup(3) fun getKClassType(annotations: Annotations, type: KotlinType, variance: Variance): KotlinType = - KotlinTypeFactory.simpleNotNullType(annotations, kClass, listOf(TypeProjectionImpl(variance, type))) + KotlinTypeFactory.simpleNotNullType(annotations.toDefaultAttributes(), kClass, listOf(TypeProjectionImpl(variance, type))) fun getKFunctionType( annotations: Annotations, @@ -69,7 +69,7 @@ class ReflectionTypes(module: ModuleDescriptor, private val notFoundClasses: Not getFunctionTypeArgumentProjections(receiverType, contextReceiverTypes, parameterTypes, parameterNames, returnType, builtIns) val classDescriptor = if (isSuspend) getKSuspendFunction(arguments.size - 1 /* return type */) else getKFunction(arguments.size - 1 /* return type */) - return KotlinTypeFactory.simpleNotNullType(annotations, classDescriptor, arguments) + return KotlinTypeFactory.simpleNotNullType(annotations.toDefaultAttributes(), classDescriptor, arguments) } fun getKPropertyType(annotations: Annotations, receiverTypes: List, returnType: KotlinType, mutable: Boolean): SimpleType { @@ -90,7 +90,7 @@ class ReflectionTypes(module: ModuleDescriptor, private val notFoundClasses: Not } val arguments = (receiverTypes + returnType).map(::TypeProjectionImpl) - return KotlinTypeFactory.simpleNotNullType(annotations, classDescriptor, arguments) + return KotlinTypeFactory.simpleNotNullType(annotations.toDefaultAttributes(), classDescriptor, arguments) } companion object { @@ -184,7 +184,7 @@ class ReflectionTypes(module: ModuleDescriptor, private val notFoundClasses: Not fun createKPropertyStarType(module: ModuleDescriptor): KotlinType? { val kPropertyClass = module.findClassAcrossModuleDependencies(StandardNames.FqNames.kProperty) ?: return null - return KotlinTypeFactory.simpleNotNullType(Annotations.EMPTY, kPropertyClass, + return KotlinTypeFactory.simpleNotNullType(TypeAttributes.Empty, kPropertyClass, listOf(StarProjectionImpl(kPropertyClass.typeConstructor.parameters.single()))) } diff --git a/core/descriptors/src/org/jetbrains/kotlin/builtins/functionTypes.kt b/core/descriptors/src/org/jetbrains/kotlin/builtins/functionTypes.kt index 6a3cb4beb75..fc9a51557f1 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/builtins/functionTypes.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/builtins/functionTypes.kt @@ -257,7 +257,7 @@ fun createFunctionType( if (contextReceiverTypes.isNotEmpty()) typeAnnotations = typeAnnotations.withContextReceiversFunctionAnnotation(builtIns, contextReceiverTypes.size) - return KotlinTypeFactory.simpleNotNullType(typeAnnotations, classDescriptor, arguments) + return KotlinTypeFactory.simpleNotNullType(typeAnnotations.toDefaultAttributes(), classDescriptor, arguments) } fun Annotations.hasExtensionFunctionAnnotation() = hasAnnotation(StandardNames.FqNames.extensionFunctionType) diff --git a/core/descriptors/src/org/jetbrains/kotlin/builtins/functions/FunctionClassDescriptor.kt b/core/descriptors/src/org/jetbrains/kotlin/builtins/functions/FunctionClassDescriptor.kt index 31dced68981..df4fcc57f38 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/builtins/functions/FunctionClassDescriptor.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/builtins/functions/FunctionClassDescriptor.kt @@ -114,7 +114,7 @@ class FunctionClassDescriptor( TypeProjectionImpl(it.defaultType) } - KotlinTypeFactory.simpleNotNullType(Annotations.EMPTY, descriptor, arguments) + KotlinTypeFactory.simpleNotNullType(TypeAttributes.Empty, descriptor, arguments) }.toList() } diff --git a/core/descriptors/src/org/jetbrains/kotlin/builtins/suspendFunctionTypes.kt b/core/descriptors/src/org/jetbrains/kotlin/builtins/suspendFunctionTypes.kt index 92b8b68de79..1a3928bc89f 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/builtins/suspendFunctionTypes.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/builtins/suspendFunctionTypes.kt @@ -49,7 +49,7 @@ fun transformSuspendFunctionToRuntimeFunctionType(suspendFunType: KotlinType): S suspendFunType.getContextReceiverTypesFromFunctionType(), suspendFunType.getValueParameterTypesFromFunctionType().map(TypeProjection::getType) + KotlinTypeFactory.simpleType( - Annotations.EMPTY, + TypeAttributes.Empty, // Continuation interface is not a part of built-ins anymore, it has been moved to stdlib. // While it must be somewhere in the dependencies, but here we don't have a reference to the module, // and it's rather complicated to inject it by now, so we just use a fake class descriptor. diff --git a/core/descriptors/src/org/jetbrains/kotlin/descriptors/impl/AbstractTypeParameterDescriptor.java b/core/descriptors/src/org/jetbrains/kotlin/descriptors/impl/AbstractTypeParameterDescriptor.java index 32fc433d24d..cadc8937fee 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/descriptors/impl/AbstractTypeParameterDescriptor.java +++ b/core/descriptors/src/org/jetbrains/kotlin/descriptors/impl/AbstractTypeParameterDescriptor.java @@ -71,7 +71,7 @@ public abstract class AbstractTypeParameterDescriptor extends DeclarationDescrip @Override public SimpleType invoke() { return KotlinTypeFactory.simpleTypeWithNonTrivialMemberScope( - Annotations.Companion.getEMPTY(), + TypeAttributes.Companion.getEmpty(), getTypeConstructor(), Collections.emptyList(), false, new LazyScopeAdapter( new Function0() { diff --git a/core/descriptors/src/org/jetbrains/kotlin/descriptors/impl/LazySubstitutingClassDescriptor.java b/core/descriptors/src/org/jetbrains/kotlin/descriptors/impl/LazySubstitutingClassDescriptor.java index e2e049b1b90..a0c0dd84d25 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/descriptors/impl/LazySubstitutingClassDescriptor.java +++ b/core/descriptors/src/org/jetbrains/kotlin/descriptors/impl/LazySubstitutingClassDescriptor.java @@ -143,7 +143,7 @@ public class LazySubstitutingClassDescriptor extends ModuleAwareClassDescriptor public SimpleType getDefaultType() { List typeProjections = TypeUtils.getDefaultTypeProjections(getTypeConstructor().getParameters()); return KotlinTypeFactory.simpleTypeWithNonTrivialMemberScope( - getAnnotations(), + DefaultTypeAttributeTranslator.INSTANCE.toAttributes(getAnnotations(), null, null), getTypeConstructor(), typeProjections, false, 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 5290fe86547..41072a385d3 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 @@ -77,6 +77,10 @@ class CapturedType( override val isMarkedNullable: Boolean = false, override val attributes: TypeAttributes = TypeAttributes.Empty ) : SimpleType(), SubtypingRepresentatives, CapturedTypeMarker { + + override val annotations: Annotations + get() = attributes.toDefaultAnnotations() + override val arguments: List get() = listOf() @@ -103,9 +107,6 @@ class CapturedType( return CapturedType(typeProjection, constructor, newNullability, attributes) } - override fun replaceAnnotations(newAnnotations: Annotations): CapturedType = - CapturedType(typeProjection, constructor, isMarkedNullable, newAnnotations.toDefaultAttributes()) - override fun replaceAttributes(newAttributes: TypeAttributes): SimpleType = CapturedType(typeProjection, constructor, isMarkedNullable, newAttributes) diff --git a/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/CompileTimeConstant.kt b/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/CompileTimeConstant.kt index 0c87f08dfde..89b0cd850fd 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/CompileTimeConstant.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/CompileTimeConstant.kt @@ -21,10 +21,7 @@ import org.jetbrains.kotlin.builtins.StandardNames import org.jetbrains.kotlin.descriptors.ModuleDescriptor import org.jetbrains.kotlin.descriptors.annotations.Annotations import org.jetbrains.kotlin.descriptors.findClassAcrossModuleDependencies -import org.jetbrains.kotlin.types.ErrorUtils -import org.jetbrains.kotlin.types.KotlinType -import org.jetbrains.kotlin.types.KotlinTypeFactory -import org.jetbrains.kotlin.types.TypeUtils +import org.jetbrains.kotlin.types.* interface CompileTimeConstant { val isError: Boolean @@ -200,7 +197,7 @@ class IntegerValueTypeConstant( } val unknownIntegerType = KotlinTypeFactory.simpleTypeWithNonTrivialMemberScope( - Annotations.EMPTY, typeConstructor, emptyList(), false, + TypeAttributes.Empty, typeConstructor, emptyList(), false, ErrorUtils.createErrorScope("Scope for number value type ($typeConstructor)", true) ) diff --git a/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/IntegerLiteralTypeConstructor.kt b/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/IntegerLiteralTypeConstructor.kt index 8278f7643f5..3f7e9999009 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/IntegerLiteralTypeConstructor.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/IntegerLiteralTypeConstructor.kt @@ -60,7 +60,7 @@ class IntegerLiteralTypeConstructor : TypeConstructor { Mode.INTERSECTION_TYPE -> left.possibleTypes union right.possibleTypes } val constructor = IntegerLiteralTypeConstructor(left.value, left.module, possibleTypes) - return KotlinTypeFactory.integerLiteralType(Annotations.EMPTY, constructor, false) + return KotlinTypeFactory.integerLiteralType(TypeAttributes.Empty, constructor, false) } private fun fold(left: IntegerLiteralTypeConstructor, right: SimpleType): SimpleType? = @@ -127,7 +127,7 @@ class IntegerLiteralTypeConstructor : TypeConstructor { this.possibleTypes = possibleTypes } - private val type = KotlinTypeFactory.integerLiteralType(Annotations.EMPTY, this, false) + private val type = KotlinTypeFactory.integerLiteralType(TypeAttributes.Empty, this, false) private fun isContainsOnlyUnsignedTypes(): Boolean = module.allSignedLiteralTypes.all { it !in possibleTypes } diff --git a/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/constantValues.kt b/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/constantValues.kt index 5d0c3120276..096fcdb5493 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/constantValues.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/constantValues.kt @@ -180,7 +180,7 @@ class KClassValue(value: Value) : ConstantValue(value) { constructor(classId: ClassId, arrayDimensions: Int) : this(ClassLiteralValue(classId, arrayDimensions)) override fun getType(module: ModuleDescriptor): KotlinType = - KotlinTypeFactory.simpleNotNullType(Annotations.EMPTY, module.builtIns.kClass, listOf(TypeProjectionImpl(getArgumentType(module)))) + KotlinTypeFactory.simpleNotNullType(TypeAttributes.Empty, module.builtIns.kClass, listOf(TypeProjectionImpl(getArgumentType(module)))) fun getArgumentType(module: ModuleDescriptor): KotlinType { when (value) { diff --git a/core/descriptors/src/org/jetbrains/kotlin/types/AnnotationsTypeAttribute.kt b/core/descriptors/src/org/jetbrains/kotlin/types/AnnotationsTypeAttribute.kt new file mode 100644 index 00000000000..9f3a2b3ffdc --- /dev/null +++ b/core/descriptors/src/org/jetbrains/kotlin/types/AnnotationsTypeAttribute.kt @@ -0,0 +1,37 @@ +/* + * Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. + */ + +package org.jetbrains.kotlin.types + +import org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptor +import org.jetbrains.kotlin.descriptors.annotations.Annotations +import kotlin.reflect.KClass + +class AnnotationsTypeAttribute(val annotations: Annotations) : TypeAttribute() { + constructor(annotations: List) : this(Annotations.create(annotations)) + + override fun union(other: AnnotationsTypeAttribute?): AnnotationsTypeAttribute? = null + + override fun intersect(other: AnnotationsTypeAttribute?): AnnotationsTypeAttribute? = null + + override fun add(other: AnnotationsTypeAttribute?): AnnotationsTypeAttribute { + if (other == null) return this + return AnnotationsTypeAttribute(Annotations.create(annotations + other.annotations)) + } + + override fun isSubtypeOf(other: AnnotationsTypeAttribute?): Boolean = true + + override val key: KClass + 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() + +val TypeAttributes.annotations: Annotations get() = annotationsAttribute?.annotations ?: Annotations.EMPTY diff --git a/core/descriptors/src/org/jetbrains/kotlin/types/CustomAnnotationTypeAttribute.kt b/core/descriptors/src/org/jetbrains/kotlin/types/CustomAnnotationTypeAttribute.kt deleted file mode 100644 index 4fdb481128d..00000000000 --- a/core/descriptors/src/org/jetbrains/kotlin/types/CustomAnnotationTypeAttribute.kt +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors. - * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. - */ - -package org.jetbrains.kotlin.types - -import org.jetbrains.kotlin.builtins.KotlinBuiltIns -import org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptor -import org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptorImpl -import org.jetbrains.kotlin.descriptors.annotations.Annotations -import kotlin.reflect.KClass - -class CustomAnnotationTypeAttribute(val annotations: Annotations) : TypeAttribute() { - constructor(annotations: List) : this(Annotations.create(annotations)) - - override fun union(other: CustomAnnotationTypeAttribute?): CustomAnnotationTypeAttribute? = null - - override fun intersect(other: CustomAnnotationTypeAttribute?): CustomAnnotationTypeAttribute? = null - - override fun add(other: CustomAnnotationTypeAttribute?): CustomAnnotationTypeAttribute { - if (other == null) return this - return CustomAnnotationTypeAttribute(Annotations.create(annotations + other.annotations)) - } - - override fun isSubtypeOf(other: CustomAnnotationTypeAttribute?): Boolean = true - - override val key: KClass - get() = CustomAnnotationTypeAttribute::class -} - -val TypeAttributes.custom: CustomAnnotationTypeAttribute? by TypeAttributes.attributeAccessor() - -val TypeAttributes.customAnnotations: Annotations get() = custom?.annotations ?: Annotations.EMPTY diff --git a/core/descriptors/src/org/jetbrains/kotlin/types/ErrorType.kt b/core/descriptors/src/org/jetbrains/kotlin/types/ErrorType.kt index 0d5ecb59154..2fbfe2e4b2b 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/types/ErrorType.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/types/ErrorType.kt @@ -36,8 +36,6 @@ open class ErrorType @JvmOverloads internal constructor( override fun toString(): String = constructor.toString() + if (arguments.isEmpty()) "" else arguments.joinToString(", ", "<", ">", -1, "...", null) - override fun replaceAnnotations(newAnnotations: Annotations): SimpleType = this - override fun replaceAttributes(newAttributes: TypeAttributes): SimpleType = this override fun makeNullableAsSpecified(newNullability: Boolean): SimpleType = diff --git a/core/descriptors/src/org/jetbrains/kotlin/types/IntersectionTypeConstructor.kt b/core/descriptors/src/org/jetbrains/kotlin/types/IntersectionTypeConstructor.kt index 27c6390593f..4cd32544219 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/types/IntersectionTypeConstructor.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/types/IntersectionTypeConstructor.kt @@ -77,7 +77,7 @@ class IntersectionTypeConstructor(typesToIntersect: Collection) : Ty @OptIn(TypeRefinement::class) fun createType(): SimpleType = KotlinTypeFactory.simpleTypeWithNonTrivialMemberScope( - Annotations.EMPTY, this, listOf(), false, this.createScopeForKotlinType() + TypeAttributes.Empty, this, listOf(), false, this.createScopeForKotlinType() ) { kotlinTypeRefiner -> this.refine(kotlinTypeRefiner).createType() } diff --git a/core/descriptors/src/org/jetbrains/kotlin/types/KotlinType.kt b/core/descriptors/src/org/jetbrains/kotlin/types/KotlinType.kt index 839eb3c60c8..b84c606319d 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/types/KotlinType.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/types/KotlinType.kt @@ -52,8 +52,6 @@ sealed class KotlinType : Annotated, KotlinTypeMarker { abstract val isMarkedNullable: Boolean abstract val memberScope: MemberScope abstract val attributes: TypeAttributes - override val annotations: Annotations - get() = attributes.toDefaultAnnotations() abstract fun unwrap(): UnwrappedType @@ -163,7 +161,6 @@ abstract class WrappedType : KotlinType() { * todo: specify what happens with internal structure when we apply some [TypeSubstitutor] */ sealed class UnwrappedType : KotlinType() { - abstract fun replaceAnnotations(newAnnotations: Annotations): UnwrappedType abstract fun replaceAttributes(newAttributes: TypeAttributes): UnwrappedType abstract fun makeNullableAsSpecified(newNullability: Boolean): UnwrappedType @@ -179,7 +176,6 @@ sealed class UnwrappedType : KotlinType() { * Or more precisely, all instances are subclasses of [SimpleType] or [WrappedType] (which contains [SimpleType] inside). */ abstract class SimpleType : UnwrappedType(), SimpleTypeMarker, TypeArgumentListMarker { - abstract override fun replaceAnnotations(newAnnotations: Annotations): SimpleType abstract override fun replaceAttributes(newAttributes: TypeAttributes): SimpleType abstract override fun makeNullableAsSpecified(newNullability: Boolean): SimpleType diff --git a/core/descriptors/src/org/jetbrains/kotlin/types/KotlinTypeFactory.kt b/core/descriptors/src/org/jetbrains/kotlin/types/KotlinTypeFactory.kt index 0a2bbcbba4e..7efd3979202 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/types/KotlinTypeFactory.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/types/KotlinTypeFactory.kt @@ -65,31 +65,6 @@ object KotlinTypeFactory { } } - @JvmStatic - @JvmOverloads - @OptIn(TypeRefinement::class) - fun simpleType( - annotations: Annotations, - constructor: TypeConstructor, - arguments: List, - nullable: Boolean, - kotlinTypeRefiner: KotlinTypeRefiner? = null - ): SimpleType { - if (annotations.isEmpty() && arguments.isEmpty() && !nullable && constructor.declarationDescriptor != null) { - return constructor.declarationDescriptor!!.defaultType - } - - return simpleTypeWithNonTrivialMemberScope( - annotations, constructor, arguments, nullable, - computeMemberScope(constructor, arguments, kotlinTypeRefiner) - ) f@{ refiner -> - val expandedTypeOrRefinedConstructor = refineConstructor(constructor, refiner, arguments) ?: return@f null - expandedTypeOrRefinedConstructor.expandedType?.let { return@f it } - - simpleType(annotations, expandedTypeOrRefinedConstructor.refinedConstructor!!, arguments, nullable, refiner) - } - } - @JvmStatic @JvmOverloads @OptIn(TypeRefinement::class) @@ -118,7 +93,7 @@ object KotlinTypeFactory { @JvmStatic fun TypeAliasDescriptor.computeExpandedType(arguments: List): SimpleType { return TypeAliasExpander(TypeAliasExpansionReportStrategy.DO_NOTHING, false).expand( - TypeAliasExpansion.create(null, this, arguments), Annotations.EMPTY + TypeAliasExpansion.create(null, this, arguments), TypeAttributes.Empty ) } @@ -141,33 +116,6 @@ object KotlinTypeFactory { private class ExpandedTypeOrRefinedConstructor(val expandedType: SimpleType?, val refinedConstructor: TypeConstructor?) - @JvmStatic - @OptIn(TypeRefinement::class) - fun simpleTypeWithNonTrivialMemberScope( - annotations: Annotations, - constructor: TypeConstructor, - arguments: List, - nullable: Boolean, - memberScope: MemberScope - ): SimpleType = - SimpleTypeImpl(constructor, arguments, nullable, memberScope) { kotlinTypeRefiner -> - val expandedTypeOrRefinedConstructor = refineConstructor(constructor, kotlinTypeRefiner, arguments) ?: return@SimpleTypeImpl null - expandedTypeOrRefinedConstructor.expandedType?.let { return@SimpleTypeImpl it } - - simpleTypeWithNonTrivialMemberScope( - annotations, - expandedTypeOrRefinedConstructor.refinedConstructor!!, - arguments, - nullable, - memberScope - ) - }.let { - if (annotations.isEmpty()) - it - else - SimpleTypeWithAttributes(it, annotations.toDefaultAttributes()) - } - @JvmStatic @OptIn(TypeRefinement::class) fun simpleTypeWithNonTrivialMemberScope( @@ -195,23 +143,6 @@ object KotlinTypeFactory { SimpleTypeWithAttributes(it, attributes) } - @JvmStatic - fun simpleTypeWithNonTrivialMemberScope( - annotations: Annotations, - constructor: TypeConstructor, - arguments: List, - nullable: Boolean, - memberScope: MemberScope, - refinedTypeFactory: RefinedTypeFactory - ): SimpleType = - SimpleTypeImpl(constructor, arguments, nullable, memberScope, refinedTypeFactory) - .let { - if (annotations.isEmpty()) - it - else - SimpleTypeWithAttributes(it, annotations.toDefaultAttributes()) - } - @JvmStatic fun simpleTypeWithNonTrivialMemberScope( attributes: TypeAttributes, @@ -229,13 +160,6 @@ object KotlinTypeFactory { SimpleTypeWithAttributes(it, attributes) } - @JvmStatic - fun simpleNotNullType( - annotations: Annotations, - descriptor: ClassDescriptor, - arguments: List - ): SimpleType = simpleType(annotations, descriptor.typeConstructor, arguments, nullable = false) - @JvmStatic fun simpleNotNullType( attributes: TypeAttributes, @@ -246,7 +170,7 @@ object KotlinTypeFactory { @JvmStatic fun simpleType( baseType: SimpleType, - annotations: Annotations = baseType.annotations, + annotations: TypeAttributes = baseType.attributes, constructor: TypeConstructor = baseType.constructor, arguments: List = baseType.arguments, nullable: Boolean = baseType.isMarkedNullable @@ -260,11 +184,11 @@ object KotlinTypeFactory { @JvmStatic fun integerLiteralType( - annotations: Annotations, + attributes: TypeAttributes, constructor: IntegerLiteralTypeConstructor, nullable: Boolean ): SimpleType = simpleTypeWithNonTrivialMemberScope( - annotations, + attributes, constructor, emptyList(), nullable, @@ -290,12 +214,6 @@ private class SimpleTypeImpl( this else SimpleTypeWithAttributes(this, newAttributes) - override fun replaceAnnotations(newAnnotations: Annotations) = - if (newAnnotations.isEmpty()) - this - else - SimpleTypeWithAttributes(this, attributes.replaceAnnotations(newAnnotations)) - override fun makeNullableAsSpecified(newNullability: Boolean) = when { newNullability == isMarkedNullable -> this newNullability -> NullableSimpleType(this) @@ -322,9 +240,6 @@ class SupposititiousSimpleType(private val realType: SimpleType, val overwritten else SupposititiousSimpleType(newType, overwrittenClass) } - override fun replaceAnnotations(newAnnotations: Annotations): SimpleType = - maybeWrap(realType.replaceAnnotations(newAnnotations)) - override fun replaceAttributes(newAttributes: TypeAttributes): SimpleType = maybeWrap(realType.replaceAttributes(newAttributes)) @@ -350,15 +265,9 @@ abstract class DelegatingSimpleTypeImpl(override val delegate: SimpleType) : Del else this - override fun replaceAnnotations(newAnnotations: Annotations) = - if (newAnnotations !== annotations) - SimpleTypeWithAttributes(this, attributes.replaceAnnotations(newAnnotations)) - else - this - override fun makeNullableAsSpecified(newNullability: Boolean): SimpleType { if (newNullability == isMarkedNullable) return this - return delegate.makeNullableAsSpecified(newNullability).replaceAnnotations(annotations) + return delegate.makeNullableAsSpecified(newNullability).replaceAttributes(attributes) } } @@ -366,6 +275,8 @@ private class SimpleTypeWithAttributes( delegate: SimpleType, override val attributes: TypeAttributes ) : DelegatingSimpleTypeImpl(delegate) { + override val annotations: Annotations + get() = attributes.toDefaultAnnotations() @TypeRefinement override fun replaceDelegate(delegate: SimpleType) = SimpleTypeWithAttributes(delegate, attributes) } diff --git a/core/descriptors/src/org/jetbrains/kotlin/types/SpecialTypes.kt b/core/descriptors/src/org/jetbrains/kotlin/types/SpecialTypes.kt index 70f8822a03c..aaa9037c868 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/types/SpecialTypes.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/types/SpecialTypes.kt @@ -48,9 +48,6 @@ abstract class DelegatingSimpleType : SimpleType() { class AbbreviatedType(override val delegate: SimpleType, val abbreviation: SimpleType) : DelegatingSimpleType() { val expandedType: SimpleType get() = delegate - override fun replaceAnnotations(newAnnotations: Annotations) = - AbbreviatedType(delegate.replaceAnnotations(newAnnotations), abbreviation) - override fun replaceAttributes(newAttributes: TypeAttributes): SimpleType = AbbreviatedType(delegate.replaceAttributes(newAttributes), abbreviation) @@ -172,9 +169,6 @@ class DefinitelyNotNullType private constructor( override fun substitutionResult(replacement: KotlinType): KotlinType = replacement.unwrap().makeDefinitelyNotNullOrNotNull(useCorrectedNullabilityForTypeParameters) - override fun replaceAnnotations(newAnnotations: Annotations): DefinitelyNotNullType = - DefinitelyNotNullType(delegate.replaceAnnotations(newAnnotations), useCorrectedNullabilityForTypeParameters) - override fun replaceAttributes(newAttributes: TypeAttributes): SimpleType = DefinitelyNotNullType(delegate.replaceAttributes(newAttributes), useCorrectedNullabilityForTypeParameters) diff --git a/core/descriptors/src/org/jetbrains/kotlin/types/StubTypes.kt b/core/descriptors/src/org/jetbrains/kotlin/types/StubTypes.kt index b2d4d22633f..8e9650e358a 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/types/StubTypes.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/types/StubTypes.kt @@ -64,8 +64,6 @@ abstract class AbstractStubType(val originalTypeVariable: TypeConstructor, overr override val attributes: TypeAttributes get() = TypeAttributes.Empty - override fun replaceAnnotations(newAnnotations: Annotations): SimpleType = this - override fun replaceAttributes(newAttributes: TypeAttributes): SimpleType = this override fun makeNullableAsSpecified(newNullability: Boolean): SimpleType { diff --git a/core/descriptors/src/org/jetbrains/kotlin/types/TypeAliasExpander.kt b/core/descriptors/src/org/jetbrains/kotlin/types/TypeAliasExpander.kt index aef0505dd38..1522d9a4474 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/types/TypeAliasExpander.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/types/TypeAliasExpander.kt @@ -10,7 +10,6 @@ import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor import org.jetbrains.kotlin.descriptors.annotations.Annotations import org.jetbrains.kotlin.descriptors.annotations.composeAnnotations import org.jetbrains.kotlin.resolve.scopes.MemberScope -import org.jetbrains.kotlin.types.checker.KotlinTypeChecker import org.jetbrains.kotlin.types.typeUtil.containsTypeAliasParameters import org.jetbrains.kotlin.types.typeUtil.requiresTypeAliasExpansion @@ -19,21 +18,21 @@ class TypeAliasExpander( private val shouldCheckBounds: Boolean ) { - fun expand(typeAliasExpansion: TypeAliasExpansion, annotations: Annotations) = + fun expand(typeAliasExpansion: TypeAliasExpansion, attributes: TypeAttributes) = expandRecursively( - typeAliasExpansion, annotations, + typeAliasExpansion, attributes, isNullable = false, recursionDepth = 0, withAbbreviatedType = true ) - fun expandWithoutAbbreviation(typeAliasExpansion: TypeAliasExpansion, annotations: Annotations) = + fun expandWithoutAbbreviation(typeAliasExpansion: TypeAliasExpansion, attributes: TypeAttributes) = expandRecursively( - typeAliasExpansion, annotations, + typeAliasExpansion, attributes, isNullable = false, recursionDepth = 0, withAbbreviatedType = false ) private fun expandRecursively( typeAliasExpansion: TypeAliasExpansion, - annotations: Annotations, + attributes: TypeAttributes, isNullable: Boolean, recursionDepth: Int, withAbbreviatedType: Boolean @@ -51,19 +50,19 @@ class TypeAliasExpander( "Type alias expansion: result for ${typeAliasExpansion.descriptor} is ${expandedProjection.projectionKind}, should be invariant" } - checkRepeatedAnnotations(expandedType.annotations, annotations) + checkRepeatedAnnotations(expandedType.annotations, attributes.annotations) val expandedTypeWithExtraAnnotations = - expandedType.combineAnnotations(annotations).let { TypeUtils.makeNullableIfNeeded(it, isNullable) } + expandedType.combineAttributes(attributes).let { TypeUtils.makeNullableIfNeeded(it, isNullable) } return if (withAbbreviatedType) - expandedTypeWithExtraAnnotations.withAbbreviation(typeAliasExpansion.createAbbreviation(annotations, isNullable)) + expandedTypeWithExtraAnnotations.withAbbreviation(typeAliasExpansion.createAbbreviation(attributes, isNullable)) else expandedTypeWithExtraAnnotations } - private fun TypeAliasExpansion.createAbbreviation(annotations: Annotations, isNullable: Boolean) = + private fun TypeAliasExpansion.createAbbreviation(attributes: TypeAttributes, isNullable: Boolean) = KotlinTypeFactory.simpleTypeWithNonTrivialMemberScope( - annotations, + attributes, descriptor.typeConstructor, arguments, isNullable, @@ -125,23 +124,23 @@ class TypeAliasExpander( val substitutedType = if (argumentType is DynamicType) - argumentType.combineAnnotations(underlyingType.annotations) + argumentType.combineAttributes(underlyingType.attributes) else argumentType.asSimpleType().combineNullabilityAndAnnotations(underlyingType) return TypeProjectionImpl(resultingVariance, substitutedType) } - private fun DynamicType.combineAnnotations(newAnnotations: Annotations): DynamicType = - replaceAnnotations(createCombinedAnnotations(newAnnotations)) + private fun DynamicType.combineAttributes(newAttributes: TypeAttributes): DynamicType = + replaceAttributes(createdCombinedAttributes(newAttributes)) - private fun SimpleType.combineAnnotations(newAnnotations: Annotations): SimpleType = - if (isError) this else replace(newAnnotations = createCombinedAnnotations(newAnnotations)) + private fun SimpleType.combineAttributes(newAttributes: TypeAttributes): SimpleType = + if (isError) this else replaceAttributes(newAttributes = createdCombinedAttributes(newAttributes)) - private fun KotlinType.createCombinedAnnotations(newAnnotations: Annotations): Annotations { - if (isError) return annotations + private fun KotlinType.createdCombinedAttributes(newAttributes: TypeAttributes): TypeAttributes { + if (isError) return attributes - return composeAnnotations(newAnnotations, annotations) + return newAttributes.add(attributes) } private fun checkRepeatedAnnotations(existingAnnotations: Annotations, newAnnotations: Annotations) { @@ -158,7 +157,7 @@ class TypeAliasExpander( TypeUtils.makeNullableIfNeeded(this, fromType.isMarkedNullable) private fun SimpleType.combineNullabilityAndAnnotations(fromType: KotlinType) = - combineNullability(fromType).combineAnnotations(fromType.annotations) + combineNullability(fromType).combineAttributes(fromType.attributes) private fun expandNonArgumentTypeProjection( originalProjection: TypeProjection, @@ -201,7 +200,7 @@ class TypeAliasExpander( TypeAliasExpansion.create(typeAliasExpansion, typeDescriptor, expandedArguments) val nestedExpandedType = expandRecursively( - nestedExpansion, type.annotations, + nestedExpansion, type.attributes, isNullable = type.isMarkedNullable, recursionDepth = recursionDepth + 1, withAbbreviatedType = false diff --git a/core/descriptors/src/org/jetbrains/kotlin/types/TypeAttributeTranslator.kt b/core/descriptors/src/org/jetbrains/kotlin/types/TypeAttributeTranslator.kt index ed48494b994..90a773754ac 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/types/TypeAttributeTranslator.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/types/TypeAttributeTranslator.kt @@ -5,19 +5,29 @@ package org.jetbrains.kotlin.types +import org.jetbrains.kotlin.descriptors.DeclarationDescriptor import org.jetbrains.kotlin.descriptors.annotations.Annotations interface TypeAttributeTranslator { - fun toAttributes(annotations: Annotations): TypeAttributes + fun toAttributes( + annotations: Annotations, + typeConstructor: TypeConstructor? = null, + containingDeclaration: DeclarationDescriptor? = null + ): TypeAttributes + fun toAnnotations(attributes: TypeAttributes): Annotations } object DefaultTypeAttributeTranslator : TypeAttributeTranslator { - override fun toAnnotations(attributes: TypeAttributes): Annotations { - return attributes.customAnnotations + override fun toAttributes( + annotations: Annotations, + typeConstructor: TypeConstructor?, + containingDeclaration: DeclarationDescriptor? + ): TypeAttributes { + return TypeAttributes.create(listOf(AnnotationsTypeAttribute(annotations))) } - override fun toAttributes(annotations: Annotations): TypeAttributes { - return TypeAttributes.create(listOf(CustomAnnotationTypeAttribute(annotations))) + override fun toAnnotations(attributes: TypeAttributes): Annotations { + return attributes.annotations } } diff --git a/core/descriptors/src/org/jetbrains/kotlin/types/TypeAttributes.kt b/core/descriptors/src/org/jetbrains/kotlin/types/TypeAttributes.kt index 2fb6364fdf4..ffc80b685c3 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/types/TypeAttributes.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/types/TypeAttributes.kt @@ -41,9 +41,7 @@ class TypeAttributes private constructor(attributes: List>) : A return generateNullableAccessor, T>(T::class) as ReadOnlyProperty } - val Empty: TypeAttributes = TypeAttributes(emptyList()) - - private fun TypeAttribute<*>.predefined(): Pair, TypeAttributes> = this to TypeAttributes(this) + val Empty: TypeAttributes = TypeAttributes(listOf(AnnotationsTypeAttribute(Annotations.EMPTY))) fun create(attributes: List>): TypeAttributes { return if (attributes.isEmpty()) { @@ -119,6 +117,6 @@ fun TypeAttributes.toDefaultAnnotations(): Annotations = fun Annotations.toDefaultAttributes(): TypeAttributes = DefaultTypeAttributeTranslator.toAttributes(this) fun TypeAttributes.replaceAnnotations(newAnnotations: Annotations): TypeAttributes { - val withoutCustom = (custom?.let { this.remove(it) } ?: this) + val withoutCustom = (annotationsAttribute?.let { this.remove(it) } ?: this) return withoutCustom.add(newAnnotations.toDefaultAttributes()) } diff --git a/core/descriptors/src/org/jetbrains/kotlin/types/TypeSubstitution.kt b/core/descriptors/src/org/jetbrains/kotlin/types/TypeSubstitution.kt index 0703b53d2d2..ad6943ebd16 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/types/TypeSubstitution.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/types/TypeSubstitution.kt @@ -160,12 +160,14 @@ fun SimpleType.replace( ): SimpleType { if (newArguments.isEmpty() && newAnnotations === annotations) return this + val newAttributes = attributes.replaceAnnotations(newAnnotations) + if (newArguments.isEmpty()) { - return replaceAnnotations(newAnnotations) + return replaceAttributes(newAttributes) } return KotlinTypeFactory.simpleType( - newAnnotations, + newAttributes, constructor, newArguments, isMarkedNullable diff --git a/core/descriptors/src/org/jetbrains/kotlin/types/TypeUtils.java b/core/descriptors/src/org/jetbrains/kotlin/types/TypeUtils.java index 9f3b34e710a..801bdb45cad 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/types/TypeUtils.java +++ b/core/descriptors/src/org/jetbrains/kotlin/types/TypeUtils.java @@ -46,12 +46,6 @@ public class TypeUtils { throw new IllegalStateException(name); } - @NotNull - @Override - public SimpleType replaceAnnotations(@NotNull Annotations newAnnotations) { - throw new IllegalStateException(name); - } - @NotNull @Override public SimpleType replaceAttributes(@NotNull TypeAttributes newAttributes) { @@ -227,7 +221,7 @@ public class TypeUtils { ) { List arguments = getDefaultTypeProjections(typeConstructor.getParameters()); return KotlinTypeFactory.simpleTypeWithNonTrivialMemberScope( - Annotations.Companion.getEMPTY(), + TypeAttributes.Companion.getEmpty(), typeConstructor, arguments, false, diff --git a/core/descriptors/src/org/jetbrains/kotlin/types/TypeUtils.kt b/core/descriptors/src/org/jetbrains/kotlin/types/TypeUtils.kt index a6972d3512c..432ef019fde 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/types/TypeUtils.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/types/TypeUtils.kt @@ -118,7 +118,7 @@ fun TypeProjection.substitute(doSubstitute: (KotlinType) -> KotlinType): TypePro fun KotlinType.replaceAnnotations(newAnnotations: Annotations): KotlinType { if (annotations.isEmpty() && newAnnotations.isEmpty()) return this - return unwrap().replaceAnnotations(newAnnotations) + return unwrap().replaceAttributes(attributes.replaceAnnotations(newAnnotations)) } fun KotlinTypeChecker.equalTypesOrNulls(type1: KotlinType?, type2: KotlinType?): Boolean { diff --git a/core/descriptors/src/org/jetbrains/kotlin/types/TypeWithEnhancement.kt b/core/descriptors/src/org/jetbrains/kotlin/types/TypeWithEnhancement.kt index d2d89ef080c..20834cc579e 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/types/TypeWithEnhancement.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/types/TypeWithEnhancement.kt @@ -34,9 +34,6 @@ class SimpleTypeWithEnhancement( override val origin get() = delegate - override fun replaceAnnotations(newAnnotations: Annotations): SimpleType = - origin.replaceAnnotations(newAnnotations).wrapEnhancement(enhancement) as SimpleType - override fun replaceAttributes(newAttributes: TypeAttributes): SimpleType = origin.replaceAttributes(newAttributes).wrapEnhancement(enhancement) as SimpleType @@ -64,9 +61,6 @@ class FlexibleTypeWithEnhancement( ) : FlexibleType(origin.lowerBound, origin.upperBound), TypeWithEnhancement { - override fun replaceAnnotations(newAnnotations: Annotations): UnwrappedType = - origin.replaceAnnotations(newAnnotations).wrapEnhancement(enhancement) - override fun replaceAttributes(newAttributes: TypeAttributes): UnwrappedType = origin.replaceAttributes(newAttributes).wrapEnhancement(enhancement) 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 9d4cb655ba3..ad4d966a32f 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/types/checker/ClassicTypeSystemContext.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/types/checker/ClassicTypeSystemContext.kt @@ -641,17 +641,15 @@ interface ClassicTypeSystemContext : TypeSystemInferenceExtensionContext, TypeSy override fun unionTypeAttributes(types: List): List { @Suppress("UNCHECKED_CAST") - types as List - return types.map { it.unwrap().attributes }.reduce { x, y -> x.union(y) }.toList() + return (types as List).map { it.unwrap().attributes }.reduce { x, y -> x.union(y) }.toList() } override fun KotlinTypeMarker.replaceTypeAttributes(newAttributes: List): KotlinTypeMarker { require(this is KotlinType) - val typeAttributes = newAttributes.filterIsInstance>() - require(typeAttributes.size == newAttributes.size) if (newAttributes.isEmpty()) return this + @Suppress("UNCHECKED_CAST") return this.unwrap().replaceAttributes( - TypeAttributes.create(typeAttributes) + TypeAttributes.create(newAttributes as List>) ) } @@ -675,6 +673,11 @@ interface ClassicTypeSystemContext : TypeSystemInferenceExtensionContext, TypeSy return this.attributes.toList() } + override fun KotlinTypeMarker.getCustomAttributes(): List { + require(this is KotlinType, this::errorMessage) + return this.attributes.filterNot { it is AnnotationsTypeAttribute } + } + override fun captureFromExpression(type: KotlinTypeMarker): KotlinTypeMarker? { return captureFromExpressionInternal(type as UnwrappedType) } diff --git a/core/descriptors/src/org/jetbrains/kotlin/types/checker/KotlinTypePreparator.kt b/core/descriptors/src/org/jetbrains/kotlin/types/checker/KotlinTypePreparator.kt index c068d6b1c77..734bfb50a3d 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/types/checker/KotlinTypePreparator.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/types/checker/KotlinTypePreparator.kt @@ -36,7 +36,7 @@ abstract class KotlinTypePreparator : AbstractTypePreparator() { val newConstructor = IntersectionTypeConstructor(constructor.supertypes.map { TypeUtils.makeNullableAsSpecified(it, type.isMarkedNullable) }) return KotlinTypeFactory.simpleTypeWithNonTrivialMemberScope( - type.annotations, + type.attributes, newConstructor, listOf(), false, 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 afc737a0936..b4c152c6124 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/types/checker/NewCapturedType.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/types/checker/NewCapturedType.kt @@ -144,7 +144,7 @@ private fun captureFromArguments(type: UnwrappedType, status: CaptureStatus): Un } private fun UnwrappedType.replaceArguments(arguments: List) = - KotlinTypeFactory.simpleType(annotations, constructor, arguments, isMarkedNullable) + KotlinTypeFactory.simpleType(attributes, constructor, arguments, isMarkedNullable) private fun captureArguments(type: UnwrappedType, status: CaptureStatus): List? { if (type.arguments.size != type.constructor.parameters.size) return null @@ -207,14 +207,14 @@ class NewCapturedType( captureStatus: CaptureStatus, lowerType: UnwrappedType?, projection: TypeProjection, typeParameter: TypeParameterDescriptor ) : this(captureStatus, NewCapturedTypeConstructor(projection, typeParameter = typeParameter), lowerType) + override val annotations: Annotations + get() = attributes.toDefaultAnnotations() + override val arguments: List get() = listOf() override val memberScope: MemberScope // todo what about foo().bar() where foo() return captured type? get() = ErrorUtils.createErrorScope("No member resolution should be done on captured type!", true) - override fun replaceAnnotations(newAnnotations: Annotations) = - NewCapturedType(captureStatus, constructor, lowerType, newAnnotations.toDefaultAttributes(), isMarkedNullable) - override fun replaceAttributes(newAttributes: TypeAttributes): SimpleType = NewCapturedType(captureStatus, constructor, lowerType, newAttributes, isMarkedNullable) diff --git a/core/descriptors/src/org/jetbrains/kotlin/types/dynamicTypes.kt b/core/descriptors/src/org/jetbrains/kotlin/types/dynamicTypes.kt index 6764d8ae960..8e0809160a0 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/types/dynamicTypes.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/types/dynamicTypes.kt @@ -52,9 +52,6 @@ class DynamicType( override val isMarkedNullable: Boolean get() = false - override fun replaceAnnotations(newAnnotations: Annotations): DynamicType = - DynamicType(delegate.builtIns, attributes.replaceAnnotations(newAnnotations)) - override fun replaceAttributes(newAttributes: TypeAttributes): DynamicType = DynamicType(delegate.builtIns, newAttributes) diff --git a/core/descriptors/src/org/jetbrains/kotlin/types/extensions/TypeAttributeTranslators.kt b/core/descriptors/src/org/jetbrains/kotlin/types/extensions/TypeAttributeTranslators.kt index 36311346a14..72b62939c95 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/types/extensions/TypeAttributeTranslators.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/types/extensions/TypeAttributeTranslators.kt @@ -6,20 +6,29 @@ package org.jetbrains.kotlin.types.extensions import com.intellij.openapi.project.Project +import org.jetbrains.kotlin.descriptors.DeclarationDescriptor import org.jetbrains.kotlin.descriptors.annotations.Annotations import org.jetbrains.kotlin.extensions.ProjectExtensionDescriptor import org.jetbrains.kotlin.types.* interface TypeAttributeTranslatorExtension : TypeAttributeTranslator +class TypeAttributeTranslatorsForInjection(project: Project) { + val translators: List = TypeAttributeTranslators(project).translators +} + class TypeAttributeTranslators private constructor(val translators: List) { constructor(project: Project) : this(getInstances(project) + DefaultTypeAttributeTranslator) - fun toAttributes(annotations: Annotations): TypeAttributes { + fun toAttributes( + annotations: Annotations, + typeConstructor: TypeConstructor, + containingDeclaration: DeclarationDescriptor? = null + ): TypeAttributes { val translated = translators.map { translator -> - translator.toAttributes(annotations) + translator.toAttributes(annotations, typeConstructor, containingDeclaration) }.flatten() return TypeAttributes.create(translated) } diff --git a/core/descriptors/src/org/jetbrains/kotlin/types/flexibleTypes.kt b/core/descriptors/src/org/jetbrains/kotlin/types/flexibleTypes.kt index d13df6e7153..f98e42ca14d 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/types/flexibleTypes.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/types/flexibleTypes.kt @@ -125,9 +125,6 @@ class FlexibleTypeImpl(lowerBound: SimpleType, upperBound: SimpleType) : Flexibl }.inheritEnhancement(unwrapped) } - override fun replaceAnnotations(newAnnotations: Annotations): UnwrappedType = - KotlinTypeFactory.flexibleType(lowerBound.replaceAnnotations(newAnnotations), upperBound.replaceAnnotations(newAnnotations)) - override fun replaceAttributes(newAttributes: TypeAttributes): UnwrappedType = KotlinTypeFactory.flexibleType(lowerBound.replaceAttributes(newAttributes), upperBound.replaceAttributes(newAttributes)) diff --git a/core/deserialization/src/org/jetbrains/kotlin/serialization/deserialization/MetadataPackageFragmentProvider.kt b/core/deserialization/src/org/jetbrains/kotlin/serialization/deserialization/MetadataPackageFragmentProvider.kt index 658b9740ee3..0da517263cb 100644 --- a/core/deserialization/src/org/jetbrains/kotlin/serialization/deserialization/MetadataPackageFragmentProvider.kt +++ b/core/deserialization/src/org/jetbrains/kotlin/serialization/deserialization/MetadataPackageFragmentProvider.kt @@ -34,7 +34,7 @@ import org.jetbrains.kotlin.resolve.scopes.MemberScope import org.jetbrains.kotlin.serialization.deserialization.builtins.BuiltInSerializerProtocol import org.jetbrains.kotlin.serialization.deserialization.descriptors.DeserializedPackageMemberScope import org.jetbrains.kotlin.storage.StorageManager -import org.jetbrains.kotlin.types.TypeAttributeTranslator +import org.jetbrains.kotlin.types.extensions.TypeAttributeTranslatorsForInjection import org.jetbrains.kotlin.types.checker.NewKotlinTypeChecker import java.io.InputStream @@ -47,7 +47,7 @@ class MetadataPackageFragmentProvider( contractDeserializer: ContractDeserializer, kotlinTypeChecker: NewKotlinTypeChecker, samConversionResolver: SamConversionResolver, - typeAttributeTranslators: List + typeAttributeTranslators: TypeAttributeTranslatorsForInjection ) : AbstractDeserializedPackageFragmentProvider(storageManager, finder, moduleDescriptor) { init { components = DeserializationComponents( @@ -68,7 +68,7 @@ class MetadataPackageFragmentProvider( BuiltInSerializerProtocol.extensionRegistry, kotlinTypeChecker, samConversionResolver, - typeAttributeTranslators = typeAttributeTranslators + typeAttributeTranslators = typeAttributeTranslators.translators ) } 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 e0251996cae..756ee73d079 100644 --- a/core/deserialization/src/org/jetbrains/kotlin/serialization/deserialization/TypeDeserializer.kt +++ b/core/deserialization/src/org/jetbrains/kotlin/serialization/deserialization/TypeDeserializer.kt @@ -67,9 +67,13 @@ class TypeDeserializer( return simpleType(proto, expandTypeAliases = true) } - private fun List.toAttributes(annotations: Annotations): TypeAttributes { + private fun List.toAttributes( + annotations: Annotations, + constructor: TypeConstructor, + containingDeclaration: DeclarationDescriptor + ): TypeAttributes { val translated = this.map { translator -> - translator.toAttributes(annotations) + translator.toAttributes(annotations, constructor, containingDeclaration) }.flatten() return TypeAttributes.create(translated) } @@ -92,7 +96,7 @@ class TypeDeserializer( c.components.annotationAndConstantLoader.loadTypeAnnotations(proto, c.nameResolver) } - val attributes = c.components.typeAttributeTranslators.toAttributes(annotations) + val attributes = c.components.typeAttributeTranslators.toAttributes(annotations, constructor, c.containingDeclaration) fun ProtoBuf.Type.collectAllArguments(): List = argumentList + outerType(c.typeTable)?.collectAllArguments().orEmpty() @@ -107,7 +111,9 @@ class TypeDeserializer( expandTypeAliases && declarationDescriptor is TypeAliasDescriptor -> { val expandedType = with(KotlinTypeFactory) { declarationDescriptor.computeExpandedType(arguments) } val expandedAttributes = c.components.typeAttributeTranslators.toAttributes( - Annotations.create(annotations + expandedType.annotations) + Annotations.create(annotations + expandedType.annotations), + constructor, + c.containingDeclaration ) expandedType .makeNullableAsSpecified(expandedType.isNullable() || proto.nullable) diff --git a/core/reflection.jvm/src/kotlin/reflect/full/KClassifiers.kt b/core/reflection.jvm/src/kotlin/reflect/full/KClassifiers.kt index 62ee114d238..d3166d47fb8 100644 --- a/core/reflection.jvm/src/kotlin/reflect/full/KClassifiers.kt +++ b/core/reflection.jvm/src/kotlin/reflect/full/KClassifiers.kt @@ -56,18 +56,18 @@ fun KClassifier.createType( // TODO: throw exception if argument does not satisfy bounds - val typeAnnotations = - if (annotations.isEmpty()) Annotations.EMPTY - else Annotations.EMPTY // TODO: support type annotations + val typeAttributes = + if (annotations.isEmpty()) TypeAttributes.Empty + else TypeAttributes.Empty // TODO: support type annotations - return KTypeImpl(createKotlinType(typeAnnotations, typeConstructor, arguments, nullable)) + return KTypeImpl(createKotlinType(typeAttributes, typeConstructor, arguments, nullable)) } private fun createKotlinType( - typeAnnotations: Annotations, typeConstructor: TypeConstructor, arguments: List, nullable: Boolean + attributes: TypeAttributes, typeConstructor: TypeConstructor, arguments: List, nullable: Boolean ): SimpleType { val parameters = typeConstructor.parameters - return KotlinTypeFactory.simpleType(typeAnnotations, typeConstructor, arguments.mapIndexed { index, typeProjection -> + return KotlinTypeFactory.simpleType(attributes, typeConstructor, arguments.mapIndexed { index, typeProjection -> val type = (typeProjection.type as KTypeImpl?)?.type when (typeProjection.variance) { KVariance.INVARIANT -> TypeProjectionImpl(Variance.INVARIANT, type!!) diff --git a/plugins/android-extensions/android-extensions-compiler/src/org/jetbrains/kotlin/android/synthetic/res/syntheticDescriptorGeneration.kt b/plugins/android-extensions/android-extensions-compiler/src/org/jetbrains/kotlin/android/synthetic/res/syntheticDescriptorGeneration.kt index fa8eba0f79d..780080df9a4 100644 --- a/plugins/android-extensions/android-extensions-compiler/src/org/jetbrains/kotlin/android/synthetic/res/syntheticDescriptorGeneration.kt +++ b/plugins/android-extensions/android-extensions-compiler/src/org/jetbrains/kotlin/android/synthetic/res/syntheticDescriptorGeneration.kt @@ -30,10 +30,7 @@ import org.jetbrains.kotlin.resolve.DescriptorFactory import org.jetbrains.kotlin.resolve.descriptorUtil.builtIns import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameUnsafe import org.jetbrains.kotlin.resolve.source.PsiSourceElement -import org.jetbrains.kotlin.types.KotlinType -import org.jetbrains.kotlin.types.KotlinTypeFactory -import org.jetbrains.kotlin.types.SimpleType -import org.jetbrains.kotlin.types.StarProjectionImpl +import org.jetbrains.kotlin.types.* private class XmlSourceElement(override val psi: PsiElement) : PsiSourceElement @@ -70,7 +67,7 @@ internal fun genPropertyForWidget( } else { KotlinTypeFactory.simpleNotNullType( - Annotations.EMPTY, classDescriptor, defaultType.constructor.parameters.map(::StarProjectionImpl)) + TypeAttributes.Empty, classDescriptor, defaultType.constructor.parameters.map(::StarProjectionImpl)) } } ?: context.view diff --git a/plugins/kotlin-serialization/kotlin-serialization-compiler/src/org/jetbrains/kotlinx/serialization/compiler/resolve/KSerializationUtil.kt b/plugins/kotlin-serialization/kotlin-serialization-compiler/src/org/jetbrains/kotlinx/serialization/compiler/resolve/KSerializationUtil.kt index a73ad89f984..53706f557e1 100644 --- a/plugins/kotlin-serialization/kotlin-serialization-compiler/src/org/jetbrains/kotlinx/serialization/compiler/resolve/KSerializationUtil.kt +++ b/plugins/kotlin-serialization/kotlin-serialization-compiler/src/org/jetbrains/kotlinx/serialization/compiler/resolve/KSerializationUtil.kt @@ -53,7 +53,7 @@ internal fun ClassDescriptor.createSerializerTypeFor(argument: SimpleType, baseS val types = listOf(TypeProjectionImpl(projectionType, argument)) val descriptor = module.findClassAcrossModuleDependencies(ClassId.topLevel(baseSerializerInterface)) ?: throw IllegalArgumentException("Can't locate $baseSerializerInterface. Is kotlinx-serialization library present in compile classpath?") - return KotlinTypeFactory.simpleNotNullType(Annotations.EMPTY, descriptor, types) + return KotlinTypeFactory.simpleNotNullType(TypeAttributes.Empty, descriptor, types) } internal fun extractKSerializerArgumentFromImplementation(implementationClass: ClassDescriptor): KotlinType? { diff --git a/plugins/kotlin-serialization/kotlin-serialization-compiler/src/org/jetbrains/kotlinx/serialization/compiler/resolve/KSerializerDescriptorResolver.kt b/plugins/kotlin-serialization/kotlin-serialization-compiler/src/org/jetbrains/kotlinx/serialization/compiler/resolve/KSerializerDescriptorResolver.kt index edaeaf4df32..9006f685359 100644 --- a/plugins/kotlin-serialization/kotlin-serialization-compiler/src/org/jetbrains/kotlinx/serialization/compiler/resolve/KSerializerDescriptorResolver.kt +++ b/plugins/kotlin-serialization/kotlin-serialization-compiler/src/org/jetbrains/kotlinx/serialization/compiler/resolve/KSerializerDescriptorResolver.kt @@ -58,7 +58,7 @@ object KSerializerDescriptorResolver { if (!classDescriptor.needSerializerFactory()) return val serializerFactoryClass = classDescriptor.module.getClassFromInternalSerializationPackage("SerializerFactory") - supertypes.add(KotlinTypeFactory.simpleNotNullType(Annotations.EMPTY, serializerFactoryClass, listOf())) + supertypes.add(KotlinTypeFactory.simpleNotNullType(TypeAttributes.Empty, serializerFactoryClass, listOf())) } fun addSerializerSupertypes(classDescriptor: ClassDescriptor, supertypes: MutableList) { @@ -393,7 +393,7 @@ object KSerializerDescriptorResolver { assert(serializableDescriptor.declaredTypeParameters.size == typeParameters.size) val args = List(serializableDescriptor.declaredTypeParameters.size) { index -> val pType = KotlinTypeFactory.simpleNotNullType( - Annotations.EMPTY, + TypeAttributes.Empty, serializerClass, listOf(TypeProjectionImpl(typeParameters[index].defaultType)) ) @@ -430,7 +430,7 @@ object KSerializerDescriptorResolver { ) val pType = - KotlinTypeFactory.simpleNotNullType(Annotations.EMPTY, serializerClass, listOf(TypeProjectionImpl(targ.defaultType))) + KotlinTypeFactory.simpleNotNullType(TypeAttributes.Empty, serializerClass, listOf(TypeProjectionImpl(targ.defaultType))) args.add( ValueParameterDescriptorImpl( @@ -467,7 +467,7 @@ object KSerializerDescriptorResolver { val kSerializerStarType = KotlinTypeFactory.simpleNotNullType( - Annotations.EMPTY, + TypeAttributes.Empty, serializerClass, listOf(StarProjectionImpl(serializerClass.typeConstructor.parameters.first())) ) @@ -517,9 +517,9 @@ object KSerializerDescriptorResolver { val (typeArgs, args) = createKSerializerParamsForEachGenericArgument(f, serializableClass) val newSerializableType = - KotlinTypeFactory.simpleNotNullType(Annotations.EMPTY, serializableClass, typeArgs.map { TypeProjectionImpl(it.defaultType) }) + KotlinTypeFactory.simpleNotNullType(TypeAttributes.Empty, serializableClass, typeArgs.map { TypeProjectionImpl(it.defaultType) }) val serialReturnType = - KotlinTypeFactory.simpleNotNullType(Annotations.EMPTY, serializerClass, listOf(TypeProjectionImpl(newSerializableType))) + KotlinTypeFactory.simpleNotNullType(TypeAttributes.Empty, serializerClass, listOf(TypeProjectionImpl(newSerializableType))) f.initialize(null, thisClass.thisAsReceiverParameter, emptyList(), typeArgs, args, serialReturnType, Modality.FINAL, DescriptorVisibilities.PUBLIC) return f @@ -550,7 +550,7 @@ object KSerializerDescriptorResolver { // object val objectType = - KotlinTypeFactory.simpleNotNullType(Annotations.EMPTY, thisClass, typeArgs.map { TypeProjectionImpl(it.defaultType) }) + KotlinTypeFactory.simpleNotNullType(TypeAttributes.Empty, thisClass, typeArgs.map { TypeProjectionImpl(it.defaultType) }) args.add( ValueParameterDescriptorImpl( containingDeclaration = f, @@ -646,7 +646,7 @@ object KSerializerDescriptorResolver { val param = serializerDescriptor.declaredTypeParameters[index] val pType = KotlinTypeFactory.simpleNotNullType( - Annotations.EMPTY, + TypeAttributes.Empty, serializerClass, listOf(TypeProjectionImpl(param.defaultType)) ) diff --git a/plugins/kotlin-serialization/kotlin-serialization-compiler/src/org/jetbrains/kotlinx/serialization/compiler/resolve/SearchUtils.kt b/plugins/kotlin-serialization/kotlin-serialization-compiler/src/org/jetbrains/kotlinx/serialization/compiler/resolve/SearchUtils.kt index a7edcd8fe4b..d5095a8a0e6 100644 --- a/plugins/kotlin-serialization/kotlin-serialization-compiler/src/org/jetbrains/kotlinx/serialization/compiler/resolve/SearchUtils.kt +++ b/plugins/kotlin-serialization/kotlin-serialization-compiler/src/org/jetbrains/kotlinx/serialization/compiler/resolve/SearchUtils.kt @@ -18,6 +18,7 @@ import org.jetbrains.kotlin.resolve.descriptorUtil.module import org.jetbrains.kotlin.resolve.lazy.descriptors.LazyAnnotationDescriptor import org.jetbrains.kotlin.types.KotlinType import org.jetbrains.kotlin.types.KotlinTypeFactory +import org.jetbrains.kotlin.types.TypeAttributes internal fun ClassConstructorDescriptor.isSerializationCtor(): Boolean { /*kind == CallableMemberDescriptor.Kind.SYNTHESIZED does not work because DeserializedClassConstructorDescriptor loses its kind*/ @@ -117,7 +118,7 @@ internal fun ClassDescriptor.getClassFromInternalSerializationPackage(classSimpl module.getClassFromInternalSerializationPackage(classSimpleName) fun ClassDescriptor.toSimpleType(nullable: Boolean = false) = - KotlinTypeFactory.simpleType(Annotations.EMPTY, this.typeConstructor, emptyList(), nullable) + KotlinTypeFactory.simpleType(TypeAttributes.Empty, this.typeConstructor, emptyList(), nullable) internal fun Annotated.annotationsWithArguments(): List, List>> = annotations.asSequence() diff --git a/plugins/scripting/scripting-ide-common/src/org/jetbrains/kotlin/scripting/ide_common/idea/util/TypeUtils.kt b/plugins/scripting/scripting-ide-common/src/org/jetbrains/kotlin/scripting/ide_common/idea/util/TypeUtils.kt index 8e6050d52fd..5d163cdc82a 100644 --- a/plugins/scripting/scripting-ide-common/src/org/jetbrains/kotlin/scripting/ide_common/idea/util/TypeUtils.kt +++ b/plugins/scripting/scripting-ide-common/src/org/jetbrains/kotlin/scripting/ide_common/idea/util/TypeUtils.kt @@ -69,7 +69,7 @@ private fun KotlinType.approximateNonDynamicFlexibleTypes( return AbbreviatedType(it.expandedType, it.abbreviation.approximateNonDynamicFlexibleTypes(preferNotNull)) } return KotlinTypeFactory.simpleTypeWithNonTrivialMemberScope( - annotations, + attributes, constructor, arguments.map { it.substitute { type -> type.approximateFlexibleTypes(preferNotNull = true) } }, isMarkedNullable,