Drop TypeUsage::MEMBER_SIGNATURE_COVARIANT
It's only used for choosing mutability of a collection class, but using annotations while loading is redundant since there is a separate phase of type enhancement where they're taken into account (for flexible types) For inflexible type it doesn't matter since they appear only in special cases like annotation methods (where collection classes can't be met)
This commit is contained in:
-1
@@ -21,7 +21,6 @@ package org.jetbrains.kotlin.load.java.components;
|
|||||||
* This enum encodes the kinds of occurrences
|
* This enum encodes the kinds of occurrences
|
||||||
*/
|
*/
|
||||||
public enum TypeUsage {
|
public enum TypeUsage {
|
||||||
MEMBER_SIGNATURE_COVARIANT,
|
|
||||||
MEMBER_SIGNATURE_CONTRAVARIANT,
|
MEMBER_SIGNATURE_CONTRAVARIANT,
|
||||||
SUPERTYPE,
|
SUPERTYPE,
|
||||||
COMMON
|
COMMON
|
||||||
|
|||||||
+1
-1
@@ -146,7 +146,7 @@ abstract class LazyJavaScope(protected val c: LazyJavaResolverContext) : MemberS
|
|||||||
protected fun computeMethodReturnType(method: JavaMethod, annotations: Annotations, c: LazyJavaResolverContext): KotlinType {
|
protected fun computeMethodReturnType(method: JavaMethod, annotations: Annotations, c: LazyJavaResolverContext): KotlinType {
|
||||||
val annotationMethod = method.containingClass.isAnnotationType
|
val annotationMethod = method.containingClass.isAnnotationType
|
||||||
val returnTypeAttrs = LazyJavaTypeAttributes(
|
val returnTypeAttrs = LazyJavaTypeAttributes(
|
||||||
TypeUsage.MEMBER_SIGNATURE_COVARIANT, annotations,
|
TypeUsage.COMMON, annotations,
|
||||||
allowFlexible = !annotationMethod,
|
allowFlexible = !annotationMethod,
|
||||||
isForAnnotationParameter = annotationMethod
|
isForAnnotationParameter = annotationMethod
|
||||||
)
|
)
|
||||||
|
|||||||
+5
-28
@@ -22,7 +22,8 @@ import org.jetbrains.kotlin.descriptors.annotations.Annotations
|
|||||||
import org.jetbrains.kotlin.descriptors.annotations.CompositeAnnotations
|
import org.jetbrains.kotlin.descriptors.annotations.CompositeAnnotations
|
||||||
import org.jetbrains.kotlin.descriptors.annotations.FilteredAnnotations
|
import org.jetbrains.kotlin.descriptors.annotations.FilteredAnnotations
|
||||||
import org.jetbrains.kotlin.load.java.ANNOTATIONS_COPIED_TO_TYPES
|
import org.jetbrains.kotlin.load.java.ANNOTATIONS_COPIED_TO_TYPES
|
||||||
import org.jetbrains.kotlin.load.java.JvmAnnotationNames.*
|
import org.jetbrains.kotlin.load.java.JvmAnnotationNames.JETBRAINS_NOT_NULL_ANNOTATION
|
||||||
|
import org.jetbrains.kotlin.load.java.JvmAnnotationNames.JETBRAINS_NULLABLE_ANNOTATION
|
||||||
import org.jetbrains.kotlin.load.java.components.TypeUsage
|
import org.jetbrains.kotlin.load.java.components.TypeUsage
|
||||||
import org.jetbrains.kotlin.load.java.components.TypeUsage.*
|
import org.jetbrains.kotlin.load.java.components.TypeUsage.*
|
||||||
import org.jetbrains.kotlin.load.java.lazy.LazyJavaAnnotations
|
import org.jetbrains.kotlin.load.java.lazy.LazyJavaAnnotations
|
||||||
@@ -150,24 +151,12 @@ class JavaTypeResolver(
|
|||||||
|
|
||||||
val javaToKotlin = JavaToKotlinClassMap
|
val javaToKotlin = JavaToKotlinClassMap
|
||||||
|
|
||||||
val howThisTypeIsUsedEffectively = when {
|
|
||||||
attr.flexibility == FLEXIBLE_LOWER_BOUND -> MEMBER_SIGNATURE_COVARIANT
|
|
||||||
attr.flexibility == FLEXIBLE_UPPER_BOUND -> MEMBER_SIGNATURE_CONTRAVARIANT
|
|
||||||
|
|
||||||
// This case has to be checked before isMarkedReadOnly/isMarkedMutable, because those two are slow
|
|
||||||
// not mapped, we don't care about being marked mutable/read-only
|
|
||||||
!javaToKotlin.isJavaPlatformClass(fqName) -> attr.howThisTypeIsUsed
|
|
||||||
|
|
||||||
// Read (possibly external) annotations
|
|
||||||
else -> attr.howThisTypeIsUsedAccordingToAnnotations
|
|
||||||
}
|
|
||||||
|
|
||||||
val kotlinDescriptor = javaToKotlin.mapJavaToKotlin(fqName, c.module.builtIns) ?: return null
|
val kotlinDescriptor = javaToKotlin.mapJavaToKotlin(fqName, c.module.builtIns) ?: return null
|
||||||
|
|
||||||
if (javaToKotlin.isReadOnly(kotlinDescriptor)) {
|
if (javaToKotlin.isReadOnly(kotlinDescriptor)) {
|
||||||
if (howThisTypeIsUsedEffectively == MEMBER_SIGNATURE_COVARIANT
|
if (attr.flexibility == FLEXIBLE_LOWER_BOUND ||
|
||||||
|| howThisTypeIsUsedEffectively == SUPERTYPE
|
attr.howThisTypeIsUsed == SUPERTYPE ||
|
||||||
|| javaType.argumentsMakeSenseOnlyForMutableContainer(readOnlyContainer = kotlinDescriptor)) {
|
javaType.argumentsMakeSenseOnlyForMutableContainer(readOnlyContainer = kotlinDescriptor)) {
|
||||||
return javaToKotlin.convertReadOnlyToMutable(kotlinDescriptor)
|
return javaToKotlin.convertReadOnlyToMutable(kotlinDescriptor)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -293,7 +282,6 @@ internal fun makeStarProjection(
|
|||||||
|
|
||||||
interface JavaTypeAttributes {
|
interface JavaTypeAttributes {
|
||||||
val howThisTypeIsUsed: TypeUsage
|
val howThisTypeIsUsed: TypeUsage
|
||||||
val howThisTypeIsUsedAccordingToAnnotations: TypeUsage
|
|
||||||
val isMarkedNotNull: Boolean
|
val isMarkedNotNull: Boolean
|
||||||
val flexibility: JavaTypeFlexibility
|
val flexibility: JavaTypeFlexibility
|
||||||
get() = INFLEXIBLE
|
get() = INFLEXIBLE
|
||||||
@@ -328,16 +316,7 @@ class LazyJavaTypeAttributes(
|
|||||||
override val isForAnnotationParameter: Boolean = false
|
override val isForAnnotationParameter: Boolean = false
|
||||||
): JavaTypeAttributes {
|
): JavaTypeAttributes {
|
||||||
override val typeAnnotations = FilteredAnnotations(annotations) { it in ANNOTATIONS_COPIED_TO_TYPES }
|
override val typeAnnotations = FilteredAnnotations(annotations) { it in ANNOTATIONS_COPIED_TO_TYPES }
|
||||||
|
|
||||||
override val howThisTypeIsUsedAccordingToAnnotations: TypeUsage get() =
|
|
||||||
if (hasAnnotation(JETBRAINS_READONLY_ANNOTATION) && !hasAnnotation(JETBRAINS_MUTABLE_ANNOTATION))
|
|
||||||
TypeUsage.MEMBER_SIGNATURE_CONTRAVARIANT
|
|
||||||
else
|
|
||||||
TypeUsage.MEMBER_SIGNATURE_COVARIANT
|
|
||||||
|
|
||||||
override val isMarkedNotNull: Boolean get() = typeAnnotations.isMarkedNotNull()
|
override val isMarkedNotNull: Boolean get() = typeAnnotations.isMarkedNotNull()
|
||||||
|
|
||||||
private fun hasAnnotation(fqName: FqName) = typeAnnotations.findAnnotation(fqName) != null
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun Annotations.isMarkedNotNull() = findAnnotation(JETBRAINS_NOT_NULL_ANNOTATION) != null
|
fun Annotations.isMarkedNotNull() = findAnnotation(JETBRAINS_NOT_NULL_ANNOTATION) != null
|
||||||
@@ -349,8 +328,6 @@ fun TypeUsage.toAttributes(
|
|||||||
upperBoundForTypeParameter: TypeParameterDescriptor? = null
|
upperBoundForTypeParameter: TypeParameterDescriptor? = null
|
||||||
) = object : JavaTypeAttributes {
|
) = object : JavaTypeAttributes {
|
||||||
override val howThisTypeIsUsed: TypeUsage = this@toAttributes
|
override val howThisTypeIsUsed: TypeUsage = this@toAttributes
|
||||||
override val howThisTypeIsUsedAccordingToAnnotations: TypeUsage
|
|
||||||
get() = howThisTypeIsUsed
|
|
||||||
override val isMarkedNotNull: Boolean = false
|
override val isMarkedNotNull: Boolean = false
|
||||||
override val allowFlexible: Boolean = allowFlexible
|
override val allowFlexible: Boolean = allowFlexible
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user