diff --git a/compiler/testData/codegen/box/ranges/javaInterop/javaArrayOfInheritedNotNullFailFast.kt b/compiler/testData/codegen/box/ranges/javaInterop/javaArrayOfInheritedNotNullFailFast.kt index 573ad5db8d6..f6cf34f139c 100644 --- a/compiler/testData/codegen/box/ranges/javaInterop/javaArrayOfInheritedNotNullFailFast.kt +++ b/compiler/testData/codegen/box/ranges/javaInterop/javaArrayOfInheritedNotNullFailFast.kt @@ -16,14 +16,14 @@ fun box(): String { try { val i = JImpl().arrayOfNotNull()[0] return "Fail: should throw on get()" - } catch (e: IllegalStateException) {} + } catch (e: NullPointerException) {} try { for (i in JImpl().arrayOfNotNull()) { return "Fail: should throw on get() in loop header" } } - catch (e: IllegalStateException) {} + catch (e: NullPointerException) {} return "OK" } diff --git a/compiler/testData/codegen/box/ranges/javaInterop/javaArrayOfMaybeNullableWithNotNullLoopVariableFailFast.kt b/compiler/testData/codegen/box/ranges/javaInterop/javaArrayOfMaybeNullableWithNotNullLoopVariableFailFast.kt index ed546b0e58d..986c8ec55c3 100644 --- a/compiler/testData/codegen/box/ranges/javaInterop/javaArrayOfMaybeNullableWithNotNullLoopVariableFailFast.kt +++ b/compiler/testData/codegen/box/ranges/javaInterop/javaArrayOfMaybeNullableWithNotNullLoopVariableFailFast.kt @@ -1,14 +1,8 @@ // !LANGUAGE: +StrictJavaNullabilityAssertions // TARGET_BACKEND: JVM -// IGNORE_BACKEND: JVM // IGNORE_BACKEND_FIR: JVM_IR // WITH_RUNTIME -// Note: This fails on JVM (non-IR) with a NullPointerException in the loop header. The not-null assertion is not generated when -// assigning to the loop variable. The root cause seems to be that the loop variable is a KtParameter and -// CodegenAnnotatingVisitor/RuntimeAssertionsOnDeclarationBodyChecker do not analyze the need for not-null assertions on KtParameters. -// The NPE is due to calling `intValue()` on the null Int; it is expected to be asserted as non-null first. - // FILE: box.kt import kotlin.test.* @@ -17,14 +11,14 @@ fun box(): String { try { val i: Int = J.arrayOfMaybeNullable()[0] return "Fail: should throw on get()" - } catch (e: IllegalStateException) {} + } catch (e: NullPointerException) {} try { for (i: Int in J.arrayOfMaybeNullable()) { return "Fail: should throw on get() in loop header" } } - catch (e: IllegalStateException) {} + catch (e: NullPointerException) {} return "OK" } diff --git a/compiler/testData/codegen/box/ranges/javaInterop/javaCollectionOfExplicitNotNullFailFast.kt b/compiler/testData/codegen/box/ranges/javaInterop/javaCollectionOfExplicitNotNullFailFast.kt index e61cdec4f2a..48e03727950 100644 --- a/compiler/testData/codegen/box/ranges/javaInterop/javaCollectionOfExplicitNotNullFailFast.kt +++ b/compiler/testData/codegen/box/ranges/javaInterop/javaCollectionOfExplicitNotNullFailFast.kt @@ -18,14 +18,14 @@ fun box(): String { try { val i = J.listOfNotNull()[0] return "Fail: should throw on get()" - } catch (e: IllegalStateException) {} + } catch (e: NullPointerException) {} try { for (i in J.listOfNotNull()) { return "Fail: should throw on get() in loop header" } } - catch (e: IllegalStateException) {} + catch (e: NullPointerException) {} return "OK" } diff --git a/compiler/testData/codegen/box/ranges/javaInterop/javaCollectionOfInheritedNotNullFailFast.kt b/compiler/testData/codegen/box/ranges/javaInterop/javaCollectionOfInheritedNotNullFailFast.kt index d7d28bbc41b..5180ee7758c 100644 --- a/compiler/testData/codegen/box/ranges/javaInterop/javaCollectionOfInheritedNotNullFailFast.kt +++ b/compiler/testData/codegen/box/ranges/javaInterop/javaCollectionOfInheritedNotNullFailFast.kt @@ -16,14 +16,14 @@ fun box(): String { try { val i = JImpl().listOfNotNull()[0] return "Fail: should throw on get()" - } catch (e: IllegalStateException) {} + } catch (e: NullPointerException) {} try { for (i in JImpl().listOfNotNull()) { return "Fail: should throw on get() in loop header" } } - catch (e: IllegalStateException) {} + catch (e: NullPointerException) {} return "OK" } diff --git a/compiler/testData/codegen/box/ranges/javaInterop/javaCollectionOfMaybeNullableWithNotNullLoopVariableFailFast.kt b/compiler/testData/codegen/box/ranges/javaInterop/javaCollectionOfMaybeNullableWithNotNullLoopVariableFailFast.kt index f7a6e9d0863..160ea694c7c 100644 --- a/compiler/testData/codegen/box/ranges/javaInterop/javaCollectionOfMaybeNullableWithNotNullLoopVariableFailFast.kt +++ b/compiler/testData/codegen/box/ranges/javaInterop/javaCollectionOfMaybeNullableWithNotNullLoopVariableFailFast.kt @@ -1,14 +1,8 @@ // !LANGUAGE: +StrictJavaNullabilityAssertions // TARGET_BACKEND: JVM -// IGNORE_BACKEND: JVM // IGNORE_BACKEND_FIR: JVM_IR // WITH_RUNTIME -// Note: This fails on JVM (non-IR) with a NullPointerException in the loop header. The not-null assertion is not generated when -// assigning to the loop variable. The root cause seems to be that the loop variable is a KtParameter and -// CodegenAnnotatingVisitor/RuntimeAssertionsOnDeclarationBodyChecker do not analyze the need for not-null assertions on KtParameters. -// The NPE is due to calling `intValue()` on the null Int; it is expected to be asserted as non-null first. - // FILE: box.kt import kotlin.test.* @@ -17,14 +11,14 @@ fun box(): String { try { val i: Int = J.listOfMaybeNullable()[0] return "Fail: should throw on get()" - } catch (e: IllegalStateException) {} + } catch (e: NullPointerException) {} try { for (i: Int in J.listOfMaybeNullable()) { return "Fail: should throw on get() in loop header" } } - catch (e: IllegalStateException) {} + catch (e: NullPointerException) {} return "OK" } diff --git a/compiler/testData/codegen/box/ranges/javaInterop/javaCollectionOfNotNullToTypedArrayFailFast.kt b/compiler/testData/codegen/box/ranges/javaInterop/javaCollectionOfNotNullToTypedArrayFailFast.kt index 014f3aca7d3..6abb1d7d5a8 100644 --- a/compiler/testData/codegen/box/ranges/javaInterop/javaCollectionOfNotNullToTypedArrayFailFast.kt +++ b/compiler/testData/codegen/box/ranges/javaInterop/javaCollectionOfNotNullToTypedArrayFailFast.kt @@ -17,14 +17,14 @@ fun box(): String { try { val i = J.listOfNotNull().toTypedArray()[0] return "Fail: should throw on get()" - } catch (e: IllegalStateException) {} + } catch (e: NullPointerException) {} try { for (i in J.listOfNotNull().toTypedArray()) { return "Fail: should throw on get() in loop header" } } - catch (e: IllegalStateException) {} + catch (e: NullPointerException) {} return "OK" } diff --git a/compiler/testData/codegen/box/ranges/javaInterop/javaIteratorOfNotNullFailFast.kt b/compiler/testData/codegen/box/ranges/javaInterop/javaIteratorOfNotNullFailFast.kt index 381298936c7..625617d1b0c 100644 --- a/compiler/testData/codegen/box/ranges/javaInterop/javaIteratorOfNotNullFailFast.kt +++ b/compiler/testData/codegen/box/ranges/javaInterop/javaIteratorOfNotNullFailFast.kt @@ -17,14 +17,14 @@ fun box(): String { try { val i = J.iteratorOfNotNull().next() return "Fail: should throw on get()" - } catch (e: IllegalStateException) {} + } catch (e: NullPointerException) {} try { for (i in J.iteratorOfNotNull()) { return "Fail: should throw on get() in loop header" } } - catch (e: IllegalStateException) {} + catch (e: NullPointerException) {} return "OK" } diff --git a/compiler/testData/codegen/box/ranges/javaInterop/withIndex/javaArrayOfInheritedNotNullWithIndexFailFast.kt b/compiler/testData/codegen/box/ranges/javaInterop/withIndex/javaArrayOfInheritedNotNullWithIndexFailFast.kt index 164ef78eb02..c091c276431 100644 --- a/compiler/testData/codegen/box/ranges/javaInterop/withIndex/javaArrayOfInheritedNotNullWithIndexFailFast.kt +++ b/compiler/testData/codegen/box/ranges/javaInterop/withIndex/javaArrayOfInheritedNotNullWithIndexFailFast.kt @@ -17,14 +17,14 @@ fun box(): String { try { val (index, i) = JImpl().arrayOfNotNull().withIndex().first() return "Fail: should throw on get()" - } catch (e: IllegalStateException) {} + } catch (e: NullPointerException) {} try { for ((index, i) in JImpl().arrayOfNotNull().withIndex()) { return "Fail: should throw on get() in loop header" } } - catch (e: IllegalStateException) {} + catch (e: NullPointerException) {} return "OK" } diff --git a/compiler/testData/codegen/box/ranges/javaInterop/withIndex/javaCollectionOfExplicitNotNullWithIndexFailFast.kt b/compiler/testData/codegen/box/ranges/javaInterop/withIndex/javaCollectionOfExplicitNotNullWithIndexFailFast.kt index 0f16bd03b40..589e6d60c35 100644 --- a/compiler/testData/codegen/box/ranges/javaInterop/withIndex/javaCollectionOfExplicitNotNullWithIndexFailFast.kt +++ b/compiler/testData/codegen/box/ranges/javaInterop/withIndex/javaCollectionOfExplicitNotNullWithIndexFailFast.kt @@ -18,14 +18,14 @@ fun box(): String { try { val (index, i) = J.listOfNotNull().withIndex().first() return "Fail: should throw on get()" - } catch (e: IllegalStateException) {} + } catch (e: NullPointerException) {} try { for ((index, i) in J.listOfNotNull().withIndex()) { return "Fail: should throw on get() in loop header" } } - catch (e: IllegalStateException) {} + catch (e: NullPointerException) {} return "OK" } diff --git a/compiler/testData/codegen/box/ranges/javaInterop/withIndex/javaIteratorOfNotNullWithIndexFailFast.kt b/compiler/testData/codegen/box/ranges/javaInterop/withIndex/javaIteratorOfNotNullWithIndexFailFast.kt index 456db42ae5b..3a23610f996 100644 --- a/compiler/testData/codegen/box/ranges/javaInterop/withIndex/javaIteratorOfNotNullWithIndexFailFast.kt +++ b/compiler/testData/codegen/box/ranges/javaInterop/withIndex/javaIteratorOfNotNullWithIndexFailFast.kt @@ -18,14 +18,14 @@ fun box(): String { try { val (index, i) = J.iteratorOfNotNull().withIndex().next() return "Fail: should throw on get()" - } catch (e: IllegalStateException) {} + } catch (e: NullPointerException) {} try { for ((index, i) in J.iteratorOfNotNull().withIndex()) { return "Fail: should throw on get() in loop header" } } - catch (e: IllegalStateException) {} + catch (e: NullPointerException) {} return "OK" } diff --git a/compiler/testData/codegen/boxInline/reified/checkCast/kt8043.kt b/compiler/testData/codegen/boxInline/reified/checkCast/kt8043.kt index 7e4425eff4d..276865fc0fe 100644 --- a/compiler/testData/codegen/boxInline/reified/checkCast/kt8043.kt +++ b/compiler/testData/codegen/boxInline/reified/checkCast/kt8043.kt @@ -12,15 +12,15 @@ fun case1(): Int = null.castTo() fun box(): String { - failTypeCast { case1(); return "failTypeCast 9" } + failNPE { case1(); return "Fail" } return "OK" } -inline fun failTypeCast(s: () -> Unit) { +inline fun failNPE(s: () -> Unit) { try { s() } - catch (e: TypeCastException) { - + catch (e: NullPointerException) { + // OK } } diff --git a/compiler/testData/codegen/boxInline/reified/checkCast/simple.kt b/compiler/testData/codegen/boxInline/reified/checkCast/simple.kt index 34008fd0b49..f76f0bbbeb4 100644 --- a/compiler/testData/codegen/boxInline/reified/checkCast/simple.kt +++ b/compiler/testData/codegen/boxInline/reified/checkCast/simple.kt @@ -1,5 +1,4 @@ // FILE: 1.kt -// WITH_RUNTIME package test class A @@ -12,34 +11,34 @@ inline fun Any?.foo(): T = this as T import test.* fun box(): String { - failTypeCast { null.foo(); return "failTypeCast 1" } - if (null.foo() != null) return "failTypeCast 2" + failNPE { null.foo(); return "Fail 1" } + if (null.foo() != null) return "Fail 2" - failTypeCast { null.foo(); return "failTypeCast 3" } - if (null.foo() != null) return "failTypeCast 4" + failNPE { null.foo(); return "Fail 3" } + if (null.foo() != null) return "Fail 4" val a = A() - if (a.foo() != a) return "failTypeCast 5" - if (a.foo() != a) return "failTypeCast 6" + if (a.foo() != a) return "Fail 5" + if (a.foo() != a) return "Fail 6" - if (a.foo() != a) return "failTypeCast 7" - if (a.foo() != a) return "failTypeCast 8" + if (a.foo() != a) return "Fail 7" + if (a.foo() != a) return "Fail 8" val b = B() - failClassCast { b.foo(); return "failTypeCast 9" } - failClassCast { b.foo(); return "failTypeCast 10" } + failClassCast { b.foo(); return "Fail 9" } + failClassCast { b.foo(); return "Fail 10" } return "OK" } -inline fun failTypeCast(s: () -> Unit) { +inline fun failNPE(s: () -> Unit) { try { s() } - catch (e: TypeCastException) { - + catch (e: NullPointerException) { + // OK } } @@ -51,6 +50,6 @@ inline fun failClassCast(s: () -> Unit) { throw e } catch (e: ClassCastException) { - + // OK } } diff --git a/compiler/testData/codegen/boxInline/reified/checkCast/simple_1_3.kt b/compiler/testData/codegen/boxInline/reified/checkCast/simple_1_3.kt new file mode 100644 index 00000000000..46ddb58f468 --- /dev/null +++ b/compiler/testData/codegen/boxInline/reified/checkCast/simple_1_3.kt @@ -0,0 +1,57 @@ +// !API_VERSION: 1.3 +// FILE: 1.kt +// WITH_RUNTIME +package test + +class A +class B + +inline fun Any?.foo(): T = this as T + +// FILE: 2.kt + +import test.* + +fun box(): String { + failTypeCast { null.foo(); return "Fail 1" } + if (null.foo() != null) return "Fail 2" + + failTypeCast { null.foo(); return "Fail 3" } + if (null.foo() != null) return "Fail 4" + + val a = A() + + if (a.foo() != a) return "Fail 5" + if (a.foo() != a) return "Fail 6" + + if (a.foo() != a) return "Fail 7" + if (a.foo() != a) return "Fail 8" + + val b = B() + + failClassCast { b.foo(); return "Fail 9" } + failClassCast { b.foo(); return "Fail 10" } + + return "OK" +} + +inline fun failTypeCast(s: () -> Unit) { + try { + s() + } + catch (e: TypeCastException) { + // OK + } +} + +inline fun failClassCast(s: () -> Unit) { + try { + s() + } + catch (e: TypeCastException) { + throw e + } + catch (e: ClassCastException) { + // OK + } +} diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxInlineCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxInlineCodegenTestGenerated.java index 9a6f3a7d74e..8646e2263a6 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxInlineCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxInlineCodegenTestGenerated.java @@ -2941,6 +2941,11 @@ public class BlackBoxInlineCodegenTestGenerated extends AbstractBlackBoxInlineCo public void testSimpleSafe() throws Exception { runTest("compiler/testData/codegen/boxInline/reified/checkCast/simpleSafe.kt"); } + + @TestMetadata("simple_1_3.kt") + public void testSimple_1_3() throws Exception { + runTest("compiler/testData/codegen/boxInline/reified/checkCast/simple_1_3.kt"); + } } @TestMetadata("compiler/testData/codegen/boxInline/reified/defaultLambda") diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/CompileKotlinAgainstInlineKotlinTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/CompileKotlinAgainstInlineKotlinTestGenerated.java index 6e3b3ef4583..58468ce4322 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/CompileKotlinAgainstInlineKotlinTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/CompileKotlinAgainstInlineKotlinTestGenerated.java @@ -2941,6 +2941,11 @@ public class CompileKotlinAgainstInlineKotlinTestGenerated extends AbstractCompi public void testSimpleSafe() throws Exception { runTest("compiler/testData/codegen/boxInline/reified/checkCast/simpleSafe.kt"); } + + @TestMetadata("simple_1_3.kt") + public void testSimple_1_3() throws Exception { + runTest("compiler/testData/codegen/boxInline/reified/checkCast/simple_1_3.kt"); + } } @TestMetadata("compiler/testData/codegen/boxInline/reified/defaultLambda") diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxInlineCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxInlineCodegenTestGenerated.java index 67ec45f4487..02ee3ea33ce 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxInlineCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxInlineCodegenTestGenerated.java @@ -2941,6 +2941,11 @@ public class IrBlackBoxInlineCodegenTestGenerated extends AbstractIrBlackBoxInli public void testSimpleSafe() throws Exception { runTest("compiler/testData/codegen/boxInline/reified/checkCast/simpleSafe.kt"); } + + @TestMetadata("simple_1_3.kt") + public void testSimple_1_3() throws Exception { + runTest("compiler/testData/codegen/boxInline/reified/checkCast/simple_1_3.kt"); + } } @TestMetadata("compiler/testData/codegen/boxInline/reified/defaultLambda") diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrCompileKotlinAgainstInlineKotlinTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrCompileKotlinAgainstInlineKotlinTestGenerated.java index dcff22a83dc..2401d19e945 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrCompileKotlinAgainstInlineKotlinTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrCompileKotlinAgainstInlineKotlinTestGenerated.java @@ -2941,6 +2941,11 @@ public class IrCompileKotlinAgainstInlineKotlinTestGenerated extends AbstractIrC public void testSimpleSafe() throws Exception { runTest("compiler/testData/codegen/boxInline/reified/checkCast/simpleSafe.kt"); } + + @TestMetadata("simple_1_3.kt") + public void testSimple_1_3() throws Exception { + runTest("compiler/testData/codegen/boxInline/reified/checkCast/simple_1_3.kt"); + } } @TestMetadata("compiler/testData/codegen/boxInline/reified/defaultLambda")