Skip to content

Commit 45a4cd8

Browse files
committed
Kotlin: specialise extension receivers the same as other function parameters
This arises when a generic class extends one of its parameters; for example, `class G<T> { val T.v; get() = 1 }`, where specialisation `G<List>` should generate a method specialisation `getV(List)`.
1 parent 19b7e9e commit 45a4cd8

File tree

6 files changed

+34
-11
lines changed

6 files changed

+34
-11
lines changed

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

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1188,22 +1188,16 @@ open class KotlinFileExtractor(
11881188
id
11891189

11901190
val extReceiver = f.extensionReceiverParameter
1191-
val idxOffset = if (extReceiver != null) 1 else 0
1192-
val fParameters = overriddenAttributes?.valueParameters ?: f.valueParameters
1191+
val fParameters = (overriddenAttributes?.valueParameters ?: f.valueParameters) + listOfNotNull(extReceiver)
11931192
val paramTypes = fParameters.mapIndexed { i, vp ->
1194-
extractValueParameter(vp, id, i + idxOffset, typeSubstitution, sourceDeclaration, classTypeArgsIncludingOuterClasses, extractTypeAccess = extractMethodAndParameterTypeAccesses, overriddenAttributes?.sourceLoc)
1193+
extractValueParameter(vp, id, i, typeSubstitution, sourceDeclaration, classTypeArgsIncludingOuterClasses, extractTypeAccess = extractMethodAndParameterTypeAccesses, overriddenAttributes?.sourceLoc)
11951194
}
1196-
val allParamTypes = if (extReceiver != null) {
1197-
val extendedType = useType(extReceiver.type)
1195+
if (extReceiver != null) {
1196+
val extendedType = paramTypes[0]
11981197
tw.writeKtExtensionFunctions(id.cast<DbMethod>(), extendedType.javaResult.id, extendedType.kotlinResult.id)
1199-
1200-
val t = extractValueParameter(extReceiver, id, 0, null, sourceDeclaration, classTypeArgsIncludingOuterClasses, extractTypeAccess = extractMethodAndParameterTypeAccesses, overriddenAttributes?.sourceLoc)
1201-
listOf(t) + paramTypes
1202-
} else {
1203-
paramTypes
12041198
}
12051199

1206-
val paramsSignature = allParamTypes.joinToString(separator = ",", prefix = "(", postfix = ")") { signatureOrWarn(it.javaResult, f) }
1200+
val paramsSignature = paramTypes.joinToString(separator = ",", prefix = "(", postfix = ")") { signatureOrWarn(it.javaResult, f) }
12071201

12081202
val adjustedReturnType = addJavaLoweringWildcards(getAdjustedReturnType(f), false, (javaCallable as? JavaMethod)?.returnType)
12091203
val substReturnType = typeSubstitution?.let { it(adjustedReturnType, TypeContext.RETURN, pluginContext) } ?: adjustedReturnType
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
public class User {
2+
3+
public static void test(KotlinClass<String> kc) {
4+
5+
kc.getKotlinVal("Hello world");
6+
7+
}
8+
9+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
| User.java:5:5:5:34 | getKotlinVal(...) | getKotlinVal | String |
2+
| test.kt:8:43:8:78 | with(...) | with | Function1<? super T,? extends R> |
3+
| test.kt:8:43:8:78 | with(...) | with | T |
4+
| test.kt:8:68:8:76 | getKotlinVal(...) | getKotlinVal | String |
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class KotlinClass<T> {
2+
3+
val T.kotlinVal: Int
4+
get() = 1
5+
6+
}
7+
8+
fun kotlinUser(kc: KotlinClass<String>) = with(kc) { "hello world".kotlinVal }
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from create_database_utils import *
2+
3+
os.mkdir('build')
4+
run_codeql_database_create(["kotlinc test.kt -d build", "javac User.java -cp build"], lang="java")
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().toString(), ma.getCallee().getAParamType().toString()

0 commit comments

Comments
 (0)