Fix compatibility of CapturedTypeConstructor and NewCapturedTypeConstructor

Now both of those classes implements one interface with `TypeProjection`
  property. That allows old captured type approximator use new captured types.

That change fixes tests related to diagnostics:
- SETTER_PROJECTED_OUT
- DEBUG_INFO_UNRESOLVED_WITH_TARGET
- UNRESOLVED_REFERENCE_WRONG_RECEIVER

Also `typeProjection` property renamed to `projection` according to naming in NI.
This commit is contained in:
Dmitriy Novozhilov
2019-01-21 14:53:26 +03:00
parent 42b333f6fd
commit 21f0557502
15 changed files with 53 additions and 48 deletions
@@ -27,22 +27,26 @@ import org.jetbrains.kotlin.types.Variance.OUT_VARIANCE
import org.jetbrains.kotlin.types.checker.NewCapturedTypeConstructor
import org.jetbrains.kotlin.types.typeUtil.builtIns
class CapturedTypeConstructor(
val typeProjection: TypeProjection
) : TypeConstructor {
interface CapturedTypeConstructor : TypeConstructor {
val projection: TypeProjection
}
class CapturedTypeConstructorImpl(
override val projection: TypeProjection
) : CapturedTypeConstructor {
var newTypeConstructor: NewCapturedTypeConstructor? = null
init {
assert(typeProjection.projectionKind != Variance.INVARIANT) {
"Only nontrivial projections can be captured, not: $typeProjection"
assert(projection.projectionKind != Variance.INVARIANT) {
"Only nontrivial projections can be captured, not: $projection"
}
}
override fun getParameters(): List<TypeParameterDescriptor> = listOf()
override fun getSupertypes(): Collection<KotlinType> {
val superType = if (typeProjection.projectionKind == Variance.OUT_VARIANCE)
typeProjection.type
val superType = if (projection.projectionKind == Variance.OUT_VARIANCE)
projection.type
else
builtIns.nullableAnyType
return listOf(superType)
@@ -54,14 +58,14 @@ class CapturedTypeConstructor(
override fun getDeclarationDescriptor() = null
override fun toString() = "CapturedTypeConstructor($typeProjection)"
override fun toString() = "CapturedTypeConstructor($projection)"
override fun getBuiltIns(): KotlinBuiltIns = typeProjection.type.constructor.builtIns
override fun getBuiltIns(): KotlinBuiltIns = projection.type.constructor.builtIns
}
class CapturedType(
val typeProjection: TypeProjection,
override val constructor: CapturedTypeConstructor = CapturedTypeConstructor(typeProjection),
override val constructor: CapturedTypeConstructor = CapturedTypeConstructorImpl(typeProjection),
override val isMarkedNullable: Boolean = false,
override val annotations: Annotations = Annotations.EMPTY
) : SimpleType(), SubtypingRepresentatives {
@@ -98,10 +98,10 @@ private fun substituteCapturedTypesWithProjections(typeProjection: TypeProjectio
val typeSubstitutor = TypeSubstitutor.create(object : TypeConstructorSubstitution() {
override fun get(key: TypeConstructor): TypeProjection? {
val capturedTypeConstructor = key as? CapturedTypeConstructor ?: return null
if (capturedTypeConstructor.typeProjection.isStarProjection) {
return TypeProjectionImpl(Variance.OUT_VARIANCE, capturedTypeConstructor.typeProjection.type)
if (capturedTypeConstructor.projection.isStarProjection) {
return TypeProjectionImpl(Variance.OUT_VARIANCE, capturedTypeConstructor.projection.type)
}
return capturedTypeConstructor.typeProjection
return capturedTypeConstructor.projection
}
})
return typeSubstitutor.substituteWithoutApproximation(typeProjection)
@@ -127,7 +127,7 @@ fun approximateCapturedTypes(type: KotlinType): ApproximationBounds<KotlinType>
val typeConstructor = type.constructor
if (type.isCaptured()) {
val typeProjection = (typeConstructor as CapturedTypeConstructor).typeProjection
val typeProjection = (typeConstructor as CapturedTypeConstructor).projection
fun KotlinType.makeNullableIfNeeded() = TypeUtils.makeNullableIfNeeded(this, type.isMarkedNullable)
val bound = typeProjection.type.makeNullableIfNeeded()
@@ -20,6 +20,7 @@ import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.descriptors.ClassifierDescriptor
import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor
import org.jetbrains.kotlin.descriptors.annotations.Annotations
import org.jetbrains.kotlin.resolve.calls.inference.CapturedTypeConstructor
import org.jetbrains.kotlin.resolve.scopes.MemberScope
import org.jetbrains.kotlin.types.*
import org.jetbrains.kotlin.types.typeUtil.asTypeProjection
@@ -145,7 +146,7 @@ class NewCapturedType(
NewCapturedType(captureStatus, constructor, lowerType, annotations, newNullability)
}
class NewCapturedTypeConstructor(val projection: TypeProjection, private var supertypes: List<UnwrappedType>? = null) : TypeConstructor {
class NewCapturedTypeConstructor(override val projection: TypeProjection, private var supertypes: List<UnwrappedType>? = null) : CapturedTypeConstructor {
fun initializeSupertypes(supertypes: List<UnwrappedType>) {
assert(this.supertypes == null) {
"Already initialized! oldValue = ${this.supertypes}, newValue = $supertypes"
@@ -22,7 +22,7 @@ import org.jetbrains.kotlin.descriptors.ClassKind
import org.jetbrains.kotlin.descriptors.TypeAliasDescriptor
import org.jetbrains.kotlin.descriptors.isFinalClass
import org.jetbrains.kotlin.resolve.calls.inference.CapturedType
import org.jetbrains.kotlin.resolve.calls.inference.CapturedTypeConstructor
import org.jetbrains.kotlin.resolve.calls.inference.CapturedTypeConstructorImpl
import org.jetbrains.kotlin.resolve.constants.IntegerValueTypeConstructor
import org.jetbrains.kotlin.types.*
import org.jetbrains.kotlin.types.checker.TypeCheckerContext.LowerCapturedTypePolicy.*
@@ -136,12 +136,12 @@ object NewKotlinTypeChecker : KotlinTypeChecker {
val constructor = type.constructor
when (constructor) {
// Type itself can be just SimpleTypeImpl, not CapturedType. see KT-16147
is CapturedTypeConstructor -> {
val lowerType = constructor.typeProjection.takeIf { it.projectionKind == Variance.IN_VARIANCE }?.type?.unwrap()
is CapturedTypeConstructorImpl -> {
val lowerType = constructor.projection.takeIf { it.projectionKind == Variance.IN_VARIANCE }?.type?.unwrap()
// it is incorrect calculate this type directly because of recursive star projections
if (constructor.newTypeConstructor == null) {
constructor.newTypeConstructor = NewCapturedTypeConstructor(constructor.typeProjection, constructor.supertypes.map { it.unwrap() })
constructor.newTypeConstructor = NewCapturedTypeConstructor(constructor.projection, constructor.supertypes.map { it.unwrap() })
}
return NewCapturedType(CaptureStatus.FOR_SUBTYPING, constructor.newTypeConstructor!!,
lowerType, type.annotations, type.isMarkedNullable)