Don't add Enum.entries to scope if it can't be called

^KT-53929 Fixed
This commit is contained in:
Roman Efremov
2023-01-03 14:44:35 +01:00
parent 2dc3871954
commit 878608b7b2
14 changed files with 88 additions and 19 deletions
@@ -52,7 +52,8 @@ class JvmBuiltInsPackageFragmentProvider(
additionalClassPartsProvider, platformDependentDeclarationFilter,
BuiltInSerializerProtocol.extensionRegistry,
kotlinTypeChecker,
samConversionResolver
samConversionResolver,
enumEntriesDeserializationSupport = JvmEnumEntriesDeserializationSupport,
)
}
@@ -27,11 +27,7 @@ import org.jetbrains.kotlin.descriptors.deserialization.PlatformDependentDeclara
import org.jetbrains.kotlin.descriptors.impl.CompositePackageFragmentProvider
import org.jetbrains.kotlin.descriptors.impl.ModuleDescriptorImpl
import org.jetbrains.kotlin.incremental.components.LookupTracker
import org.jetbrains.kotlin.load.java.AnnotationTypeQualifierResolver
import org.jetbrains.kotlin.load.java.JavaClassFinder
import org.jetbrains.kotlin.load.java.JavaClassesTracker
import org.jetbrains.kotlin.load.java.JavaModuleAnnotationsProvider
import org.jetbrains.kotlin.load.java.JavaTypeEnhancementState
import org.jetbrains.kotlin.load.java.*
import org.jetbrains.kotlin.load.java.components.JavaPropertyInitializerEvaluator
import org.jetbrains.kotlin.load.java.components.JavaResolverCache
import org.jetbrains.kotlin.load.java.components.SignaturePropagator
@@ -82,7 +78,8 @@ class DeserializationComponentsForJava(
platformDependentDeclarationFilter = jvmBuiltIns?.customizer ?: PlatformDependentDeclarationFilter.NoPlatformDependent,
extensionRegistryLite = JvmProtoBufUtil.EXTENSION_REGISTRY,
kotlinTypeChecker = kotlinTypeChecker, samConversionResolver = SamConversionResolverImpl(storageManager, emptyList()),
typeAttributeTranslators = typeAttributeTranslators.translators
typeAttributeTranslators = typeAttributeTranslators.translators,
enumEntriesDeserializationSupport = JvmEnumEntriesDeserializationSupport,
)
}
@@ -31,7 +31,8 @@ import org.jetbrains.kotlin.utils.SmartList
// We don't need to track lookups here since this scope used only for introduce special Enum class members
class StaticScopeForKotlinEnum(
storageManager: StorageManager,
private val containingClass: ClassDescriptor
private val containingClass: ClassDescriptor,
private val enumEntriesCanBeUsed: Boolean,
) : MemberScopeImpl() {
init {
assert(containingClass.kind == ClassKind.ENUM_CLASS) { "Class should be an enum: $containingClass" }
@@ -44,7 +45,12 @@ class StaticScopeForKotlinEnum(
}
private val properties: List<PropertyDescriptor> by storageManager.createLazyValue {
listOfNotNull(createEnumEntriesProperty(containingClass))
if (enumEntriesCanBeUsed) {
// It still might be filtered out later in tower resolve if feature disabled
listOfNotNull(createEnumEntriesProperty(containingClass))
} else {
emptyList()
}
}
override fun getContributedDescriptors(kindFilter: DescriptorKindFilter, nameFilter: (Name) -> Boolean) = functions + properties
@@ -0,0 +1,25 @@
/*
* Copyright 2010-2023 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.serialization.deserialization
interface EnumEntriesDeserializationSupport {
/**
* Determines whether `Enum.entries` property can be synthesized for enums in this module,
* when this property is not present in compiled code.
* Returns `null` if it's not known.
*/
fun canSynthesizeEnumEntries(): Boolean?
object Default : EnumEntriesDeserializationSupport {
override fun canSynthesizeEnumEntries(): Boolean? = null
}
}
object JvmEnumEntriesDeserializationSupport : EnumEntriesDeserializationSupport {
// In JVM modules "entries" can be called even on enum compiled without this property.
override fun canSynthesizeEnumEntries(): Boolean = true
}
@@ -55,7 +55,8 @@ class DeserializationComponents(
val kotlinTypeChecker: NewKotlinTypeChecker = NewKotlinTypeChecker.Default,
val samConversionResolver: SamConversionResolver,
val platformDependentTypeTransformer: PlatformDependentTypeTransformer = PlatformDependentTypeTransformer.None,
val typeAttributeTranslators: List<TypeAttributeTranslator> = listOf(DefaultTypeAttributeTranslator)
val typeAttributeTranslators: List<TypeAttributeTranslator> = listOf(DefaultTypeAttributeTranslator),
val enumEntriesDeserializationSupport: EnumEntriesDeserializationSupport = EnumEntriesDeserializationSupport.Default,
) {
val classDeserializer: ClassDeserializer = ClassDeserializer(this)
@@ -53,10 +53,13 @@ class DeserializedClassDescriptor(
)
private val staticScope =
if (kind == ClassKind.ENUM_CLASS)
StaticScopeForKotlinEnum(c.storageManager, this)
else
if (kind == ClassKind.ENUM_CLASS) {
val enumEntriesCanBeUsed = Flags.HAS_ENUM_ENTRIES.get(classProto.flags) ||
c.components.enumEntriesDeserializationSupport.canSynthesizeEnumEntries() == true
StaticScopeForKotlinEnum(c.storageManager, this, enumEntriesCanBeUsed)
} else {
MemberScope.Empty
}
private val typeConstructor = DeserializedClassTypeConstructor()