Add more tests for -Xassertions=jvm corner cases

This commit is contained in:
Steven Schäfer
2019-07-05 15:24:54 +02:00
committed by max-kammerer
parent 3e25166832
commit d11344ce2e
15 changed files with 491 additions and 1 deletions
@@ -0,0 +1,54 @@
// IGNORE_BACKEND: JVM_IR
// TARGET_BACKEND: JVM
// KOTLIN_CONFIGURATION_FLAGS: ASSERTIONS_MODE=jvm
// WITH_RUNTIME
// Assertions which run before the class initializer are always checked
package initializerAssertionsEnabled
class Checker {
fun test() = Baz.testAsserts()
}
open class Bar {
companion object {
val barAssertionThrown = try {
Baz().assertFalse()
false
} catch(error: java.lang.AssertionError) {
true
}
}
}
class Baz : Bar() {
fun assertFalse() = assert(false)
companion object {
val bazAssertionThrown = try {
Baz().assertFalse()
false
} catch(error: java.lang.AssertionError) {
true
}
fun testAsserts(): String {
if (!barAssertionThrown) return "Fail 1"
if (bazAssertionThrown) return "Fail 2"
return "OK"
}
}
}
class Dummy
fun disableAssertions(): Checker {
val loader = Dummy::class.java.classLoader
loader.setPackageAssertionStatus("initializerAssertionsEnabled", false)
return loader.loadClass("initializerAssertionsEnabled.Checker").newInstance() as Checker
}
fun box(): String {
return disableAssertions().test()
}
@@ -0,0 +1,34 @@
// TARGET_BACKEND: JVM
// KOTLIN_CONFIGURATION_FLAGS: ASSERTIONS_MODE=jvm
// WITH_RUNTIME
package classAssertions
class ShouldBeEnabled {
fun checkTrue(): Boolean {
var hit = false
assert({ hit = true; true }())
return hit
}
}
class ShouldBeDisabled {
fun checkFalse(): Boolean {
var hit = false
assert({ hit = true; true }())
return hit
}
}
class Dummy
fun box(): String {
val loader = Dummy::class.java.classLoader
loader.setClassAssertionStatus("classAssertions.ShouldBeEnabled", true)
loader.setClassAssertionStatus("classAssertions.ShouldBeDisabled", false)
val c1 = loader.loadClass("classAssertions.ShouldBeEnabled").newInstance() as ShouldBeEnabled
val c2 = loader.loadClass("classAssertions.ShouldBeDisabled").newInstance() as ShouldBeDisabled
if (!c1.checkTrue()) return "FAIL 0"
if (c2.checkFalse()) return "FAIL 1"
return "OK"
}
@@ -0,0 +1,40 @@
// TARGET_BACKEND: JVM
// KOTLIN_CONFIGURATION_FLAGS: ASSERTIONS_MODE=jvm
// WITH_RUNTIME
package classAssertions
class ShouldBeEnabled {
fun checkTrue() = ShouldBeEnabled.hit
companion object {
var hit = false
init {
assert({ hit = true; true }())
}
}
}
class ShouldBeDisabled {
fun checkFalse() = ShouldBeDisabled.hit
companion object {
var hit = false
init {
assert({ hit = true; true }())
}
}
}
class Dummy
fun box(): String {
val loader = Dummy::class.java.classLoader
loader.setClassAssertionStatus("classAssertions.ShouldBeEnabled", true)
loader.setClassAssertionStatus("classAssertions.ShouldBeDisabled", false)
val c1 = loader.loadClass("classAssertions.ShouldBeEnabled").newInstance() as ShouldBeEnabled
val c2 = loader.loadClass("classAssertions.ShouldBeDisabled").newInstance() as ShouldBeDisabled
if (!c1.checkTrue()) return "FAIL 0"
if (c2.checkFalse()) return "FAIL 1"
return "OK"
}
@@ -0,0 +1,41 @@
// TARGET_BACKEND: JVM
// IGNORE_BACKEND: JVM
// KOTLIN_CONFIGURATION_FLAGS: ASSERTIONS_MODE=jvm
// WITH_RUNTIME
package classAssertions
class ShouldBeEnabled {
fun checkTrue() = Inner().hit
inner class Inner {
var hit = false
init {
assert({ hit = true; true }())
}
}
}
class ShouldBeDisabled {
fun checkFalse() = Inner().hit
inner class Inner {
var hit = false
init {
assert({ hit = true; true }())
}
}
}
class Dummy
fun box(): String {
val loader = Dummy::class.java.classLoader
loader.setClassAssertionStatus("classAssertions.ShouldBeEnabled", true)
loader.setClassAssertionStatus("classAssertions.ShouldBeDisabled", false)
val c1 = loader.loadClass("classAssertions.ShouldBeEnabled").newInstance() as ShouldBeEnabled
val c2 = loader.loadClass("classAssertions.ShouldBeDisabled").newInstance() as ShouldBeDisabled
if (!c1.checkTrue()) return "FAIL 0"
if (c2.checkFalse()) return "FAIL 1"
return "OK"
}
@@ -0,0 +1,43 @@
// TARGET_BACKEND: JVM
// IGNORE_BACKEND: JVM
// KOTLIN_CONFIGURATION_FLAGS: ASSERTIONS_MODE=jvm
// WITH_RUNTIME
package classAssertions
class ShouldBeEnabled {
fun checkTrue(): Boolean {
class Local {
var hit = false
init {
assert({ hit = true; true}())
}
}
return Local().hit
}
}
class ShouldBeDisabled {
fun checkFalse(): Boolean {
class Local {
var hit = false
init {
assert({ hit = true; true}())
}
}
return Local().hit
}
}
class Dummy
fun box(): String {
val loader = Dummy::class.java.classLoader
loader.setClassAssertionStatus("classAssertions.ShouldBeEnabled", true)
loader.setClassAssertionStatus("classAssertions.ShouldBeDisabled", false)
val c1 = loader.loadClass("classAssertions.ShouldBeEnabled").newInstance() as ShouldBeEnabled
val c2 = loader.loadClass("classAssertions.ShouldBeDisabled").newInstance() as ShouldBeDisabled
if (!c1.checkTrue()) return "FAIL 0"
if (c2.checkFalse()) return "FAIL 1"
return "OK"
}
@@ -0,0 +1,47 @@
// TARGET_BACKEND: JVM
// IGNORE_BACKEND: JVM
// KOTLIN_CONFIGURATION_FLAGS: ASSERTIONS_MODE=jvm
// WITH_RUNTIME
package classAssertions
class ShouldBeEnabled {
fun checkTrue() = A.B().hit
class A {
class B {
var hit = false
init {
assert({ hit = true; true }())
}
}
}
}
class ShouldBeDisabled {
fun checkFalse() = A.B().hit
class A {
class B {
var hit = false
init {
assert({ hit = true; true }())
}
}
}
}
class Dummy
fun box(): String {
val loader = Dummy::class.java.classLoader
loader.setClassAssertionStatus("classAssertions.ShouldBeEnabled", true)
loader.setClassAssertionStatus("classAssertions.ShouldBeDisabled", false)
val c1 = loader.loadClass("classAssertions.ShouldBeEnabled").newInstance() as ShouldBeEnabled
val c2 = loader.loadClass("classAssertions.ShouldBeDisabled").newInstance() as ShouldBeDisabled
if (!c1.checkTrue()) return "FAIL 0"
if (c2.checkFalse()) return "FAIL 1"
return "OK"
}
@@ -0,0 +1,51 @@
// IGNORE_BACKEND: JVM_IR
// TARGET_BACKEND: JVM
// KOTLIN_CONFIGURATION_FLAGS: ASSERTIONS_MODE=jvm
// WITH_RUNTIME
// If assertions are disabled, neither argument to assert should be evaluated.
// If assertions are enabled, both arguments should be evaluate to values before
// checking the assertion.
package assertions
interface Checker {
fun check(): Boolean
}
class Checker1 : Checker {
override fun check(): Boolean {
var result = true
assert(true, {
result = false
{ "Assertion failure" }
}())
return result
}
}
class Checker2 : Checker {
override fun check(): Boolean {
var result = true
assert(true, {
result = false
{ "Assertion failure" }
}())
return result
}
}
fun checkerWithAssertions(enabled: Boolean): Checker {
val loader = Checker::class.java.classLoader
loader.setPackageAssertionStatus("assertions", enabled)
val c = loader.loadClass(if (enabled) "assertions.Checker1" else "assertions.Checker2")
return c.newInstance() as Checker
}
fun box(): String {
var c = checkerWithAssertions(true)
if (c.check()) return "Fail 1"
c = checkerWithAssertions(false)
if (!c.check()) return "Fail 2"
return "OK"
}
@@ -0,0 +1,26 @@
// TARGET_BACKEND: JVM
// KOTLIN_CONFIGURATION_FLAGS: ASSERTIONS_MODE=jvm
// WITH_RUNTIME
// Reusing the $assertionsDisabled field in the Outer class might seem like a good idea,
// but it would result in an error in this case.
class Outer {
companion object {
init { error("") }
}
init { assert(true) }
class Inner {
init { assert(true) }
}
}
fun box(): String {
try {
Outer.Inner()
} catch (e: Throwable) {
return "Fail"
}
return "OK"
}
@@ -1,3 +1,4 @@
// IGNORE_BACKEND: JVM
// IGNORE_BACKEND: JVM_IR
// KOTLIN_CONFIGURATION_FLAGS: ASSERTIONS_MODE=jvm
@@ -15,7 +16,7 @@ class A {
// inlineSite:
// 1 GETSTATIC A\$inlineSite\$\$inlined\$inlineMe\$1.\$assertionsDisabled
// A.<clinit>:
// 1 LDC LA\$inlineSite\$\$inlined\$inlineMe\$1;.class\s*INVOKEVIRTUAL java/lang/Class.desiredAssertionStatus \(\)Z
// 1 LDC LA;.class\s*INVOKEVIRTUAL java/lang/Class.desiredAssertionStatus \(\)Z
// 1 PUTSTATIC A\$inlineSite\$\$inlined\$inlineMe\$1.\$assertionsDisabled : Z
// in declaration site and in inline site
// 2 INVOKEVIRTUAL java/lang/Class.desiredAssertionStatus \(\)Z
@@ -0,0 +1,23 @@
// IGNORE_BACKEND: JVM
// IGNORE_BACKEND: JVM_IR
// KOTLIN_CONFIGURATION_FLAGS: ASSERTIONS_MODE=jvm
class Outer {
class Inner {
fun f() { assert(true) }
}
}
// We set the assertion status based on top-level classes.
// 0 LDC LOuter\$Inner;.class
// 1 desiredAssertionStatus
// The assertion disabled field should be package local.
// 1 final static synthetic Z \$assertionsDisabled
// 0 public final static synthetic Z \$assertionsDisabled
// 0 protected final static synthetic Z \$assertionsDisabled
// 0 private final static synthetic Z \$assertionsDisabled
// Outer\$Inner.<clinit>:
// 1 LDC LOuter;.class\s*INVOKEVIRTUAL java/lang/Class.desiredAssertionStatus \(\)Z
// 1 PUTSTATIC Outer\$Inner.\$assertionsDisabled : Z
// Outer\$Inner.f:
// 1 GETSTATIC Outer\$Inner.\$assertionsDisabled
@@ -854,6 +854,36 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/box/assert/jvm"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JVM, true);
}
@TestMetadata("assertionsEnabledBeforeClassInitializers.kt")
public void testAssertionsEnabledBeforeClassInitializers() throws Exception {
runTest("compiler/testData/codegen/box/assert/jvm/assertionsEnabledBeforeClassInitializers.kt");
}
@TestMetadata("classAssertions.kt")
public void testClassAssertions() throws Exception {
runTest("compiler/testData/codegen/box/assert/jvm/classAssertions.kt");
}
@TestMetadata("classAssertionsForCompanion.kt")
public void testClassAssertionsForCompanion() throws Exception {
runTest("compiler/testData/codegen/box/assert/jvm/classAssertionsForCompanion.kt");
}
@TestMetadata("classAssertionsForInnerClasses.kt")
public void testClassAssertionsForInnerClasses() throws Exception {
runTest("compiler/testData/codegen/box/assert/jvm/classAssertionsForInnerClasses.kt");
}
@TestMetadata("classAssertionsForLocalClasses.kt")
public void testClassAssertionsForLocalClasses() throws Exception {
runTest("compiler/testData/codegen/box/assert/jvm/classAssertionsForLocalClasses.kt");
}
@TestMetadata("classAssertionsForNestedClasses.kt")
public void testClassAssertionsForNestedClasses() throws Exception {
runTest("compiler/testData/codegen/box/assert/jvm/classAssertionsForNestedClasses.kt");
}
@TestMetadata("interfaceAssertionsDisabled.kt")
public void testInterfaceAssertionsDisabled() throws Exception {
runTest("compiler/testData/codegen/box/assert/jvm/interfaceAssertionsDisabled.kt");
@@ -864,6 +894,11 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
runTest("compiler/testData/codegen/box/assert/jvm/interfaceAssertionsEnabled.kt");
}
@TestMetadata("lambdaNotEvaluated.kt")
public void testLambdaNotEvaluated() throws Exception {
runTest("compiler/testData/codegen/box/assert/jvm/lambdaNotEvaluated.kt");
}
@TestMetadata("localAnonymousFunction.kt")
public void testLocalAnonymousFunction() throws Exception {
runTest("compiler/testData/codegen/box/assert/jvm/localAnonymousFunction.kt");
@@ -889,6 +924,11 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
runTest("compiler/testData/codegen/box/assert/jvm/localObject.kt");
}
@TestMetadata("noUnnecessaryClassInitialization.kt")
public void testNoUnnecessaryClassInitialization() throws Exception {
runTest("compiler/testData/codegen/box/assert/jvm/noUnnecessaryClassInitialization.kt");
}
@TestMetadata("nonLocalReturn.kt")
public void testNonLocalReturn() throws Exception {
runTest("compiler/testData/codegen/box/assert/jvm/nonLocalReturn.kt");
@@ -433,6 +433,11 @@ public class BytecodeTextTestGenerated extends AbstractBytecodeTextTest {
public void testJvmInlineLambda() throws Exception {
runTest("compiler/testData/codegen/bytecodeText/assert/jvmInlineLambda.kt");
}
@TestMetadata("jvmNestedClass.kt")
public void testJvmNestedClass() throws Exception {
runTest("compiler/testData/codegen/bytecodeText/assert/jvmNestedClass.kt");
}
}
@TestMetadata("compiler/testData/codegen/bytecodeText/boxing")
@@ -842,6 +842,21 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class Jvm extends AbstractLightAnalysisModeTest {
@TestMetadata("classAssertionsForInnerClasses.kt")
public void ignoreClassAssertionsForInnerClasses() throws Exception {
runTest("compiler/testData/codegen/box/assert/jvm/classAssertionsForInnerClasses.kt");
}
@TestMetadata("classAssertionsForLocalClasses.kt")
public void ignoreClassAssertionsForLocalClasses() throws Exception {
runTest("compiler/testData/codegen/box/assert/jvm/classAssertionsForLocalClasses.kt");
}
@TestMetadata("classAssertionsForNestedClasses.kt")
public void ignoreClassAssertionsForNestedClasses() throws Exception {
runTest("compiler/testData/codegen/box/assert/jvm/classAssertionsForNestedClasses.kt");
}
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doTest, TargetBackend.JVM, testDataFilePath);
}
@@ -854,6 +869,21 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/box/assert/jvm"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JVM, true);
}
@TestMetadata("assertionsEnabledBeforeClassInitializers.kt")
public void testAssertionsEnabledBeforeClassInitializers() throws Exception {
runTest("compiler/testData/codegen/box/assert/jvm/assertionsEnabledBeforeClassInitializers.kt");
}
@TestMetadata("classAssertions.kt")
public void testClassAssertions() throws Exception {
runTest("compiler/testData/codegen/box/assert/jvm/classAssertions.kt");
}
@TestMetadata("classAssertionsForCompanion.kt")
public void testClassAssertionsForCompanion() throws Exception {
runTest("compiler/testData/codegen/box/assert/jvm/classAssertionsForCompanion.kt");
}
@TestMetadata("interfaceAssertionsDisabled.kt")
public void testInterfaceAssertionsDisabled() throws Exception {
runTest("compiler/testData/codegen/box/assert/jvm/interfaceAssertionsDisabled.kt");
@@ -864,6 +894,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes
runTest("compiler/testData/codegen/box/assert/jvm/interfaceAssertionsEnabled.kt");
}
@TestMetadata("lambdaNotEvaluated.kt")
public void testLambdaNotEvaluated() throws Exception {
runTest("compiler/testData/codegen/box/assert/jvm/lambdaNotEvaluated.kt");
}
@TestMetadata("localAnonymousFunction.kt")
public void testLocalAnonymousFunction() throws Exception {
runTest("compiler/testData/codegen/box/assert/jvm/localAnonymousFunction.kt");
@@ -889,6 +924,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes
runTest("compiler/testData/codegen/box/assert/jvm/localObject.kt");
}
@TestMetadata("noUnnecessaryClassInitialization.kt")
public void testNoUnnecessaryClassInitialization() throws Exception {
runTest("compiler/testData/codegen/box/assert/jvm/noUnnecessaryClassInitialization.kt");
}
@TestMetadata("nonLocalReturn.kt")
public void testNonLocalReturn() throws Exception {
runTest("compiler/testData/codegen/box/assert/jvm/nonLocalReturn.kt");
@@ -854,6 +854,36 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/box/assert/jvm"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JVM_IR, true);
}
@TestMetadata("assertionsEnabledBeforeClassInitializers.kt")
public void testAssertionsEnabledBeforeClassInitializers() throws Exception {
runTest("compiler/testData/codegen/box/assert/jvm/assertionsEnabledBeforeClassInitializers.kt");
}
@TestMetadata("classAssertions.kt")
public void testClassAssertions() throws Exception {
runTest("compiler/testData/codegen/box/assert/jvm/classAssertions.kt");
}
@TestMetadata("classAssertionsForCompanion.kt")
public void testClassAssertionsForCompanion() throws Exception {
runTest("compiler/testData/codegen/box/assert/jvm/classAssertionsForCompanion.kt");
}
@TestMetadata("classAssertionsForInnerClasses.kt")
public void testClassAssertionsForInnerClasses() throws Exception {
runTest("compiler/testData/codegen/box/assert/jvm/classAssertionsForInnerClasses.kt");
}
@TestMetadata("classAssertionsForLocalClasses.kt")
public void testClassAssertionsForLocalClasses() throws Exception {
runTest("compiler/testData/codegen/box/assert/jvm/classAssertionsForLocalClasses.kt");
}
@TestMetadata("classAssertionsForNestedClasses.kt")
public void testClassAssertionsForNestedClasses() throws Exception {
runTest("compiler/testData/codegen/box/assert/jvm/classAssertionsForNestedClasses.kt");
}
@TestMetadata("interfaceAssertionsDisabled.kt")
public void testInterfaceAssertionsDisabled() throws Exception {
runTest("compiler/testData/codegen/box/assert/jvm/interfaceAssertionsDisabled.kt");
@@ -864,6 +894,11 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
runTest("compiler/testData/codegen/box/assert/jvm/interfaceAssertionsEnabled.kt");
}
@TestMetadata("lambdaNotEvaluated.kt")
public void testLambdaNotEvaluated() throws Exception {
runTest("compiler/testData/codegen/box/assert/jvm/lambdaNotEvaluated.kt");
}
@TestMetadata("localAnonymousFunction.kt")
public void testLocalAnonymousFunction() throws Exception {
runTest("compiler/testData/codegen/box/assert/jvm/localAnonymousFunction.kt");
@@ -889,6 +924,11 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
runTest("compiler/testData/codegen/box/assert/jvm/localObject.kt");
}
@TestMetadata("noUnnecessaryClassInitialization.kt")
public void testNoUnnecessaryClassInitialization() throws Exception {
runTest("compiler/testData/codegen/box/assert/jvm/noUnnecessaryClassInitialization.kt");
}
@TestMetadata("nonLocalReturn.kt")
public void testNonLocalReturn() throws Exception {
runTest("compiler/testData/codegen/box/assert/jvm/nonLocalReturn.kt");
@@ -433,6 +433,11 @@ public class IrBytecodeTextTestGenerated extends AbstractIrBytecodeTextTest {
public void testJvmInlineLambda() throws Exception {
runTest("compiler/testData/codegen/bytecodeText/assert/jvmInlineLambda.kt");
}
@TestMetadata("jvmNestedClass.kt")
public void testJvmNestedClass() throws Exception {
runTest("compiler/testData/codegen/bytecodeText/assert/jvmNestedClass.kt");
}
}
@TestMetadata("compiler/testData/codegen/bytecodeText/boxing")