Skip to content

Commit

Permalink
Add isNullableCompat and superTypesCompat for backward compatibility …
Browse files Browse the repository at this point in the history
…with 2.1.0 and 2.1.10
  • Loading branch information
drewhamilton committed Jan 30, 2025
1 parent 32e6999 commit c62b27a
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@ import org.jetbrains.kotlin.ir.builders.IrBuilderWithScope
import org.jetbrains.kotlin.ir.builders.irCall
import org.jetbrains.kotlin.ir.expressions.IrCall
import org.jetbrains.kotlin.ir.expressions.IrStatementOrigin
import org.jetbrains.kotlin.ir.symbols.IrClassifierSymbol
import org.jetbrains.kotlin.ir.symbols.IrSimpleFunctionSymbol
import org.jetbrains.kotlin.ir.symbols.UnsafeDuringIrConstructionAPI
import org.jetbrains.kotlin.ir.types.IrType
import org.jetbrains.kotlin.ir.util.isNullable
import org.jetbrains.kotlin.ir.util.superTypes
import org.jetbrains.kotlin.ir.types.isNullable as isNullableDeprecated
import org.jetbrains.kotlin.ir.types.superTypes as superTypesDeprecated

/**
* Alias for [irCall] from 2.1.0 – 2.1.20.
Expand Down Expand Up @@ -55,3 +60,31 @@ internal fun IrBuilderWithScope.irCallCompat(
) as IrCall
}
}

/**
* Alias for [isNullable] from 2.1.0 – 2.1.20.
*
* Remove when support for 2.1.0 & 2.1.1x is dropped.
*/
internal fun IrType.isNullableCompat(): Boolean {
return try {
isNullable()
} catch (noSuchMethodError: NoSuchMethodError) {
@Suppress("DEPRECATION")
isNullableDeprecated()
}
}

/**
* Alias for [superTypes] from 2.1.0 – 2.1.20.
*
* Remove when support for 2.1.0 & 2.1.1x is dropped.
*/
internal fun IrClassifierSymbol.superTypesCompat(): List<IrType> {
return try {
superTypes()
} catch (noSuchMethodError: NoSuchMethodError) {
@Suppress("DEPRECATION")
superTypesDeprecated()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ import org.jetbrains.kotlin.ir.types.classifierOrFail
import org.jetbrains.kotlin.ir.types.classifierOrNull
import org.jetbrains.kotlin.ir.util.defaultType
import org.jetbrains.kotlin.ir.util.isArrayOrPrimitiveArray
import org.jetbrains.kotlin.ir.util.isNullable
import org.jetbrains.kotlin.ir.util.render
import org.jetbrains.kotlin.name.CallableId
import org.jetbrains.kotlin.name.ClassId
Expand Down Expand Up @@ -228,7 +227,7 @@ private fun findContentDeepEqualsFunctionSymbol(
// Find the single function with the relevant array type and disambiguate against the
// older non-nullable receiver overload:
functionSymbol.owner.extensionReceiverParameter?.type?.let {
it.classifierOrNull == classifier && it.isNullable()
it.classifierOrNull == classifier && it.isNullableCompat()
} ?: false
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import org.jetbrains.kotlin.ir.util.hasAnnotation
import org.jetbrains.kotlin.ir.util.isAnnotationClass
import org.jetbrains.kotlin.ir.util.isInterface
import org.jetbrains.kotlin.ir.util.render
import org.jetbrains.kotlin.ir.util.superTypes
import org.jetbrains.kotlin.name.ClassId

/**
Expand Down Expand Up @@ -79,7 +78,7 @@ internal fun IrClassifierSymbol?.mayBeRuntimeArray(
private fun IrTypeParameterSymbol.hasArrayOrPrimitiveArrayUpperBound(
context: IrGeneratorContextInterface,
): Boolean {
superTypes().forEach { superType ->
superTypesCompat().forEach { superType ->
val superTypeClassifier = superType.classifierOrNull
// Note: A generic type cannot have an array as an upper bound, else that would also
// be checked here.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ import org.jetbrains.kotlin.ir.types.isInt
import org.jetbrains.kotlin.ir.types.isUInt
import org.jetbrains.kotlin.ir.util.functions
import org.jetbrains.kotlin.ir.util.isArrayOrPrimitiveArray
import org.jetbrains.kotlin.ir.util.isNullable
import org.jetbrains.kotlin.ir.util.render
import org.jetbrains.kotlin.name.CallableId
import org.jetbrains.kotlin.name.ClassId
Expand Down Expand Up @@ -133,7 +132,7 @@ private fun IrBlockBodyBuilder.getHashCodeOfProperty(
val field = property.backingField!!
val irGetField = { irGetField(receiver(function), field) }
return when {
property.type.isNullable() -> irIfNull(
property.type.isNullableCompat() -> irIfNull(
type = context.irBuiltIns.intType,
subject = irGetField(),
thenPart = irInt(0),
Expand Down Expand Up @@ -293,7 +292,7 @@ private fun findArrayContentDeepHashCodeFunction(
).single { functionSymbol ->
// Disambiguate against the older non-nullable receiver overload:
functionSymbol.owner.extensionReceiverParameter?.type?.let {
it.classifierOrNull == propertyClassifier && it.isNullable()
it.classifierOrNull == propertyClassifier && it.isNullableCompat()
} ?: false
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import org.jetbrains.kotlin.ir.symbols.UnsafeDuringIrConstructionAPI
import org.jetbrains.kotlin.ir.types.classifierOrFail
import org.jetbrains.kotlin.ir.types.classifierOrNull
import org.jetbrains.kotlin.ir.util.isArrayOrPrimitiveArray
import org.jetbrains.kotlin.ir.util.isNullable
import org.jetbrains.kotlin.ir.util.render
import org.jetbrains.kotlin.name.CallableId
import org.jetbrains.kotlin.name.ClassId
Expand Down Expand Up @@ -201,7 +200,7 @@ private fun findContentDeepToStringFunctionSymbol(
// Find the single function with the relevant array type and disambiguate against the
// older non-nullable receiver overload:
functionSymbol.owner.extensionReceiverParameter?.type?.let {
it.classifierOrNull == propertyClassifier && it.isNullable()
it.classifierOrNull == propertyClassifier && it.isNullableCompat()
} ?: false
}
}
Expand Down

0 comments on commit c62b27a

Please sign in to comment.