Skip to content

Commit 2728517

Browse files
committed
Improve error handling
1 parent 0f96706 commit 2728517

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3709,7 +3709,7 @@ open class KotlinFileExtractor(
37093709
constructorBlock = tw.getFreshIdLabel()
37103710
)
37113711

3712-
val declarationParent = declarationStack.peekAsDeclarationParent()
3712+
val declarationParent = declarationStack.peekAsDeclarationParent(propertyReferenceExpr) ?: return
37133713
val prefix = if (kPropertyClass.owner.name.asString().startsWith("KMutableProperty")) "Mutable" else ""
37143714
val baseClass = pluginContext.referenceClass(FqName("kotlin.jvm.internal.${prefix}PropertyReference${kPropertyType.arguments.size - 1}"))?.owner?.typeWith()
37153715
?: pluginContext.irBuiltIns.anyType
@@ -3916,7 +3916,7 @@ open class KotlinFileExtractor(
39163916
if (fnInterfaceType == null) {
39173917
logger.warnElement("Cannot find functional interface type for function reference", functionReferenceExpr)
39183918
} else {
3919-
val declarationParent = declarationStack.peekAsDeclarationParent()
3919+
val declarationParent = declarationStack.peekAsDeclarationParent(functionReferenceExpr) ?: return
39203920
// `FunctionReference` base class is required, because that's implementing `KFunction`.
39213921
val baseClass = pluginContext.referenceClass(FqName("kotlin.jvm.internal.FunctionReference"))?.owner?.typeWith()
39223922
?: pluginContext.irBuiltIns.anyType
@@ -4525,7 +4525,7 @@ open class KotlinFileExtractor(
45254525
val locId = tw.getLocation(e)
45264526
val helper = GeneratedClassHelper(locId, ids)
45274527

4528-
val declarationParent = declarationStack.peekAsDeclarationParent()
4528+
val declarationParent = declarationStack.peekAsDeclarationParent(e) ?: return
45294529
val classId = extractGeneratedClass(ids, listOf(pluginContext.irBuiltIns.anyType, e.typeOperand), locId, e, declarationParent)
45304530

45314531
// add field
@@ -4747,14 +4747,18 @@ open class KotlinFileExtractor(
47474747

47484748
fun peek() = stack.peek()
47494749

4750-
fun peekAsDeclarationParent(): IrDeclarationParent {
4750+
fun peekAsDeclarationParent(elementToReportOn: IrElement): IrDeclarationParent? {
47514751
val trapWriter = tw
47524752
if (isEmpty() && trapWriter is SourceFileTrapWriter) {
47534753
// If the current declaration is used as a parent, we might end up with an empty stack. In this case, the source file is the parent.
47544754
return trapWriter.irFile
47554755
}
47564756

4757-
return peek() as IrDeclarationParent
4757+
val dp = peek() as? IrDeclarationParent
4758+
if (dp == null) {
4759+
logger.errorElement("Couldn't find current declaration parent", elementToReportOn)
4760+
}
4761+
return dp
47584762
}
47594763
}
47604764

0 commit comments

Comments
 (0)