From 2b4dc1199aba87e2b028df2b5577c46cc14432e7 Mon Sep 17 00:00:00 2001 From: Alexander Udalov Date: Fri, 10 Jan 2020 15:37:00 +0100 Subject: [PATCH] JVM IR: fix monitorEnterMonitorExit.kt After an inaccurate merge of 31936890 and d6ed93b2, this test was now failing with the newly added assertion. It seems that currently the best way to handle it would be to special-case these two methods in SyntheticAccessorLowering.kt. --- .../jvm/lower/SyntheticAccessorLowering.kt | 8 +++++++- .../src/org/jetbrains/kotlin/ir/util/IrUtils.kt | 17 +++++++++-------- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/SyntheticAccessorLowering.kt b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/SyntheticAccessorLowering.kt index d03a95e312b..fee3cb26c38 100644 --- a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/SyntheticAccessorLowering.kt +++ b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/SyntheticAccessorLowering.kt @@ -48,7 +48,7 @@ internal class SyntheticAccessorLowering(val context: JvmBackendContext) : IrEle irFile.transformChildrenVoid(this) for (accessor in pendingAccessorsToAdd) { - assert(accessor.file == irFile) { + assert(accessor.fileOrNull == irFile || accessor.isAllowedToBeAddedToForeignFile()) { "SyntheticAccessorLowering should not attempt to modify other files!\n" + "While lowering this file: ${irFile.render()}\n" + "Trying to add this accessor: ${accessor.render()}" @@ -560,4 +560,10 @@ internal class SyntheticAccessorLowering(val context: JvmBackendContext) : IrEle else -> true } } + + // monitorEnter/monitorExit are the only functions which are accessed "illegally" (see kotlin/util/Synchronized.kt). + // Since they are intrinsified in the codegen, SyntheticAccessorLowering should not crash on attempt to add accessors for them. + private fun IrFunction.isAllowedToBeAddedToForeignFile(): Boolean = + (name.asString() == "access\$monitorEnter" || name.asString() == "access\$monitorExit") && + context.irIntrinsics.getIntrinsic(symbol) != null } diff --git a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/util/IrUtils.kt b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/util/IrUtils.kt index 6800c8635d4..eee4782b031 100644 --- a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/util/IrUtils.kt +++ b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/util/IrUtils.kt @@ -600,16 +600,17 @@ private fun IrMemberAccessExpression.copyTypeAndValueArgumentsFrom( } } -val IrDeclaration.file: IrFile - get() = parent.let { - when (it) { - is IrFile -> it - is IrPackageFragment -> TODO("Unknown file") - is IrDeclaration -> it.file - else -> TODO("Unexpected declaration parent") - } +val IrDeclaration.fileOrNull: IrFile? + get() = when (val parent = parent) { + is IrFile -> parent + is IrPackageFragment -> null + is IrDeclaration -> parent.fileOrNull + else -> TODO("Unexpected declaration parent") } +val IrDeclaration.file: IrFile + get() = fileOrNull ?: TODO("Unknown file") + val IrFunction.allTypeParameters: List get() = if (this is IrConstructor) parentAsClass.typeParameters + typeParameters