[MPP] Make type refinement capability aware of HMPP state

Introduce intermediate state for cases when module doens't
have a type refiner even though HMPP is enabled.

KT-46691
This commit is contained in:
Pavel Kirpichenkov
2021-07-16 16:01:57 +03:00
committed by teamcityserver
parent 566640aa6d
commit 2d91917ac7
4 changed files with 20 additions and 14 deletions
@@ -23,9 +23,6 @@ import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.platform.TargetPlatform
import org.jetbrains.kotlin.storage.StorageManager
import org.jetbrains.kotlin.types.checker.REFINER_CAPABILITY
import org.jetbrains.kotlin.types.checker.Ref
import org.jetbrains.kotlin.types.refinement.TypeRefinement
import org.jetbrains.kotlin.utils.sure
class ModuleDescriptorImpl @JvmOverloads constructor(
@@ -44,9 +41,7 @@ class ModuleDescriptorImpl @JvmOverloads constructor(
if (!moduleName.isSpecial) {
throw IllegalArgumentException("Module name must be special: $moduleName")
}
this.capabilities = capabilities.toMutableMap()
@OptIn(TypeRefinement::class)
this.capabilities[REFINER_CAPABILITY] = Ref(null)
this.capabilities = capabilities
packageViewDescriptorFactory = getCapability(PackageViewDescriptorFactory.CAPABILITY) ?: PackageViewDescriptorFactory.Default
}
@@ -25,12 +25,11 @@ import org.jetbrains.kotlin.resolve.DescriptorUtils.getContainingClass
import org.jetbrains.kotlin.resolve.constants.ConstantValue
import org.jetbrains.kotlin.resolve.constants.EnumValue
import org.jetbrains.kotlin.resolve.isInlineClass
import org.jetbrains.kotlin.resolve.scopes.DescriptorKindFilter
import org.jetbrains.kotlin.resolve.scopes.MemberScope
import org.jetbrains.kotlin.types.*
import org.jetbrains.kotlin.types.checker.KotlinTypeChecker
import org.jetbrains.kotlin.types.checker.KotlinTypeRefiner
import org.jetbrains.kotlin.types.checker.REFINER_CAPABILITY
import org.jetbrains.kotlin.types.checker.TypeRefinementSupport
import org.jetbrains.kotlin.types.refinement.TypeRefinement
import org.jetbrains.kotlin.types.typeUtil.contains
import org.jetbrains.kotlin.types.typeUtil.isAnyOrNullableAny
@@ -419,10 +418,15 @@ fun DeclarationDescriptor.isPrimaryConstructorOfInlineClass(): Boolean =
this is ConstructorDescriptor && this.isPrimary && this.constructedClass.isInlineClass()
@TypeRefinement
fun ModuleDescriptor.getKotlinTypeRefiner(): KotlinTypeRefiner = getCapability(REFINER_CAPABILITY)?.value ?: KotlinTypeRefiner.Default
fun ModuleDescriptor.getKotlinTypeRefiner(): KotlinTypeRefiner =
when (val refinerCapability = getCapability(REFINER_CAPABILITY)?.value) {
is TypeRefinementSupport.Enabled -> refinerCapability.typeRefiner
else -> KotlinTypeRefiner.Default
}
@OptIn(TypeRefinement::class)
fun ModuleDescriptor.isTypeRefinementEnabled(): Boolean = getCapability(REFINER_CAPABILITY)?.value != null
fun ModuleDescriptor.isTypeRefinementEnabled(): Boolean =
getCapability(REFINER_CAPABILITY)?.value?.isEnabled == true
val VariableDescriptor.isUnderscoreNamed
get() = !name.isSpecial && name.identifier == "_"
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
* 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.
*/
@@ -14,6 +14,7 @@ import org.jetbrains.kotlin.storage.StorageManager
import org.jetbrains.kotlin.types.checker.KotlinTypeRefiner
import org.jetbrains.kotlin.types.checker.NewCapturedTypeConstructor
import org.jetbrains.kotlin.types.checker.REFINER_CAPABILITY
import org.jetbrains.kotlin.types.checker.TypeRefinementSupport
import org.jetbrains.kotlin.types.refinement.TypeRefinement
import org.jetbrains.kotlin.utils.DFS
import org.jetbrains.kotlin.utils.addToStdlib.safeAs
@@ -24,7 +25,7 @@ class KotlinTypeRefinerImpl(
storageManager: StorageManager
) : KotlinTypeRefiner() {
init {
moduleDescriptor.getCapability(REFINER_CAPABILITY)?.value = this
moduleDescriptor.getCapability(REFINER_CAPABILITY)?.value = TypeRefinementSupport.Enabled(this)
}
private val refinedTypeCache = storageManager.createCacheWithNotNullValues<TypeConstructor, KotlinType>()
@@ -75,7 +75,13 @@ abstract class KotlinTypeRefiner {
@TypeRefinement
fun KotlinTypeRefiner.refineTypes(types: Iterable<KotlinType>): List<KotlinType> = types.map { refineType(it) }
class Ref<T : Any>(var value: T?)
class Ref<T : Any>(var value: T)
@TypeRefinement
val REFINER_CAPABILITY = ModuleCapability<Ref<KotlinTypeRefiner>>("KotlinTypeRefiner")
val REFINER_CAPABILITY = ModuleCapability<Ref<TypeRefinementSupport>>("KotlinTypeRefiner")
sealed class TypeRefinementSupport(val isEnabled: Boolean) {
object Disabled : TypeRefinementSupport(isEnabled = false)
object EnabledUninitialized : TypeRefinementSupport(isEnabled = true)
class Enabled(val typeRefiner: KotlinTypeRefiner) : TypeRefinementSupport(isEnabled = true)
}