Merge pull request #13148 from igfoo/igfoo/arrays

Kotlin: Add some documentation on arrays, and tweak the tests we use for them
This commit is contained in:
Ian Lynagh
2023-05-12 18:52:16 +01:00
committed by GitHub

View File

@@ -239,8 +239,6 @@ open class KotlinUsesExtractor(
return UseClassInstanceResult(classTypeResult, extractClass) return UseClassInstanceResult(classTypeResult, extractClass)
} }
private fun isArray(t: IrSimpleType) = t.isBoxedArray || t.isPrimitiveArray()
private fun extractClassLaterIfExternal(c: IrClass) { private fun extractClassLaterIfExternal(c: IrClass) {
if (isExternalDeclaration(c)) { if (isExternalDeclaration(c)) {
extractExternalClassLater(c) extractExternalClassLater(c)
@@ -551,6 +549,22 @@ open class KotlinUsesExtractor(
) )
} }
/*
Kotlin arrays can be broken down as:
isArray(t)
|- t.isBoxedArray
| |- t.isArray() e.g. Array<Boolean>, Array<Boolean?>
| |- t.isNullableArray() e.g. Array<Boolean>?, Array<Boolean?>?
|- t.isPrimitiveArray() e.g. BooleanArray
For the corresponding Java types:
Boxed arrays are represented as e.g. java.lang.Boolean[].
Primitive arrays are represented as e.g. boolean[].
*/
private fun isArray(t: IrType) = t.isBoxedArray || t.isPrimitiveArray()
data class ArrayInfo(val elementTypeResults: TypeResults, data class ArrayInfo(val elementTypeResults: TypeResults,
val componentTypeResults: TypeResults, val componentTypeResults: TypeResults,
val dimensions: Int) val dimensions: Int)
@@ -565,7 +579,7 @@ open class KotlinUsesExtractor(
*/ */
private fun useArrayType(t: IrType, isPrimitiveArray: Boolean): ArrayInfo { private fun useArrayType(t: IrType, isPrimitiveArray: Boolean): ArrayInfo {
if (!t.isBoxedArray && !t.isPrimitiveArray()) { if (!isArray(t)) {
val nullableT = if (t.isPrimitiveType() && !isPrimitiveArray) t.makeNullable() else t val nullableT = if (t.isPrimitiveType() && !isPrimitiveArray) t.makeNullable() else t
val typeResults = useType(nullableT) val typeResults = useType(nullableT)
return ArrayInfo(typeResults, typeResults, 0) return ArrayInfo(typeResults, typeResults, 0)
@@ -1141,13 +1155,13 @@ open class KotlinUsesExtractor(
} }
} else { } else {
t.classOrNull?.let { tCls -> t.classOrNull?.let { tCls ->
if (t.isArray() || t.isNullableArray()) { if (t.isBoxedArray) {
(t.arguments.singleOrNull() as? IrTypeProjection)?.let { elementTypeArg -> (t.arguments.singleOrNull() as? IrTypeProjection)?.let { elementTypeArg ->
val elementType = elementTypeArg.type val elementType = elementTypeArg.type
val replacedElementType = kClassToJavaClass(elementType) val replacedElementType = kClassToJavaClass(elementType)
if (replacedElementType !== elementType) { if (replacedElementType !== elementType) {
val newArg = makeTypeProjection(replacedElementType, elementTypeArg.variance) val newArg = makeTypeProjection(replacedElementType, elementTypeArg.variance)
return tCls.typeWithArguments(listOf(newArg)).codeQlWithHasQuestionMark(t.isNullableArray()) return tCls.typeWithArguments(listOf(newArg)).codeQlWithHasQuestionMark(t.isNullable())
} }
} }
} }
@@ -1578,7 +1592,7 @@ open class KotlinUsesExtractor(
} }
if (owner is IrClass) { if (owner is IrClass) {
if (t.isArray() || t.isNullableArray()) { if (t.isBoxedArray) {
val elementType = t.getArrayElementType(pluginContext.irBuiltIns) val elementType = t.getArrayElementType(pluginContext.irBuiltIns)
val erasedElementType = erase(elementType) val erasedElementType = erase(elementType)
return owner.typeWith(erasedElementType).codeQlWithHasQuestionMark(t.isNullable()) return owner.typeWith(erasedElementType).codeQlWithHasQuestionMark(t.isNullable())