JVM_IR: Add null-checks in SAM wrapper constructors (KT-50108)

This commit is contained in:
Steven Schäfer
2021-12-08 11:17:58 +01:00
committed by Alexander Udalov
parent 7f531d8426
commit 0da23198e6
7 changed files with 102 additions and 3 deletions
+32
View File
@@ -0,0 +1,32 @@
// TARGET_BACKEND: JVM
// IGNORE_BACKEND: JVM
fun interface IFoo {
fun foo(): String
}
abstract class Base {
abstract val fn: () -> String
init {
// This should throw a NPE, since the constructor of the IFoo
// SAM wrapper expects a non-nullable function type.
//
// In the JVM backend this expression evaluates to `null` instead,
// which isn't a valid result according to the type system.
IFoo(fn)
}
}
class Derived : Base() {
override val fn: () -> String = { "OK" }
}
fun box(): String {
try {
Derived()
} catch (e: java.lang.NullPointerException) {
return "OK"
}
return "Fail"
}
+23
View File
@@ -0,0 +1,23 @@
// TARGET_BACKEND: JVM
// FILE: test.kt
fun interface IFoo {
fun foo(s: String)
}
val foo = IFoo {}
fun box(): String {
try {
J.callWithNull(foo)
return "J.callWithNull(foo) should throw NPE"
} catch (e: NullPointerException) {
return "OK"
}
}
// FILE: J.java
public class J {
public static void callWithNull(IFoo iFoo) {
iFoo.foo(null);
}
}