Skip to content

Commit 3af2e71

Browse files
authored
Merge pull request #9874 from smowton/smowton/fix/kotlin-for-loop-iterators
Kotlin: fix for-loop iterators over primitive or wildcard types
2 parents 8cd0a9d + 1cbe26a commit 3af2e71

File tree

4 files changed

+31
-1
lines changed

4 files changed

+31
-1
lines changed

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2124,7 +2124,13 @@ open class KotlinFileExtractor(
21242124
}
21252125
isFunction(target, "kotlin", "(some array type)", { isArrayType(it) }, "iterator") && c.origin == IrStatementOrigin.FOR_LOOP_ITERATOR -> {
21262126
findTopLevelFunctionOrWarn("kotlin.jvm.internal.iterator", "kotlin.jvm.internal.ArrayIteratorKt", c)?.let { iteratorFn ->
2127-
extractRawMethodAccess(iteratorFn, c, callable, parent, idx, enclosingStmt, listOf(c.dispatchReceiver), null, null, listOf((c.dispatchReceiver!!.type as IrSimpleType).arguments.first().typeOrNull!!))
2127+
val typeArgs = (c.dispatchReceiver!!.type as IrSimpleType).arguments.map {
2128+
when(it) {
2129+
is IrTypeProjection -> it.type
2130+
else -> pluginContext.irBuiltIns.anyNType
2131+
}
2132+
}
2133+
extractRawMethodAccess(iteratorFn, c, callable, parent, idx, enclosingStmt, listOf(c.dispatchReceiver), null, null, typeArgs)
21282134
}
21292135
}
21302136
isFunction(target, "kotlin", "(some array type)", { isArrayType(it) }, "get") && c.origin == IrStatementOrigin.GET_ARRAY_ELEMENT -> {
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
| test.kt:5:14:5:14 | hasNext(...) |
2+
| test.kt:5:14:5:14 | iterator(...) |
3+
| test.kt:5:14:5:14 | next(...) |
4+
| test.kt:6:14:6:14 | hasNext(...) |
5+
| test.kt:6:14:6:14 | iterator(...) |
6+
| test.kt:6:14:6:14 | next(...) |
7+
| test.kt:7:14:7:14 | hasNext(...) |
8+
| test.kt:7:14:7:14 | iterator(...) |
9+
| test.kt:7:14:7:14 | next(...) |
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
fun test(x: Array<String>, y: Array<*>, z: IntArray): Int {
2+
3+
var ret = 0
4+
5+
for (el in x) { ret += 1 }
6+
for (el in y) { ret += 1 }
7+
for (el in z) { ret += 1 }
8+
9+
return ret
10+
11+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import java
2+
3+
from MethodAccess ma
4+
select ma

0 commit comments

Comments
 (0)