Generate type-safe barrier in method body
In cases when signature of special bridge is the same as current method, but type is not 'Any?'. Also there is tiny optimization: only null check needed if value parameter type is mapped to Object, but it's not nullable. #KT-9973 Fixed
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
private object NotEmptyMap : MutableMap<Any, Int> {
|
||||
override fun containsKey(key: Any): Boolean = true
|
||||
override fun containsValue(value: Int): Boolean = true
|
||||
|
||||
// non-special bridges get(Object)Integer -> get(Object)I
|
||||
override fun get(key: Any): Int = 1
|
||||
override fun remove(key: Any): Int = 1
|
||||
|
||||
override val size: Int get() = 0
|
||||
override fun isEmpty(): Boolean = true
|
||||
override fun put(key: Any, value: Int): Int? = throw UnsupportedOperationException()
|
||||
override fun putAll(from: Map<out Any, Int>): Unit = throw UnsupportedOperationException()
|
||||
override fun clear(): Unit = throw UnsupportedOperationException()
|
||||
override val entries: MutableSet<MutableMap.MutableEntry<Any, Int>> get() = null!!
|
||||
override val keys: MutableSet<Any> get() = null!!
|
||||
override val values: MutableCollection<Int> get() = null!!
|
||||
}
|
||||
|
||||
|
||||
fun box(): String {
|
||||
val n = NotEmptyMap as MutableMap<Any?, Any?>
|
||||
|
||||
if (n.get(null) != null) return "fail 1"
|
||||
if (n.containsKey(null)) return "fail 2"
|
||||
if (n.containsValue(null)) return "fail 3"
|
||||
if (n.remove(null) != null) return "fail 4"
|
||||
|
||||
if (n.get(1) == null) return "fail 5"
|
||||
if (!n.containsKey("")) return "fail 6"
|
||||
if (!n.containsValue(3)) return "fail 7"
|
||||
if (n.remove("") == null) return "fail 8"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
private object NotEmptyList : MutableList<Any> {
|
||||
override fun contains(element: Any): Boolean = true
|
||||
override fun indexOf(element: Any): Int = 0
|
||||
override fun lastIndexOf(element: Any): Int = 0
|
||||
override fun remove(element: Any): Boolean = true
|
||||
|
||||
override val size: Int
|
||||
get() = throw UnsupportedOperationException()
|
||||
|
||||
override fun containsAll(elements: Collection<Any>): Boolean = elements.isEmpty()
|
||||
override fun isEmpty(): Boolean = throw UnsupportedOperationException()
|
||||
override fun get(index: Int): Any = throw UnsupportedOperationException()
|
||||
override fun add(element: Any): Boolean = throw UnsupportedOperationException()
|
||||
override fun addAll(elements: Collection<Any>): Boolean = throw UnsupportedOperationException()
|
||||
override fun addAll(index: Int, elements: Collection<Any>): Boolean = throw UnsupportedOperationException()
|
||||
override fun removeAll(elements: Collection<Any>): Boolean = throw UnsupportedOperationException()
|
||||
override fun retainAll(elements: Collection<Any>): Boolean = throw UnsupportedOperationException()
|
||||
override fun clear(): Unit = throw UnsupportedOperationException()
|
||||
override fun set(index: Int, element: Any): Any = throw UnsupportedOperationException()
|
||||
override fun add(index: Int, element: Any): Unit = throw UnsupportedOperationException()
|
||||
override fun removeAt(index: Int): Any = throw UnsupportedOperationException()
|
||||
override fun listIterator(): MutableListIterator<Any> = throw UnsupportedOperationException()
|
||||
override fun listIterator(index: Int): MutableListIterator<Any> = throw UnsupportedOperationException()
|
||||
override fun subList(fromIndex: Int, toIndex: Int): MutableList<Any> = throw UnsupportedOperationException()
|
||||
override fun iterator(): MutableIterator<Any> = throw UnsupportedOperationException()
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
val n = NotEmptyList as MutableList<Any?>
|
||||
|
||||
if (n.contains(null)) return "fail 1"
|
||||
if (n.indexOf(null) != -1) return "fail 2"
|
||||
if (n.lastIndexOf(null) != -1) return "fail 3"
|
||||
|
||||
if (!n.contains("")) return "fail 3"
|
||||
if (n.indexOf("") != 0) return "fail 4"
|
||||
if (n.lastIndexOf("") != 0) return "fail 5"
|
||||
|
||||
if (n.remove(null)) return "fail 6"
|
||||
if (!n.remove("")) return "fail 7"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
private object NotEmptyMap : MutableMap<Any, Any> {
|
||||
override fun containsKey(key: Any): Boolean = true
|
||||
override fun containsValue(value: Any): Boolean = true
|
||||
override fun get(key: Any): Any? = Any()
|
||||
override fun remove(key: Any): Any? = Any()
|
||||
|
||||
override val size: Int get() = 0
|
||||
override fun isEmpty(): Boolean = true
|
||||
override fun put(key: Any, value: Any): Any? = throw UnsupportedOperationException()
|
||||
override fun putAll(from: Map<out Any, Any>): Unit = throw UnsupportedOperationException()
|
||||
override fun clear(): Unit = throw UnsupportedOperationException()
|
||||
override val entries: MutableSet<MutableMap.MutableEntry<Any, Any>> get() = null!!
|
||||
override val keys: MutableSet<Any> get() = null!!
|
||||
override val values: MutableCollection<Any> get() = null!!
|
||||
}
|
||||
|
||||
|
||||
fun box(): String {
|
||||
val n = NotEmptyMap as MutableMap<Any?, Any?>
|
||||
|
||||
if (n.get(null) != null) return "fail 1"
|
||||
if (n.containsKey(null)) return "fail 2"
|
||||
if (n.containsValue(null)) return "fail 3"
|
||||
if (n.remove(null) != null) return "fail 4"
|
||||
|
||||
if (n.get("") == null) return "fail 5"
|
||||
if (!n.containsKey("")) return "fail 6"
|
||||
if (!n.containsValue("")) return "fail 7"
|
||||
if (n.remove("") == null) return "fail 8"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
+2
@@ -6,3 +6,5 @@ abstract class A7 : MutableCollection<Int> {
|
||||
|
||||
// 1 public final bridge contains\(Ljava/lang/Object;\)Z
|
||||
// 1 public final bridge remove\(Ljava/lang/Object;\)Z
|
||||
/* 2 INSTANCEOF: one for 'remove', one for 'contains' type-safe bridges */
|
||||
// 2 INSTANCEOF java/lang/Integer
|
||||
|
||||
Vendored
+2
@@ -26,3 +26,5 @@ abstract class A6 : java.util.AbstractList<String>() {
|
||||
// 2 public final bridge remove\(Ljava/lang/Object;\)Z
|
||||
// 2 public final bridge indexOf\(Ljava/lang/Object;\)I
|
||||
// 2 public final bridge lastIndexOf\(Ljava/lang/Object;\)I
|
||||
/* 2 INSTANCEOF for each class: one for 'remove', one for 'contains' type-safe bridges */
|
||||
// 8 INSTANCEOF java/lang/String
|
||||
|
||||
Vendored
+2
@@ -23,3 +23,5 @@ abstract class A2 : MutableCollection<String> {
|
||||
// 1 public final bridge contains\(Ljava/lang/Object;\)Z
|
||||
// 1 public final bridge remove\(Ljava/lang/Object;\)Z
|
||||
// 1 INVOKEVIRTUAL A[0-9]\.contains \(Ljava/lang/String;\)Z
|
||||
/* 2 INSTANCEOF: one for 'remove', one for 'contains' type-safe bridges */
|
||||
// 2 INSTANCEOF
|
||||
|
||||
+4
@@ -22,3 +22,7 @@ abstract class A : I2
|
||||
|
||||
// 1 public final bridge contains\(Ljava/lang/Object;\)Z
|
||||
// 1 public final bridge remove\(Ljava/lang/Object;\)Z
|
||||
/* 2 INSTANCEOF: one for 'remove', one for 'contains' type-safe bridges of A
|
||||
There should be no bridges in interfaces
|
||||
*/
|
||||
// 2 INSTANCEOF java/lang/String
|
||||
|
||||
Vendored
+11
@@ -0,0 +1,11 @@
|
||||
abstract class A8 : MutableCollection<Any> {
|
||||
override fun contains(o: Any): Boolean {
|
||||
throw UnsupportedOperationException()
|
||||
}
|
||||
}
|
||||
|
||||
// 1 bridge
|
||||
// 1 public final bridge size
|
||||
// 0 INSTANCEOF
|
||||
/* Only 1 IFNONNULL should be within contains method */
|
||||
// 1 IFNONNULL
|
||||
Vendored
+11
@@ -0,0 +1,11 @@
|
||||
abstract class A<T : Any> : MutableCollection<T> {
|
||||
override fun contains(o: T): Boolean {
|
||||
throw UnsupportedOperationException()
|
||||
}
|
||||
}
|
||||
|
||||
// 1 bridge
|
||||
// 1 public final bridge size
|
||||
// 0 INSTANCEOF
|
||||
/* Only 1 IFNONNULL should be within contains method (because T is not nullable) */
|
||||
// 1 IFNONNULL
|
||||
Vendored
+2
@@ -6,3 +6,5 @@ abstract class A8 : MutableCollection<Any?> {
|
||||
|
||||
// 1 bridge
|
||||
// 1 public final bridge size
|
||||
// 0 INSTANCEOF
|
||||
// 0 NULL
|
||||
|
||||
Reference in New Issue
Block a user