From e821b25288c9dd1afb41607714d789df27e2a02f Mon Sep 17 00:00:00 2001 From: Mikhail Zarechenskiy Date: Thu, 4 May 2017 03:11:40 +0300 Subject: [PATCH] Resolve type alias SAM constructors in synthetic scope --- .../jvm/platform/JvmPlatformConfigurator.kt | 4 +- .../JavaSyntheticConstructorsProvider.kt | 56 ------------------- .../synthetic/SamAdapterFunctionsScope.kt | 25 ++++++++- .../kotlin/resolve/TargetPlatform.kt | 2 - .../calls/tower/NewResolutionOldInference.kt | 7 +-- .../resolve/calls/tower/ImplicitScopeTower.kt | 9 ++- .../kotlin/resolve/calls/tower/TowerLevels.kt | 19 ++----- .../scopes/SyntheticConstructorsProvider.kt | 30 ---------- .../smart/TypeInstantiationItems.kt | 17 ++---- .../js/resolve/JsPlatformConfigurator.kt | 4 +- 10 files changed, 43 insertions(+), 130 deletions(-) delete mode 100644 compiler/frontend.java/src/org/jetbrains/kotlin/synthetic/JavaSyntheticConstructorsProvider.kt delete mode 100644 core/descriptors/src/org/jetbrains/kotlin/resolve/scopes/SyntheticConstructorsProvider.kt diff --git a/compiler/frontend.java/src/org/jetbrains/kotlin/resolve/jvm/platform/JvmPlatformConfigurator.kt b/compiler/frontend.java/src/org/jetbrains/kotlin/resolve/jvm/platform/JvmPlatformConfigurator.kt index ddc991b9952..74bc03b6cc0 100644 --- a/compiler/frontend.java/src/org/jetbrains/kotlin/resolve/jvm/platform/JvmPlatformConfigurator.kt +++ b/compiler/frontend.java/src/org/jetbrains/kotlin/resolve/jvm/platform/JvmPlatformConfigurator.kt @@ -1,5 +1,5 @@ /* - * Copyright 2010-2016 JetBrains s.r.o. + * Copyright 2010-2017 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. @@ -25,7 +25,6 @@ import org.jetbrains.kotlin.resolve.calls.checkers.ReifiedTypeParameterSubstitut import org.jetbrains.kotlin.resolve.checkers.HeaderImplDeclarationChecker import org.jetbrains.kotlin.resolve.jvm.* import org.jetbrains.kotlin.resolve.jvm.checkers.* -import org.jetbrains.kotlin.synthetic.JavaSyntheticConstructorsProvider import org.jetbrains.kotlin.synthetic.JavaSyntheticScopes import org.jetbrains.kotlin.types.DynamicTypesSettings @@ -86,7 +85,6 @@ object JvmPlatformConfigurator : PlatformConfigurator( container.useImpl() container.useImpl() container.useImpl() - container.useInstance(JavaSyntheticConstructorsProvider) container.useInstance(JvmTypeSpecificityComparator) } } diff --git a/compiler/frontend.java/src/org/jetbrains/kotlin/synthetic/JavaSyntheticConstructorsProvider.kt b/compiler/frontend.java/src/org/jetbrains/kotlin/synthetic/JavaSyntheticConstructorsProvider.kt deleted file mode 100644 index f179ed39d7b..00000000000 --- a/compiler/frontend.java/src/org/jetbrains/kotlin/synthetic/JavaSyntheticConstructorsProvider.kt +++ /dev/null @@ -1,56 +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.synthetic - -import org.jetbrains.kotlin.descriptors.* -import org.jetbrains.kotlin.incremental.components.LookupLocation -import org.jetbrains.kotlin.load.java.descriptors.SamConstructorDescriptorImpl -import org.jetbrains.kotlin.load.java.lazy.descriptors.LazyJavaClassDescriptor -import org.jetbrains.kotlin.load.java.sam.SingleAbstractMethodUtils -import org.jetbrains.kotlin.resolve.scopes.SyntheticConstructorsProvider -import java.lang.AssertionError - -object JavaSyntheticConstructorsProvider : SyntheticConstructorsProvider { - override fun getSyntheticConstructors(classifier: ClassifierDescriptor, location: LookupLocation): Collection { - if (classifier is TypeAliasDescriptor) { - return getSyntheticTypeAliasConstructors(classifier, location) - } - - return emptyList() - } - - private fun getSyntheticTypeAliasConstructors(typeAliasDescriptor: TypeAliasDescriptor, location: LookupLocation): Collection { - val classDescriptor = typeAliasDescriptor.classDescriptor - if (classDescriptor !is LazyJavaClassDescriptor || classDescriptor.functionTypeForSamInterface == null) return emptyList() - - val containingDeclaration = classDescriptor.containingDeclaration - - val outerScope = when (containingDeclaration) { - is ClassDescriptor -> - containingDeclaration.staticScope - is PackageFragmentDescriptor -> - containingDeclaration.getMemberScope() - else -> - throw AssertionError("Unexpected containing declaration for $classDescriptor: $containingDeclaration") - } - - return outerScope.getContributedFunctions(classDescriptor.name, location) - .filterIsInstance() - .filter { it.baseDescriptorForSynthetic == classDescriptor } - .map { SingleAbstractMethodUtils.createTypeAliasSamConstructorFunction(typeAliasDescriptor, it) } - } -} \ No newline at end of file diff --git a/compiler/frontend.java/src/org/jetbrains/kotlin/synthetic/SamAdapterFunctionsScope.kt b/compiler/frontend.java/src/org/jetbrains/kotlin/synthetic/SamAdapterFunctionsScope.kt index 0da5551e73b..4db51ec64f3 100644 --- a/compiler/frontend.java/src/org/jetbrains/kotlin/synthetic/SamAdapterFunctionsScope.kt +++ b/compiler/frontend.java/src/org/jetbrains/kotlin/synthetic/SamAdapterFunctionsScope.kt @@ -133,12 +133,33 @@ class SamAdapterFunctionsScope( override fun getSyntheticExtensionProperties(receiverTypes: Collection): Collection = emptyList() override fun getSyntheticStaticFunctions(scope: ResolutionScope, name: Name, location: LookupLocation): Collection { - val samConstructor = scope.getContributedClassifier(name, location)?.let { samConstructorForClassifier(it) } + val classifier = scope.getContributedClassifier(name, location) + val samConstructor = classifier?.let { getSamConstructor(it) } + return scope.getContributedFunctions(name, location).mapNotNull { samAdapterForStaticFunction(it) } + listOfNotNull(samConstructor) } override fun getSyntheticStaticFunctions(scope: ResolutionScope): Collection { - return scope.getContributedDescriptors(DescriptorKindFilter.FUNCTIONS).mapNotNull { samAdapterForStaticFunction(it) } + val samConstructors = + scope.getContributedDescriptors(DescriptorKindFilter.CLASSIFIERS) + .filterIsInstance() + .mapNotNull{ getSamConstructor(it) } + + return scope.getContributedDescriptors(DescriptorKindFilter.FUNCTIONS).mapNotNull { samAdapterForStaticFunction(it) } + + samConstructors + } + + private fun getSamConstructor(classifier: ClassifierDescriptor): SamConstructorDescriptor? { + return when (classifier) { + is TypeAliasDescriptor -> getTypeAliasSamConstructor(classifier) + else -> samConstructorForClassifier(classifier) + } + } + + private fun getTypeAliasSamConstructor(classifier: TypeAliasDescriptor): SamConstructorDescriptor? { + val classDescriptor = classifier.classDescriptor ?: return null + val samConstructor = samConstructorForClassifier(classDescriptor) ?: return null + return SingleAbstractMethodUtils.createTypeAliasSamConstructorFunction(classifier, samConstructor) } private class MyFunctionDescriptor( diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/TargetPlatform.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/TargetPlatform.kt index 6a9c377a752..9649626e9f2 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/TargetPlatform.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/TargetPlatform.kt @@ -25,7 +25,6 @@ import org.jetbrains.kotlin.resolve.calls.checkers.* import org.jetbrains.kotlin.resolve.calls.results.TypeSpecificityComparator import org.jetbrains.kotlin.resolve.checkers.* import org.jetbrains.kotlin.resolve.lazy.DelegationFilter -import org.jetbrains.kotlin.resolve.scopes.SyntheticConstructorsProvider import org.jetbrains.kotlin.resolve.scopes.SyntheticScopes import org.jetbrains.kotlin.storage.LockBasedStorageManager import org.jetbrains.kotlin.types.DynamicTypesSettings @@ -70,7 +69,6 @@ abstract class TargetPlatform(val platformName: String) { ) { override fun configureModuleComponents(container: StorageComponentContainer) { container.useInstance(SyntheticScopes.Empty) - container.useInstance(SyntheticConstructorsProvider.Empty) container.useInstance(TypeSpecificityComparator.NONE) } } diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/NewResolutionOldInference.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/NewResolutionOldInference.kt index 09c8afb6101..7dd26ec0804 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/NewResolutionOldInference.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/NewResolutionOldInference.kt @@ -1,5 +1,5 @@ /* - * Copyright 2010-2016 JetBrains s.r.o. + * Copyright 2010-2017 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. @@ -48,7 +48,6 @@ import org.jetbrains.kotlin.resolve.descriptorUtil.hasDynamicExtensionAnnotation import org.jetbrains.kotlin.resolve.isHiddenInResolution import org.jetbrains.kotlin.resolve.scopes.LexicalScope import org.jetbrains.kotlin.resolve.scopes.MemberScope -import org.jetbrains.kotlin.resolve.scopes.SyntheticConstructorsProvider import org.jetbrains.kotlin.resolve.scopes.SyntheticScopes import org.jetbrains.kotlin.resolve.scopes.receivers.* import org.jetbrains.kotlin.types.DeferredType @@ -66,7 +65,6 @@ class NewResolutionOldInference( private val resolutionResultsHandler: ResolutionResultsHandler, private val dynamicCallableDescriptors: DynamicCallableDescriptors, private val syntheticScopes: SyntheticScopes, - private val syntheticConstructorsProvider: SyntheticConstructorsProvider, private val languageVersionSettings: LanguageVersionSettings, private val coroutineInferenceSupport: CoroutineInferenceSupport ) { @@ -156,7 +154,7 @@ class NewResolutionOldInference( } val dynamicScope = dynamicCallableDescriptors.createDynamicDescriptorScope(context.call, context.scope.ownerDescriptor) - val scopeTower = ImplicitScopeTowerImpl(context, dynamicScope, syntheticScopes, syntheticConstructorsProvider, context.call.createLookupLocation()) + val scopeTower = ImplicitScopeTowerImpl(context, dynamicScope, syntheticScopes, context.call.createLookupLocation()) val shouldUseOperatorRem = languageVersionSettings.supportsFeature(LanguageFeature.OperatorRem) val isBinaryRemOperator = isBinaryRemOperator(context.call) @@ -303,7 +301,6 @@ class NewResolutionOldInference( val resolutionContext: ResolutionContext<*>, override val dynamicScope: MemberScope, override val syntheticScopes: SyntheticScopes, - override val syntheticConstructorsProvider: SyntheticConstructorsProvider, override val location: LookupLocation ): ImplicitScopeTower { private val cache = HashMap() diff --git a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/tower/ImplicitScopeTower.kt b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/tower/ImplicitScopeTower.kt index 086b71a47fc..c88c0698f2a 100644 --- a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/tower/ImplicitScopeTower.kt +++ b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/tower/ImplicitScopeTower.kt @@ -1,5 +1,5 @@ /* - * Copyright 2010-2016 JetBrains s.r.o. + * Copyright 2010-2017 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,12 +16,13 @@ package org.jetbrains.kotlin.resolve.calls.tower -import org.jetbrains.kotlin.descriptors.* +import org.jetbrains.kotlin.descriptors.CallableDescriptor +import org.jetbrains.kotlin.descriptors.ClassDescriptor +import org.jetbrains.kotlin.descriptors.DeclarationDescriptorWithVisibility import org.jetbrains.kotlin.incremental.components.LookupLocation import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.resolve.scopes.LexicalScope import org.jetbrains.kotlin.resolve.scopes.MemberScope -import org.jetbrains.kotlin.resolve.scopes.SyntheticConstructorsProvider import org.jetbrains.kotlin.resolve.scopes.SyntheticScopes import org.jetbrains.kotlin.resolve.scopes.receivers.ReceiverValueWithSmartCastInfo import org.jetbrains.kotlin.types.KotlinType @@ -35,8 +36,6 @@ interface ImplicitScopeTower { val syntheticScopes: SyntheticScopes - val syntheticConstructorsProvider: SyntheticConstructorsProvider - val location: LookupLocation val isDebuggerContext: Boolean diff --git a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/tower/TowerLevels.kt b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/tower/TowerLevels.kt index f00931abe27..cead12b38eb 100644 --- a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/tower/TowerLevels.kt +++ b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/tower/TowerLevels.kt @@ -163,7 +163,6 @@ internal class QualifierScopeTowerLevel(scopeTower: ImplicitScopeTower, val qual override fun getFunctions(name: Name, extensionReceiver: ReceiverValueWithSmartCastInfo?) = qualifier.staticScope .getContributedFunctionsAndConstructors(name, location, - scopeTower.syntheticConstructorsProvider, scopeTower.syntheticScopes, qualifier.staticScope).map { createCandidateDescriptor(it, dispatchReceiver = null) @@ -191,7 +190,6 @@ internal open class ScopeBasedTowerLevel protected constructor( override fun getFunctions(name: Name, extensionReceiver: ReceiverValueWithSmartCastInfo?): Collection = resolutionScope.getContributedFunctionsAndConstructors(name, location, - scopeTower.syntheticConstructorsProvider, scopeTower.syntheticScopes, resolutionScope).map { createCandidateDescriptor(it, dispatchReceiver = null) @@ -271,30 +269,25 @@ private fun KotlinType?.getInnerConstructors(name: Name, location: LookupLocatio private fun ResolutionScope.getContributedFunctionsAndConstructors( name: Name, location: LookupLocation, - syntheticConstructorsProvider: SyntheticConstructorsProvider, syntheticScopes: SyntheticScopes, scope: ResolutionScope ): Collection { val result = ArrayList(getContributedFunctions(name, location)) val classifier = getContributedClassifier(name, location) - if (classifier != null) { - classifier.getCallableConstructors().filterTo(result) { it.dispatchReceiverParameter == null } - syntheticConstructorsProvider.getSyntheticConstructors(classifier, location).filterTo(result) { it.dispatchReceiverParameter == null } + val callableConstructors = when (classifier) { + is TypeAliasDescriptor -> if (classifier.canHaveCallableConstructors) classifier.constructors else emptyList() + is ClassDescriptor -> if (classifier.canHaveCallableConstructors) classifier.constructors else emptyList() + else -> emptyList() } + callableConstructors.filterTo(result) { it.dispatchReceiverParameter == null } + result.addAll(syntheticScopes.collectSyntheticStaticFunctions(scope, name, location)) return result.toList() } -private fun ClassifierDescriptor.getCallableConstructors(): Collection = - when (this) { - is TypeAliasDescriptor -> if (canHaveCallableConstructors) constructors else emptyList() - is ClassDescriptor -> if (canHaveCallableConstructors) constructors else emptyList() - else -> emptyList() - } - private fun ResolutionScope.getContributedObjectVariables(name: Name, location: LookupLocation): Collection { val objectDescriptor = getFakeDescriptorForObject(getContributedClassifier(name, location)) diff --git a/core/descriptors/src/org/jetbrains/kotlin/resolve/scopes/SyntheticConstructorsProvider.kt b/core/descriptors/src/org/jetbrains/kotlin/resolve/scopes/SyntheticConstructorsProvider.kt deleted file mode 100644 index 300ba40c231..00000000000 --- a/core/descriptors/src/org/jetbrains/kotlin/resolve/scopes/SyntheticConstructorsProvider.kt +++ /dev/null @@ -1,30 +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.resolve.scopes - -import org.jetbrains.kotlin.descriptors.ClassifierDescriptor -import org.jetbrains.kotlin.descriptors.FunctionDescriptor -import org.jetbrains.kotlin.incremental.components.LookupLocation - -interface SyntheticConstructorsProvider { - fun getSyntheticConstructors(classifier: ClassifierDescriptor, location: LookupLocation): Collection - - object Empty : SyntheticConstructorsProvider { - override fun getSyntheticConstructors(classifier: ClassifierDescriptor, location: LookupLocation): Collection = - emptyList() - } -} \ No newline at end of file diff --git a/idea/idea-completion/src/org/jetbrains/kotlin/idea/completion/smart/TypeInstantiationItems.kt b/idea/idea-completion/src/org/jetbrains/kotlin/idea/completion/smart/TypeInstantiationItems.kt index 8e8713a93bb..05b9bde6733 100644 --- a/idea/idea-completion/src/org/jetbrains/kotlin/idea/completion/smart/TypeInstantiationItems.kt +++ b/idea/idea-completion/src/org/jetbrains/kotlin/idea/completion/smart/TypeInstantiationItems.kt @@ -1,5 +1,5 @@ /* - * Copyright 2010-2016 JetBrains s.r.o. + * Copyright 2010-2017 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. @@ -31,6 +31,7 @@ import org.jetbrains.kotlin.descriptors.* import org.jetbrains.kotlin.descriptors.annotations.Annotations import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptor import org.jetbrains.kotlin.idea.codeInsight.DescriptorToSourceUtilsIde +import org.jetbrains.kotlin.idea.codeInsight.collectSyntheticStaticMembers import org.jetbrains.kotlin.idea.completion.* import org.jetbrains.kotlin.idea.completion.handlers.KotlinFunctionInsertHandler import org.jetbrains.kotlin.idea.core.ExpectedInfo @@ -42,14 +43,13 @@ import org.jetbrains.kotlin.idea.resolve.ResolutionFacade import org.jetbrains.kotlin.idea.util.* import org.jetbrains.kotlin.incremental.components.NoLookupLocation import org.jetbrains.kotlin.load.java.descriptors.SamConstructorDescriptor -import org.jetbrains.kotlin.load.java.descriptors.SamTypeAliasConstructorDescriptor import org.jetbrains.kotlin.platform.JavaToKotlinClassMap import org.jetbrains.kotlin.psi.KtClassOrObject import org.jetbrains.kotlin.psi.KtDeclaration import org.jetbrains.kotlin.resolve.BindingContext import org.jetbrains.kotlin.resolve.DescriptorUtils import org.jetbrains.kotlin.resolve.descriptorUtil.resolveTopLevelClass -import org.jetbrains.kotlin.synthetic.JavaSyntheticConstructorsProvider +import org.jetbrains.kotlin.resolve.scopes.DescriptorKindFilter import org.jetbrains.kotlin.types.* import org.jetbrains.kotlin.util.constructors import org.jetbrains.kotlin.util.kind @@ -305,19 +305,14 @@ class TypeInstantiationItems( classDescriptor: ClassDescriptor?, tail: Tail?) { if (classDescriptor?.kind == ClassKind.INTERFACE) { - val samConstructor = if (classifier is TypeAliasDescriptor) { - JavaSyntheticConstructorsProvider.getSyntheticConstructors(classifier, NoLookupLocation.FROM_IDE) - .filterIsInstance() - .singleOrNull() ?: return - } - else { + val samConstructor = run { val container = classifier.containingDeclaration val scope = when (container) { is PackageFragmentDescriptor -> container.getMemberScope() - is ClassDescriptor -> container.staticScope + is ClassDescriptor -> container.unsubstitutedMemberScope else -> return } - scope.getContributedFunctions(classifier.name, NoLookupLocation.FROM_IDE) + scope.collectSyntheticStaticMembers(resolutionFacade, DescriptorKindFilter.FUNCTIONS, { classifier.name == it }) .filterIsInstance() .singleOrNull() ?: return } diff --git a/js/js.frontend/src/org/jetbrains/kotlin/js/resolve/JsPlatformConfigurator.kt b/js/js.frontend/src/org/jetbrains/kotlin/js/resolve/JsPlatformConfigurator.kt index 3812070056f..aa3665a9734 100644 --- a/js/js.frontend/src/org/jetbrains/kotlin/js/resolve/JsPlatformConfigurator.kt +++ b/js/js.frontend/src/org/jetbrains/kotlin/js/resolve/JsPlatformConfigurator.kt @@ -1,5 +1,5 @@ /* - * Copyright 2010-2016 JetBrains s.r.o. + * Copyright 2010-2017 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. @@ -28,7 +28,6 @@ import org.jetbrains.kotlin.resolve.PlatformConfigurator import org.jetbrains.kotlin.resolve.calls.checkers.ReifiedTypeParameterSubstitutionChecker import org.jetbrains.kotlin.resolve.checkers.HeaderImplDeclarationChecker import org.jetbrains.kotlin.resolve.lazy.DelegationFilter -import org.jetbrains.kotlin.resolve.scopes.SyntheticConstructorsProvider import org.jetbrains.kotlin.resolve.scopes.SyntheticScopes import org.jetbrains.kotlin.types.DynamicTypesAllowed @@ -60,7 +59,6 @@ object JsPlatformConfigurator : PlatformConfigurator( override fun configureModuleComponents(container: StorageComponentContainer) { container.useImpl() container.useInstance(SyntheticScopes.Empty) - container.useInstance(SyntheticConstructorsProvider.Empty) container.useInstance(JsTypeSpecificityComparator) container.useInstance(JsNameClashChecker()) container.useInstance(JsNameCharsChecker())