Skip to content

Commit 595a66a

Browse files
committed
Fix extraction of primitive-typed arrays
1 parent e1c43c6 commit 595a66a

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

java/kotlin-extractor/src/main/kotlin/KotlinFileExtractor.kt

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2613,8 +2613,12 @@ open class KotlinFileExtractor(
26132613
|| isBuiltinCallKotlin(c, "byteArrayOf")
26142614
|| isBuiltinCallKotlin(c, "booleanArrayOf") -> {
26152615

2616-
// TODO: is there any reason not to always use getArrayElementType?
2617-
val elementType = if (isBuiltinCallKotlin(c, "arrayOf")) {
2616+
2617+
val isPrimitiveArrayCreation = !isBuiltinCallKotlin(c, "arrayOf")
2618+
val elementType = if (isPrimitiveArrayCreation) {
2619+
c.type.getArrayElementType(pluginContext.irBuiltIns)
2620+
} else {
2621+
// TODO: is there any reason not to always use getArrayElementType?
26182622
if (c.typeArgumentsCount == 1) {
26192623
c.getTypeArgument(0).also {
26202624
if (it == null) {
@@ -2625,8 +2629,6 @@ open class KotlinFileExtractor(
26252629
logger.errorElement("Expected to find one type argument in arrayOf call", c)
26262630
null
26272631
}
2628-
} else {
2629-
c.type.getArrayElementType(pluginContext.irBuiltIns)
26302632
}
26312633

26322634
val arg = if (c.valueArgumentsCount == 1) c.getValueArgument(0) else {
@@ -2639,7 +2641,7 @@ open class KotlinFileExtractor(
26392641
}
26402642
}
26412643

2642-
extractArrayCreation(arg, c.type, elementType, c, parent, idx, callable, enclosingStmt)
2644+
extractArrayCreation(arg, c.type, elementType, isPrimitiveArrayCreation, c, parent, idx, callable, enclosingStmt)
26432645
}
26442646
isBuiltinCall(c, "<get-java>", "kotlin.jvm") -> {
26452647
// Special case for KClass<*>.java, which is used in the Parcelize plugin. In normal cases, this is already rewritten to the property referenced below:
@@ -2825,7 +2827,7 @@ open class KotlinFileExtractor(
28252827
}
28262828
}
28272829

2828-
private fun extractArrayCreation(elementList: IrVararg?, resultType: IrType, elementType: IrType?, locElement: IrElement, parent: Label<out DbExprparent>, idx: Int, enclosingCallable: Label<out DbCallable>, enclosingStmt: Label<out DbStmt>) {
2830+
private fun extractArrayCreation(elementList: IrVararg?, resultType: IrType, elementType: IrType?, allowPrimitiveElementType: Boolean, locElement: IrElement, parent: Label<out DbExprparent>, idx: Int, enclosingCallable: Label<out DbCallable>, enclosingStmt: Label<out DbStmt>) {
28292831
// If this is [someType]ArrayOf(*x), x, otherwise null
28302832
val clonedArray = elementList?.let {
28312833
if (it.elements.size == 1) {
@@ -2852,7 +2854,8 @@ open class KotlinFileExtractor(
28522854
tw.writeCallableEnclosingExpr(id, enclosingCallable)
28532855

28542856
if (elementType != null) {
2855-
extractTypeAccessRecursive(elementType, locId, id, -1, enclosingCallable, enclosingStmt, TypeContext.GENERIC_ARGUMENT)
2857+
val typeContext = if (allowPrimitiveElementType) TypeContext.OTHER else TypeContext.GENERIC_ARGUMENT
2858+
extractTypeAccessRecursive(elementType, locId, id, -1, enclosingCallable, enclosingStmt, typeContext)
28562859
}
28572860

28582861
if (elementList != null) {
@@ -3657,7 +3660,7 @@ open class KotlinFileExtractor(
36573660
// This AST element can also occur as a collection literal in an annotation class, such as
36583661
// annotation class Ann(val strings: Array<String> = [])
36593662
val exprParent = parent.expr(e, callable)
3660-
extractArrayCreation(e, e.type, e.varargElementType, e, exprParent.parent, exprParent.idx, callable, exprParent.enclosingStmt)
3663+
extractArrayCreation(e, e.type, e.varargElementType, true, e, exprParent.parent, exprParent.idx, callable, exprParent.enclosingStmt)
36613664
}
36623665
is IrGetObjectValue -> {
36633666
// For `object MyObject { ... }`, the .class has an

java/ql/test/kotlin/library-tests/collection-literals/PrintAst.expected

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ test.kt:
5757
# 0| 0: [IntegerLiteral] 1
5858
# 0| 1: [IntegerLiteral] 2
5959
# 0| 2: [IntegerLiteral] 3
60-
# 0| -1: [TypeAccess] Integer
60+
# 0| -1: [TypeAccess] int
6161
# 0| 0: [IntegerLiteral] 3
6262
# 1| 2: [ThisConstructorInvocationStmt] this(...)
6363
# 1| 0: [VarAccess] p0

0 commit comments

Comments
 (0)