KT-9377 Support is-checks for read-only collections

Intrinsics for is/as/as? with mutable Kotlin collections and related types.
This commit is contained in:
Dmitry Petrov
2015-09-30 18:23:26 +03:00
parent 35881198c3
commit 6cb0e5151c
32 changed files with 1291 additions and 50 deletions
@@ -17,13 +17,14 @@ fun expectUoe(block: () -> Any) {
}
fun box(): String {
val collection = MyCollection<String>() as MutableCollection<String>
val myCollection = MyCollection<String>()
val collection = myCollection as java.util.Collection<String>
expectUoe { collection.add("") }
expectUoe { collection.remove("") }
expectUoe { collection.addAll(collection) }
expectUoe { collection.removeAll(collection) }
expectUoe { collection.retainAll(collection) }
expectUoe { collection.addAll(myCollection) }
expectUoe { collection.removeAll(myCollection) }
expectUoe { collection.retainAll(myCollection) }
expectUoe { collection.clear() }
return "OK"
@@ -5,7 +5,7 @@ class MyIterator<T>(val v: T): Iterator<T> {
fun box(): String {
try {
(MyIterator<String>("") as MutableIterator<String>).remove()
(MyIterator<String>("") as java.util.Iterator<String>).remove()
throw AssertionError()
} catch (e: UnsupportedOperationException) {
return "OK"
@@ -6,6 +6,6 @@ class MyIterator<T>(val v: T): Iterator<T> {
}
fun box(): String {
(MyIterator<String>("") as MutableIterator<String>).remove()
(MyIterator<String>("") as java.util.Iterator<String>).remove()
return "OK"
}
+1 -1
View File
@@ -23,7 +23,7 @@ fun expectUoe(block: () -> Any) {
}
fun box(): String {
val list = MyList<String>() as MutableList<String>
val list = MyList<String>() as java.util.List<String>
expectUoe { list.add("") }
expectUoe { list.remove("") }
@@ -16,7 +16,7 @@ fun expectUoe(block: () -> Any) {
}
fun box(): String {
val list = MyListIterator<String>() as MutableListIterator<String>
val list = MyListIterator<String>() as java.util.ListIterator<String>
expectUoe { list.set("") }
expectUoe { list.add("") }
@@ -26,7 +26,7 @@ class MyList<T>(val v: T): List<T> {
}
fun box(): String {
val list = MyList<String>("") as MutableList<String>
val list = MyList<String>("") as java.util.List<String>
list.add("")
list.remove("")
@@ -28,7 +28,7 @@ class MyList<T>(v: T): Super<T>(v), List<T> {
}
fun box(): String {
val list = MyList<String>("") as MutableList<String>
val list = MyList<String>("") as java.util.List<String>
list.add("")
list.remove("")
+3 -2
View File
@@ -18,11 +18,12 @@ fun expectUoe(block: () -> Unit) {
}
fun box(): String {
val map = MyMap<String, Int>() as MutableMap<String, Int>
val myMap = MyMap<String, Int>()
val map = myMap as java.util.Map<String, Int>
expectUoe { map.put("", 1) }
expectUoe { map.remove("") }
expectUoe { map.putAll(map) }
expectUoe { map.putAll(myMap) }
expectUoe { map.clear() }
return "OK"
@@ -7,7 +7,7 @@ class MyMapEntry<K, V>: Map.Entry<K, V> {
fun box(): String {
try {
(MyMapEntry<String, Int>() as MutableMap.MutableEntry<String, Int>).setValue(1)
(MyMapEntry<String, Int>() as java.util.Map.Entry<String, Int>).setValue(1)
throw AssertionError()
} catch (e: UnsupportedOperationException) {
return "OK"
@@ -8,7 +8,7 @@ class MyMapEntry<K, V>: Map.Entry<K, V> {
}
fun box(): String {
(MyMapEntry<String, Int>() as MutableMap.MutableEntry<String, Int>).setValue(1)
(MyMapEntry<String, Int>() as java.util.Map.Entry<String, Int>).setValue(1)
return "OK"
}
@@ -15,11 +15,12 @@ class MyMap<K, V>: Map<K, V> {
}
fun box(): String {
val map = MyMap<String, Int>() as MutableMap<String, Int>
val myMap = MyMap<String, Int>()
val map = myMap as java.util.Map<String, Int>
map.put("", 1)
map.remove("")
map.putAll(map)
map.putAll(myMap)
map.clear()
return "OK"
@@ -23,7 +23,7 @@ fun expectUoe(block: () -> Any) {
}
fun box(): String {
val list = MyList() as MutableList<String>
val list = MyList() as java.util.List<String>
expectUoe { list.add("") }
expectUoe { list.remove("") }
@@ -11,7 +11,7 @@ class B(var result: String) : A() {
}
fun box(): String {
val a = B("Fail") as MutableIterator<String>
val a = B("Fail") as java.util.Iterator<String>
a.next()
a.hasNext()
a.remove()
@@ -13,7 +13,7 @@ fun expectUoe(block: () -> Any) {
}
fun box(): String {
val a = A<String>() as MutableList<String>
val a = A<String>() as java.util.List<String>
expectUoe { a.add("") }
expectUoe { a.remove("") }
expectUoe { a.addAll(a) }
@@ -28,7 +28,7 @@ fun box(): String {
a.listIterator(0)
a.subList(0, 0)
val b = B() as MutableList<String>
val b = B() as java.util.List<String>
expectUoe { b.add("") }
expectUoe { b.remove("") }
expectUoe { b.addAll(b) }
@@ -21,7 +21,7 @@ fun box(): String {
val a = C()
if (!a.add("")) return "Fail 1"
if (!(a as Addable).add("")) return "Fail 2"
if (!(a as MutableList<String>).add("")) return "Fail 3"
if (!(a as java.util.List<String>).add("")) return "Fail 3"
return "OK"
} catch (e: UnsupportedOperationException) {
return "Fail: no stub method should be generated"
@@ -13,7 +13,7 @@ class S : Set<String>, SetStringImpl() {
}
fun box(): String {
val s = S() as MutableSet<String>
val s = S() as java.util.Set<String>
s.add("")
s.remove("")
s.clear()
@@ -17,7 +17,7 @@ fun expectUoe(block: () -> Any) {
}
fun box(): String {
val a = A<Int, Int, Int>() as MutableSet<Int>
val a = A<Int, Int, Int>() as java.util.Set<Int>
a.iterator()
@@ -9,7 +9,7 @@ class MyCollection<T> : Collection<List<Iterator<T>>> {
}
fun box(): String {
val c = MyCollection<String>() as MutableCollection<List<Iterator<String>>>
val c = MyCollection<String>() as java.util.Collection<List<Iterator<String>>>
try {
c.add(ArrayList())
return "Fail"
@@ -5,7 +5,7 @@ class MyIterator<E : Number> : Iterator<E> {
fun box(): String {
try {
(MyIterator<Int>() as MutableIterator<Number>).remove()
(MyIterator<Int>() as java.util.Iterator<Number>).remove()
return "Fail"
} catch (e: UnsupportedOperationException) {
return "OK"