Skip to content

Commit ed772e5

Browse files
authored
Merge pull request #10328 from tamasvajk/kotlin-kfunction-fix
Kotlin: fix `KFunctionX.invoke` extraction
2 parents be21b26 + 56ef173 commit ed772e5

File tree

7 files changed

+63
-0
lines changed

7 files changed

+63
-0
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1333,6 +1333,14 @@ open class KotlinFileExtractor(
13331333
val receiverClass = receiverType.classifier.owner as? IrClass ?: return listOf()
13341334
val ancestorTypes = ArrayList<IrSimpleType>()
13351335

1336+
// KFunctionX doesn't implement FunctionX on versions before 1.7.0:
1337+
if ((callTarget.name.asString() == "invoke") &&
1338+
(receiverClass.fqNameWhenAvailable?.asString()?.startsWith("kotlin.reflect.KFunction") == true) &&
1339+
(callTarget.parentClassOrNull?.fqNameWhenAvailable?.asString()?.startsWith("kotlin.Function") == true)
1340+
) {
1341+
return receiverType.arguments
1342+
}
1343+
13361344
// Populate ancestorTypes with the path from receiverType's class to its ancestor, callTarget's declaring type.
13371345
fun walkFrom(c: IrClass): Boolean {
13381346
if(declaringType == c)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* This file was generated by the Gradle 'init' task.
3+
*
4+
* This generated file contains a sample Kotlin application project to get you started.
5+
* For more details take a look at the 'Building Java & JVM projects' chapter in the Gradle
6+
* User Manual available at https://docs.gradle.org/7.0.2/userguide/building_java_projects.html
7+
*/
8+
9+
plugins {
10+
// Apply the org.jetbrains.kotlin.jvm Plugin to add support for Kotlin.
11+
id 'org.jetbrains.kotlin.jvm' version '1.6.20'
12+
13+
// Apply the application plugin to add support for building a CLI application in Java.
14+
id 'application'
15+
}
16+
17+
repositories {
18+
// Use Maven Central for resolving dependencies.
19+
mavenCentral()
20+
}
21+
22+
application {
23+
// Define the main class for the application.
24+
mainClass = 'testProject.AppKt'
25+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import kotlin.reflect.*
2+
3+
fun fn() {
4+
val ref: KFunction2<Ccc, Int, Double> = Ccc::m
5+
ref.invoke(Ccc(), 1)
6+
}
7+
8+
class Ccc {
9+
fun m(i:Int):Double = 5.0
10+
}

java/ql/integration-tests/posix-only/kotlin/kotlin_kfunction/diagnostics.expected

Whitespace-only changes.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import java
2+
import semmle.code.java.Diagnostics
3+
4+
query predicate diag(Diagnostic d) { any() }
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/*
2+
* This file was generated by the Gradle 'init' task.
3+
*
4+
* The settings file is used to specify which projects to include in your build.
5+
*
6+
* Detailed information about configuring a multi-project build in Gradle can be found
7+
* in the user manual at https://docs.gradle.org/7.0.2/userguide/multi_project_builds.html
8+
*/
9+
10+
rootProject.name = 'testProject'
11+
include('app')
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from create_database_utils import *
2+
3+
run_codeql_database_create(
4+
["gradle build --no-daemon --no-build-cache --rerun-tasks"], lang="java")
5+
runSuccessfully(["gradle", "clean"])

0 commit comments

Comments
 (0)