Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ce87a89

Browse files
committed
Replace Map and similar functions with their Java cousins
This didn't appear to be necessary because the Kotlin and Java versions of Map (for example) are designed to be compatible, but in certain cases their functions have the same erasure but not the same type (e.g. Map.getOrDefault(K, V) vs. Map.getOrDefault(Object, V). These have different erasures which was leading to callable-binding inconsistencies.
1 parent fa0bd03 commit ce87a89

3 files changed

Lines changed: 26 additions & 2 deletions

File tree

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

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -899,17 +899,36 @@ open class KotlinUsesExtractor(
899899
return id
900900
}
901901

902+
fun kotlinFunctionToJavaEquivalent(f: IrFunction) =
903+
f.parent.let {
904+
when (it) {
905+
is IrClass ->
906+
getJavaEquivalentClass(it)?.let { javaClass ->
907+
if (javaClass != it)
908+
javaClass.declarations.find { decl ->
909+
decl is IrFunction && decl.name == f.name && decl.valueParameters.size == f.valueParameters.size
910+
} as IrFunction
911+
else
912+
null
913+
}
914+
else -> null
915+
} ?: f
916+
}
917+
902918
fun <T: DbCallable> useFunction(f: IrFunction, classTypeArgsIncludingOuterClasses: List<IrTypeArgument>? = null): Label<out T> {
903919
if (f.isLocalFunction()) {
904920
val ids = getLocallyVisibleFunctionLabels(f)
905921
return ids.function.cast<T>()
906922
} else {
907-
return useFunctionCommon<T>(f, getFunctionLabel(f, classTypeArgsIncludingOuterClasses))
923+
val realFunction = kotlinFunctionToJavaEquivalent(f)
924+
return useFunctionCommon<T>(realFunction, getFunctionLabel(realFunction, classTypeArgsIncludingOuterClasses))
908925
}
909926
}
910927

911928
fun <T: DbCallable> useFunction(f: IrFunction, parentId: Label<out DbElement>, classTypeArgsIncludingOuterClasses: List<IrTypeArgument>?) =
912-
useFunctionCommon<T>(f, getFunctionLabel(f, parentId, classTypeArgsIncludingOuterClasses))
929+
kotlinFunctionToJavaEquivalent(f).let {
930+
useFunctionCommon<T>(it, getFunctionLabel(it, parentId, classTypeArgsIncludingOuterClasses))
931+
}
913932

914933
fun getTypeArgumentLabel(
915934
arg: IrTypeArgument
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
fun test(m: Map<Int, Int>) = m.getOrDefault(1, 2)
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.getCallee().getAParameter().getType().toString()

0 commit comments

Comments
 (0)