JVM: generate $assertionsDisabled before inlining the node
This fixes the problem where compiling a class initializer that contains a call to an `assert`ing function in a separate module causes the assertion to always be enabled (i.e. the attached test used to fail in CompileKotlinAgainstInlineKotlin mode).
This commit is contained in:
@@ -231,12 +231,6 @@ abstract class InlineCodegen<out T : BaseExpressionCodegen>(
|
||||
//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<out T : BaseExpressionCodegen>(
|
||||
removeFinallyMarkers(adapter)
|
||||
}
|
||||
|
||||
adapter.accept(MethodBodyVisitor(codegen.v))
|
||||
// In case `codegen.v` is `<clinit>`, 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
|
||||
}
|
||||
|
||||
|
||||
@@ -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"
|
||||
}
|
||||
+5
@@ -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");
|
||||
|
||||
Generated
+5
@@ -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");
|
||||
|
||||
+5
@@ -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");
|
||||
|
||||
Generated
+5
@@ -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");
|
||||
|
||||
Reference in New Issue
Block a user