Skip to content

Commit 20ac15d

Browse files
authored
Merge pull request #10152 from igfoo/igfoo/not-null-exprs
Kotlin: Remove more not-null expressions
2 parents 28d58be + b5f20e4 commit 20ac15d

File tree

1 file changed

+57
-15
lines changed

1 file changed

+57
-15
lines changed

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

Lines changed: 57 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1478,9 +1478,21 @@ open class KotlinFileExtractor(
14781478
logger.warn("Cannot find functional interface type for raw method access")
14791479
null
14801480
} else {
1481-
val interfaceType = functionalInterface.classOrNull!!.owner
1482-
val substituted = getJavaEquivalentClass(interfaceType) ?: interfaceType
1483-
findFunction(substituted, OperatorNameConventions.INVOKE.asString())!!
1481+
val functionalInterfaceClass = functionalInterface.classOrNull
1482+
if (functionalInterfaceClass == null) {
1483+
logger.warn("Cannot find functional interface class for raw method access")
1484+
null
1485+
} else {
1486+
val interfaceType = functionalInterfaceClass.owner
1487+
val substituted = getJavaEquivalentClass(interfaceType) ?: interfaceType
1488+
val function = findFunction(substituted, OperatorNameConventions.INVOKE.asString())
1489+
if (function == null) {
1490+
logger.warn("Cannot find invoke function for raw method access")
1491+
null
1492+
} else {
1493+
function
1494+
}
1495+
}
14841496
}
14851497
} else {
14861498
callTarget
@@ -1768,10 +1780,15 @@ open class KotlinFileExtractor(
17681780
fun extractMethodAccess(syntacticCallTarget: IrFunction, extractMethodTypeArguments: Boolean = true, extractClassTypeArguments: Boolean = false) {
17691781
val typeArgs =
17701782
if (extractMethodTypeArguments)
1771-
(0 until c.typeArgumentsCount).map { c.getTypeArgument(it)!! }
1783+
(0 until c.typeArgumentsCount).map { c.getTypeArgument(it) }.requireNoNullsOrNull()
17721784
else
17731785
listOf()
17741786

1787+
if (typeArgs == null) {
1788+
logger.warn("Missing type argument in extractMethodAccess")
1789+
return
1790+
}
1791+
17751792
extractRawMethodAccess(syntacticCallTarget, c, callable, parent, idx, enclosingStmt, (0 until c.valueArgumentsCount).map { c.getValueArgument(it) }, c.dispatchReceiver, c.extensionReceiver, typeArgs, extractClassTypeArguments, c.superQualifierSymbol)
17761793
}
17771794

@@ -2036,7 +2053,12 @@ open class KotlinFileExtractor(
20362053
tw.writeCallableEnclosingExpr(id, callable)
20372054

20382055
if (c.typeArgumentsCount == 1) {
2039-
extractTypeAccessRecursive(c.getTypeArgument(0)!!, locId, id, -1, callable, enclosingStmt, TypeContext.GENERIC_ARGUMENT)
2056+
val typeArgument = c.getTypeArgument(0)
2057+
if (typeArgument == null) {
2058+
logger.errorElement("Type argument missing in an arrayOfNulls call", c)
2059+
} else {
2060+
extractTypeAccessRecursive(typeArgument, locId, id, -1, callable, enclosingStmt, TypeContext.GENERIC_ARGUMENT)
2061+
}
20402062
} else {
20412063
logger.errorElement("Expected to find exactly one type argument in an arrayOfNulls call", c)
20422064
}
@@ -2099,7 +2121,12 @@ open class KotlinFileExtractor(
20992121

21002122
if (isBuiltinCallKotlin(c, "arrayOf")) {
21012123
if (c.typeArgumentsCount == 1) {
2102-
extractTypeAccessRecursive(c.getTypeArgument(0)!!, locId, id, -1, callable, enclosingStmt, TypeContext.GENERIC_ARGUMENT)
2124+
val typeArgument = c.getTypeArgument(0)
2125+
if (typeArgument == null) {
2126+
logger.errorElement("Type argument missing in an arrayOf call", c)
2127+
} else {
2128+
extractTypeAccessRecursive(typeArgument, locId, id, -1, callable, enclosingStmt, TypeContext.GENERIC_ARGUMENT)
2129+
}
21032130
} else {
21042131
logger.errorElement("Expected to find one type argument in arrayOf call", c )
21052132
}
@@ -2152,13 +2179,18 @@ open class KotlinFileExtractor(
21522179
}
21532180
isFunction(target, "kotlin", "(some array type)", { isArrayType(it) }, "iterator") && c.origin == IrStatementOrigin.FOR_LOOP_ITERATOR -> {
21542181
findTopLevelFunctionOrWarn("kotlin.jvm.internal.iterator", "kotlin.jvm.internal.ArrayIteratorKt", c)?.let { iteratorFn ->
2155-
val typeArgs = (c.dispatchReceiver!!.type as IrSimpleType).arguments.map {
2156-
when(it) {
2157-
is IrTypeProjection -> it.type
2158-
else -> pluginContext.irBuiltIns.anyNType
2182+
val dispatchReceiver = c.dispatchReceiver
2183+
if (dispatchReceiver == null) {
2184+
logger.errorElement("No dispatch receiver found for array iterator call", c)
2185+
} else {
2186+
val typeArgs = (dispatchReceiver.type as IrSimpleType).arguments.map {
2187+
when(it) {
2188+
is IrTypeProjection -> it.type
2189+
else -> pluginContext.irBuiltIns.anyNType
2190+
}
21592191
}
2192+
extractRawMethodAccess(iteratorFn, c, callable, parent, idx, enclosingStmt, listOf(c.dispatchReceiver), null, null, typeArgs)
21602193
}
2161-
extractRawMethodAccess(iteratorFn, c, callable, parent, idx, enclosingStmt, listOf(c.dispatchReceiver), null, null, typeArgs)
21622194
}
21632195
}
21642196
isFunction(target, "kotlin", "(some array type)", { isArrayType(it) }, "get") && c.origin == IrStatementOrigin.GET_ARRAY_ELEMENT -> {
@@ -2204,12 +2236,22 @@ open class KotlinFileExtractor(
22042236
isBuiltinCall(c, "<unsafe-coerce>", "kotlin.jvm.internal") -> {
22052237

22062238
if (c.valueArgumentsCount != 1) {
2207-
logger.errorElement("Expected to find only one argument for a kotlin.jvm.internal.<unsafe-coerce>() call", c)
2239+
logger.errorElement("Expected to find one argument for a kotlin.jvm.internal.<unsafe-coerce>() call, but found ${c.valueArgumentsCount}", c)
22082240
return
22092241
}
22102242

22112243
if (c.typeArgumentsCount != 2) {
2212-
logger.errorElement("Expected to find two type arguments for a kotlin.jvm.internal.<unsafe-coerce>() call", c)
2244+
logger.errorElement("Expected to find two type arguments for a kotlin.jvm.internal.<unsafe-coerce>() call, but found ${c.typeArgumentsCount}", c)
2245+
return
2246+
}
2247+
val valueArg = c.getValueArgument(0)
2248+
if (valueArg == null) {
2249+
logger.errorElement("Cannot find value argument for a kotlin.jvm.internal.<unsafe-coerce>() call", c)
2250+
return
2251+
}
2252+
val typeArg = c.getTypeArgument(1)
2253+
if (typeArg == null) {
2254+
logger.errorElement("Cannot find type argument for a kotlin.jvm.internal.<unsafe-coerce>() call", c)
22132255
return
22142256
}
22152257

@@ -2221,8 +2263,8 @@ open class KotlinFileExtractor(
22212263
tw.writeHasLocation(id, locId)
22222264
tw.writeCallableEnclosingExpr(id, callable)
22232265
tw.writeStatementEnclosingExpr(id, enclosingStmt)
2224-
extractTypeAccessRecursive(c.getTypeArgument(1)!!, locId, id, 0, callable, enclosingStmt)
2225-
extractExpressionExpr(c.getValueArgument(0)!!, callable, id, 1, enclosingStmt)
2266+
extractTypeAccessRecursive(typeArg, locId, id, 0, callable, enclosingStmt)
2267+
extractExpressionExpr(valueArg, callable, id, 1, enclosingStmt)
22262268
}
22272269
isBuiltinCallInternal(c, "dataClassArrayMemberToString") -> {
22282270
val arrayArg = c.getValueArgument(0)

0 commit comments

Comments
 (0)