JVM IR: Avoid CHECKCASTs on type operators (KT-39520)

The type information coming from Java or Kotlin generics may be wrong
due to type erasure and a CHECKCAST instruction could throw an
exception.
This commit is contained in:
Steven Schäfer
2020-08-06 15:33:46 +02:00
committed by Alexander Udalov
parent 469b164555
commit 9026f89ba5
13 changed files with 261 additions and 18 deletions
@@ -0,0 +1,17 @@
// KT-39520
// TARGET_BACKEND: JVM
// FILE: A.java
public class A<T> {
private T value;
private A(T x) { value = x; }
public static <T> T f() {
return ((A<T>) new A(1)).value;
}
}
// FILE: test.kt
fun box(): String {
val x = A.f<String>() as Int
return if (x == 1) "OK" else "Fail"
}
@@ -0,0 +1,17 @@
// KT-39520
// TARGET_BACKEND: JVM
// FILE: A.java
public class A<T> {
private T value;
private A(T x) { value = x; }
public static <T> T f() {
return ((A<T>) new A(1)).value;
}
}
// FILE: test.kt
fun box(): String {
val x: Any = A.f<String>()
return if (x == 1) "OK" else "Fail"
}
@@ -0,0 +1,16 @@
// KT-39520
// TARGET_BACKEND: JVM
// FILE: A.java
public class A<T> {
private T value;
private A(T x) { value = x; }
public static <T> T f() {
return ((A<T>) new A(1)).value;
}
}
// FILE: test.kt
fun box(): String {
return if (A.f<String?>() is CharSequence?) "Fail" else "OK"
}
@@ -0,0 +1,16 @@
// KT-39520
// TARGET_BACKEND: JVM
// FILE: A.java
public class A<T> {
private T value;
private A(T x) { value = x; }
public static <T> T f() {
return ((A<T>) new A(1)).value;
}
}
// FILE: test.kt
fun box(): String {
return A.f<String>() as? String ?: "OK"
}