Revert "[NI] Disable capturing/approximation type in TypeSubstitutor with enabled NI"

This reverts commit 7c4101e21c.

 #KT-31866 Fixed
 #KT-31868 Fixed
 #EA-125401 Fixed
 #KT-25290 Open
This commit is contained in:
Mikhail Zarechenskiy
2019-06-04 17:41:43 +03:00
parent f48640f91c
commit 787a8bb9bd
79 changed files with 197 additions and 887 deletions
@@ -60,20 +60,6 @@ val CallableDescriptor.returnTypeOrNothing: UnwrappedType
fun TypeSubstitutor.substitute(type: UnwrappedType): UnwrappedType = safeSubstitute(type, Variance.INVARIANT).unwrap()
fun CallableDescriptor.substituteAndApproximateIntegerLiteralTypes(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()).let { substitutedType ->
TypeApproximator(builtIns).approximateToSuperType(substitutedType, TypeApproximatorConfiguration.IntegerLiteralsTypesApproximation)
?: substitutedType
}
}
return substitute(TypeSubstitutor.create(wrappedSubstitution))
}
fun CallableDescriptor.substitute(substitutor: NewTypeSubstitutor): CallableDescriptor {
val wrappedSubstitution = object : TypeSubstitution() {
override fun get(key: KotlinType): TypeProjection? = null
@@ -82,25 +68,18 @@ fun CallableDescriptor.substitute(substitutor: NewTypeSubstitutor): CallableDesc
return substitute(TypeSubstitutor.create(wrappedSubstitution))
}
fun <D: CallableDescriptor> D.approximateCapturedTypes(): D {
val approximator = TypeApproximator(builtIns)
var anyChanges = false
fun CallableDescriptor.substituteAndApproximateCapturedTypes(substitutor: NewTypeSubstitutor): CallableDescriptor {
val wrappedSubstitution = object : TypeSubstitution() {
override fun get(key: KotlinType): TypeProjection? = null
override fun prepareTopLevelType(topLevelType: KotlinType, position: Variance): KotlinType {
val type = topLevelType.unwrap()
val approximatedType =
approximator.approximateToSuperType(type, TypeApproximatorConfiguration.CapturedAndIntegerLiteralsTypesApproximation)
if (approximatedType != null) {
anyChanges = true
override fun prepareTopLevelType(topLevelType: KotlinType, position: Variance) =
substitutor.safeSubstitute(topLevelType.unwrap()).let { substitutedType ->
TypeApproximator(builtIns).approximateToSuperType(substitutedType, TypeApproximatorConfiguration.CapturedAndIntegerLiteralsTypesApproximation)
?: substitutedType
}
return approximatedType as? KotlinType ?: type
}
}
val substitutedDescriptor = substitute(TypeSubstitutor.create(wrappedSubstitution)) as D
return if (anyChanges) substitutedDescriptor else this
return substitute(TypeSubstitutor.create(wrappedSubstitution))
}
internal fun <E> MutableList<E>.trimToSize(newSize: Int) = subList(newSize, size).clear()
internal fun <E> MutableList<E>.trimToSize(newSize: Int) = subList(newSize, size).clear()
@@ -21,10 +21,14 @@ import org.jetbrains.kotlin.config.LanguageFeature
import org.jetbrains.kotlin.config.LanguageVersionSettings
import org.jetbrains.kotlin.resolve.calls.NewCommonSuperTypeCalculator
import org.jetbrains.kotlin.resolve.calls.components.ClassicTypeSystemContextForCS
import org.jetbrains.kotlin.resolve.calls.inference.model.TypeVariableTypeConstructor
import org.jetbrains.kotlin.resolve.constants.IntegerLiteralTypeConstructor
import org.jetbrains.kotlin.types.TypeApproximatorConfiguration.IntersectionStrategy.*
import org.jetbrains.kotlin.types.checker.NewCapturedType
import org.jetbrains.kotlin.types.checker.NewCapturedTypeConstructor
import org.jetbrains.kotlin.types.model.*
import org.jetbrains.kotlin.types.model.CaptureStatus.*
import org.jetbrains.kotlin.types.typeUtil.builtIns
open class TypeApproximatorConfiguration {
@@ -424,8 +428,6 @@ abstract class AbstractTypeApproximator(val ctx: TypeSystemInferenceExtensionCon
if (argument.isStarProjection()) continue
val argumentType = argument.getType()//.unwrap()
val argumentTypeIsNullable = argumentType.asSimpleType()?.isMarkedNullable() ?: false
val effectiveVariance = AbstractTypeChecker.effectiveVariance(parameter.getVariance(), argument.getVariance())
when (effectiveVariance) {
null -> {
@@ -489,7 +491,7 @@ abstract class AbstractTypeApproximator(val ctx: TypeSystemInferenceExtensionCon
*/
if (argumentType.typeConstructor() is NewCapturedTypeConstructor) {
val subType = approximateToSubType(argumentType, conf, depth) ?: continue@loop
if (!subType.isTrivialSub(argumentTypeIsNullable)) {
if (!subType.isTrivialSub()) {
newArguments[index] = createTypeArgument(subType, TypeVariance.IN)
continue@loop
}
@@ -500,7 +502,7 @@ abstract class AbstractTypeApproximator(val ctx: TypeSystemInferenceExtensionCon
if (approximatedSuperType.isTrivialSuper()) {
val approximatedSubType =
approximateToSubType(argumentType, conf, depth) ?: continue@loop // seems like this is never null
if (!approximatedSubType.isTrivialSub(argumentTypeIsNullable)) {
if (!approximatedSubType.isTrivialSub()) {
newArguments[index] = createTypeArgument(approximatedSubType, TypeVariance.IN)
continue@loop
}
@@ -529,12 +531,7 @@ abstract class AbstractTypeApproximator(val ctx: TypeSystemInferenceExtensionCon
private fun KotlinTypeMarker.isTrivialSuper() = upperBoundIfFlexible().isNullableAny()
// Nothing or Nothing!
private fun KotlinTypeMarker.isTrivialSub(originalIsNullable: Boolean) = lowerBoundIfFlexible().let {
if (originalIsNullable)
it.isNothing() || it.isNullableNothing()
else
it.isNothing()
}
private fun KotlinTypeMarker.isTrivialSub() = lowerBoundIfFlexible().isNothing()
}
//
//internal fun KotlinTypeMarker.typeDepth() =