From a1d052b8fa0fbee14a05b0f3616289952f8cf4d9 Mon Sep 17 00:00:00 2001 From: Stanislav Erokhin Date: Fri, 22 Apr 2016 18:40:23 +0300 Subject: [PATCH] Refactoring. Remove FlexibleTypeFactory.DEFAULT and refactor flexible type creation by special fq-name for tests. --- .../kotlin/frontend/java/di/injection.kt | 4 +- .../java/InternalFlexibleTypeTransformer.kt | 42 +++++++++++++++++++ .../java/JavaFlexibleTypeFactoryProvider.kt | 25 ----------- .../jetbrains/kotlin/resolve/TypeResolver.kt | 20 ++++----- .../kotlin/checkers/BaseDiagnosticsTest.java | 8 ++-- .../jetbrains/kotlin/types/flexibleTypes.kt | 19 --------- .../res/syntheticDescriptorGeneration.kt | 4 +- 7 files changed, 58 insertions(+), 64 deletions(-) create mode 100644 compiler/frontend.java/src/org/jetbrains/kotlin/load/java/InternalFlexibleTypeTransformer.kt delete mode 100644 compiler/frontend.java/src/org/jetbrains/kotlin/load/java/JavaFlexibleTypeFactoryProvider.kt diff --git a/compiler/frontend.java/src/org/jetbrains/kotlin/frontend/java/di/injection.kt b/compiler/frontend.java/src/org/jetbrains/kotlin/frontend/java/di/injection.kt index 3b6f21297ba..e145e37d0a9 100644 --- a/compiler/frontend.java/src/org/jetbrains/kotlin/frontend/java/di/injection.kt +++ b/compiler/frontend.java/src/org/jetbrains/kotlin/frontend/java/di/injection.kt @@ -24,8 +24,8 @@ import org.jetbrains.kotlin.context.ModuleContext import org.jetbrains.kotlin.descriptors.PackagePartProvider import org.jetbrains.kotlin.frontend.di.configureModule import org.jetbrains.kotlin.incremental.components.LookupTracker +import org.jetbrains.kotlin.load.java.InternalFlexibleTypeTransformer import org.jetbrains.kotlin.load.java.JavaClassFinderImpl -import org.jetbrains.kotlin.load.java.JavaFlexibleTypeFactoryProvider import org.jetbrains.kotlin.load.java.components.* import org.jetbrains.kotlin.load.java.lazy.ModuleClassResolver import org.jetbrains.kotlin.load.java.lazy.SingleModuleClassResolver @@ -62,7 +62,7 @@ fun StorageComponentContainer.configureJavaTopDownAnalysis(moduleContentScope: G useInstance(SamConversionResolverImpl) useImpl() useImpl() - useInstance(JavaFlexibleTypeFactoryProvider) + useInstance(InternalFlexibleTypeTransformer) } fun createContainerForLazyResolveWithJava( diff --git a/compiler/frontend.java/src/org/jetbrains/kotlin/load/java/InternalFlexibleTypeTransformer.kt b/compiler/frontend.java/src/org/jetbrains/kotlin/load/java/InternalFlexibleTypeTransformer.kt new file mode 100644 index 00000000000..bf26dc7affe --- /dev/null +++ b/compiler/frontend.java/src/org/jetbrains/kotlin/load/java/InternalFlexibleTypeTransformer.kt @@ -0,0 +1,42 @@ +/* + * Copyright 2010-2016 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.kotlin.load.java + +import org.jetbrains.kotlin.load.java.lazy.types.LazyJavaTypeResolver.FlexibleJavaClassifierTypeFactory +import org.jetbrains.kotlin.name.ClassId +import org.jetbrains.kotlin.name.FqName +import org.jetbrains.kotlin.resolve.DescriptorUtils +import org.jetbrains.kotlin.resolve.TypeResolver.TypeTransformerForTests +import org.jetbrains.kotlin.types.KotlinType + +object InternalFlexibleTypeTransformer : TypeTransformerForTests() { + // This is a "magic" classifier: when type resolver sees it in the code, e.g. ft, instead of creating a normal type, + // it creates a flexible type, e.g. (Foo..Foo?). + // This is used in tests and Evaluate Expression to have flexible types in the code, + // but normal users should not be referencing this classifier + @JvmField + val FLEXIBLE_TYPE_CLASSIFIER: ClassId = ClassId.topLevel(FqName("kotlin.internal.flexible.ft")) + + override fun transformType(kotlinType: KotlinType): KotlinType? { + val descriptor = kotlinType.constructor.declarationDescriptor + if (descriptor != null && FLEXIBLE_TYPE_CLASSIFIER.asSingleFqName().toUnsafe() == DescriptorUtils.getFqName(descriptor) + && kotlinType.arguments.size == 2) { + return FlexibleJavaClassifierTypeFactory.create(kotlinType.arguments[0].type, kotlinType.arguments[1].type) + } + return null + } +} diff --git a/compiler/frontend.java/src/org/jetbrains/kotlin/load/java/JavaFlexibleTypeFactoryProvider.kt b/compiler/frontend.java/src/org/jetbrains/kotlin/load/java/JavaFlexibleTypeFactoryProvider.kt deleted file mode 100644 index a252b7ac5fb..00000000000 --- a/compiler/frontend.java/src/org/jetbrains/kotlin/load/java/JavaFlexibleTypeFactoryProvider.kt +++ /dev/null @@ -1,25 +0,0 @@ -/* - * Copyright 2010-2016 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.jetbrains.kotlin.load.java - -import org.jetbrains.kotlin.load.java.lazy.types.LazyJavaTypeResolver -import org.jetbrains.kotlin.resolve.TypeResolver.FlexibleTypeFactoryProvider -import org.jetbrains.kotlin.types.FlexibleTypeFactory - -object JavaFlexibleTypeFactoryProvider : FlexibleTypeFactoryProvider() { - override val factory: FlexibleTypeFactory get() = LazyJavaTypeResolver.FlexibleJavaClassifierTypeFactory -} diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/TypeResolver.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/TypeResolver.kt index 378c94df63f..f2fef4bfe65 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/TypeResolver.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/TypeResolver.kt @@ -50,7 +50,7 @@ class TypeResolver( private val annotationResolver: AnnotationResolver, private val qualifiedExpressionResolver: QualifiedExpressionResolver, private val moduleDescriptor: ModuleDescriptor, - private val flexibleTypeFactoryProvider: FlexibleTypeFactoryProvider, + private val typeTransformerForTests: TypeTransformerForTests, private val storageManager: StorageManager, private val lazinessToken: TypeLazinessToken, private val dynamicTypesSettings: DynamicTypesSettings, @@ -58,8 +58,8 @@ class TypeResolver( private val identifierChecker: IdentifierChecker ) { - open class FlexibleTypeFactoryProvider { - open val factory: FlexibleTypeFactory get() = FlexibleTypeFactory.DEFAULT + open class TypeTransformerForTests { + open fun transformType(kotlinType: KotlinType): KotlinType? = null } fun resolveType(scope: LexicalScope, typeReference: KtTypeReference, trace: BindingTrace, checkBounds: Boolean): KotlinType { @@ -367,16 +367,12 @@ class TypeResolver( " but ${collectedArgumentAsTypeProjections.size} instead of ${parameters.size} found in ${type.text}" } - if (Flexibility.FLEXIBLE_TYPE_CLASSIFIER.asSingleFqName().toUnsafe() == DescriptorUtils.getFqName(classDescriptor) - && parameters.size == 2) { - // We create flexible types by convention here - // This is not intended to be used in normal users' environments, only for tests and debugger etc - return type(flexibleTypeFactoryProvider.factory.create(arguments[0].type, - arguments[1].type) - ) - } - val resultingType = KotlinTypeImpl.create(annotations, classDescriptor, false, 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 + typeTransformerForTests.transformType(resultingType)?.let { return type(it) } + if (c.checkBounds) { val substitutor = TypeSubstitutor.create(resultingType) for (i in parameters.indices) { diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/BaseDiagnosticsTest.java b/compiler/tests/org/jetbrains/kotlin/checkers/BaseDiagnosticsTest.java index c9345e1c194..1aef8035191 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/BaseDiagnosticsTest.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/BaseDiagnosticsTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2010-2015 JetBrains s.r.o. + * Copyright 2010-2016 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -35,12 +35,12 @@ import org.jetbrains.annotations.Nullable; import org.jetbrains.kotlin.asJava.DuplicateJvmSignatureUtilKt; import org.jetbrains.kotlin.descriptors.DeclarationDescriptor; import org.jetbrains.kotlin.diagnostics.*; +import org.jetbrains.kotlin.load.java.InternalFlexibleTypeTransformer; import org.jetbrains.kotlin.psi.KtDeclaration; import org.jetbrains.kotlin.psi.KtFile; import org.jetbrains.kotlin.resolve.BindingContext; import org.jetbrains.kotlin.resolve.diagnostics.Diagnostics; import org.jetbrains.kotlin.test.KotlinTestUtils; -import org.jetbrains.kotlin.types.Flexibility; import org.junit.Assert; import java.io.File; @@ -73,8 +73,8 @@ public abstract class BaseDiagnosticsTest public static final String CHECK_TYPE_IMPORT = "import " + CHECK_TYPE_PACKAGE + ".*"; public static final String EXPLICIT_FLEXIBLE_TYPES_DIRECTIVE = "EXPLICIT_FLEXIBLE_TYPES"; - public static final String EXPLICIT_FLEXIBLE_PACKAGE = Flexibility.Companion.getFLEXIBLE_TYPE_CLASSIFIER().getPackageFqName().asString(); - public static final String EXPLICIT_FLEXIBLE_CLASS_NAME = Flexibility.Companion.getFLEXIBLE_TYPE_CLASSIFIER().getRelativeClassName().asString(); + public static final String EXPLICIT_FLEXIBLE_PACKAGE = InternalFlexibleTypeTransformer.FLEXIBLE_TYPE_CLASSIFIER.getPackageFqName().asString(); + public static final String EXPLICIT_FLEXIBLE_CLASS_NAME = InternalFlexibleTypeTransformer.FLEXIBLE_TYPE_CLASSIFIER.getRelativeClassName().asString(); private static final String EXPLICIT_FLEXIBLE_TYPES_DECLARATIONS = "\npackage " + EXPLICIT_FLEXIBLE_PACKAGE + "\npublic class " + EXPLICIT_FLEXIBLE_CLASS_NAME + ""; diff --git a/core/descriptors/src/org/jetbrains/kotlin/types/flexibleTypes.kt b/core/descriptors/src/org/jetbrains/kotlin/types/flexibleTypes.kt index a36bbfcac1c..f8e3aacfab7 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/types/flexibleTypes.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/types/flexibleTypes.kt @@ -16,34 +16,15 @@ package org.jetbrains.kotlin.types -import org.jetbrains.kotlin.name.ClassId -import org.jetbrains.kotlin.name.FqName import org.jetbrains.kotlin.types.checker.KotlinTypeChecker interface FlexibleTypeFactory { val id: String fun create(lowerBound: KotlinType, upperBound: KotlinType): KotlinType - - object DEFAULT : FlexibleTypeFactory { - override val id: String get() = "NONE" - - override fun create(lowerBound: KotlinType, upperBound: KotlinType): KotlinType { - if (lowerBound == upperBound) return lowerBound - return object : DelegatingFlexibleType(lowerBound, upperBound, this@DEFAULT) {} - } - } } interface Flexibility : TypeCapability, SubtypingRepresentatives { - companion object { - // This is a "magic" classifier: when type resolver sees it in the code, e.g. ft, instead of creating a normal type, - // it creates a flexible type, e.g. (Foo..Foo?). - // This is used in tests and Evaluate Expression to have flexible types in the code, - // but normal users should not be referencing this classifier - val FLEXIBLE_TYPE_CLASSIFIER: ClassId = ClassId.topLevel(FqName("kotlin.internal.flexible.ft")) - } - // lowerBound is a subtype of upperBound val lowerBound: KotlinType val upperBound: KotlinType 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 2e6876e224c..c6a62753f09 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 @@ -25,11 +25,11 @@ import org.jetbrains.kotlin.descriptors.annotations.Annotations import org.jetbrains.kotlin.descriptors.impl.PropertyDescriptorImpl import org.jetbrains.kotlin.descriptors.impl.PropertyGetterDescriptorImpl import org.jetbrains.kotlin.descriptors.impl.SimpleFunctionDescriptorImpl +import org.jetbrains.kotlin.load.java.lazy.types.LazyJavaTypeResolver import org.jetbrains.kotlin.name.Name 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.FlexibleTypeFactory import org.jetbrains.kotlin.types.KotlinType import org.jetbrains.kotlin.types.KotlinTypeImpl import org.jetbrains.kotlin.types.StarProjectionImpl @@ -109,7 +109,7 @@ private fun genProperty( override val resourceId = id } - val flexibleType = FlexibleTypeFactory.DEFAULT.create(type, type.makeNullable()) + val flexibleType = LazyJavaTypeResolver.FlexibleJavaClassifierTypeFactory.create(type, type.makeNullable()) property.setType( flexibleType, emptyList(),