Initial OverloadingConflictResolver abstraction from KotlinTypes

This commit is contained in:
Simon Ogorodnik
2019-04-03 16:20:57 +03:00
parent 9a1678728d
commit 8e595f015e
17 changed files with 122 additions and 57 deletions
@@ -16,30 +16,29 @@
package org.jetbrains.kotlin.resolve.jvm
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.resolve.calls.results.TypeSpecificityComparator
import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.types.asFlexibleType
import org.jetbrains.kotlin.types.isFlexible
import org.jetbrains.kotlin.types.model.KotlinTypeMarker
import org.jetbrains.kotlin.types.model.TypeSystemInferenceExtensionContextDelegate
object JvmTypeSpecificityComparator : TypeSpecificityComparator {
class JvmTypeSpecificityComparator(val context: TypeSystemInferenceExtensionContextDelegate) : TypeSpecificityComparator {
override fun isDefinitelyLessSpecific(specific: KotlinType, general: KotlinType): Boolean {
if (!specific.isFlexible() || general.isFlexible()) return false
override fun isDefinitelyLessSpecific(specific: KotlinTypeMarker, general: KotlinTypeMarker): Boolean = with(context) {
val simpleGeneral = general.asSimpleType()
if (!specific.isFlexible() || simpleGeneral == null) return false
// general is inflexible
val flexibility = specific.asFlexibleType()
val flexibility = specific.asFlexibleType()!!
// For primitive types we have to take care of the case when there are two overloaded methods like
// foo(int) and foo(Integer)
// if we do not discriminate one of them, any call to foo(kotlin.Int) will result in overload resolution ambiguity
// so, for such cases, we discriminate Integer in favour of int
if (!KotlinBuiltIns.isPrimitiveType(general) || !KotlinBuiltIns.isPrimitiveType(flexibility.lowerBound)) {
if (!simpleGeneral.isPrimitiveType() || !flexibility.lowerBound().isPrimitiveType()) {
return false
}
// Int? >< Int!
if (general.isMarkedNullable) return false
if (simpleGeneral.isMarkedNullable()) return false
// Int! lessSpecific Int
return true
}
@@ -96,7 +96,7 @@ object JvmPlatformConfigurator : PlatformConfiguratorBase(
container.useImpl<InlinePlatformCompatibilityChecker>()
container.useImpl<JvmModuleAccessibilityChecker>()
container.useImpl<JvmModuleAccessibilityChecker.ClassifierUsage>()
container.useInstance(JvmTypeSpecificityComparator)
container.useImpl<JvmTypeSpecificityComparator>()
container.useImpl<JvmDefaultSuperCallChecker>()
container.useImpl<JvmSamConversionTransformer>()
container.useInstance(FunctionWithBigAritySupport.LANGUAGE_VERSION_DEPENDENT)
@@ -27,9 +27,10 @@ import org.jetbrains.kotlin.resolve.descriptorUtil.isExtensionProperty
import org.jetbrains.kotlin.resolve.descriptorUtil.varargParameterPosition
import org.jetbrains.kotlin.types.ErrorUtils
import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.types.model.KotlinTypeMarker
object OverloadabilitySpecificityCallbacks : SpecificityComparisonCallbacks {
override fun isNonSubtypeNotLessSpecific(specific: KotlinType, general: KotlinType): Boolean =
override fun isNonSubtypeNotLessSpecific(specific: KotlinTypeMarker, general: KotlinTypeMarker): Boolean =
false
}
@@ -34,11 +34,17 @@ import org.jetbrains.kotlin.resolve.descriptorUtil.hasExactAnnotation
import org.jetbrains.kotlin.resolve.descriptorUtil.hasNoInferAnnotation
import org.jetbrains.kotlin.types.*
import org.jetbrains.kotlin.types.TypeUtils.DONT_CARE
import org.jetbrains.kotlin.types.checker.SimpleClassicTypeSystemContext
import org.jetbrains.kotlin.types.checker.TypeCheckingProcedure
import org.jetbrains.kotlin.types.checker.TypeCheckingProcedureCallbacks
import org.jetbrains.kotlin.types.checker.requireOrDescribe
import org.jetbrains.kotlin.types.model.KotlinTypeMarker
import org.jetbrains.kotlin.types.model.TypeParameterMarker
import org.jetbrains.kotlin.types.model.TypeSystemInferenceExtensionContext
import org.jetbrains.kotlin.types.typeUtil.builtIns
import org.jetbrains.kotlin.types.typeUtil.defaultProjections
import org.jetbrains.kotlin.types.typeUtil.isDefaultBound
import org.jetbrains.kotlin.utils.addToStdlib.cast
import java.lang.IllegalArgumentException
import java.lang.IllegalStateException
import java.util.*
@@ -426,13 +432,18 @@ open class ConstraintSystemBuilderImpl(private val mode: Mode = ConstraintSystem
companion object {
fun forSpecificity(): SimpleConstraintSystem = object : ConstraintSystemBuilderImpl(Mode.SPECIFICITY), SimpleConstraintSystem {
override val context: TypeSystemInferenceExtensionContext
get() = SimpleClassicTypeSystemContext
var counter = 0
override fun registerTypeVariables(typeParameters: Collection<TypeParameterDescriptor>) =
registerTypeVariables(CallHandle.NONE, typeParameters)
override fun registerTypeVariables(typeParameters: Collection<TypeParameterMarker>) =
registerTypeVariables(CallHandle.NONE, typeParameters.cast())
override fun addSubtypeConstraint(subType: UnwrappedType, superType: UnwrappedType) =
override fun addSubtypeConstraint(subType: KotlinTypeMarker, superType: KotlinTypeMarker) {
requireOrDescribe(subType is UnwrappedType, subType)
requireOrDescribe(superType is UnwrappedType, superType)
addSubtypeConstraint(subType, superType, ConstraintPositionKind.VALUE_PARAMETER_POSITION.position(counter++))
}
override fun hasContradiction(): Boolean {
fixVariables()
@@ -25,17 +25,23 @@ import org.jetbrains.kotlin.resolve.calls.inference.model.TypeVariableFromCallab
import org.jetbrains.kotlin.resolve.calls.inference.substitute
import org.jetbrains.kotlin.resolve.calls.results.SimpleConstraintSystem
import org.jetbrains.kotlin.types.TypeConstructorSubstitution
import org.jetbrains.kotlin.types.checker.requireOrDescribe
import org.jetbrains.kotlin.types.TypeSubstitutor
import org.jetbrains.kotlin.types.UnwrappedType
import org.jetbrains.kotlin.types.model.KotlinTypeMarker
import org.jetbrains.kotlin.types.model.TypeParameterMarker
import org.jetbrains.kotlin.types.model.TypeSystemInferenceExtensionContext
import org.jetbrains.kotlin.types.typeUtil.asTypeProjection
class SimpleConstraintSystemImpl(constraintInjector: ConstraintInjector, builtIns: KotlinBuiltIns) : SimpleConstraintSystem {
val system = NewConstraintSystemImpl(constraintInjector, ClassicTypeSystemContextForCS(builtIns))
val csBuilder: ConstraintSystemBuilder =
NewConstraintSystemImpl(constraintInjector, ClassicTypeSystemContextForCS(builtIns)).getBuilder()
system.getBuilder()
override fun registerTypeVariables(typeParameters: Collection<TypeParameterMarker>): TypeSubstitutor {
override fun registerTypeVariables(typeParameters: Collection<TypeParameterDescriptor>): TypeSubstitutor {
val substitutionMap = typeParameters.associate {
requireOrDescribe(it is TypeParameterDescriptor, it)
val variable = TypeVariableFromCallableDescriptor(it)
csBuilder.registerVariable(variable)
@@ -43,6 +49,7 @@ class SimpleConstraintSystemImpl(constraintInjector: ConstraintInjector, builtIn
}
val substitutor = TypeConstructorSubstitution.createByConstructorsMap(substitutionMap).buildSubstitutor()
for (typeParameter in typeParameters) {
requireOrDescribe(typeParameter is TypeParameterDescriptor, typeParameter)
for (upperBound in typeParameter.upperBounds) {
addSubtypeConstraint(substitutor.substitute(typeParameter.defaultType), substitutor.substitute(upperBound.unwrap()))
}
@@ -50,7 +57,9 @@ class SimpleConstraintSystemImpl(constraintInjector: ConstraintInjector, builtIn
return substitutor
}
override fun addSubtypeConstraint(subType: UnwrappedType, superType: UnwrappedType) {
override fun addSubtypeConstraint(subType: KotlinTypeMarker, superType: KotlinTypeMarker) {
require(subType is UnwrappedType)
require(superType is UnwrappedType)
csBuilder.addSubtypeConstraint(
subType,
superType,
@@ -61,4 +70,7 @@ class SimpleConstraintSystemImpl(constraintInjector: ConstraintInjector, builtIn
override fun hasContradiction() = csBuilder.hasContradiction
override val captureFromArgument get() = true
override val context: TypeSystemInferenceExtensionContext
get() = system
}
@@ -18,30 +18,31 @@ package org.jetbrains.kotlin.resolve.calls.results
import org.jetbrains.kotlin.descriptors.CallableDescriptor
import org.jetbrains.kotlin.descriptors.MemberDescriptor
import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor
import org.jetbrains.kotlin.descriptors.ValueParameterDescriptor
import org.jetbrains.kotlin.descriptors.synthetic.SyntheticMemberDescriptor
import org.jetbrains.kotlin.resolve.calls.components.hasDefaultValue
import org.jetbrains.kotlin.types.*
import org.jetbrains.kotlin.types.checker.KotlinTypeChecker
import org.jetbrains.kotlin.types.checker.captureFromExpression
import org.jetbrains.kotlin.types.model.KotlinTypeMarker
import org.jetbrains.kotlin.types.model.TypeParameterMarker
import org.jetbrains.kotlin.types.model.TypeSystemInferenceExtensionContext
interface SpecificityComparisonCallbacks {
fun isNonSubtypeNotLessSpecific(specific: KotlinType, general: KotlinType): Boolean
fun isNonSubtypeNotLessSpecific(specific: KotlinTypeMarker, general: KotlinTypeMarker): Boolean
}
interface TypeSpecificityComparator {
fun isDefinitelyLessSpecific(specific: KotlinType, general: KotlinType): Boolean
fun isDefinitelyLessSpecific(specific: KotlinTypeMarker, general: KotlinTypeMarker): Boolean
object NONE : TypeSpecificityComparator {
override fun isDefinitelyLessSpecific(specific: KotlinType, general: KotlinType) = false
override fun isDefinitelyLessSpecific(specific: KotlinTypeMarker, general: KotlinTypeMarker) = false
}
}
class FlatSignature<out T> private constructor(
class FlatSignature<out T> constructor(
val origin: T,
val typeParameters: Collection<TypeParameterDescriptor>,
val valueParameterTypes: List<KotlinType?>,
val typeParameters: Collection<TypeParameterMarker>,
val valueParameterTypes: List<KotlinTypeMarker?>,
val hasExtensionReceiver: Boolean,
val hasVarargs: Boolean,
val numDefaults: Int,
@@ -113,12 +114,14 @@ class FlatSignature<out T> private constructor(
interface SimpleConstraintSystem {
fun registerTypeVariables(typeParameters: Collection<TypeParameterDescriptor>): TypeSubstitutor
fun addSubtypeConstraint(subType: UnwrappedType, superType: UnwrappedType)
fun registerTypeVariables(typeParameters: Collection<TypeParameterMarker>): TypeSubstitutor
fun addSubtypeConstraint(subType: KotlinTypeMarker, superType: KotlinTypeMarker)
fun hasContradiction(): Boolean
// todo hack for migration
val captureFromArgument get() = false
val context: TypeSystemInferenceExtensionContext
}
fun <T> SimpleConstraintSystem.isSignatureNotLessSpecific(
@@ -131,7 +134,6 @@ fun <T> SimpleConstraintSystem.isSignatureNotLessSpecific(
if (specific.valueParameterTypes.size != general.valueParameterTypes.size) return false
val typeParameters = general.typeParameters
val typeSubstitutor = registerTypeVariables(typeParameters)
for ((specificType, generalType) in specific.valueParameterTypes.zip(general.valueParameterTypes)) {
if (specificType == null || generalType == null) continue
@@ -140,13 +142,17 @@ fun <T> SimpleConstraintSystem.isSignatureNotLessSpecific(
return false
}
if (typeParameters.isEmpty() || !TypeUtils.dependsOnTypeParameters(generalType, typeParameters)) {
if (!KotlinTypeChecker.DEFAULT.isSubtypeOf(specificType, generalType)) {
if (typeParameters.isEmpty() /*|| !TypeUtils.dependsOnTypeParameters(generalType, typeParameters)*/) {
if (!AbstractTypeChecker.isSubtypeOf(context, specificType, generalType)) {
if (!callbacks.isNonSubtypeNotLessSpecific(specificType, generalType)) {
return false
}
}
} else {
val typeSubstitutor = registerTypeVariables(typeParameters)
require(generalType is KotlinType) { TODO("not supported") }
require(specificType is KotlinType) { TODO("not supported") }
val substitutedGeneralType = typeSubstitutor.safeSubstitute(generalType, Variance.INVARIANT)
/**
@@ -28,6 +28,8 @@ import org.jetbrains.kotlin.resolve.calls.context.CheckArgumentTypesMode
import org.jetbrains.kotlin.resolve.descriptorUtil.varargParameterPosition
import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.types.TypeUtils
import org.jetbrains.kotlin.types.checker.requireOrDescribe
import org.jetbrains.kotlin.types.model.KotlinTypeMarker
import java.util.*
open class OverloadingConflictResolver<C : Any>(
@@ -284,7 +286,10 @@ open class OverloadingConflictResolver<C : Any>(
}
private val SpecificityComparisonWithNumerics = object : SpecificityComparisonCallbacks {
override fun isNonSubtypeNotLessSpecific(specific: KotlinType, general: KotlinType): Boolean {
override fun isNonSubtypeNotLessSpecific(specific: KotlinTypeMarker, general: KotlinTypeMarker): Boolean {
requireOrDescribe(specific is KotlinType, specific)
requireOrDescribe(general is KotlinType, general)
val _double = builtIns.doubleType
val _float = builtIns.floatType
@@ -21,6 +21,8 @@ import org.jetbrains.kotlin.types.model.CaptureStatus
import org.jetbrains.kotlin.types.typeUtil.asTypeProjection
import org.jetbrains.kotlin.types.typeUtil.contains
import kotlin.math.max
import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.contract
interface ClassicTypeSystemContext : TypeSystemInferenceExtensionContext {
override fun TypeConstructorMarker.isDenotable(): Boolean {
@@ -463,6 +465,11 @@ interface ClassicTypeSystemContext : TypeSystemInferenceExtensionContext {
require(this is IntegerLiteralTypeConstructor, this::errorMessage)
return this.getApproximatedType().unwrap()
}
override fun SimpleTypeMarker.isPrimitiveType(): Boolean {
require(this is KotlinType, this::errorMessage)
return KotlinBuiltIns.isPrimitiveType(this)
}
}
private fun hasNoInferInternal(type: UnwrappedType): Boolean {
@@ -520,3 +527,18 @@ fun Variance.convertVariance(): TypeVariance {
Variance.OUT_VARIANCE -> TypeVariance.OUT
}
}
@Suppress("EXPERIMENTAL_IS_NOT_ENABLED")
@UseExperimental(ExperimentalContracts::class)
fun requireOrDescribe(condition: Boolean, value: Any?) {
contract {
returns() implies condition
}
require(condition) {
val typeInfo = if (value != null) {
", type = '${value::class}'"
} else ""
"Unexpected: value = '$value'$typeInfo"
}
}
@@ -199,6 +199,8 @@ interface TypeSystemContext : TypeSystemOptimizationContext {
fun KotlinTypeMarker.lowerBoundIfFlexible(): SimpleTypeMarker = this.asFlexibleType()?.lowerBound() ?: this.asSimpleType()!!
fun KotlinTypeMarker.upperBoundIfFlexible(): SimpleTypeMarker = this.asFlexibleType()?.upperBound() ?: this.asSimpleType()!!
fun KotlinTypeMarker.isFlexible(): Boolean = asFlexibleType() != null
fun KotlinTypeMarker.isDynamic(): Boolean = asFlexibleType()?.asDynamicType() != null
fun KotlinTypeMarker.isDefinitelyNotNullType(): Boolean = asSimpleType()?.asDefinitelyNotNullType() != null
@@ -261,6 +263,8 @@ interface TypeSystemContext : TypeSystemOptimizationContext {
fun KotlinTypeMarker.isSimpleType() = asSimpleType() != null
fun prepareType(type: KotlinTypeMarker): KotlinTypeMarker
fun SimpleTypeMarker.isPrimitiveType(): Boolean
}
enum class CaptureStatus {
@@ -29,6 +29,7 @@ import org.jetbrains.kotlin.asJava.classes.KtLightClassForSourceDeclaration
import org.jetbrains.kotlin.asJava.namedUnwrappedElement
import org.jetbrains.kotlin.descriptors.ClassifierDescriptor
import org.jetbrains.kotlin.idea.caches.resolve.analyze
import org.jetbrains.kotlin.idea.caches.resolve.getResolutionFacade
import org.jetbrains.kotlin.idea.caches.resolve.unsafeResolveToDescriptor
import org.jetbrains.kotlin.idea.refactoring.withExpectedActuals
import org.jetbrains.kotlin.idea.references.KtSimpleNameReference
@@ -98,10 +99,9 @@ class RenameKotlinClassifierProcessor : RenameKotlinPsiProcessor() {
result: MutableList<UsageInfo>
) {
val declaration = element.namedUnwrappedElement as? KtNamedDeclaration ?: return
val descriptor = declaration.unsafeResolveToDescriptor() as ClassifierDescriptor
val collisions = SmartList<UsageInfo>()
checkRedeclarations(descriptor, newName, collisions)
checkRedeclarations(declaration, newName, collisions)
checkOriginalUsagesRetargeting(declaration, newName, result, collisions)
checkNewNameUsagesRetargeting(declaration, newName, collisions)
result += collisions
@@ -39,6 +39,7 @@ import org.jetbrains.kotlin.codegen.state.KotlinTypeMapper.InternalNameMapper.de
import org.jetbrains.kotlin.codegen.state.KotlinTypeMapper.InternalNameMapper.getModuleNameSuffix
import org.jetbrains.kotlin.codegen.state.KotlinTypeMapper.InternalNameMapper.mangleInternalName
import org.jetbrains.kotlin.descriptors.FunctionDescriptor
import org.jetbrains.kotlin.idea.caches.resolve.getResolutionFacade
import org.jetbrains.kotlin.idea.caches.resolve.unsafeResolveToDescriptor
import org.jetbrains.kotlin.idea.refactoring.Pass
import org.jetbrains.kotlin.idea.refactoring.checkSuperMethods
@@ -96,10 +97,9 @@ class RenameKotlinFunctionProcessor : RenameKotlinPsiProcessor() {
result: MutableList<UsageInfo>
) {
val declaration = element.unwrapped as? KtNamedFunction ?: return
val descriptor = declaration.unsafeResolveToDescriptor()
checkConflictsAndReplaceUsageInfos(element, allRenames, result)
result += SmartList<UsageInfo>().also { collisions ->
checkRedeclarations(descriptor, newName, collisions)
checkRedeclarations(declaration, newName, collisions)
checkOriginalUsagesRetargeting(declaration, newName, result, collisions)
checkNewNameUsagesRetargeting(declaration, newName, collisions)
}
@@ -22,6 +22,7 @@ import com.intellij.refactoring.listeners.RefactoringElementListener
import com.intellij.usageView.UsageInfo
import org.jetbrains.kotlin.asJava.namedUnwrappedElement
import org.jetbrains.kotlin.descriptors.VariableDescriptor
import org.jetbrains.kotlin.idea.caches.resolve.getResolutionFacade
import org.jetbrains.kotlin.idea.caches.resolve.unsafeResolveToDescriptor
import org.jetbrains.kotlin.psi.KtFunction
import org.jetbrains.kotlin.psi.KtNamedDeclaration
@@ -44,10 +45,9 @@ class RenameKotlinParameterProcessor : RenameKotlinPsiProcessor() {
result: MutableList<UsageInfo>
) {
val declaration = element.namedUnwrappedElement as? KtNamedDeclaration ?: return
val descriptor = declaration.unsafeResolveToDescriptor() as VariableDescriptor
val collisions = SmartList<UsageInfo>()
checkRedeclarations(descriptor, newName, collisions)
checkRedeclarations(declaration, newName, collisions)
checkOriginalUsagesRetargeting(declaration, newName, result, collisions)
checkNewNameUsagesRetargeting(declaration, newName, collisions)
result += collisions
@@ -44,6 +44,7 @@ import org.jetbrains.kotlin.descriptors.PropertyDescriptor
import org.jetbrains.kotlin.descriptors.ValueParameterDescriptor
import org.jetbrains.kotlin.descriptors.VariableDescriptor
import org.jetbrains.kotlin.idea.caches.resolve.analyze
import org.jetbrains.kotlin.idea.caches.resolve.getResolutionFacade
import org.jetbrains.kotlin.idea.caches.resolve.unsafeResolveToDescriptor
import org.jetbrains.kotlin.idea.core.getDeepestSuperDeclarations
import org.jetbrains.kotlin.idea.core.isEnumCompanionPropertyWithEntryConflict
@@ -204,10 +205,11 @@ class RenameKotlinPropertyProcessor : RenameKotlinPsiProcessor() {
result: MutableList<UsageInfo>
) {
val declaration = element.namedUnwrappedElement as? KtNamedDeclaration ?: return
val descriptor = declaration.unsafeResolveToDescriptor() as VariableDescriptor
val resolutionFacade = declaration.getResolutionFacade()
val descriptor = declaration.unsafeResolveToDescriptor(resolutionFacade) as VariableDescriptor
val collisions = SmartList<UsageInfo>()
checkRedeclarations(descriptor, newName, collisions)
checkRedeclarations(declaration, newName, collisions, resolutionFacade, descriptor)
checkAccidentalOverrides(declaration, newName, descriptor, collisions)
checkOriginalUsagesRetargeting(declaration, newName, result, collisions)
checkNewNameUsagesRetargeting(declaration, newName, collisions)
@@ -18,6 +18,7 @@ package org.jetbrains.kotlin.idea.refactoring.rename
import com.intellij.psi.PsiElement
import com.intellij.usageView.UsageInfo
import org.jetbrains.kotlin.idea.caches.resolve.getResolutionFacade
import org.jetbrains.kotlin.idea.caches.resolve.unsafeResolveToDescriptor
import org.jetbrains.kotlin.psi.KtTypeParameter
@@ -31,7 +32,6 @@ class RenameKotlinTypeParameterProcessor : RenameKotlinPsiProcessor() {
result: MutableList<UsageInfo>
) {
val declaration = element as? KtTypeParameter ?: return
val descriptor = declaration.unsafeResolveToDescriptor()
checkRedeclarations(descriptor, newName, result)
checkRedeclarations(declaration, newName, result)
}
}
@@ -24,6 +24,7 @@ import com.intellij.usageView.UsageViewUtil
import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.idea.analysis.analyzeInContext
import org.jetbrains.kotlin.idea.caches.resolve.analyze
import org.jetbrains.kotlin.idea.caches.resolve.getResolutionFacade
import org.jetbrains.kotlin.idea.caches.resolve.unsafeResolveToDescriptor
import org.jetbrains.kotlin.idea.codeInsight.DescriptorToSourceUtilsIde
import org.jetbrains.kotlin.idea.core.NewDeclarationNameValidator
@@ -33,6 +34,7 @@ import org.jetbrains.kotlin.idea.project.TargetPlatformDetector
import org.jetbrains.kotlin.idea.refactoring.explicateAsText
import org.jetbrains.kotlin.idea.refactoring.getThisLabelName
import org.jetbrains.kotlin.idea.references.mainReference
import org.jetbrains.kotlin.idea.resolve.ResolutionFacade
import org.jetbrains.kotlin.idea.search.and
import org.jetbrains.kotlin.idea.search.restrictToKotlinSources
import org.jetbrains.kotlin.idea.util.*
@@ -53,6 +55,7 @@ import org.jetbrains.kotlin.resolve.calls.resolvedCallUtil.getExplicitReceiverVa
import org.jetbrains.kotlin.resolve.calls.resolvedCallUtil.getImplicitReceiverValue
import org.jetbrains.kotlin.resolve.calls.results.TypeSpecificityComparator
import org.jetbrains.kotlin.resolve.descriptorUtil.getImportableDescriptor
import org.jetbrains.kotlin.resolve.descriptorUtil.module
import org.jetbrains.kotlin.resolve.jvm.JvmTypeSpecificityComparator
import org.jetbrains.kotlin.resolve.jvm.platform.JvmPlatform
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
@@ -88,9 +91,11 @@ internal fun PsiElement.representativeContainer(): PsiNamedElement? =
internal fun DeclarationDescriptor.canonicalRender(): String = DescriptorRenderer.FQ_NAMES_IN_TYPES.render(this)
internal fun checkRedeclarations(
descriptor: DeclarationDescriptor,
newName: String,
result: MutableList<UsageInfo>
declaration: KtNamedDeclaration,
newName: String,
result: MutableList<UsageInfo>,
resolutionFacade: ResolutionFacade = declaration.getResolutionFacade(),
descriptor: DeclarationDescriptor = declaration.unsafeResolveToDescriptor(resolutionFacade)
) {
fun DeclarationDescriptor.isTopLevelPrivate(): Boolean {
return this is DeclarationDescriptorWithVisibility
@@ -165,12 +170,7 @@ internal fun checkRedeclarations(
is PropertyDescriptor,
is FunctionDescriptor,
is ClassifierDescriptor -> {
val psi = (descriptor as? DeclarationDescriptorWithSource)?.source?.getPsi() as? KtElement ?: return
val typeSpecificityComparator = when (TargetPlatformDetector.getPlatform(psi.containingKtFile)) {
is JvmPlatform -> JvmTypeSpecificityComparator
is JsPlatform -> JsTypeSpecificityComparator
else -> TypeSpecificityComparator.NONE
}
val typeSpecificityComparator = resolutionFacade.getFrontendService(descriptor.module, TypeSpecificityComparator::class.java)
OverloadChecker(typeSpecificityComparator)
}
else -> null
@@ -64,7 +64,7 @@ object JsPlatformConfigurator : PlatformConfiguratorBase(
container.useImpl<JsCallChecker>()
container.useInstance(SyntheticScopes.Empty)
container.useInstance(SamConversionTransformer.Empty)
container.useInstance(JsTypeSpecificityComparator)
container.useImpl<JsTypeSpecificityComparator>()
container.useImpl<JsNameClashChecker>()
container.useImpl<JsNameCharsChecker>()
container.useImpl<JsBuiltinNameClashChecker>()
@@ -20,18 +20,21 @@ import org.jetbrains.kotlin.resolve.calls.results.TypeSpecificityComparator
import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.types.isDynamic
import org.jetbrains.kotlin.types.isFlexible
import org.jetbrains.kotlin.types.model.KotlinTypeMarker
import org.jetbrains.kotlin.types.model.TypeSystemInferenceExtensionContext
import org.jetbrains.kotlin.types.model.TypeSystemInferenceExtensionContextDelegate
object JsTypeSpecificityComparator: TypeSpecificityComparator {
class JsTypeSpecificityComparator(val context: TypeSystemInferenceExtensionContextDelegate) : TypeSpecificityComparator {
private fun checkOnlyDynamicFlexibleType(type: KotlinType) {
if (type.isFlexible()) {
private fun TypeSystemInferenceExtensionContext.checkOnlyDynamicFlexibleType(type: KotlinTypeMarker) {
if (type.asFlexibleType() != null) {
assert(type.isDynamic()) {
"Unexpected flexible type in Js: $type"
}
}
}
override fun isDefinitelyLessSpecific(specific: KotlinType, general: KotlinType): Boolean {
override fun isDefinitelyLessSpecific(specific: KotlinTypeMarker, general: KotlinTypeMarker): Boolean = with(context) {
checkOnlyDynamicFlexibleType(specific)
checkOnlyDynamicFlexibleType(general)