KT-6815 Representing raw types when used as supertypes for Java classes
#KT-6815 Fixed
This commit is contained in:
+11
-13
@@ -37,6 +37,7 @@ import org.jetbrains.kotlin.descriptors.annotations.Annotations
|
||||
import org.jetbrains.kotlin.load.java.JvmAnnotationNames
|
||||
import kotlin.platform.platformStatic
|
||||
import org.jetbrains.kotlin.types.typeUtil.replaceAnnotations
|
||||
import kotlin.properties.*
|
||||
|
||||
class LazyJavaTypeResolver(
|
||||
private val c: LazyJavaResolverContext,
|
||||
@@ -89,17 +90,14 @@ class LazyJavaTypeResolver(
|
||||
}.replaceAnnotations(attr.annotations)
|
||||
}
|
||||
|
||||
private class LazyStarProjection(
|
||||
val typeParameter: TypeParameterDescriptor,
|
||||
val attr: JavaTypeAttributes
|
||||
) : TypeProjectionBase() {
|
||||
override fun isStarProjection() = true
|
||||
|
||||
override fun getProjectionKind() =
|
||||
// projections are not allowed in immediate arguments of supertypes
|
||||
if (typeParameter.getVariance() == OUT_VARIANCE || attr.howThisTypeIsUsed == SUPERTYPE) INVARIANT else OUT_VARIANCE
|
||||
|
||||
override fun getType() = typeParameter.getUpperBoundsAsType()
|
||||
fun makeStarProjection(
|
||||
typeParameter: TypeParameterDescriptor,
|
||||
attr: JavaTypeAttributes
|
||||
): TypeProjection {
|
||||
return if (attr.howThisTypeIsUsed == SUPERTYPE)
|
||||
TypeProjectionImpl(typeParameter.starProjectionType())
|
||||
else
|
||||
StarProjectionImpl(typeParameter)
|
||||
}
|
||||
|
||||
private inner class LazyJavaClassifierType(
|
||||
@@ -197,7 +195,7 @@ class LazyJavaTypeResolver(
|
||||
TypeProjectionImpl(projectionKind, KotlinBuiltIns.getInstance().getNullableAnyType())
|
||||
}
|
||||
else
|
||||
LazyStarProjection(parameter, attr)
|
||||
makeStarProjection(parameter, attr)
|
||||
}
|
||||
}
|
||||
if (isConstructorTypeParameter()) {
|
||||
@@ -228,7 +226,7 @@ class LazyJavaTypeResolver(
|
||||
is JavaWildcardType -> {
|
||||
val bound = javaType.getBound()
|
||||
if (bound == null)
|
||||
LazyStarProjection(typeParameter, attr)
|
||||
makeStarProjection(typeParameter, attr)
|
||||
else {
|
||||
var projectionKind = if (javaType.isExtends()) OUT_VARIANCE else IN_VARIANCE
|
||||
if (projectionKind == typeParameter.getVariance()) {
|
||||
|
||||
@@ -26,24 +26,28 @@ class StarProjectionImpl(
|
||||
|
||||
override fun getProjectionKind() = Variance.OUT_VARIANCE
|
||||
|
||||
// No synchronization here: there's no problem in accidentally computing this twice
|
||||
private val _type: JetType by Delegates.lazy {
|
||||
val classDescriptor = typeParameter.getContainingDeclaration() as ClassDescriptor
|
||||
val typeParameters = classDescriptor.getTypeConstructor().getParameters().map { it.getTypeConstructor() }
|
||||
TypeSubstitutor.create(
|
||||
object : TypeSubstitution() {
|
||||
override fun get(key: TypeConstructor) = when (key) {
|
||||
typeParameter.getTypeConstructor() -> this@StarProjectionImpl
|
||||
in typeParameters -> TypeUtils.makeStarProjection(key.getDeclarationDescriptor() as TypeParameterDescriptor)
|
||||
else -> null
|
||||
}
|
||||
|
||||
}
|
||||
).substitute(typeParameter.getUpperBounds().iterator().next(), Variance.OUT_VARIANCE)!!
|
||||
typeParameter.starProjectionType()
|
||||
}
|
||||
|
||||
override fun getType() = _type
|
||||
}
|
||||
|
||||
public fun TypeParameterDescriptor.starProjectionType(): JetType {
|
||||
val classDescriptor = this.getContainingDeclaration() as ClassDescriptor
|
||||
val typeParameters = classDescriptor.getTypeConstructor().getParameters().map { it.getTypeConstructor() }
|
||||
return TypeSubstitutor.create(
|
||||
object : TypeSubstitution() {
|
||||
override fun get(key: TypeConstructor) =
|
||||
if (key in typeParameters)
|
||||
TypeUtils.makeStarProjection(key.getDeclarationDescriptor() as TypeParameterDescriptor)
|
||||
else null
|
||||
|
||||
}
|
||||
).substitute(this.getUpperBounds().first(), Variance.OUT_VARIANCE)!!
|
||||
}
|
||||
|
||||
class TypeBasedStarProjectionImpl(
|
||||
private val _type: JetType
|
||||
) : TypeProjectionBase() {
|
||||
|
||||
@@ -210,12 +210,12 @@ public class TypeSubstitutor {
|
||||
}
|
||||
JetType substitutedType;
|
||||
CustomTypeVariable typeVariable = TypesPackage.getCustomTypeVariable(type);
|
||||
if (typeVariable != null) {
|
||||
substitutedType = typeVariable.substitutionResult(replacement.getType());
|
||||
}
|
||||
else if (replacement.isStarProjection()) {
|
||||
if (replacement.isStarProjection()) {
|
||||
return replacement;
|
||||
}
|
||||
else if (typeVariable != null) {
|
||||
substitutedType = typeVariable.substitutionResult(replacement.getType());
|
||||
}
|
||||
else {
|
||||
// this is a simple type T or T?: if it's T, we should just take replacement, if T? - we make replacement nullable
|
||||
substitutedType = TypeUtils.makeNullableIfNeeded(replacement.getType(), type.isMarkedNullable());
|
||||
|
||||
Reference in New Issue
Block a user