mirror of
https://github.com/github/codeql.git
synced 2025-12-24 04:36:35 +01:00
Simplify kotlinFunctionToJavaEquivalent and accept consistency errors
This commit is contained in:
@@ -1258,13 +1258,6 @@ open class KotlinUsesExtractor(
|
|||||||
return tw.lm.locallyVisibleFunctionLabelMapping[f]?.function
|
return tw.lm.locallyVisibleFunctionLabelMapping[f]?.function
|
||||||
}
|
}
|
||||||
|
|
||||||
// These are classes with Java equivalents, but whose methods don't all exist on those Java equivalents--
|
|
||||||
// for example, the numeric classes define arithmetic functions (Int.plus, Long.or and so on) that lower to
|
|
||||||
// primitive arithmetic on the JVM, but which we extract as calls to reflect the source syntax more closely.
|
|
||||||
private val expectedMissingEquivalents = setOf(
|
|
||||||
"kotlin.Boolean", "kotlin.Byte", "kotlin.Char", "kotlin.Double", "kotlin.Float", "kotlin.Int", "kotlin.Long", "kotlin.Number", "kotlin.Short"
|
|
||||||
)
|
|
||||||
|
|
||||||
private fun kotlinFunctionToJavaEquivalent(f: IrFunction, noReplace: Boolean): IrFunction =
|
private fun kotlinFunctionToJavaEquivalent(f: IrFunction, noReplace: Boolean): IrFunction =
|
||||||
if (noReplace)
|
if (noReplace)
|
||||||
f
|
f
|
||||||
@@ -1272,18 +1265,19 @@ open class KotlinUsesExtractor(
|
|||||||
f.parentClassOrNull?.let { parentClass ->
|
f.parentClassOrNull?.let { parentClass ->
|
||||||
getJavaEquivalentClass(parentClass)?.let { javaClass ->
|
getJavaEquivalentClass(parentClass)?.let { javaClass ->
|
||||||
if (javaClass != parentClass) {
|
if (javaClass != parentClass) {
|
||||||
val jvmName = getJvmName(f) ?: f.name.asString()
|
var jvmName = getFunctionShortName(f).nameInDB
|
||||||
|
if (f.name.asString() == "get" && parentClass.fqNameWhenAvailable?.asString() == "kotlin.String") {
|
||||||
|
// `kotlin.String.get` has an equivalent `java.lang.String.get`, which in turn will be stored in the DB as `java.lang.String.charAt`.
|
||||||
|
// Maybe all operators should be handled the same way, but so far I only found this case that needed to be special cased. This is the
|
||||||
|
// only operator in `JvmNames.specialFunctions`
|
||||||
|
jvmName = "get"
|
||||||
|
}
|
||||||
// Look for an exact type match...
|
// Look for an exact type match...
|
||||||
javaClass.declarations.findSubType<IrFunction> { decl ->
|
javaClass.declarations.findSubType<IrFunction> { decl ->
|
||||||
decl.name.asString() == jvmName &&
|
decl.name.asString() == jvmName &&
|
||||||
decl.valueParameters.size == f.valueParameters.size &&
|
decl.valueParameters.size == f.valueParameters.size &&
|
||||||
decl.valueParameters.zip(f.valueParameters).all { p -> erase(p.first.type) == erase(p.second.type) }
|
decl.valueParameters.zip(f.valueParameters).all { p -> erase(p.first.type) == erase(p.second.type) }
|
||||||
} ?:
|
} ?:
|
||||||
// Or if there is none, look for the only viable overload
|
|
||||||
javaClass.declarations.singleOrNullSubType<IrFunction> { decl ->
|
|
||||||
decl.name.asString() == jvmName &&
|
|
||||||
decl.valueParameters.size == f.valueParameters.size
|
|
||||||
} ?:
|
|
||||||
// Or check property accessors:
|
// Or check property accessors:
|
||||||
(f.propertyIfAccessor as? IrProperty)?.let { kotlinProp ->
|
(f.propertyIfAccessor as? IrProperty)?.let { kotlinProp ->
|
||||||
val javaProp = javaClass.declarations.findSubType<IrProperty> { decl ->
|
val javaProp = javaClass.declarations.findSubType<IrProperty> { decl ->
|
||||||
@@ -1296,9 +1290,7 @@ open class KotlinUsesExtractor(
|
|||||||
else null
|
else null
|
||||||
} ?: run {
|
} ?: run {
|
||||||
val parentFqName = parentClass.fqNameWhenAvailable?.asString()
|
val parentFqName = parentClass.fqNameWhenAvailable?.asString()
|
||||||
if (!expectedMissingEquivalents.contains(parentFqName)) {
|
logger.warn("Couldn't find a Java equivalent function to $parentFqName.${f.name} in ${javaClass.fqNameWhenAvailable}")
|
||||||
logger.warn("Couldn't find a Java equivalent function to $parentFqName.${f.name} in ${javaClass.fqNameWhenAvailable}")
|
|
||||||
}
|
|
||||||
null
|
null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,2 @@
|
|||||||
|
| file://:0:0:0:0 | Couldn't find a Java equivalent function to kotlin.Enum.<init> in java.lang.Enum |
|
||||||
|
| file://:0:0:0:0 | Couldn't find a Java equivalent function to kotlin.Enum.<init> in java.lang.Enum |
|
||||||
@@ -1 +1 @@
|
|||||||
| file://:0:0:0:0 | Couldn't find a Java equivalent function to kotlin.String.get in java.lang.String |
|
| file://:0:0:0:0 | Couldn't find a Java equivalent function to kotlin.Enum.<init> in java.lang.Enum |
|
||||||
@@ -1 +1 @@
|
|||||||
| file://:0:0:0:0 | Couldn't find a Java equivalent function to kotlin.String.get in java.lang.String |
|
| file://:0:0:0:0 | Couldn't find a Java equivalent function to kotlin.Int.dec in java.lang.Integer |
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
| file://:0:0:0:0 | Couldn't find a Java equivalent function to kotlin.Int.dec in java.lang.Integer |
|
||||||
|
| file://:0:0:0:0 | Couldn't find a Java equivalent function to kotlin.Int.dec in java.lang.Integer |
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
| file://:0:0:0:0 | Couldn't find a Java equivalent function to kotlin.Enum.<init> in java.lang.Enum |
|
||||||
@@ -0,0 +1,45 @@
|
|||||||
|
| file://:0:0:0:0 | Couldn't find a Java equivalent function to kotlin.Boolean.not in java.lang.Boolean |
|
||||||
|
| file://:0:0:0:0 | Couldn't find a Java equivalent function to kotlin.Boolean.not in java.lang.Boolean |
|
||||||
|
| file://:0:0:0:0 | Couldn't find a Java equivalent function to kotlin.Byte.toInt in java.lang.Byte |
|
||||||
|
| file://:0:0:0:0 | Couldn't find a Java equivalent function to kotlin.Byte.toInt in java.lang.Byte |
|
||||||
|
| file://:0:0:0:0 | Couldn't find a Java equivalent function to kotlin.Byte.toInt in java.lang.Byte |
|
||||||
|
| file://:0:0:0:0 | Couldn't find a Java equivalent function to kotlin.Byte.toInt in java.lang.Byte |
|
||||||
|
| file://:0:0:0:0 | Couldn't find a Java equivalent function to kotlin.Byte.toInt in java.lang.Byte |
|
||||||
|
| file://:0:0:0:0 | Couldn't find a Java equivalent function to kotlin.Byte.toInt in java.lang.Byte |
|
||||||
|
| file://:0:0:0:0 | Couldn't find a Java equivalent function to kotlin.Byte.toInt in java.lang.Byte |
|
||||||
|
| file://:0:0:0:0 | Couldn't find a Java equivalent function to kotlin.Byte.toInt in java.lang.Byte |
|
||||||
|
| file://:0:0:0:0 | Couldn't find a Java equivalent function to kotlin.Byte.toInt in java.lang.Byte |
|
||||||
|
| file://:0:0:0:0 | Couldn't find a Java equivalent function to kotlin.Byte.toInt in java.lang.Byte |
|
||||||
|
| file://:0:0:0:0 | Couldn't find a Java equivalent function to kotlin.Byte.toInt in java.lang.Byte |
|
||||||
|
| file://:0:0:0:0 | Couldn't find a Java equivalent function to kotlin.Byte.toInt in java.lang.Byte |
|
||||||
|
| file://:0:0:0:0 | Couldn't find a Java equivalent function to kotlin.Enum.<init> in java.lang.Enum |
|
||||||
|
| file://:0:0:0:0 | Couldn't find a Java equivalent function to kotlin.Enum.<init> in java.lang.Enum |
|
||||||
|
| file://:0:0:0:0 | Couldn't find a Java equivalent function to kotlin.Int.and in java.lang.Integer |
|
||||||
|
| file://:0:0:0:0 | Couldn't find a Java equivalent function to kotlin.Int.dec in java.lang.Integer |
|
||||||
|
| file://:0:0:0:0 | Couldn't find a Java equivalent function to kotlin.Int.inv in java.lang.Integer |
|
||||||
|
| file://:0:0:0:0 | Couldn't find a Java equivalent function to kotlin.Int.or in java.lang.Integer |
|
||||||
|
| file://:0:0:0:0 | Couldn't find a Java equivalent function to kotlin.Int.rangeTo in java.lang.Integer |
|
||||||
|
| file://:0:0:0:0 | Couldn't find a Java equivalent function to kotlin.Int.rangeTo in java.lang.Integer |
|
||||||
|
| file://:0:0:0:0 | Couldn't find a Java equivalent function to kotlin.Int.shl in java.lang.Integer |
|
||||||
|
| file://:0:0:0:0 | Couldn't find a Java equivalent function to kotlin.Int.shr in java.lang.Integer |
|
||||||
|
| file://:0:0:0:0 | Couldn't find a Java equivalent function to kotlin.Int.ushr in java.lang.Integer |
|
||||||
|
| file://:0:0:0:0 | Couldn't find a Java equivalent function to kotlin.Int.xor in java.lang.Integer |
|
||||||
|
| file://:0:0:0:0 | Couldn't find a Java equivalent function to kotlin.Long.and in java.lang.Long |
|
||||||
|
| file://:0:0:0:0 | Couldn't find a Java equivalent function to kotlin.Long.inv in java.lang.Long |
|
||||||
|
| file://:0:0:0:0 | Couldn't find a Java equivalent function to kotlin.Long.or in java.lang.Long |
|
||||||
|
| file://:0:0:0:0 | Couldn't find a Java equivalent function to kotlin.Long.shl in java.lang.Long |
|
||||||
|
| file://:0:0:0:0 | Couldn't find a Java equivalent function to kotlin.Long.shr in java.lang.Long |
|
||||||
|
| file://:0:0:0:0 | Couldn't find a Java equivalent function to kotlin.Long.ushr in java.lang.Long |
|
||||||
|
| file://:0:0:0:0 | Couldn't find a Java equivalent function to kotlin.Long.xor in java.lang.Long |
|
||||||
|
| file://:0:0:0:0 | Couldn't find a Java equivalent function to kotlin.Short.toInt in java.lang.Short |
|
||||||
|
| file://:0:0:0:0 | Couldn't find a Java equivalent function to kotlin.Short.toInt in java.lang.Short |
|
||||||
|
| file://:0:0:0:0 | Couldn't find a Java equivalent function to kotlin.Short.toInt in java.lang.Short |
|
||||||
|
| file://:0:0:0:0 | Couldn't find a Java equivalent function to kotlin.Short.toInt in java.lang.Short |
|
||||||
|
| file://:0:0:0:0 | Couldn't find a Java equivalent function to kotlin.Short.toInt in java.lang.Short |
|
||||||
|
| file://:0:0:0:0 | Couldn't find a Java equivalent function to kotlin.Short.toInt in java.lang.Short |
|
||||||
|
| file://:0:0:0:0 | Couldn't find a Java equivalent function to kotlin.Short.toInt in java.lang.Short |
|
||||||
|
| file://:0:0:0:0 | Couldn't find a Java equivalent function to kotlin.Short.toInt in java.lang.Short |
|
||||||
|
| file://:0:0:0:0 | Couldn't find a Java equivalent function to kotlin.Short.toInt in java.lang.Short |
|
||||||
|
| file://:0:0:0:0 | Couldn't find a Java equivalent function to kotlin.Short.toInt in java.lang.Short |
|
||||||
|
| file://:0:0:0:0 | Couldn't find a Java equivalent function to kotlin.Short.toInt in java.lang.Short |
|
||||||
|
| file://:0:0:0:0 | Couldn't find a Java equivalent function to kotlin.Short.toInt in java.lang.Short |
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
| file://:0:0:0:0 | Couldn't find a Java equivalent function to kotlin.Enum.<init> in java.lang.Enum |
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
| file://:0:0:0:0 | Couldn't find a Java equivalent function to kotlin.Byte.toByte in java.lang.Byte |
|
||||||
|
| file://:0:0:0:0 | Couldn't find a Java equivalent function to kotlin.Byte.toDouble in java.lang.Byte |
|
||||||
|
| file://:0:0:0:0 | Couldn't find a Java equivalent function to kotlin.Byte.toFloat in java.lang.Byte |
|
||||||
|
| file://:0:0:0:0 | Couldn't find a Java equivalent function to kotlin.Byte.toInt in java.lang.Byte |
|
||||||
|
| file://:0:0:0:0 | Couldn't find a Java equivalent function to kotlin.Byte.toLong in java.lang.Byte |
|
||||||
|
| file://:0:0:0:0 | Couldn't find a Java equivalent function to kotlin.Byte.toShort in java.lang.Byte |
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
| file://:0:0:0:0 | Couldn't find a Java equivalent function to kotlin.Number.toChar in java.lang.Number |
|
||||||
@@ -76,3 +76,103 @@ test.kt:
|
|||||||
# 13| 0: [SubExpr] ... - ...
|
# 13| 0: [SubExpr] ... - ...
|
||||||
# 13| 0: [VarAccess] i
|
# 13| 0: [VarAccess] i
|
||||||
# 13| 1: [IntegerLiteral] 10
|
# 13| 1: [IntegerLiteral] 10
|
||||||
|
# 15| 8: [Method] special
|
||||||
|
# 15| 3: [TypeAccess] Unit
|
||||||
|
#-----| 4: (Parameters)
|
||||||
|
# 15| 0: [Parameter] n
|
||||||
|
# 15| 0: [TypeAccess] Number
|
||||||
|
# 15| 1: [Parameter] m
|
||||||
|
# 15| 0: [TypeAccess] Map<String,String>
|
||||||
|
# 15| 0: [TypeAccess] String
|
||||||
|
# 15| 1: [TypeAccess] String
|
||||||
|
# 15| 2: [Parameter] s
|
||||||
|
# 15| 0: [TypeAccess] String
|
||||||
|
# 15| 3: [Parameter] l
|
||||||
|
# 15| 0: [TypeAccess] List<Integer>
|
||||||
|
# 15| 0: [TypeAccess] Integer
|
||||||
|
# 15| 5: [BlockStmt] { ... }
|
||||||
|
# 16| 0: [ExprStmt] <Expr>;
|
||||||
|
# 16| 0: [ImplicitCoercionToUnitExpr] <implicit coercion to unit>
|
||||||
|
# 16| 0: [TypeAccess] Unit
|
||||||
|
# 16| 1: [MethodAccess] charAt(...)
|
||||||
|
# 16| -1: [VarAccess] s
|
||||||
|
# 16| 0: [IntegerLiteral] 1
|
||||||
|
# 17| 1: [ExprStmt] <Expr>;
|
||||||
|
# 17| 0: [ImplicitCoercionToUnitExpr] <implicit coercion to unit>
|
||||||
|
# 17| 0: [TypeAccess] Unit
|
||||||
|
# 17| 1: [MethodAccess] charAt(...)
|
||||||
|
# 17| -1: [VarAccess] s
|
||||||
|
# 17| 0: [IntegerLiteral] 1
|
||||||
|
# 18| 2: [ExprStmt] <Expr>;
|
||||||
|
# 18| 0: [ImplicitCoercionToUnitExpr] <implicit coercion to unit>
|
||||||
|
# 18| 0: [TypeAccess] Unit
|
||||||
|
# 18| 1: [MethodAccess] doubleValue(...)
|
||||||
|
# 18| -1: [VarAccess] n
|
||||||
|
# 19| 3: [ExprStmt] <Expr>;
|
||||||
|
# 19| 0: [ImplicitCoercionToUnitExpr] <implicit coercion to unit>
|
||||||
|
# 19| 0: [TypeAccess] Unit
|
||||||
|
# 19| 1: [MethodAccess] byteValue(...)
|
||||||
|
# 19| -1: [VarAccess] n
|
||||||
|
# 20| 4: [ExprStmt] <Expr>;
|
||||||
|
# 20| 0: [ImplicitCoercionToUnitExpr] <implicit coercion to unit>
|
||||||
|
# 20| 0: [TypeAccess] Unit
|
||||||
|
# 20| 1: [MethodAccess] toChar(...)
|
||||||
|
# 20| -1: [VarAccess] n
|
||||||
|
# 21| 5: [ExprStmt] <Expr>;
|
||||||
|
# 21| 0: [ImplicitCoercionToUnitExpr] <implicit coercion to unit>
|
||||||
|
# 21| 0: [TypeAccess] Unit
|
||||||
|
# 21| 1: [MethodAccess] floatValue(...)
|
||||||
|
# 21| -1: [VarAccess] n
|
||||||
|
# 22| 6: [ExprStmt] <Expr>;
|
||||||
|
# 22| 0: [ImplicitCoercionToUnitExpr] <implicit coercion to unit>
|
||||||
|
# 22| 0: [TypeAccess] Unit
|
||||||
|
# 22| 1: [MethodAccess] intValue(...)
|
||||||
|
# 22| -1: [VarAccess] n
|
||||||
|
# 23| 7: [ExprStmt] <Expr>;
|
||||||
|
# 23| 0: [ImplicitCoercionToUnitExpr] <implicit coercion to unit>
|
||||||
|
# 23| 0: [TypeAccess] Unit
|
||||||
|
# 23| 1: [MethodAccess] shortValue(...)
|
||||||
|
# 23| -1: [VarAccess] n
|
||||||
|
# 24| 8: [ExprStmt] <Expr>;
|
||||||
|
# 24| 0: [ImplicitCoercionToUnitExpr] <implicit coercion to unit>
|
||||||
|
# 24| 0: [TypeAccess] Unit
|
||||||
|
# 24| 1: [MethodAccess] keySet(...)
|
||||||
|
# 24| -1: [VarAccess] m
|
||||||
|
# 25| 9: [ExprStmt] <Expr>;
|
||||||
|
# 25| 0: [ImplicitCoercionToUnitExpr] <implicit coercion to unit>
|
||||||
|
# 25| 0: [TypeAccess] Unit
|
||||||
|
# 25| 1: [MethodAccess] values(...)
|
||||||
|
# 25| -1: [VarAccess] m
|
||||||
|
# 26| 10: [ExprStmt] <Expr>;
|
||||||
|
# 26| 0: [ImplicitCoercionToUnitExpr] <implicit coercion to unit>
|
||||||
|
# 26| 0: [TypeAccess] Unit
|
||||||
|
# 26| 1: [MethodAccess] entrySet(...)
|
||||||
|
# 26| -1: [VarAccess] m
|
||||||
|
# 27| 11: [ExprStmt] <Expr>;
|
||||||
|
# 27| 0: [ImplicitCoercionToUnitExpr] <implicit coercion to unit>
|
||||||
|
# 27| 0: [TypeAccess] Unit
|
||||||
|
# 27| 1: [MethodAccess] remove(...)
|
||||||
|
# 27| -1: [VarAccess] l
|
||||||
|
# 27| 0: [IntegerLiteral] 1
|
||||||
|
# 28| 12: [ExprStmt] <Expr>;
|
||||||
|
# 28| 0: [ImplicitCoercionToUnitExpr] <implicit coercion to unit>
|
||||||
|
# 28| 0: [TypeAccess] Unit
|
||||||
|
# 28| 1: [MethodAccess] getKey(...)
|
||||||
|
# 28| -1: [MethodAccess] first(...)
|
||||||
|
# 28| -2: [TypeAccess] Entry<String,String>
|
||||||
|
# 28| 0: [TypeAccess] String
|
||||||
|
# 28| 1: [TypeAccess] String
|
||||||
|
# 28| -1: [TypeAccess] CollectionsKt
|
||||||
|
# 28| 0: [MethodAccess] entrySet(...)
|
||||||
|
# 28| -1: [VarAccess] m
|
||||||
|
# 29| 13: [ExprStmt] <Expr>;
|
||||||
|
# 29| 0: [ImplicitCoercionToUnitExpr] <implicit coercion to unit>
|
||||||
|
# 29| 0: [TypeAccess] Unit
|
||||||
|
# 29| 1: [MethodAccess] getValue(...)
|
||||||
|
# 29| -1: [MethodAccess] first(...)
|
||||||
|
# 29| -2: [TypeAccess] Entry<String,String>
|
||||||
|
# 29| 0: [TypeAccess] String
|
||||||
|
# 29| 1: [TypeAccess] String
|
||||||
|
# 29| -1: [TypeAccess] CollectionsKt
|
||||||
|
# 29| 0: [MethodAccess] entrySet(...)
|
||||||
|
# 29| -1: [VarAccess] m
|
||||||
|
|||||||
@@ -1,4 +1,7 @@
|
|||||||
diagnostics
|
diagnostics
|
||||||
|
| file://:0:0:0:0 | Couldn't find a Java equivalent function to kotlin.Number.toChar in java.lang.Number |
|
||||||
#select
|
#select
|
||||||
| Integer |
|
| Integer |
|
||||||
|
| Iterable<? extends T> |
|
||||||
| Object |
|
| Object |
|
||||||
|
| int |
|
||||||
|
|||||||
@@ -11,3 +11,20 @@ fun fn2(s: String) = s + ""
|
|||||||
|
|
||||||
fun fn1(i: Int) = i.minus(10)
|
fun fn1(i: Int) = i.minus(10)
|
||||||
fun fn2(i: Int) = i - 10
|
fun fn2(i: Int) = i - 10
|
||||||
|
|
||||||
|
fun special(n: Number, m: Map<String, String>, s: String, l: MutableList<Int>) {
|
||||||
|
s[1]
|
||||||
|
s.get(1)
|
||||||
|
n.toDouble()
|
||||||
|
n.toByte()
|
||||||
|
n.toChar()
|
||||||
|
n.toFloat()
|
||||||
|
n.toInt()
|
||||||
|
n.toShort()
|
||||||
|
m.keys
|
||||||
|
m.values
|
||||||
|
m.entries
|
||||||
|
l.removeAt(1)
|
||||||
|
m.entries.first().key
|
||||||
|
m.entries.first().value
|
||||||
|
}
|
||||||
|
|||||||
@@ -0,0 +1 @@
|
|||||||
|
| file://:0:0:0:0 | Couldn't find a Java equivalent function to kotlin.Enum.<init> in java.lang.Enum |
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
| file://:0:0:0:0 | Couldn't find a Java equivalent function to kotlin.Enum.<init> in java.lang.Enum |
|
||||||
Reference in New Issue
Block a user