From e06bacafad35c5603115df1f1b6b53a89c58cdab Mon Sep 17 00:00:00 2001 From: pyos Date: Wed, 10 Mar 2021 14:14:21 +0100 Subject: [PATCH] JVM: fix inline cycle detection 1. use the correct descriptor in the old backend; 2. clear the temporary variables for arguments in the IR backend. #KT-45292 Fixed --- .../kotlin/codegen/inline/InlineCodegen.kt | 6 ++---- .../kotlin/codegen/inline/PsiInlineCodegen.kt | 4 ++-- .../backend/jvm/codegen/IrInlineCallGenerator.kt | 13 +++++++------ .../kotlin/backend/jvm/codegen/IrInlineCodegen.kt | 4 ++++ .../testsWithJvmBackend/propertyInlineCycle.kt | 3 +++ .../testsWithJvmBackend/propertyInlineCycle.txt | 3 +++ .../DiagnosticsTestWithJvmIrBackendGenerated.java | 6 ++++++ .../DiagnosticsTestWithOldJvmBackendGenerated.java | 6 ++++++ 8 files changed, 33 insertions(+), 12 deletions(-) create mode 100644 compiler/testData/diagnostics/testsWithJvmBackend/propertyInlineCycle.kt create mode 100644 compiler/testData/diagnostics/testsWithJvmBackend/propertyInlineCycle.txt diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/InlineCodegen.kt b/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/InlineCodegen.kt index 7fcbc4ea5c1..ef9a709cdc4 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/InlineCodegen.kt +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/InlineCodegen.kt @@ -108,11 +108,9 @@ abstract class InlineCodegen( ) } - protected fun generateStub(resolvedCall: ResolvedCall<*>?, codegen: BaseExpressionCodegen) { + protected fun generateStub(text: String, codegen: BaseExpressionCodegen) { leaveTemps() - assert(resolvedCall != null) - val message = "Call is part of inline cycle: " + resolvedCall!!.call.callElement.text - AsmUtil.genThrow(codegen.v, "java/lang/UnsupportedOperationException", message) + AsmUtil.genThrow(codegen.v, "java/lang/UnsupportedOperationException", "Call is part of inline cycle: $text") } protected fun endCall(result: InlineResult, registerLineNumberAfterwards: Boolean) { diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/PsiInlineCodegen.kt b/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/PsiInlineCodegen.kt index 1e6e2aaaa28..c1969075ddd 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/PsiInlineCodegen.kt +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/PsiInlineCodegen.kt @@ -60,8 +60,8 @@ class PsiInlineCodegen( callDefault: Boolean, codegen: ExpressionCodegen ) { - if (!state.globalInlineContext.enterIntoInlining(resolvedCall?.resultingDescriptor, resolvedCall?.call?.callElement)) { - generateStub(resolvedCall, codegen) + if (!state.globalInlineContext.enterIntoInlining(functionDescriptor, resolvedCall?.call?.callElement)) { + generateStub(resolvedCall?.call?.callElement?.text ?: "", codegen) return } try { diff --git a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/codegen/IrInlineCallGenerator.kt b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/codegen/IrInlineCallGenerator.kt index 5950dbd344c..7c6fcedbf75 100644 --- a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/codegen/IrInlineCallGenerator.kt +++ b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/codegen/IrInlineCallGenerator.kt @@ -10,7 +10,6 @@ import org.jetbrains.kotlin.backend.jvm.lower.suspendFunctionOriginal import org.jetbrains.kotlin.codegen.AsmUtil import org.jetbrains.kotlin.ir.descriptors.toIrBasedDescriptor import org.jetbrains.kotlin.ir.expressions.IrFunctionAccessExpression -import org.jetbrains.kotlin.ir.util.render interface IrInlineCallGenerator : IrCallGenerator { override fun genCall( @@ -21,11 +20,9 @@ interface IrInlineCallGenerator : IrCallGenerator { ) { val element = PsiSourceManager.findPsiElement(expression, codegen.irFunction) ?: PsiSourceManager.findPsiElement(codegen.irFunction) - if (!codegen.state.globalInlineContext.enterIntoInlining( - expression.symbol.owner.suspendFunctionOriginal().toIrBasedDescriptor(), element) - ) { - val message = "Call is a part of inline call cycle: ${expression.render()}" - AsmUtil.genThrow(codegen.visitor, "java/lang/UnsupportedOperationException", message) + val descriptor = expression.symbol.owner.suspendFunctionOriginal().toIrBasedDescriptor() + if (!codegen.state.globalInlineContext.enterIntoInlining(descriptor, element)) { + genCycleStub(expression.psiElement?.text ?: "", codegen) return } try { @@ -41,4 +38,8 @@ interface IrInlineCallGenerator : IrCallGenerator { expression: IrFunctionAccessExpression, isInsideIfCondition: Boolean, ) + + fun genCycleStub(text: String, codegen: ExpressionCodegen) { + AsmUtil.genThrow(codegen.visitor, "java/lang/UnsupportedOperationException", "Call is a part of inline call cycle: $text") + } } diff --git a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/codegen/IrInlineCodegen.kt b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/codegen/IrInlineCodegen.kt index 7d946c0eb6a..a0d643d8174 100644 --- a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/codegen/IrInlineCodegen.kt +++ b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/codegen/IrInlineCodegen.kt @@ -153,6 +153,10 @@ class IrInlineCodegen( ) } + override fun genCycleStub(text: String, codegen: ExpressionCodegen) { + generateStub(text, codegen) + } + override fun extractDefaultLambdas(node: MethodNode): List { if (maskStartIndex == -1) return listOf() return expandMaskConditionsAndUpdateVariableNodes( diff --git a/compiler/testData/diagnostics/testsWithJvmBackend/propertyInlineCycle.kt b/compiler/testData/diagnostics/testsWithJvmBackend/propertyInlineCycle.kt new file mode 100644 index 00000000000..a1cd7c78028 --- /dev/null +++ b/compiler/testData/diagnostics/testsWithJvmBackend/propertyInlineCycle.kt @@ -0,0 +1,3 @@ +// TARGET_BACKEND: JVM +inline val String.foo: String + get() = foo diff --git a/compiler/testData/diagnostics/testsWithJvmBackend/propertyInlineCycle.txt b/compiler/testData/diagnostics/testsWithJvmBackend/propertyInlineCycle.txt new file mode 100644 index 00000000000..83b230d4c6c --- /dev/null +++ b/compiler/testData/diagnostics/testsWithJvmBackend/propertyInlineCycle.txt @@ -0,0 +1,3 @@ +package + +public val kotlin.String.foo: kotlin.String diff --git a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/DiagnosticsTestWithJvmIrBackendGenerated.java b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/DiagnosticsTestWithJvmIrBackendGenerated.java index 54c39aef3a2..5df794c0fb2 100644 --- a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/DiagnosticsTestWithJvmIrBackendGenerated.java +++ b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/DiagnosticsTestWithJvmIrBackendGenerated.java @@ -49,6 +49,12 @@ public class DiagnosticsTestWithJvmIrBackendGenerated extends AbstractDiagnostic runTest("compiler/testData/diagnostics/testsWithJvmBackend/noWarningInLV1_5.kt"); } + @Test + @TestMetadata("propertyInlineCycle.kt") + public void testPropertyInlineCycle() throws Exception { + runTest("compiler/testData/diagnostics/testsWithJvmBackend/propertyInlineCycle.kt"); + } + @Test @TestMetadata("suspendInlineCycle_ir.kt") public void testSuspendInlineCycle_ir() throws Exception { diff --git a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/DiagnosticsTestWithOldJvmBackendGenerated.java b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/DiagnosticsTestWithOldJvmBackendGenerated.java index 151ca0cde71..cb5f874a886 100644 --- a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/DiagnosticsTestWithOldJvmBackendGenerated.java +++ b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/DiagnosticsTestWithOldJvmBackendGenerated.java @@ -49,6 +49,12 @@ public class DiagnosticsTestWithOldJvmBackendGenerated extends AbstractDiagnosti runTest("compiler/testData/diagnostics/testsWithJvmBackend/noWarningInLV1_5.kt"); } + @Test + @TestMetadata("propertyInlineCycle.kt") + public void testPropertyInlineCycle() throws Exception { + runTest("compiler/testData/diagnostics/testsWithJvmBackend/propertyInlineCycle.kt"); + } + @Test @TestMetadata("suspendInlineCycle.kt") public void testSuspendInlineCycle() throws Exception {