Skip to content

Commit 8eee450

Browse files
authored
Merge pull request github#11064 from smowton/smowton/fix/kotlin-inherited-defaults
Kotlin: handle default parameter values inherited from an overridden function
2 parents 197be69 + cad2684 commit 8eee450

File tree

4 files changed

+36
-5
lines changed

4 files changed

+36
-5
lines changed

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

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package com.github.codeql
22

33
import com.github.codeql.comments.CommentExtractor
44
import com.github.codeql.utils.*
5+
import com.github.codeql.utils.versions.allOverriddenIncludingSelf
56
import com.github.codeql.utils.versions.functionN
67
import com.github.codeql.utils.versions.isUnderscoreParameter
78
import com.semmle.extractor.java.OdasaOutput
@@ -1847,16 +1848,27 @@ open class KotlinFileExtractor(
18471848
// Ensure the real target gets extracted, as we might not every directly touch it thanks to this call being redirected to a $default method.
18481849
useFunction<DbCallable>(callTarget)
18491850
}
1850-
val defaultMethodLabel = getDefaultsMethodLabel(callTarget)
1851+
1852+
// Default parameter values are inherited by overrides; in this case the call should dispatch against the $default method belonging to the class
1853+
// that specified the default values, which will in turn dynamically dispatch back to the relevant override.
1854+
val overriddenCallTarget = (callTarget as? IrSimpleFunction)?.allOverriddenIncludingSelf()?.firstOrNull {
1855+
it.overriddenSymbols.isEmpty() && it.valueParameters.any { p -> p.defaultValue != null }
1856+
} ?: callTarget
1857+
if (isExternalDeclaration(overriddenCallTarget)) {
1858+
// Likewise, ensure the overridden target gets extracted.
1859+
useFunction<DbCallable>(overriddenCallTarget)
1860+
}
1861+
1862+
val defaultMethodLabel = getDefaultsMethodLabel(overriddenCallTarget)
18511863
val id = extractMethodAccessWithoutArgs(resultType, locId, enclosingCallable, callsiteParent, childIdx, enclosingStmt, defaultMethodLabel)
18521864

1853-
if (callTarget.isLocalFunction()) {
1854-
extractTypeAccess(getLocallyVisibleFunctionLabels(callTarget).type, locId, id, -1, enclosingCallable, enclosingStmt)
1865+
if (overriddenCallTarget.isLocalFunction()) {
1866+
extractTypeAccess(getLocallyVisibleFunctionLabels(overriddenCallTarget).type, locId, id, -1, enclosingCallable, enclosingStmt)
18551867
} else {
1856-
extractStaticTypeAccessQualifierUnchecked(callTarget.parent, id, locId, enclosingCallable, enclosingStmt)
1868+
extractStaticTypeAccessQualifierUnchecked(overriddenCallTarget.parent, id, locId, enclosingCallable, enclosingStmt)
18571869
}
18581870

1859-
extractDefaultsCallArguments(id, callTarget, enclosingCallable, enclosingStmt, valueArguments, dispatchReceiver, extensionReceiver)
1871+
extractDefaultsCallArguments(id, overriddenCallTarget, enclosingCallable, enclosingStmt, valueArguments, dispatchReceiver, extensionReceiver)
18601872
}
18611873

18621874
private fun extractDefaultsCallArguments(
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
| test.kt:3:8:3:28 | f(...) | test.kt:3:8:3:28 | f | test.kt:1:1:5:1 | A |
2+
| test.kt:11:16:11:23 | f$default(...) | test.kt:3:8:3:28 | f$default | test.kt:1:1:5:1 | A |
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
open class A {
2+
3+
open fun f(x: Int = 0) = x
4+
5+
}
6+
7+
class B : A() {
8+
9+
override fun f(x: Int) = x + 1
10+
11+
fun user() = this.f()
12+
13+
}
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, ma.getCallee(), ma.getCallee().getDeclaringType()

0 commit comments

Comments
 (0)