Refactoring. Do not use SubtypingRepresentatives as TypeCapability.
This commit is contained in:
+21
-21
@@ -19,6 +19,7 @@ package org.jetbrains.kotlin.resolve.calls.inference
|
||||
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
|
||||
import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.annotations.Annotations
|
||||
import org.jetbrains.kotlin.resolve.scopes.MemberScope
|
||||
import org.jetbrains.kotlin.types.*
|
||||
import org.jetbrains.kotlin.types.Variance.IN_VARIANCE
|
||||
import org.jetbrains.kotlin.types.Variance.OUT_VARIANCE
|
||||
@@ -57,27 +58,19 @@ class CapturedTypeConstructor(
|
||||
}
|
||||
|
||||
class CapturedType(
|
||||
private val typeProjection: TypeProjection
|
||||
): DelegatingType(), SubtypingRepresentatives {
|
||||
private val typeProjection: TypeProjection,
|
||||
override val constructor: TypeConstructor = CapturedTypeConstructor(typeProjection),
|
||||
override val isMarkedNullable: Boolean = false,
|
||||
override val annotations: Annotations = Annotations.EMPTY
|
||||
): AbstractKotlinType(), SubtypingRepresentatives, TypeWithCustomReplacement {
|
||||
|
||||
private val delegateType = run {
|
||||
val scope = ErrorUtils.createErrorScope(
|
||||
override val arguments: List<TypeProjection>
|
||||
get() = listOf()
|
||||
override val memberScope: MemberScope
|
||||
get() = ErrorUtils.createErrorScope(
|
||||
"No member resolution should be done on captured type, it used only during constraint system resolution", true)
|
||||
KotlinTypeFactory.simpleType(Annotations.EMPTY, CapturedTypeConstructor(typeProjection), listOf(), false, scope)
|
||||
}
|
||||
|
||||
override fun getDelegate(): KotlinType = delegateType
|
||||
|
||||
override val capabilities: TypeCapabilities get() = object : TypeCapabilities {
|
||||
override fun <T : TypeCapability> getCapability(capabilityClass: Class<T>) =
|
||||
this@CapturedType.getCapability(capabilityClass)
|
||||
}
|
||||
|
||||
override fun <T : TypeCapability> getCapability(capabilityClass: Class<T>): T? {
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
return if (capabilityClass == SubtypingRepresentatives::class.java) this as T
|
||||
else super.getCapability(capabilityClass)
|
||||
}
|
||||
override val isError: Boolean
|
||||
get() = false
|
||||
|
||||
override val subTypeRepresentative: KotlinType
|
||||
get() = representative(OUT_VARIANCE, builtIns.nullableAnyType)
|
||||
@@ -88,9 +81,16 @@ class CapturedType(
|
||||
private fun representative(variance: Variance, default: KotlinType) =
|
||||
if (typeProjection.projectionKind == variance) typeProjection.type else default
|
||||
|
||||
override fun sameTypeConstructor(type: KotlinType) = delegateType.constructor === type.constructor
|
||||
override fun sameTypeConstructor(type: KotlinType) = constructor === type.constructor
|
||||
|
||||
override fun toString() = "Captured($typeProjection)"
|
||||
override fun toString() = "Captured($typeProjection)${if (isMarkedNullable) '?' else ""}"
|
||||
|
||||
override fun makeNullableAsSpecified(nullable: Boolean): KotlinType {
|
||||
if (nullable == isMarkedNullable) return this
|
||||
return CapturedType(typeProjection, constructor, nullable, annotations)
|
||||
}
|
||||
|
||||
override fun replaceAnnotations(newAnnotations: Annotations): KotlinType = CapturedType(typeProjection, constructor, isMarkedNullable, newAnnotations)
|
||||
}
|
||||
|
||||
fun createCapturedType(typeProjection: TypeProjection): KotlinType
|
||||
|
||||
@@ -56,6 +56,12 @@ fun KotlinType.unwrap(): KotlinType {
|
||||
|
||||
interface SimpleType : KotlinType
|
||||
|
||||
interface TypeWithCustomReplacement : KotlinType {
|
||||
fun makeNullableAsSpecified(nullable: Boolean): KotlinType
|
||||
|
||||
fun replaceAnnotations(newAnnotations: Annotations): KotlinType
|
||||
}
|
||||
|
||||
abstract class WrappedType() : KotlinType, LazyType {
|
||||
override val annotations: Annotations get() = delegate.annotations
|
||||
override val constructor: TypeConstructor get() = delegate.constructor
|
||||
|
||||
@@ -74,7 +74,7 @@ fun KotlinType.getCustomTypeVariable(): CustomTypeVariable? =
|
||||
if (it.isTypeVariable) it else null
|
||||
}
|
||||
|
||||
interface SubtypingRepresentatives : TypeCapability {
|
||||
interface SubtypingRepresentatives {
|
||||
val subTypeRepresentative: KotlinType
|
||||
val superTypeRepresentative: KotlinType
|
||||
|
||||
@@ -82,15 +82,14 @@ interface SubtypingRepresentatives : TypeCapability {
|
||||
}
|
||||
|
||||
fun KotlinType.getSubtypeRepresentative(): KotlinType =
|
||||
this.getCapability(SubtypingRepresentatives::class.java)?.subTypeRepresentative ?: this
|
||||
(unwrap() as? SubtypingRepresentatives)?.subTypeRepresentative ?: this
|
||||
|
||||
fun KotlinType.getSupertypeRepresentative(): KotlinType =
|
||||
this.getCapability(SubtypingRepresentatives::class.java)?.superTypeRepresentative ?: this
|
||||
(unwrap() as? SubtypingRepresentatives)?.superTypeRepresentative ?: this
|
||||
|
||||
fun sameTypeConstructors(first: KotlinType, second: KotlinType): Boolean {
|
||||
val typeRangeCapability = SubtypingRepresentatives::class.java
|
||||
return first.getCapability(typeRangeCapability)?.sameTypeConstructor(second) ?: false
|
||||
|| second.getCapability(typeRangeCapability)?.sameTypeConstructor(first) ?: false
|
||||
return (first.unwrap() as? SubtypingRepresentatives)?.sameTypeConstructor(second) ?: false
|
||||
|| (second.unwrap() as? SubtypingRepresentatives)?.sameTypeConstructor(first) ?: false
|
||||
}
|
||||
|
||||
interface AbbreviatedType : TypeCapability {
|
||||
|
||||
@@ -136,8 +136,8 @@ public class TypeUtils {
|
||||
@NotNull
|
||||
public static KotlinType makeNullableAsSpecified(@NotNull KotlinType type, boolean nullable) {
|
||||
KotlinType unwrappedType = KotlinTypeKt.unwrap(type);
|
||||
if (unwrappedType instanceof FlexibleType) {
|
||||
return ((FlexibleType) unwrappedType).makeNullableAsSpecified(nullable);
|
||||
if (unwrappedType instanceof TypeWithCustomReplacement) {
|
||||
return ((TypeWithCustomReplacement) unwrappedType).makeNullableAsSpecified(nullable);
|
||||
}
|
||||
else {
|
||||
SimpleType simpleType = (SimpleType) unwrappedType;
|
||||
|
||||
@@ -85,7 +85,8 @@ fun TypeProjection.substitute(doSubstitute: (KotlinType) -> KotlinType): TypePro
|
||||
|
||||
fun KotlinType.replaceAnnotations(newAnnotations: Annotations): KotlinType {
|
||||
if (annotations.isEmpty() && newAnnotations.isEmpty()) return this
|
||||
if (isFlexible()) return asFlexibleType().replaceAnnotations(newAnnotations)
|
||||
val unwrappedType = unwrap()
|
||||
if (unwrappedType is TypeWithCustomReplacement) return unwrappedType.replaceAnnotations(newAnnotations)
|
||||
|
||||
return asSimpleType().lazyReplaceAnnotations(newAnnotations)
|
||||
}
|
||||
|
||||
@@ -21,7 +21,7 @@ import org.jetbrains.kotlin.descriptors.annotations.Annotations
|
||||
import org.jetbrains.kotlin.types.checker.KotlinTypeChecker
|
||||
import org.jetbrains.kotlin.types.typeUtil.replaceAnnotations
|
||||
|
||||
interface FlexibleType : SubtypingRepresentatives {
|
||||
interface FlexibleType : SubtypingRepresentatives, TypeWithCustomReplacement {
|
||||
// lowerBound is a subtype of upperBound
|
||||
val lowerBound: SimpleType
|
||||
val upperBound: SimpleType
|
||||
@@ -33,10 +33,6 @@ interface FlexibleType : SubtypingRepresentatives {
|
||||
get() = upperBound
|
||||
|
||||
override fun sameTypeConstructor(type: KotlinType) = false
|
||||
|
||||
fun makeNullableAsSpecified(nullable: Boolean): KotlinType
|
||||
|
||||
fun replaceAnnotations(newAnnotations: Annotations): KotlinType
|
||||
}
|
||||
|
||||
fun KotlinType.isFlexible(): Boolean = unwrap() is FlexibleType
|
||||
@@ -115,14 +111,6 @@ abstract class DelegatingFlexibleType protected constructor(
|
||||
|
||||
protected abstract val delegateType: KotlinType
|
||||
|
||||
override fun <T : TypeCapability> getCapability(capabilityClass: Class<T>): T? {
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
return when(capabilityClass) {
|
||||
SubtypingRepresentatives::class.java -> this as T
|
||||
else -> super.getCapability(capabilityClass)
|
||||
}
|
||||
}
|
||||
|
||||
override fun makeNullableAsSpecified(nullable: Boolean): KotlinType {
|
||||
return KotlinTypeFactory.flexibleType(TypeUtils.makeNullableAsSpecified(lowerBound, nullable), TypeUtils.makeNullableAsSpecified(upperBound, nullable))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user