Fix null Java type argument problem & add relevant test

This commit is contained in:
Mikhail Glukhikh
2019-03-01 12:10:05 +03:00
parent 7563a98999
commit fb788dc4c0
14 changed files with 52 additions and 18 deletions
@@ -68,7 +68,7 @@ internal fun FirTypeRef.toNotNullConeKotlinType(): ConeKotlinType =
else -> ConeKotlinErrorType("Unexpected type reference in JavaClassUseSiteScope: ${this::class.java}")
}
internal fun JavaType.toNotNullConeKotlinType(session: FirSession): ConeLookupTagBasedType {
internal fun JavaType?.toNotNullConeKotlinType(session: FirSession): ConeLookupTagBasedType {
return toConeKotlinTypeWithNullability(session, isNullable = false)
}
@@ -85,7 +85,7 @@ internal fun JavaClassifierType.toFirResolvedTypeRef(session: FirSession): FirRe
)
}
internal fun JavaType.toConeKotlinTypeWithNullability(session: FirSession, isNullable: Boolean): ConeLookupTagBasedType {
internal fun JavaType?.toConeKotlinTypeWithNullability(session: FirSession, isNullable: Boolean): ConeLookupTagBasedType {
return when (this) {
is JavaClassifierType -> {
toConeKotlinTypeWithNullability(session, isNullable)
@@ -118,6 +118,10 @@ internal fun JavaType.toConeKotlinTypeWithNullability(session: FirSession, isNul
val classId = ClassId(FqName("kotlin"), FqName("Any"), false)
classId.toConeKotlinType(emptyArray(), isNullable)
}
null -> {
val classId = ClassId(FqName("kotlin"), FqName("Any"), false)
classId.toConeKotlinType(emptyArray(), isNullable)
}
else -> error("Strange JavaType: ${this::class.java}")
}
}
@@ -172,8 +176,9 @@ internal fun FirAbstractAnnotatedElement.addAnnotationsFrom(javaAnnotationOwner:
}
}
internal fun JavaType.toConeProjection(session: FirSession, boundTypeParameter: FirTypeParameter?): ConeKotlinTypeProjection {
internal fun JavaType?.toConeProjection(session: FirSession, boundTypeParameter: FirTypeParameter?): ConeKotlinTypeProjection {
return when (this) {
null -> ConeStarProjection
is JavaWildcardType -> {
val bound = this.bound
val argumentVariance = if (isExtends) OUT_VARIANCE else IN_VARIANCE
@@ -95,8 +95,8 @@ internal class EnhancementSignatureParts(
if (type is FirJavaTypeRef) {
for (arg in type.type.typeArguments()) {
if (arg is JavaWildcardType) {
// TODO: wildcards
if (arg is JavaWildcardType || arg == null) {
// TODO: wildcards / raw types
} else {
add(arg.toFirJavaTypeRef(context.session))
}
@@ -46,14 +46,14 @@ internal fun FirJavaTypeRef.enhance(
// Example: for `A<B, C<D, E>>`, indices go as follows: `0 - A<...>, 1 - B, 2 - C<D, E>, 3 - D, 4 - E`,
// which corresponds to the left-to-right breadth-first walk of the tree representation of the type.
// For flexible types, both bounds are indexed in the same way: `(A<B>..C<D>)` gives `0 - (A<B>..C<D>), 1 - B and D`.
private fun JavaType.enhancePossiblyFlexible(
private fun JavaType?.enhancePossiblyFlexible(
session: FirSession,
annotations: List<FirAnnotationCall>,
qualifiers: (Int) -> JavaTypeQualifiers,
index: Int
): FirResolvedTypeRef {
val type = this
val arguments = typeArguments()
val arguments = this?.typeArguments().orEmpty()
return when (type) {
is JavaClassifierType -> {
val lowerResult = type.enhanceInflexibleType(
@@ -76,9 +76,9 @@ private fun JavaType.enhancePossiblyFlexible(
}
}
private fun JavaType.subtreeSize(): Int {
private fun JavaType?.subtreeSize(): Int {
if (this !is JavaClassifierType) return 1
return 1 + typeArguments.sumBy { it.subtreeSize() }
return 1 + typeArguments.sumBy { it?.subtreeSize() ?: 0 }
}
private val KOTLIN_COLLECTIONS = FqName("kotlin.collections")
@@ -102,7 +102,7 @@ private fun ClassId.mutableToReadOnly(): ClassId? {
private fun JavaClassifierType.enhanceInflexibleType(
session: FirSession,
annotations: List<FirAnnotationCall>,
arguments: List<JavaType>,
arguments: List<JavaType?>,
position: TypeComponentPosition,
qualifiers: (Int) -> JavaTypeQualifiers,
index: Int
@@ -199,7 +199,7 @@ internal data class TypeAndDefaultQualifiers(
internal fun FirTypeRef.typeArguments(): List<FirTypeProjection> =
(this as? FirUserTypeRef)?.qualifier?.lastOrNull()?.typeArguments.orEmpty()
internal fun JavaType.typeArguments(): List<JavaType> = (this as? JavaClassifierType)?.typeArguments.orEmpty()
internal fun JavaType.typeArguments(): List<JavaType?> = (this as? JavaClassifierType)?.typeArguments.orEmpty()
fun FirValueParameter.getDefaultValueFromAnnotation(): AnnotationDefaultValue? {
annotations.find { it.resolvedFqName == JvmAnnotationNames.DEFAULT_VALUE_FQ_NAME }
@@ -102,7 +102,7 @@ class JavaClassEnhancementScope(
original: ConeFunctionSymbol,
name: Name
): FirFunctionSymbol {
val firMethod = (original as FirBasedSymbol<*>).fir as? FirJavaMethod ?: error("Can't make enhancement for $original")
val firMethod = (original as FirFunctionSymbol).fir as? FirJavaMethod ?: return original
val memberContext = context.copyWithNewDefaultTypeQualifiers(typeQualifierResolver, jsr305State, firMethod.annotations)