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 2308c3774ee..91e9bbd4555 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/InlineCodegen.kt +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/InlineCodegen.kt @@ -231,12 +231,6 @@ abstract class InlineCodegen( //through generation captured parameters will be added to invocationParamBuilder putClosureParametersOnStack() - val shouldSpillStack = !canSkipStackSpillingOnInline(node) - - if (shouldSpillStack) { - addInlineMarker(codegen.v, true) - } - val parameters = invocationParamBuilder.buildParameters() val info = RootInliningContext( @@ -271,16 +265,21 @@ abstract class InlineCodegen( removeFinallyMarkers(adapter) } - adapter.accept(MethodBodyVisitor(codegen.v)) + // In case `codegen.v` is ``, initializer for the `$assertionsDisabled` field + // needs to be inserted before the code that actually uses it. + generateAssertFieldIfNeeded(info) + val shouldSpillStack = !canSkipStackSpillingOnInline(node) + if (shouldSpillStack) { + addInlineMarker(codegen.v, true) + } + adapter.accept(MethodBodyVisitor(codegen.v)) if (shouldSpillStack) { addInlineMarker(codegen.v, false) } defaultSourceMapper.callSiteMarker = null - generateAssertFieldIfNeeded(info) - return result } diff --git a/compiler/testData/codegen/boxInline/assert/jvmClassInitializer.kt b/compiler/testData/codegen/boxInline/assert/jvmClassInitializer.kt new file mode 100644 index 00000000000..4459ad7884c --- /dev/null +++ b/compiler/testData/codegen/boxInline/assert/jvmClassInitializer.kt @@ -0,0 +1,41 @@ +// TARGET_BACKEND: JVM +// IGNORE_BACKEND: JVM_IR +// IGNORE_BACKEND_MULTI_MODULE: JVM_IR +// FILE: inline.kt +// KOTLIN_CONFIGURATION_FLAGS: ASSERTIONS_MODE=jvm +// WITH_RUNTIME +// FULL_JDK +package test + +class A { + inline fun doAssert() { + assert(false) + } +} + +// FILE: inlineSite.kt +// KOTLIN_CONFIGURATION_FLAGS: ASSERTIONS_MODE=jvm +import test.* + +class B { + companion object { + @JvmField + val triggered: Boolean = try { + A().doAssert() + false + } catch (e: AssertionError) { + true + } + } +} + +class Dummy + +fun box(): String { + val loader = Dummy::class.java.classLoader + loader.setDefaultAssertionStatus(false) + return if (loader.loadClass("B").getField("triggered").get(null) == true) + "FAIL: assertion triggered" + else + "OK" +} diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxInlineCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxInlineCodegenTestGenerated.java index 27b3cb9e0a9..62ec4ab3561 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxInlineCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxInlineCodegenTestGenerated.java @@ -640,6 +640,11 @@ public class BlackBoxInlineCodegenTestGenerated extends AbstractBlackBoxInlineCo runTest("compiler/testData/codegen/boxInline/assert/jvmAssertInlineLambda.kt"); } + @TestMetadata("jvmClassInitializer.kt") + public void testJvmClassInitializer() throws Exception { + runTest("compiler/testData/codegen/boxInline/assert/jvmClassInitializer.kt"); + } + @TestMetadata("jvmCompanion.kt") public void testJvmCompanion() throws Exception { runTest("compiler/testData/codegen/boxInline/assert/jvmCompanion.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/CompileKotlinAgainstInlineKotlinTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/CompileKotlinAgainstInlineKotlinTestGenerated.java index 55dc9663bff..ac81eda295b 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/CompileKotlinAgainstInlineKotlinTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/CompileKotlinAgainstInlineKotlinTestGenerated.java @@ -640,6 +640,11 @@ public class CompileKotlinAgainstInlineKotlinTestGenerated extends AbstractCompi runTest("compiler/testData/codegen/boxInline/assert/jvmAssertInlineLambda.kt"); } + @TestMetadata("jvmClassInitializer.kt") + public void testJvmClassInitializer() throws Exception { + runTest("compiler/testData/codegen/boxInline/assert/jvmClassInitializer.kt"); + } + @TestMetadata("jvmCompanion.kt") public void testJvmCompanion() throws Exception { runTest("compiler/testData/codegen/boxInline/assert/jvmCompanion.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxInlineCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxInlineCodegenTestGenerated.java index cdabfa2b0e1..26a85313ae9 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxInlineCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxInlineCodegenTestGenerated.java @@ -640,6 +640,11 @@ public class IrBlackBoxInlineCodegenTestGenerated extends AbstractIrBlackBoxInli runTest("compiler/testData/codegen/boxInline/assert/jvmAssertInlineLambda.kt"); } + @TestMetadata("jvmClassInitializer.kt") + public void testJvmClassInitializer() throws Exception { + runTest("compiler/testData/codegen/boxInline/assert/jvmClassInitializer.kt"); + } + @TestMetadata("jvmCompanion.kt") public void testJvmCompanion() throws Exception { runTest("compiler/testData/codegen/boxInline/assert/jvmCompanion.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrCompileKotlinAgainstInlineKotlinTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrCompileKotlinAgainstInlineKotlinTestGenerated.java index 3098880f328..0a4af37ec62 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrCompileKotlinAgainstInlineKotlinTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrCompileKotlinAgainstInlineKotlinTestGenerated.java @@ -640,6 +640,11 @@ public class IrCompileKotlinAgainstInlineKotlinTestGenerated extends AbstractIrC runTest("compiler/testData/codegen/boxInline/assert/jvmAssertInlineLambda.kt"); } + @TestMetadata("jvmClassInitializer.kt") + public void testJvmClassInitializer() throws Exception { + runTest("compiler/testData/codegen/boxInline/assert/jvmClassInitializer.kt"); + } + @TestMetadata("jvmCompanion.kt") public void testJvmCompanion() throws Exception { runTest("compiler/testData/codegen/boxInline/assert/jvmCompanion.kt");