Skip to content

Commit 58da62e

Browse files
committed
Kotlin: Handle null parent IDs in getFunctionLabel correctly
1 parent f377d25 commit 58da62e

File tree

2 files changed

+47
-14
lines changed

2 files changed

+47
-14
lines changed

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

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,7 @@ open class KotlinFileExtractor(
472472

473473
private fun extractObinitFunction(c: IrClass, parentId: Label<out DbClassorinterface>) {
474474
// add method:
475-
val obinitLabel = getObinitLabel(c)
475+
val obinitLabel = getObinitLabel(c, parentId)
476476
val obinitId = tw.getLabelFor<DbMethod>(obinitLabel)
477477
val returnType = useType(pluginContext.irBuiltIns.unitType, TypeContext.RETURN)
478478
tw.writeMethods(obinitId, "<obinit>", "<obinit>()", returnType.javaResult.id, parentId, obinitId)
@@ -1160,6 +1160,10 @@ open class KotlinFileExtractor(
11601160
return
11611161

11621162
val id = getDefaultsMethodLabel(f)
1163+
if (id == null) {
1164+
logger.errorElement("Cannot get defaults method label for function", f)
1165+
return
1166+
}
11631167
val locId = getLocation(f, null)
11641168
val extReceiver = f.extensionReceiverParameter
11651169
val dispatchReceiver = if (f.shouldExtractAsStatic) null else f.dispatchReceiverParameter
@@ -1284,6 +1288,10 @@ open class KotlinFileExtractor(
12841288
useDeclarationParent(f.parent, false)
12851289
else
12861290
parentId
1291+
if (sourceParentId == null) {
1292+
logger.errorElement("Cannot get source parent ID for function", f)
1293+
return
1294+
}
12871295
val sourceDeclId = tw.getLabelFor<DbCallable>(getFunctionLabel(f, sourceParentId, listOf(), overloadParameters))
12881296
val overriddenAttributes = OverriddenFunctionAttributes(id = overloadId, sourceDeclarationId = sourceDeclId, valueParameters = overloadParameters)
12891297
forceExtractFunction(f, parentId, extractBody = false, extractMethodAndParameterTypeAccesses, extractAnnotations = false, typeSubstitution, classTypeArgsIncludingOuterClasses, overriddenAttributes = overriddenAttributes)
@@ -1301,7 +1309,7 @@ open class KotlinFileExtractor(
13011309
val constructorCallId = tw.getFreshIdLabel<DbConstructorinvocationstmt>()
13021310
tw.writeStmts_constructorinvocationstmt(constructorCallId, blockId, 0, overloadId)
13031311
tw.writeHasLocation(constructorCallId, realFunctionLocId)
1304-
tw.writeCallableBinding(constructorCallId, getDefaultsMethodLabel(f))
1312+
tw.writeCallableBinding(constructorCallId, getDefaultsMethodLabel(f, parentId))
13051313

13061314
extractDefaultsCallArguments(constructorCallId, f, overloadId, constructorCallId, regularArgs, null, null)
13071315
} else {
@@ -2081,13 +2089,23 @@ open class KotlinFileExtractor(
20812089
getFunctionShortName(f).nameInDB + "\$default"
20822090
}
20832091

2084-
private fun getDefaultsMethodLabel(f: IrFunction): Label<out DbCallable> {
2092+
private fun getDefaultsMethodLabel(f: IrFunction): Label<out DbCallable>? {
2093+
val classTypeArgsIncludingOuterClasses = null
2094+
val parentId = useDeclarationParent(f.parent, false, classTypeArgsIncludingOuterClasses, true)
2095+
if (parentId == null) {
2096+
logger.errorElement("Couldn't get parent ID for defaults method", f)
2097+
return null
2098+
}
2099+
return getDefaultsMethodLabel(f, parentId)
2100+
}
2101+
2102+
private fun getDefaultsMethodLabel(f: IrFunction, parentId: Label<out DbElement>): Label<out DbCallable> {
20852103
val defaultsMethodName = if (f is IrConstructor) "<init>" else getDefaultsMethodName(f)
20862104
val argTypes = getDefaultsMethodArgTypes(f)
20872105

20882106
val defaultMethodLabelStr = getFunctionLabel(
20892107
f.parent,
2090-
maybeParentId = null,
2108+
parentId,
20912109
defaultsMethodName,
20922110
argTypes,
20932111
erase(f.returnType),
@@ -3300,9 +3318,9 @@ open class KotlinFileExtractor(
33003318

33013319
private fun needsObinitFunction(c: IrClass) = c.primaryConstructor == null && c.constructors.count() > 1
33023320

3303-
private fun getObinitLabel(c: IrClass) = getFunctionLabel(
3321+
private fun getObinitLabel(c: IrClass, parentId: Label<out DbElement>): String = getFunctionLabel(
33043322
c,
3305-
null,
3323+
parentId,
33063324
"<obinit>",
33073325
listOf(),
33083326
pluginContext.irBuiltIns.unitType,
@@ -3332,7 +3350,12 @@ open class KotlinFileExtractor(
33323350
val valueArgs = (0 until e.valueArgumentsCount).map { e.getValueArgument(it) }
33333351

33343352
val id = if (e !is IrEnumConstructorCall && callUsesDefaultArguments(e.symbol.owner, valueArgs)) {
3335-
extractNewExpr(getDefaultsMethodLabel(e.symbol.owner).cast(), type, locId, parent, idx, callable, enclosingStmt).also {
3353+
val defaultsMethodId = getDefaultsMethodLabel(e.symbol.owner)
3354+
if (defaultsMethodId == null) {
3355+
logger.errorElement("Cannot get defaults method ID", e)
3356+
return
3357+
}
3358+
extractNewExpr(defaultsMethodId.cast(), type, locId, parent, idx, callable, enclosingStmt).also {
33363359
extractDefaultsCallArguments(it, e.symbol.owner, callable, enclosingStmt, valueArgs, null, null)
33373360
}
33383361
} else {
@@ -3817,7 +3840,13 @@ open class KotlinFileExtractor(
38173840
val id = tw.getFreshIdLabel<DbMethodaccess>()
38183841
val type = useType(pluginContext.irBuiltIns.unitType)
38193842
val locId = tw.getLocation(e)
3820-
val methodLabel = getObinitLabel(irConstructor.parentAsClass)
3843+
val parentClass = irConstructor.parentAsClass
3844+
val parentId = useDeclarationParent(parentClass, false, null, true)
3845+
if (parentId == null) {
3846+
logger.errorElement("Cannot get parent ID for obinit", e)
3847+
return
3848+
}
3849+
val methodLabel = getObinitLabel(parentClass, parentId)
38213850
val methodId = tw.getLabelFor<DbMethod>(methodLabel)
38223851
tw.writeExprs_methodaccess(id, type.javaResult.id, exprParent.parent, exprParent.idx)
38233852
tw.writeExprsKotlinType(id, type.kotlinResult.id)

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1034,8 +1034,13 @@ open class KotlinUsesExtractor(
10341034
* enclosing classes to get the instantiation that this function is
10351035
* in.
10361036
*/
1037-
fun getFunctionLabel(f: IrFunction, classTypeArgsIncludingOuterClasses: List<IrTypeArgument>?) : String {
1038-
return getFunctionLabel(f, null, classTypeArgsIncludingOuterClasses)
1037+
fun getFunctionLabel(f: IrFunction, classTypeArgsIncludingOuterClasses: List<IrTypeArgument>?): String? {
1038+
val parentId = useDeclarationParent(f.parent, false, classTypeArgsIncludingOuterClasses, true)
1039+
if (parentId == null) {
1040+
logger.error("Couldn't get parent ID for function label")
1041+
return null
1042+
}
1043+
return getFunctionLabel(f, parentId, classTypeArgsIncludingOuterClasses)
10391044
}
10401045

10411046
/*
@@ -1052,10 +1057,10 @@ open class KotlinUsesExtractor(
10521057
* that omit one or more parameters that has a default value specified.
10531058
*/
10541059
@OptIn(ObsoleteDescriptorBasedAPI::class)
1055-
fun getFunctionLabel(f: IrFunction, maybeParentId: Label<out DbElement>?, classTypeArgsIncludingOuterClasses: List<IrTypeArgument>?, maybeParameterList: List<IrValueParameter>? = null) =
1060+
fun getFunctionLabel(f: IrFunction, parentId: Label<out DbElement>, classTypeArgsIncludingOuterClasses: List<IrTypeArgument>?, maybeParameterList: List<IrValueParameter>? = null): String =
10561061
getFunctionLabel(
10571062
f.parent,
1058-
maybeParentId,
1063+
parentId,
10591064
getFunctionShortName(f).nameInDB,
10601065
(maybeParameterList ?: f.valueParameters).map { it.type },
10611066
getAdjustedReturnType(f),
@@ -1078,7 +1083,7 @@ open class KotlinUsesExtractor(
10781083
// The parent of the function; normally f.parent.
10791084
parent: IrDeclarationParent,
10801085
// The ID of the function's parent, or null if we should work it out ourselves.
1081-
maybeParentId: Label<out DbElement>?,
1086+
parentId: Label<out DbElement>,
10821087
// The name of the function; normally f.name.asString().
10831088
name: String,
10841089
// The types of the value parameters that the functions takes; normally f.valueParameters.map { it.type }.
@@ -1102,7 +1107,6 @@ open class KotlinUsesExtractor(
11021107
// The prefix used in the label. "callable", unless a property label is created, then it's "property".
11031108
prefix: String = "callable"
11041109
): String {
1105-
val parentId = maybeParentId ?: useDeclarationParent(parent, false, classTypeArgsIncludingOuterClasses, true)
11061110
val allParamTypes = if (extensionParamType == null) parameterTypes else listOf(extensionParamType) + parameterTypes
11071111

11081112
val substitutionMap = classTypeArgsIncludingOuterClasses?.let { notNullArgs ->

0 commit comments

Comments
 (0)