From e5e3d7cab1a406347f8a06ae4aa2661fe0f7219f Mon Sep 17 00:00:00 2001 From: Dmitriy Novozhilov Date: Tue, 25 Aug 2020 17:00:29 +0300 Subject: [PATCH] Extract FE 1.0 related parts from NewConstraintSystem --- .../inference/ConstraintSystemBuilder.kt | 44 ++------- .../DescriptorRelatedInferenceUtils.kt | 77 ++++++++++++++++ .../resolve/calls/inference/InferenceUtils.kt | 91 +++++-------------- .../calls/inference/NewConstraintSystem.kt | 5 +- .../model/MutableConstraintStorage.kt | 2 +- .../model/NewConstraintSystemImpl.kt | 1 + .../org/jetbrains/kotlin/utils/addToStdlib.kt | 6 +- 7 files changed, 119 insertions(+), 107 deletions(-) create mode 100644 compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/DescriptorRelatedInferenceUtils.kt diff --git a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/ConstraintSystemBuilder.kt b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/ConstraintSystemBuilder.kt index 63d31ab71c2..eebf0c82a78 100644 --- a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/ConstraintSystemBuilder.kt +++ b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/ConstraintSystemBuilder.kt @@ -16,19 +16,13 @@ package org.jetbrains.kotlin.resolve.calls.inference -import org.jetbrains.kotlin.resolve.calls.components.PostponedArgumentsAnalyzer import org.jetbrains.kotlin.resolve.calls.inference.model.ConstraintKind import org.jetbrains.kotlin.resolve.calls.inference.model.ConstraintPosition import org.jetbrains.kotlin.resolve.calls.inference.model.ConstraintStorage -import org.jetbrains.kotlin.resolve.calls.model.CallableReferenceKotlinCallArgument -import org.jetbrains.kotlin.resolve.calls.model.KotlinCallArgument -import org.jetbrains.kotlin.resolve.calls.model.LHSResult -import org.jetbrains.kotlin.resolve.calls.model.SubKotlinCallArgument import org.jetbrains.kotlin.types.model.KotlinTypeMarker import org.jetbrains.kotlin.types.model.TypeConstructorMarker import org.jetbrains.kotlin.types.model.TypeSubstitutorMarker import org.jetbrains.kotlin.types.model.TypeVariableMarker -import org.jetbrains.kotlin.utils.addToStdlib.safeAs interface ConstraintSystemOperation { val hasContradiction: Boolean @@ -50,7 +44,6 @@ interface ConstraintSystemOperation { } interface ConstraintSystemBuilder : ConstraintSystemOperation { - //val builtIns: KotlinBuiltIns // if runOperations return true, then this operation will be applied, and function return true fun runTransaction(runOperations: ConstraintSystemOperation.() -> Boolean): Boolean @@ -63,45 +56,26 @@ fun ConstraintSystemBuilder.addSubtypeConstraintIfCompatible( lowerType: KotlinTypeMarker, upperType: KotlinTypeMarker, position: ConstraintPosition -): Boolean = - addConstraintIfCompatible(lowerType, upperType, position, ConstraintKind.LOWER) +): Boolean = addConstraintIfCompatible(lowerType, upperType, position, ConstraintKind.LOWER) fun ConstraintSystemBuilder.addEqualityConstraintIfCompatible( lowerType: KotlinTypeMarker, upperType: KotlinTypeMarker, position: ConstraintPosition -): Boolean = - addConstraintIfCompatible(lowerType, upperType, position, ConstraintKind.EQUALITY) +): Boolean = addConstraintIfCompatible(lowerType, upperType, position, ConstraintKind.EQUALITY) private fun ConstraintSystemBuilder.addConstraintIfCompatible( lowerType: KotlinTypeMarker, upperType: KotlinTypeMarker, position: ConstraintPosition, kind: ConstraintKind -) = - runTransaction { - if (!hasContradiction) { - when (kind) { - ConstraintKind.LOWER -> addSubtypeConstraint(lowerType, upperType, position) - ConstraintKind.UPPER -> addSubtypeConstraint(upperType, lowerType, position) - ConstraintKind.EQUALITY -> addEqualityConstraint(lowerType, upperType, position) - } +): Boolean = runTransaction { + if (!hasContradiction) { + when (kind) { + ConstraintKind.LOWER -> addSubtypeConstraint(lowerType, upperType, position) + ConstraintKind.UPPER -> addSubtypeConstraint(upperType, lowerType, position) + ConstraintKind.EQUALITY -> addEqualityConstraint(lowerType, upperType, position) } - !hasContradiction - } - - -fun PostponedArgumentsAnalyzer.Context.addSubsystemFromArgument(argument: KotlinCallArgument?): Boolean { - return when (argument) { - is SubKotlinCallArgument -> { - addOtherSystem(argument.callResult.constraintSystem) - true - } - - is CallableReferenceKotlinCallArgument -> { - addSubsystemFromArgument(argument.lhsResult.safeAs()?.lshCallArgument) - } - - else -> false } + !hasContradiction } diff --git a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/DescriptorRelatedInferenceUtils.kt b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/DescriptorRelatedInferenceUtils.kt new file mode 100644 index 00000000000..0db48ad6ea1 --- /dev/null +++ b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/DescriptorRelatedInferenceUtils.kt @@ -0,0 +1,77 @@ +/* + * Copyright 2010-2020 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.resolve.calls.inference + +import org.jetbrains.kotlin.descriptors.CallableDescriptor +import org.jetbrains.kotlin.resolve.calls.components.PostponedArgumentsAnalyzer +import org.jetbrains.kotlin.resolve.calls.inference.components.NewTypeSubstitutor +import org.jetbrains.kotlin.resolve.calls.inference.model.ConstraintStorage +import org.jetbrains.kotlin.resolve.calls.model.CallableReferenceKotlinCallArgument +import org.jetbrains.kotlin.resolve.calls.model.KotlinCallArgument +import org.jetbrains.kotlin.resolve.calls.model.LHSResult +import org.jetbrains.kotlin.resolve.calls.model.SubKotlinCallArgument +import org.jetbrains.kotlin.resolve.descriptorUtil.builtIns +import org.jetbrains.kotlin.types.* +import org.jetbrains.kotlin.types.model.TypeSystemInferenceExtensionContext +import org.jetbrains.kotlin.utils.addToStdlib.safeAs + +fun ConstraintStorage.buildResultingSubstitutor( + context: TypeSystemInferenceExtensionContext, + transformTypeVariablesToErrorTypes: Boolean = true +): NewTypeSubstitutor { + return buildAbstractResultingSubstitutor(context, transformTypeVariablesToErrorTypes) as NewTypeSubstitutor +} + +val CallableDescriptor.returnTypeOrNothing: UnwrappedType + get() { + returnType?.let { return it.unwrap() } + + return builtIns.nothingType + } + +fun TypeSubstitutor.substitute(type: UnwrappedType): UnwrappedType = safeSubstitute(type, Variance.INVARIANT).unwrap() + +fun CallableDescriptor.substitute(substitutor: NewTypeSubstitutor): CallableDescriptor { + val wrappedSubstitution = object : TypeSubstitution() { + override fun get(key: KotlinType): TypeProjection? = null + override fun prepareTopLevelType(topLevelType: KotlinType, position: Variance) = substitutor.safeSubstitute(topLevelType.unwrap()) + } + return substitute(TypeSubstitutor.create(wrappedSubstitution)) +} + +fun CallableDescriptor.substituteAndApproximateTypes( + substitutor: NewTypeSubstitutor, + typeApproximator: TypeApproximator? +): CallableDescriptor { + val wrappedSubstitution = object : TypeSubstitution() { + override fun get(key: KotlinType): TypeProjection? = null + + override fun prepareTopLevelType(topLevelType: KotlinType, position: Variance) = + substitutor.safeSubstitute(topLevelType.unwrap()).let { substitutedType -> + typeApproximator?.approximateToSuperType( + substitutedType, + TypeApproximatorConfiguration.FinalApproximationAfterResolutionAndInference + ) ?: substitutedType + } + } + + return substitute(TypeSubstitutor.create(wrappedSubstitution)) ?: this +} + +fun PostponedArgumentsAnalyzer.Context.addSubsystemFromArgument(argument: KotlinCallArgument?): Boolean { + return when (argument) { + is SubKotlinCallArgument -> { + addOtherSystem(argument.callResult.constraintSystem) + true + } + + is CallableReferenceKotlinCallArgument -> { + addSubsystemFromArgument(argument.lhsResult.safeAs()?.lshCallArgument) + } + + else -> false + } +} diff --git a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/InferenceUtils.kt b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/InferenceUtils.kt index ed32381c6f9..d383b1dfa1b 100644 --- a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/InferenceUtils.kt +++ b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/InferenceUtils.kt @@ -16,11 +16,7 @@ package org.jetbrains.kotlin.resolve.calls.inference -import org.jetbrains.kotlin.descriptors.CallableDescriptor -import org.jetbrains.kotlin.resolve.calls.inference.components.NewTypeSubstitutor import org.jetbrains.kotlin.resolve.calls.inference.model.ConstraintStorage -import org.jetbrains.kotlin.resolve.descriptorUtil.builtIns -import org.jetbrains.kotlin.types.* import org.jetbrains.kotlin.types.model.* fun ConstraintStorage.buildCurrentSubstitutor( @@ -30,71 +26,34 @@ fun ConstraintStorage.buildCurrentSubstitutor( return context.typeSubstitutorByTypeConstructor(fixedTypeVariables.entries.associate { it.key to it.value } + additionalBindings) } -fun ConstraintStorage.buildAbstractResultingSubstitutor(context: TypeSystemInferenceExtensionContext, transformTypeVariablesToErrorTypes: Boolean = true): TypeSubstitutorMarker = - with(context) { - if (allTypeVariables.isEmpty()) return createEmptySubstitutor() +fun ConstraintStorage.buildAbstractResultingSubstitutor( + context: TypeSystemInferenceExtensionContext, + transformTypeVariablesToErrorTypes: Boolean = true +): TypeSubstitutorMarker = with(context) { + if (allTypeVariables.isEmpty()) return createEmptySubstitutor() - val currentSubstitutorMap = fixedTypeVariables.entries.associate { - it.key to it.value - } - val uninferredSubstitutorMap = if (transformTypeVariablesToErrorTypes) { - notFixedTypeVariables.entries.associate { (freshTypeConstructor, typeVariable) -> - freshTypeConstructor to context.createErrorTypeWithCustomConstructor( - "Uninferred type", - (typeVariable.typeVariable).freshTypeConstructor() - ) - } - } else { - notFixedTypeVariables.entries.associate { (freshTypeConstructor, typeVariable) -> - freshTypeConstructor to typeVariable.typeVariable.defaultType(this) - } - } - return context.typeSubstitutorByTypeConstructor(currentSubstitutorMap + uninferredSubstitutorMap) + val currentSubstitutorMap = fixedTypeVariables.entries.associate { + it.key to it.value } + val uninferredSubstitutorMap = if (transformTypeVariablesToErrorTypes) { + notFixedTypeVariables.entries.associate { (freshTypeConstructor, typeVariable) -> + freshTypeConstructor to context.createErrorTypeWithCustomConstructor( + "Uninferred type", + (typeVariable.typeVariable).freshTypeConstructor() + ) + } + } else { + notFixedTypeVariables.entries.associate { (freshTypeConstructor, typeVariable) -> + freshTypeConstructor to typeVariable.typeVariable.defaultType(this) + } + } + return context.typeSubstitutorByTypeConstructor(currentSubstitutorMap + uninferredSubstitutorMap) +} -fun ConstraintStorage.buildNotFixedVariablesToNonSubtypableTypesSubstitutor(context: TypeSystemInferenceExtensionContext) = - context.typeSubstitutorByTypeConstructor( +fun ConstraintStorage.buildNotFixedVariablesToNonSubtypableTypesSubstitutor( + context: TypeSystemInferenceExtensionContext +): TypeSubstitutorMarker { + return context.typeSubstitutorByTypeConstructor( notFixedTypeVariables.mapValues { context.createStubType(it.value.typeVariable) } ) - -fun ConstraintStorage.buildResultingSubstitutor(context: TypeSystemInferenceExtensionContext, transformTypeVariablesToErrorTypes: Boolean = true): NewTypeSubstitutor { - return buildAbstractResultingSubstitutor(context, transformTypeVariablesToErrorTypes) as NewTypeSubstitutor } - -val CallableDescriptor.returnTypeOrNothing: UnwrappedType - get() { - returnType?.let { return it.unwrap() } - - return builtIns.nothingType - } - -fun TypeSubstitutor.substitute(type: UnwrappedType): UnwrappedType = safeSubstitute(type, Variance.INVARIANT).unwrap() - -fun CallableDescriptor.substitute(substitutor: NewTypeSubstitutor): CallableDescriptor { - val wrappedSubstitution = object : TypeSubstitution() { - override fun get(key: KotlinType): TypeProjection? = null - override fun prepareTopLevelType(topLevelType: KotlinType, position: Variance) = substitutor.safeSubstitute(topLevelType.unwrap()) - } - return substitute(TypeSubstitutor.create(wrappedSubstitution)) -} - -fun CallableDescriptor.substituteAndApproximateTypes( - substitutor: NewTypeSubstitutor, - typeApproximator: TypeApproximator? -): CallableDescriptor { - val wrappedSubstitution = object : TypeSubstitution() { - override fun get(key: KotlinType): TypeProjection? = null - - override fun prepareTopLevelType(topLevelType: KotlinType, position: Variance) = - substitutor.safeSubstitute(topLevelType.unwrap()).let { substitutedType -> - typeApproximator?.approximateToSuperType( - substitutedType, - TypeApproximatorConfiguration.FinalApproximationAfterResolutionAndInference - ) ?: substitutedType - } - } - - return substitute(TypeSubstitutor.create(wrappedSubstitution)) ?: this -} - -internal fun MutableList.trimToSize(newSize: Int) = subList(newSize, size).clear() diff --git a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/NewConstraintSystem.kt b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/NewConstraintSystem.kt index 39e153045c9..9217b0797fd 100644 --- a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/NewConstraintSystem.kt +++ b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/NewConstraintSystem.kt @@ -16,8 +16,6 @@ package org.jetbrains.kotlin.resolve.calls.inference -import org.jetbrains.kotlin.builtins.KotlinBuiltIns -import org.jetbrains.kotlin.resolve.calls.components.KotlinCallCompleter import org.jetbrains.kotlin.resolve.calls.components.PostponedArgumentsAnalyzer import org.jetbrains.kotlin.resolve.calls.inference.components.KotlinConstraintSystemCompleter import org.jetbrains.kotlin.resolve.calls.inference.components.PostponedArgumentInputTypesResolver @@ -25,7 +23,6 @@ import org.jetbrains.kotlin.resolve.calls.inference.model.ConstraintStorage import org.jetbrains.kotlin.resolve.calls.model.KotlinCallDiagnostic interface NewConstraintSystem { -// val builtIns: KotlinBuiltIns val hasContradiction: Boolean val diagnostics: List @@ -37,4 +34,4 @@ interface NewConstraintSystem { fun asConstraintSystemCompleterContext(): KotlinConstraintSystemCompleter.Context fun asPostponedArgumentsAnalyzerContext(): PostponedArgumentsAnalyzer.Context fun asPostponedArgumentInputTypesResolverContext(): PostponedArgumentInputTypesResolver.Context -} \ No newline at end of file +} diff --git a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/model/MutableConstraintStorage.kt b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/model/MutableConstraintStorage.kt index 77b13e9b247..8f9c32f2806 100644 --- a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/model/MutableConstraintStorage.kt +++ b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/model/MutableConstraintStorage.kt @@ -5,7 +5,6 @@ package org.jetbrains.kotlin.resolve.calls.inference.model -import org.jetbrains.kotlin.resolve.calls.inference.trimToSize import org.jetbrains.kotlin.resolve.calls.model.KotlinCallDiagnostic import org.jetbrains.kotlin.resolve.calls.tower.isSuccess import org.jetbrains.kotlin.types.* @@ -14,6 +13,7 @@ import org.jetbrains.kotlin.types.model.TypeConstructorMarker import org.jetbrains.kotlin.types.model.TypeVariableMarker import org.jetbrains.kotlin.types.typeUtil.unCapture import org.jetbrains.kotlin.utils.SmartList +import org.jetbrains.kotlin.utils.addToStdlib.trimToSize class MutableVariableWithConstraints private constructor( diff --git a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/model/NewConstraintSystemImpl.kt b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/model/NewConstraintSystemImpl.kt index 103a8b129b0..bd8a4ba4013 100644 --- a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/model/NewConstraintSystemImpl.kt +++ b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/model/NewConstraintSystemImpl.kt @@ -20,6 +20,7 @@ import org.jetbrains.kotlin.types.model.* import org.jetbrains.kotlin.utils.SmartList import org.jetbrains.kotlin.utils.SmartSet import org.jetbrains.kotlin.utils.addToStdlib.safeAs +import org.jetbrains.kotlin.utils.addToStdlib.trimToSize import kotlin.math.max class NewConstraintSystemImpl( diff --git a/core/util.runtime/src/org/jetbrains/kotlin/utils/addToStdlib.kt b/core/util.runtime/src/org/jetbrains/kotlin/utils/addToStdlib.kt index a80d9d4f7f6..174b5d5bf13 100644 --- a/core/util.runtime/src/org/jetbrains/kotlin/utils/addToStdlib.kt +++ b/core/util.runtime/src/org/jetbrains/kotlin/utils/addToStdlib.kt @@ -172,4 +172,8 @@ inline fun Collection.foldMap(transform: (T) -> R, operation: (R, R) - result = operation(result, transform(iterator.next())) } return result -} \ No newline at end of file +} + +fun MutableList.trimToSize(newSize: Int) { + subList(newSize, size).clear() +}