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:
@@ -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"
|
||||
}
|
||||
|
||||
@@ -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("") }
|
||||
|
||||
+1
-1
@@ -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("")
|
||||
|
||||
+1
-1
@@ -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
@@ -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"
|
||||
}
|
||||
|
||||
+3
-2
@@ -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"
|
||||
|
||||
+1
-1
@@ -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()
|
||||
|
||||
+1
-1
@@ -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"
|
||||
|
||||
@@ -0,0 +1,126 @@
|
||||
import java.util.*
|
||||
|
||||
class Itr : Iterator<String> by ArrayList<String>().iterator()
|
||||
class MItr : MutableIterator<String> by ArrayList<String>().iterator()
|
||||
class LItr : ListIterator<String> by ArrayList<String>().listIterator()
|
||||
class MLItr : MutableListIterator<String> by ArrayList<String>().listIterator()
|
||||
|
||||
class It : Iterable<String> by ArrayList<String>()
|
||||
class MIt : MutableIterable<String> by ArrayList<String>()
|
||||
class C : Collection<String> by ArrayList<String>()
|
||||
class MC : MutableCollection<String> by ArrayList<String>()
|
||||
class L : List<String> by ArrayList<String>()
|
||||
class ML : MutableList<String> by ArrayList<String>()
|
||||
class S : Set<String> by HashSet<String>()
|
||||
class MS : MutableSet<String> by HashSet<String>()
|
||||
|
||||
class M : Map<String, String> by HashMap<String, String>()
|
||||
class MM : MutableMap<String, String> by HashMap<String, String>()
|
||||
|
||||
class ME : Map.Entry<String, String> {
|
||||
override fun getKey(): String = throw UnsupportedOperationException()
|
||||
override fun getValue(): String = throw UnsupportedOperationException()
|
||||
}
|
||||
|
||||
class MME : MutableMap.MutableEntry<String, String> {
|
||||
override fun getKey(): String = throw UnsupportedOperationException()
|
||||
override fun getValue(): String = throw UnsupportedOperationException()
|
||||
override fun setValue(value: String): String = throw UnsupportedOperationException()
|
||||
}
|
||||
|
||||
inline fun asFailsWithCCE(operation: String, block: () -> Unit) {
|
||||
try {
|
||||
block()
|
||||
}
|
||||
catch (e: java.lang.ClassCastException) {
|
||||
return
|
||||
}
|
||||
catch (e: Throwable) {
|
||||
throw AssertionError("$operation: should throw ClassCastException, got $e")
|
||||
}
|
||||
throw AssertionError("$operation: should throw ClassCastException, no exception thrown")
|
||||
}
|
||||
|
||||
inline fun asSucceeds(operation: String, block: () -> Unit) {
|
||||
try {
|
||||
block()
|
||||
}
|
||||
catch (e: Throwable) {
|
||||
throw AssertionError("$operation: should not throw exceptions, got $e")
|
||||
}
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
val itr = Itr() as Any
|
||||
val mitr = MItr()
|
||||
|
||||
asFailsWithCCE("itr as MutableIterator") { itr as MutableIterator<*> }
|
||||
asSucceeds("mitr as MutableIterator") { mitr as MutableIterator<*> }
|
||||
|
||||
val litr = LItr() as Any
|
||||
val mlitr = MLItr()
|
||||
|
||||
asFailsWithCCE("litr as MutableIterator") { litr as MutableIterator<*> }
|
||||
asFailsWithCCE("litr as MutableListIterator") { litr as MutableListIterator<*> }
|
||||
asSucceeds("mlitr as MutableIterator") { mlitr as MutableIterator<*> }
|
||||
asSucceeds("mlitr as MutableListIterator") { mlitr as MutableListIterator<*> }
|
||||
|
||||
val it = It() as Any
|
||||
val mit = MIt()
|
||||
val arrayList = ArrayList<String>()
|
||||
|
||||
asFailsWithCCE("it as MutableIterable") { it as MutableIterable<*> }
|
||||
asSucceeds("mit as MutableIterable") { mit as MutableIterable<*> }
|
||||
asSucceeds("arrayList as MutableIterable") { arrayList as MutableIterable<*> }
|
||||
|
||||
val coll = C() as Any
|
||||
val mcoll = MC()
|
||||
|
||||
asFailsWithCCE("coll as MutableIterable") { coll as MutableIterable<*> }
|
||||
asFailsWithCCE("coll as MutableCollection") { coll as MutableCollection<*> }
|
||||
asSucceeds("mcoll as MutableIterable") { mcoll as MutableIterable<*> }
|
||||
asSucceeds("mcoll as MutableCollection") { mcoll as MutableCollection<*> }
|
||||
asSucceeds("arrayList as MutableCollection") { arrayList as MutableCollection<*> }
|
||||
|
||||
val list = L() as Any
|
||||
val mlist = ML()
|
||||
|
||||
asFailsWithCCE("list as MutableIterable") { list as MutableIterable<*> }
|
||||
asFailsWithCCE("list as MutableCollection") { list as MutableCollection<*> }
|
||||
asFailsWithCCE("list as MutableList") { list as MutableList<*> }
|
||||
asSucceeds("mlist as MutableIterable") { mlist as MutableIterable<*> }
|
||||
asSucceeds("mlist as MutableCollection") { mlist as MutableCollection<*> }
|
||||
asSucceeds("mlist as MutableList") { mlist as MutableList<*> }
|
||||
|
||||
val set = S() as Any
|
||||
val mset = MS()
|
||||
val hashSet = HashSet<String>()
|
||||
|
||||
asFailsWithCCE("set as MutableIterable") { set as MutableIterable<*> }
|
||||
asFailsWithCCE("set as MutableCollection") { set as MutableCollection<*> }
|
||||
asFailsWithCCE("set as MutableSet") { set as MutableSet<*> }
|
||||
asSucceeds("mset as MutableIterable") { mset as MutableIterable<*> }
|
||||
asSucceeds("mset as MutableCollection") { mset as MutableCollection<*> }
|
||||
asSucceeds("mset as MutableSet") { mset as MutableSet<*> }
|
||||
asSucceeds("hashSet as MutableSet") { hashSet as MutableSet<*> }
|
||||
|
||||
val map = M() as Any
|
||||
val mmap = MM()
|
||||
val hashMap = HashMap<String, String>()
|
||||
|
||||
asFailsWithCCE("map as MutableMap") { map as MutableMap<*, *> }
|
||||
asSucceeds("mmap as MutableMap") { mmap as MutableMap<*, *> }
|
||||
|
||||
val entry = ME() as Any
|
||||
val mentry = MME()
|
||||
|
||||
asFailsWithCCE("entry as MutableMap.MutableEntry") { entry as MutableMap.MutableEntry<*, *> }
|
||||
asSucceeds("mentry as MutableMap.MutableEntry") { mentry as MutableMap.MutableEntry<*, *> }
|
||||
|
||||
hashMap[""] = ""
|
||||
val hashMapEntry = hashMap.entrySet().first()
|
||||
|
||||
asSucceeds("hashMapEntry as MutableMap.MutableEntry") { hashMapEntry as MutableMap.MutableEntry<*, *> }
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
import java.util.*
|
||||
|
||||
class Itr : Iterator<String> by ArrayList<String>().iterator()
|
||||
class MItr : MutableIterator<String> by ArrayList<String>().iterator()
|
||||
class LItr : ListIterator<String> by ArrayList<String>().listIterator()
|
||||
class MLItr : MutableListIterator<String> by ArrayList<String>().listIterator()
|
||||
|
||||
class It : Iterable<String> by ArrayList<String>()
|
||||
class MIt : MutableIterable<String> by ArrayList<String>()
|
||||
class C : Collection<String> by ArrayList<String>()
|
||||
class MC : MutableCollection<String> by ArrayList<String>()
|
||||
class L : List<String> by ArrayList<String>()
|
||||
class ML : MutableList<String> by ArrayList<String>()
|
||||
class S : Set<String> by HashSet<String>()
|
||||
class MS : MutableSet<String> by HashSet<String>()
|
||||
|
||||
class M : Map<String, String> by HashMap<String, String>()
|
||||
class MM : MutableMap<String, String> by HashMap<String, String>()
|
||||
|
||||
class ME : Map.Entry<String, String> {
|
||||
override fun getKey(): String = throw UnsupportedOperationException()
|
||||
override fun getValue(): String = throw UnsupportedOperationException()
|
||||
}
|
||||
|
||||
class MME : MutableMap.MutableEntry<String, String> {
|
||||
override fun getKey(): String = throw UnsupportedOperationException()
|
||||
override fun getValue(): String = throw UnsupportedOperationException()
|
||||
override fun setValue(value: String): String = throw UnsupportedOperationException()
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
val itr = Itr() as Any
|
||||
val mitr = MItr()
|
||||
|
||||
assert(itr !is MutableIterator<*>) { "Itr should satisfy '!is MutableIterator'" }
|
||||
assert(mitr is MutableIterator<*>) { "MItr should satisfy 'is MutableIterator'" }
|
||||
|
||||
val litr = LItr() as Any
|
||||
val mlitr = MLItr()
|
||||
|
||||
assert(litr !is MutableIterator<*>) { "LItr should satisfy '!is MutableIterator'" }
|
||||
assert(litr !is MutableListIterator<*>) { "LItr should satisfy '!is MutableListIterator'" }
|
||||
assert(mlitr is MutableListIterator<*>) { "MLItr should satisfy 'is MutableListIterator'" }
|
||||
|
||||
val it = It() as Any
|
||||
val mit = MIt()
|
||||
val arrayList = ArrayList<String>()
|
||||
|
||||
assert(it !is MutableIterable<*>) { "It should satisfy '!is MutableIterable'" }
|
||||
assert(mit is MutableIterable<*>) { "MIt should satisfy 'is MutableIterable'" }
|
||||
assert(arrayList is MutableIterable<*>) { "ArrayList should satisfy 'is MutableIterable'" }
|
||||
|
||||
val coll = C() as Any
|
||||
val mcoll = MC()
|
||||
|
||||
assert(coll !is MutableCollection<*>) { "C should satisfy '!is MutableCollection'" }
|
||||
assert(coll !is MutableIterable<*>) { "C should satisfy '!is MutableIterable'" }
|
||||
assert(mcoll is MutableCollection<*>) { "MC should satisfy 'is MutableCollection'" }
|
||||
assert(mcoll is MutableIterable<*>) { "MC should satisfy 'is MutableIterable'" }
|
||||
assert(arrayList is MutableCollection<*>) { "ArrayList should satisfy 'is MutableCollection'" }
|
||||
|
||||
val list = L() as Any
|
||||
val mlist = ML()
|
||||
|
||||
assert(list !is MutableList<*>) { "L should satisfy '!is MutableList'" }
|
||||
assert(list !is MutableCollection<*>) { "L should satisfy '!is MutableCollection'" }
|
||||
assert(list !is MutableIterable<*>) { "L should satisfy '!is MutableIterable'" }
|
||||
assert(mlist is MutableList<*>) { "ML should satisfy 'is MutableList'" }
|
||||
assert(mlist is MutableCollection<*>) { "ML should satisfy 'is MutableCollection'" }
|
||||
assert(mlist is MutableIterable<*>) { "ML should satisfy 'is MutableIterable'" }
|
||||
assert(arrayList is MutableList<*>) { "ArrayList should satisfy 'is MutableList'" }
|
||||
|
||||
val set = S() as Any
|
||||
val mset = MS()
|
||||
val hashSet = HashSet<String>()
|
||||
|
||||
assert(set !is MutableSet<*>) { "S should satisfy '!is MutableSet'" }
|
||||
assert(set !is MutableCollection<*>) { "S should satisfy '!is MutableCollection'" }
|
||||
assert(set !is MutableIterable<*>) { "S should satisfy '!is MutableIterable'" }
|
||||
assert(mset is MutableSet<*>) { "MS should satisfy 'is MutableSet'" }
|
||||
assert(mset is MutableCollection<*>) { "MS should satisfy 'is MutableCollection'" }
|
||||
assert(mset is MutableIterable<*>) { "MS should satisfy 'is MutableIterable'" }
|
||||
assert(hashSet is MutableSet<*>) { "HashSet should satisfy 'is MutableSet'" }
|
||||
assert(hashSet is MutableCollection<*>) { "HashSet should satisfy 'is MutableCollection'" }
|
||||
assert(hashSet is MutableIterable<*>) { "HashSet should satisfy 'is MutableIterable'" }
|
||||
|
||||
val map = M() as Any
|
||||
val mmap = MM()
|
||||
val hashMap = HashMap<String, String>()
|
||||
|
||||
assert(map !is MutableMap<*, *>) { "M should satisfy '!is MutableMap'" }
|
||||
assert(mmap is MutableMap<*, *>) { "MM should satisfy 'is MutableMap'"}
|
||||
assert(hashMap is MutableMap<*, *>) { "HashMap should satisfy 'is MutableMap'" }
|
||||
|
||||
val entry = ME() as Any
|
||||
val mentry = MME()
|
||||
|
||||
hashMap[""] = ""
|
||||
val hashMapEntry = hashMap.entrySet().first()
|
||||
|
||||
assert(entry !is MutableMap.MutableEntry<*, *>) { "ME should satisfy '!is MutableMap.MutableEntry'"}
|
||||
assert(mentry is MutableMap.MutableEntry<*, *>) { "MME should satisfy 'is MutableMap.MutableEntry'"}
|
||||
assert(hashMapEntry is MutableMap.MutableEntry<*, *>) { "HashMap.Entry should satisfy 'is MutableMap.MutableEntry'"}
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,128 @@
|
||||
import java.util.*
|
||||
|
||||
class Itr : Iterator<String> by ArrayList<String>().iterator()
|
||||
class MItr : MutableIterator<String> by ArrayList<String>().iterator()
|
||||
class LItr : ListIterator<String> by ArrayList<String>().listIterator()
|
||||
class MLItr : MutableListIterator<String> by ArrayList<String>().listIterator()
|
||||
|
||||
class It : Iterable<String> by ArrayList<String>()
|
||||
class MIt : MutableIterable<String> by ArrayList<String>()
|
||||
class C : Collection<String> by ArrayList<String>()
|
||||
class MC : MutableCollection<String> by ArrayList<String>()
|
||||
class L : List<String> by ArrayList<String>()
|
||||
class ML : MutableList<String> by ArrayList<String>()
|
||||
class S : Set<String> by HashSet<String>()
|
||||
class MS : MutableSet<String> by HashSet<String>()
|
||||
|
||||
class M : Map<String, String> by HashMap<String, String>()
|
||||
class MM : MutableMap<String, String> by HashMap<String, String>()
|
||||
|
||||
class ME : Map.Entry<String, String> {
|
||||
override fun getKey(): String = throw UnsupportedOperationException()
|
||||
override fun getValue(): String = throw UnsupportedOperationException()
|
||||
}
|
||||
|
||||
class MME : MutableMap.MutableEntry<String, String> {
|
||||
override fun getKey(): String = throw UnsupportedOperationException()
|
||||
override fun getValue(): String = throw UnsupportedOperationException()
|
||||
override fun setValue(value: String): String = throw UnsupportedOperationException()
|
||||
}
|
||||
|
||||
inline fun <reified T> reifiedAsSucceeds(x: Any, operation: String) {
|
||||
try {
|
||||
x as T
|
||||
}
|
||||
catch (e: Throwable) {
|
||||
throw AssertionError("$operation: should not throw exceptions, got $e")
|
||||
}
|
||||
}
|
||||
|
||||
inline fun <reified T> reifiedAsFailsWithCCE(x: Any, operation: String) {
|
||||
try {
|
||||
x as T
|
||||
}
|
||||
catch (e: java.lang.ClassCastException) {
|
||||
return
|
||||
}
|
||||
catch (e: Throwable) {
|
||||
throw AssertionError("$operation: should throw ClassCastException, got $e")
|
||||
}
|
||||
throw AssertionError("$operation: should fail with CCE, no exception thrown")
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
val itr = Itr() as Any
|
||||
val mitr = MItr()
|
||||
|
||||
reifiedAsFailsWithCCE<MutableIterator<*>>(itr, "reifiedAs<MutableIterator<*>>(itr)")
|
||||
reifiedAsSucceeds<MutableIterator<*>>(mitr, "reifiedAs<MutableIterator<*>>(mitr)")
|
||||
|
||||
val litr = LItr() as Any
|
||||
val mlitr = MLItr()
|
||||
|
||||
reifiedAsFailsWithCCE<MutableIterator<*>>(litr, "reifiedAs<MutableIterator<*>>(litr)")
|
||||
reifiedAsFailsWithCCE<MutableListIterator<*>>(litr, "reifiedAs<MutableListIterator<*>>(litr)")
|
||||
reifiedAsSucceeds<MutableListIterator<*>>(mlitr, "reifiedAs<MutableListIterator<*>>(mlitr)")
|
||||
|
||||
val it = It() as Any
|
||||
val mit = MIt()
|
||||
val arrayList = ArrayList<String>()
|
||||
|
||||
reifiedAsFailsWithCCE<MutableIterable<*>>(it, "reifiedAs<MutableIterable<*>>(it)")
|
||||
reifiedAsSucceeds<MutableIterable<*>>(mit, "reifiedAs<MutableIterable<*>>(mit)")
|
||||
reifiedAsSucceeds<MutableIterable<*>>(arrayList, "reifiedAs<MutableIterable<*>>(arrayList)")
|
||||
|
||||
val coll = C() as Any
|
||||
val mcoll = MC()
|
||||
|
||||
reifiedAsFailsWithCCE<MutableCollection<*>>(coll, "reifiedAs<MutableCollection<*>>(coll)")
|
||||
reifiedAsFailsWithCCE<MutableIterable<*>>(coll, "reifiedAs<MutableIterable<*>>(coll)")
|
||||
reifiedAsSucceeds<MutableCollection<*>>(mcoll, "reifiedAs<MutableCollection<*>>(mcoll)")
|
||||
reifiedAsSucceeds<MutableIterable<*>>(mcoll, "reifiedAs<MutableIterable<*>>(mcoll)")
|
||||
reifiedAsSucceeds<MutableCollection<*>>(arrayList, "reifiedAs<MutableCollection<*>>(arrayList)")
|
||||
|
||||
val list = L() as Any
|
||||
val mlist = ML()
|
||||
|
||||
reifiedAsFailsWithCCE<MutableList<*>>(list, "reifiedAs<MutableList<*>>(list)")
|
||||
reifiedAsFailsWithCCE<MutableCollection<*>>(list, "reifiedAs<MutableCollection<*>>(list)")
|
||||
reifiedAsFailsWithCCE<MutableIterable<*>>(list, "reifiedAs<MutableIterable<*>>(list)")
|
||||
reifiedAsSucceeds<MutableList<*>>(mlist, "reifiedAs<MutableList<*>>(mlist)")
|
||||
reifiedAsSucceeds<MutableCollection<*>>(mlist, "reifiedAs<MutableCollection<*>>(mlist)")
|
||||
reifiedAsSucceeds<MutableIterable<*>>(mlist, "reifiedAs<MutableIterable<*>>(mlist)")
|
||||
reifiedAsSucceeds<MutableList<*>>(arrayList, "reifiedAs<MutableList<*>>(arrayList)")
|
||||
|
||||
val set = S() as Any
|
||||
val mset = MS()
|
||||
val hashSet = HashSet<String>()
|
||||
|
||||
reifiedAsFailsWithCCE<MutableSet<*>>(set, "reifiedAs<MutableSet<*>>(set)")
|
||||
reifiedAsFailsWithCCE<MutableCollection<*>>(set, "reifiedAs<MutableCollection<*>>(set)")
|
||||
reifiedAsFailsWithCCE<MutableIterable<*>>(set, "reifiedAs<MutableIterable<*>>(set)")
|
||||
reifiedAsSucceeds<MutableSet<*>>(mset, "reifiedAs<MutableSet<*>>(mset)")
|
||||
reifiedAsSucceeds<MutableCollection<*>>(mset, "reifiedAs<MutableCollection<*>>(mset)")
|
||||
reifiedAsSucceeds<MutableIterable<*>>(mset, "reifiedAs<MutableIterable<*>>(mset)")
|
||||
reifiedAsSucceeds<MutableSet<*>>(hashSet, "reifiedAs<MutableSet<*>>(hashSet)")
|
||||
reifiedAsSucceeds<MutableCollection<*>>(hashSet, "reifiedAs<MutableCollection<*>>(hashSet)")
|
||||
reifiedAsSucceeds<MutableIterable<*>>(hashSet, "reifiedAs<MutableIterable<*>>(hashSet)")
|
||||
|
||||
val map = M() as Any
|
||||
val mmap = MM()
|
||||
val hashMap = HashMap<String, String>()
|
||||
|
||||
reifiedAsFailsWithCCE<MutableMap<*, *>>(map, "reifiedAs<MutableMap<*, *>>(map)")
|
||||
reifiedAsSucceeds<MutableMap<*, *>>(mmap, "reifiedAs<MutableMap<*, *>>(mmap)")
|
||||
reifiedAsSucceeds<MutableMap<*, *>>(hashMap, "reifiedAs<MutableMap<*, *>>(hashMap)")
|
||||
|
||||
val entry = ME() as Any
|
||||
val mentry = MME()
|
||||
|
||||
hashMap[""] = ""
|
||||
val hashMapEntry = hashMap.entrySet().first()
|
||||
|
||||
reifiedAsFailsWithCCE<MutableMap.MutableEntry<*, *>>(entry, "reifiedAs<MutableMap.MutableEntry<*, *>>(entry)")
|
||||
reifiedAsSucceeds<MutableMap.MutableEntry<*, *>>(mentry, "reifiedAs<MutableMap.MutableEntry<*, *>>(mentry)")
|
||||
reifiedAsSucceeds<MutableMap.MutableEntry<*, *>>(hashMapEntry, "reifiedAs<MutableMap.MutableEntry<*, *>>(hashMapEntry)")
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
import java.util.*
|
||||
|
||||
class Itr : Iterator<String> by ArrayList<String>().iterator()
|
||||
class MItr : MutableIterator<String> by ArrayList<String>().iterator()
|
||||
class LItr : ListIterator<String> by ArrayList<String>().listIterator()
|
||||
class MLItr : MutableListIterator<String> by ArrayList<String>().listIterator()
|
||||
|
||||
class It : Iterable<String> by ArrayList<String>()
|
||||
class MIt : MutableIterable<String> by ArrayList<String>()
|
||||
class C : Collection<String> by ArrayList<String>()
|
||||
class MC : MutableCollection<String> by ArrayList<String>()
|
||||
class L : List<String> by ArrayList<String>()
|
||||
class ML : MutableList<String> by ArrayList<String>()
|
||||
class S : Set<String> by HashSet<String>()
|
||||
class MS : MutableSet<String> by HashSet<String>()
|
||||
|
||||
class M : Map<String, String> by HashMap<String, String>()
|
||||
class MM : MutableMap<String, String> by HashMap<String, String>()
|
||||
|
||||
class ME : Map.Entry<String, String> {
|
||||
override fun getKey(): String = throw UnsupportedOperationException()
|
||||
override fun getValue(): String = throw UnsupportedOperationException()
|
||||
}
|
||||
|
||||
class MME : MutableMap.MutableEntry<String, String> {
|
||||
override fun getKey(): String = throw UnsupportedOperationException()
|
||||
override fun getValue(): String = throw UnsupportedOperationException()
|
||||
override fun setValue(value: String): String = throw UnsupportedOperationException()
|
||||
}
|
||||
|
||||
inline fun <reified T> reifiedIs(x: Any): Boolean = x is T
|
||||
inline fun <reified T> reifiedIsNot(x: Any): Boolean = x !is T
|
||||
|
||||
fun box(): String {
|
||||
val itr = Itr() as Any
|
||||
val mitr = MItr()
|
||||
|
||||
assert(reifiedIsNot<MutableIterator<*>>(itr)) { "reifiedIsNot<MutableIterator<*>>(itr)" }
|
||||
assert(reifiedIs<MutableIterator<*>>(mitr)) { "reifiedIs<MutableIterator<*>>(mitr)" }
|
||||
|
||||
val litr = LItr() as Any
|
||||
val mlitr = MLItr()
|
||||
|
||||
assert(reifiedIsNot<MutableIterator<*>>(litr)) { "reifiedIsNot<MutableIterator<*>>(litr)" }
|
||||
assert(reifiedIsNot<MutableListIterator<*>>(litr)) { "reifiedIsNot<MutableListIterator<*>>(litr)" }
|
||||
assert(reifiedIs<MutableListIterator<*>>(mlitr)) { "reifiedIs<MutableListIterator<*>>(mlitr)" }
|
||||
|
||||
val it = It() as Any
|
||||
val mit = MIt()
|
||||
val arrayList = ArrayList<String>()
|
||||
|
||||
assert(reifiedIsNot<MutableIterable<*>>(it)) { "reifiedIsNot<MutableIterable<*>>(it)" }
|
||||
assert(reifiedIs<MutableIterable<*>>(mit)) { "reifiedIs<MutableIterable<*>>(mit)" }
|
||||
assert(reifiedIs<MutableIterable<*>>(arrayList)) { "reifiedIs<MutableIterable<*>>(arrayList)" }
|
||||
|
||||
val coll = C() as Any
|
||||
val mcoll = MC()
|
||||
|
||||
assert(reifiedIsNot<MutableCollection<*>>(coll)) { "reifiedIsNot<MutableCollection<*>>(coll)" }
|
||||
assert(reifiedIsNot<MutableIterable<*>>(coll)) { "reifiedIsNot<MutableIterable<*>>(coll)" }
|
||||
assert(reifiedIs<MutableCollection<*>>(mcoll)) { "reifiedIs<MutableCollection<*>>(mcoll)" }
|
||||
assert(reifiedIs<MutableIterable<*>>(mcoll)) { "reifiedIs<MutableIterable<*>>(mcoll)" }
|
||||
assert(reifiedIs<MutableCollection<*>>(arrayList)) { "reifiedIs<MutableCollection<*>>(arrayList)" }
|
||||
|
||||
val list = L() as Any
|
||||
val mlist = ML()
|
||||
|
||||
assert(reifiedIsNot<MutableList<*>>(list)) { "reifiedIsNot<MutableList<*>>(list)" }
|
||||
assert(reifiedIsNot<MutableCollection<*>>(list)) { "reifiedIsNot<MutableCollection<*>>(list)" }
|
||||
assert(reifiedIsNot<MutableIterable<*>>(list)) { "reifiedIsNot<MutableIterable<*>>(list)" }
|
||||
assert(reifiedIs<MutableList<*>>(mlist)) { "reifiedIs<MutableList<*>>(mlist)" }
|
||||
assert(reifiedIs<MutableCollection<*>>(mlist)) { "reifiedIs<MutableCollection<*>>(mlist)" }
|
||||
assert(reifiedIs<MutableIterable<*>>(mlist)) { "reifiedIs<MutableIterable<*>>(mlist)" }
|
||||
assert(reifiedIs<MutableList<*>>(arrayList)) { "reifiedIs<MutableList<*>>(arrayList)" }
|
||||
|
||||
val set = S() as Any
|
||||
val mset = MS()
|
||||
val hashSet = HashSet<String>()
|
||||
|
||||
assert(reifiedIsNot<MutableSet<*>>(set)) { "reifiedIsNot<MutableSet<*>>(set)" }
|
||||
assert(reifiedIsNot<MutableCollection<*>>(set)) { "reifiedIsNot<MutableCollection<*>>(set)" }
|
||||
assert(reifiedIsNot<MutableIterable<*>>(set)) { "reifiedIsNot<MutableIterable<*>>(set)" }
|
||||
assert(reifiedIs<MutableSet<*>>(mset)) { "reifiedIs<MutableSet<*>>(mset)" }
|
||||
assert(reifiedIs<MutableCollection<*>>(mset)) { "reifiedIs<MutableCollection<*>>(mset)" }
|
||||
assert(reifiedIs<MutableIterable<*>>(mset)) { "reifiedIs<MutableIterable<*>>(mset)" }
|
||||
assert(reifiedIs<MutableSet<*>>(hashSet)) { "reifiedIs<MutableSet<*>>(hashSet)" }
|
||||
assert(reifiedIs<MutableCollection<*>>(hashSet)) { "reifiedIs<MutableCollection<*>>(hashSet)" }
|
||||
assert(reifiedIs<MutableIterable<*>>(hashSet)) { "reifiedIs<MutableIterable<*>>(hashSet)" }
|
||||
|
||||
val map = M() as Any
|
||||
val mmap = MM()
|
||||
val hashMap = HashMap<String, String>()
|
||||
|
||||
assert(reifiedIsNot<MutableMap<*, *>>(map)) { "reifiedIsNot<MutableMap<*, *>>(map)" }
|
||||
assert(reifiedIs<MutableMap<*, *>>(mmap)) { "reifiedIs<MutableMap<*, *>>(mmap)"}
|
||||
assert(reifiedIs<MutableMap<*, *>>(hashMap)) { "reifiedIs<MutableMap<*, *>>(hashMap)" }
|
||||
|
||||
val entry = ME() as Any
|
||||
val mentry = MME()
|
||||
|
||||
hashMap[""] = ""
|
||||
val hashMapEntry = hashMap.entrySet().first()
|
||||
|
||||
assert(reifiedIsNot<MutableMap.MutableEntry<*, *>>(entry)) { "reifiedIsNot<MutableMap.MutableEntry<*, *>>(entry)"}
|
||||
assert(reifiedIs<MutableMap.MutableEntry<*, *>>(mentry)) { "reifiedIs<MutableMap.MutableEntry<*, *>>(mentry)"}
|
||||
assert(reifiedIs<MutableMap.MutableEntry<*, *>>(hashMapEntry)) { "reifiedIs<MutableMap.MutableEntry<*, *>>(hashMapEntry)"}
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,139 @@
|
||||
import java.util.*
|
||||
|
||||
class Itr : Iterator<String> by ArrayList<String>().iterator()
|
||||
class MItr : MutableIterator<String> by ArrayList<String>().iterator()
|
||||
class LItr : ListIterator<String> by ArrayList<String>().listIterator()
|
||||
class MLItr : MutableListIterator<String> by ArrayList<String>().listIterator()
|
||||
|
||||
class It : Iterable<String> by ArrayList<String>()
|
||||
class MIt : MutableIterable<String> by ArrayList<String>()
|
||||
class C : Collection<String> by ArrayList<String>()
|
||||
class MC : MutableCollection<String> by ArrayList<String>()
|
||||
class L : List<String> by ArrayList<String>()
|
||||
class ML : MutableList<String> by ArrayList<String>()
|
||||
class S : Set<String> by HashSet<String>()
|
||||
class MS : MutableSet<String> by HashSet<String>()
|
||||
|
||||
class M : Map<String, String> by HashMap<String, String>()
|
||||
class MM : MutableMap<String, String> by HashMap<String, String>()
|
||||
|
||||
class ME : Map.Entry<String, String> {
|
||||
override fun getKey(): String = throw UnsupportedOperationException()
|
||||
override fun getValue(): String = throw UnsupportedOperationException()
|
||||
}
|
||||
|
||||
class MME : MutableMap.MutableEntry<String, String> {
|
||||
override fun getKey(): String = throw UnsupportedOperationException()
|
||||
override fun getValue(): String = throw UnsupportedOperationException()
|
||||
override fun setValue(value: String): String = throw UnsupportedOperationException()
|
||||
}
|
||||
|
||||
inline fun <reified T> reifiedSafeAsReturnsNonNull(x: Any?, operation: String) {
|
||||
val y = try {
|
||||
x as? T
|
||||
}
|
||||
catch (e: Throwable) {
|
||||
throw AssertionError("$operation: should not throw exceptions, got $e")
|
||||
}
|
||||
if (y == null) {
|
||||
throw AssertionError("$operation: should return non-null, got null")
|
||||
}
|
||||
}
|
||||
|
||||
inline fun <reified T> reifiedSafeAsReturnsNull(x: Any?, operation: String) {
|
||||
val y = try {
|
||||
x as? T
|
||||
}
|
||||
catch (e: Throwable) {
|
||||
throw AssertionError("$operation: should not throw exceptions, got $e")
|
||||
}
|
||||
if (y != null) {
|
||||
throw AssertionError("$operation: should return null, got $y")
|
||||
}
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
val itr = Itr() as Any
|
||||
val mitr = MItr()
|
||||
|
||||
reifiedSafeAsReturnsNull<MutableIterator<*>>(itr, "reifiedSafeAs<MutableIterator<*>>(itr)")
|
||||
reifiedSafeAsReturnsNonNull<MutableIterator<*>>(mitr, "reifiedSafeAs<MutableIterator<*>>(mitr)")
|
||||
|
||||
val litr = LItr() as Any
|
||||
val mlitr = MLItr()
|
||||
|
||||
reifiedSafeAsReturnsNull<MutableIterator<*>>(litr, "reifiedSafeAs<MutableIterator<*>>(litr)")
|
||||
reifiedSafeAsReturnsNull<MutableListIterator<*>>(litr, "reifiedSafeAs<MutableListIterator<*>>(litr)")
|
||||
reifiedSafeAsReturnsNonNull<MutableListIterator<*>>(mlitr, "reifiedSafeAs<MutableListIterator<*>>(mlitr)")
|
||||
|
||||
val it = It() as Any
|
||||
val mit = MIt()
|
||||
val arrayList = ArrayList<String>()
|
||||
|
||||
reifiedSafeAsReturnsNull<MutableIterable<*>>(it, "reifiedSafeAs<MutableIterable<*>>(it)")
|
||||
reifiedSafeAsReturnsNonNull<MutableIterable<*>>(mit, "reifiedSafeAs<MutableIterable<*>>(mit)")
|
||||
reifiedSafeAsReturnsNonNull<MutableIterable<*>>(arrayList, "reifiedSafeAs<MutableIterable<*>>(arrayList)")
|
||||
|
||||
val coll = C() as Any
|
||||
val mcoll = MC()
|
||||
|
||||
reifiedSafeAsReturnsNull<MutableCollection<*>>(coll, "reifiedSafeAs<MutableCollection<*>>(coll)")
|
||||
reifiedSafeAsReturnsNull<MutableIterable<*>>(coll, "reifiedSafeAs<MutableIterable<*>>(coll)")
|
||||
reifiedSafeAsReturnsNonNull<MutableCollection<*>>(mcoll, "reifiedSafeAs<MutableCollection<*>>(mcoll)")
|
||||
reifiedSafeAsReturnsNonNull<MutableIterable<*>>(mcoll, "reifiedSafeAs<MutableIterable<*>>(mcoll)")
|
||||
reifiedSafeAsReturnsNonNull<MutableCollection<*>>(arrayList, "reifiedSafeAs<MutableCollection<*>>(arrayList)")
|
||||
|
||||
val list = L() as Any
|
||||
val mlist = ML()
|
||||
|
||||
reifiedSafeAsReturnsNull<MutableList<*>>(list, "reifiedSafeAs<MutableList<*>>(list)")
|
||||
reifiedSafeAsReturnsNull<MutableCollection<*>>(list, "reifiedSafeAs<MutableCollection<*>>(list)")
|
||||
reifiedSafeAsReturnsNull<MutableIterable<*>>(list, "reifiedSafeAs<MutableIterable<*>>(list)")
|
||||
reifiedSafeAsReturnsNonNull<MutableList<*>>(mlist, "reifiedSafeAs<MutableList<*>>(mlist)")
|
||||
reifiedSafeAsReturnsNonNull<MutableCollection<*>>(mlist, "reifiedSafeAs<MutableCollection<*>>(mlist)")
|
||||
reifiedSafeAsReturnsNonNull<MutableIterable<*>>(mlist, "reifiedSafeAs<MutableIterable<*>>(mlist)")
|
||||
reifiedSafeAsReturnsNonNull<MutableList<*>>(arrayList, "reifiedSafeAs<MutableList<*>>(arrayList)")
|
||||
|
||||
val set = S() as Any
|
||||
val mset = MS()
|
||||
val hashSet = HashSet<String>()
|
||||
|
||||
reifiedSafeAsReturnsNull<MutableSet<*>>(set, "reifiedSafeAs<MutableSet<*>>(set)")
|
||||
reifiedSafeAsReturnsNull<MutableCollection<*>>(set, "reifiedSafeAs<MutableCollection<*>>(set)")
|
||||
reifiedSafeAsReturnsNull<MutableIterable<*>>(set, "reifiedSafeAs<MutableIterable<*>>(set)")
|
||||
reifiedSafeAsReturnsNonNull<MutableSet<*>>(mset, "reifiedSafeAs<MutableSet<*>>(mset)")
|
||||
reifiedSafeAsReturnsNonNull<MutableCollection<*>>(mset, "reifiedSafeAs<MutableCollection<*>>(mset)")
|
||||
reifiedSafeAsReturnsNonNull<MutableIterable<*>>(mset, "reifiedSafeAs<MutableIterable<*>>(mset)")
|
||||
reifiedSafeAsReturnsNonNull<MutableSet<*>>(hashSet, "reifiedSafeAs<MutableSet<*>>(hashSet)")
|
||||
reifiedSafeAsReturnsNonNull<MutableCollection<*>>(hashSet, "reifiedSafeAs<MutableCollection<*>>(hashSet)")
|
||||
reifiedSafeAsReturnsNonNull<MutableIterable<*>>(hashSet, "reifiedSafeAs<MutableIterable<*>>(hashSet)")
|
||||
|
||||
val map = M() as Any
|
||||
val mmap = MM()
|
||||
val hashMap = HashMap<String, String>()
|
||||
|
||||
reifiedSafeAsReturnsNull<MutableMap<*, *>>(map, "reifiedSafeAs<MutableMap<*, *>>(map)")
|
||||
reifiedSafeAsReturnsNonNull<MutableMap<*, *>>(mmap, "reifiedSafeAs<MutableMap<*, *>>(mmap)")
|
||||
reifiedSafeAsReturnsNonNull<MutableMap<*, *>>(hashMap, "reifiedSafeAs<MutableMap<*, *>>(hashMap)")
|
||||
|
||||
val entry = ME() as Any
|
||||
val mentry = MME()
|
||||
|
||||
hashMap[""] = ""
|
||||
val hashMapEntry = hashMap.entrySet().first()
|
||||
|
||||
reifiedSafeAsReturnsNull<MutableMap.MutableEntry<*, *>>(entry, "reifiedSafeAs<MutableMap.MutableEntry<*, *>>(entry)")
|
||||
reifiedSafeAsReturnsNonNull<MutableMap.MutableEntry<*, *>>(mentry, "reifiedSafeAs<MutableMap.MutableEntry<*, *>>(mentry)")
|
||||
reifiedSafeAsReturnsNonNull<MutableMap.MutableEntry<*, *>>(hashMapEntry, "reifiedSafeAs<MutableMap.MutableEntry<*, *>>(hashMapEntry)")
|
||||
|
||||
reifiedSafeAsReturnsNull<MutableIterator<*>>(null, "reifiedSafeAs<MutableIterator<*>>(null)")
|
||||
reifiedSafeAsReturnsNull<MutableListIterator<*>>(null, "reifiedSafeAs<MutableListIterator<*>>(null)")
|
||||
reifiedSafeAsReturnsNull<MutableIterable<*>>(null, "reifiedSafeAs<MutableIterable<*>>(null)")
|
||||
reifiedSafeAsReturnsNull<MutableCollection<*>>(null, "reifiedSafeAs<MutableCollection<*>>(null)")
|
||||
reifiedSafeAsReturnsNull<MutableList<*>>(null, "reifiedSafeAs<MutableList<*>>(null)")
|
||||
reifiedSafeAsReturnsNull<MutableSet<*>>(null, "reifiedSafeAs<MutableSet<*>>(null)")
|
||||
reifiedSafeAsReturnsNull<MutableMap<*, *>>(null, "reifiedSafeAs<MutableMap<*, *>>(null)")
|
||||
reifiedSafeAsReturnsNull<MutableMap.MutableEntry<*, *>>(null, "reifiedSafeAs<MutableMap.MutableEntry<*, *>>(null)")
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,133 @@
|
||||
import java.util.*
|
||||
|
||||
class Itr : Iterator<String> by ArrayList<String>().iterator()
|
||||
class MItr : MutableIterator<String> by ArrayList<String>().iterator()
|
||||
class LItr : ListIterator<String> by ArrayList<String>().listIterator()
|
||||
class MLItr : MutableListIterator<String> by ArrayList<String>().listIterator()
|
||||
|
||||
class It : Iterable<String> by ArrayList<String>()
|
||||
class MIt : MutableIterable<String> by ArrayList<String>()
|
||||
class C : Collection<String> by ArrayList<String>()
|
||||
class MC : MutableCollection<String> by ArrayList<String>()
|
||||
class L : List<String> by ArrayList<String>()
|
||||
class ML : MutableList<String> by ArrayList<String>()
|
||||
class S : Set<String> by HashSet<String>()
|
||||
class MS : MutableSet<String> by HashSet<String>()
|
||||
|
||||
class M : Map<String, String> by HashMap<String, String>()
|
||||
class MM : MutableMap<String, String> by HashMap<String, String>()
|
||||
|
||||
class ME : Map.Entry<String, String> {
|
||||
override fun getKey(): String = throw UnsupportedOperationException()
|
||||
override fun getValue(): String = throw UnsupportedOperationException()
|
||||
}
|
||||
|
||||
class MME : MutableMap.MutableEntry<String, String> {
|
||||
override fun getKey(): String = throw UnsupportedOperationException()
|
||||
override fun getValue(): String = throw UnsupportedOperationException()
|
||||
override fun setValue(value: String): String = throw UnsupportedOperationException()
|
||||
}
|
||||
|
||||
inline fun safeAsReturnsNull(operation: String, block: () -> Any?) {
|
||||
try {
|
||||
val x = block()
|
||||
assert(x == null) { "$operation: should return null, got $x" }
|
||||
}
|
||||
catch (e: Throwable) {
|
||||
assert(false) { "$operation: should not throw exceptions, got $e" }
|
||||
}
|
||||
}
|
||||
|
||||
inline fun safeAsReturnsNonNull(operation: String, block: () -> Any?) {
|
||||
try {
|
||||
val x = block()
|
||||
assert(x != null) { "$operation: should return non-null" }
|
||||
}
|
||||
catch (e: Throwable) {
|
||||
assert(false) { "$operation: should not throw exceptions, got $e" }
|
||||
}
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
val itr = Itr() as Any
|
||||
val mitr = MItr()
|
||||
|
||||
safeAsReturnsNull("itr as? MutableIterator") { itr as? MutableIterator<*> }
|
||||
safeAsReturnsNonNull("mitr as? MutableIterator") { mitr as? MutableIterator<*> }
|
||||
|
||||
val litr = LItr() as Any
|
||||
val mlitr = MLItr()
|
||||
|
||||
safeAsReturnsNull("litr as? MutableIterator") { litr as? MutableIterator<*> }
|
||||
safeAsReturnsNull("litr as? MutableListIterator") { litr as? MutableListIterator<*> }
|
||||
safeAsReturnsNonNull("mlitr as? MutableIterator") { mlitr as? MutableIterator<*> }
|
||||
safeAsReturnsNonNull("mlitr as? MutableListIterator") { mlitr as? MutableListIterator<*> }
|
||||
|
||||
val it = It() as Any
|
||||
val mit = MIt()
|
||||
val arrayList = ArrayList<String>()
|
||||
|
||||
safeAsReturnsNull("it as? MutableIterable") { it as? MutableIterable<*> }
|
||||
safeAsReturnsNonNull("mit as? MutableIterable") { mit as? MutableIterable<*> }
|
||||
safeAsReturnsNonNull("arrayList as? MutableIterable") { arrayList as? MutableIterable<*> }
|
||||
|
||||
val coll = C() as Any
|
||||
val mcoll = MC()
|
||||
|
||||
safeAsReturnsNull("coll as? MutableIterable") { coll as? MutableIterable<*> }
|
||||
safeAsReturnsNull("coll as? MutableCollection") { coll as? MutableCollection<*> }
|
||||
safeAsReturnsNonNull("mcoll as? MutableIterable") { mcoll as? MutableIterable<*> }
|
||||
safeAsReturnsNonNull("mcoll as? MutableCollection") { mcoll as? MutableCollection<*> }
|
||||
safeAsReturnsNonNull("arrayList as? MutableCollection") { arrayList as? MutableCollection<*> }
|
||||
|
||||
val list = L() as Any
|
||||
val mlist = ML()
|
||||
|
||||
safeAsReturnsNull("list as? MutableIterable") { list as? MutableIterable<*> }
|
||||
safeAsReturnsNull("list as? MutableCollection") { list as? MutableCollection<*> }
|
||||
safeAsReturnsNull("list as? MutableList") { list as? MutableList<*> }
|
||||
safeAsReturnsNonNull("mlist as? MutableIterable") { mlist as? MutableIterable<*> }
|
||||
safeAsReturnsNonNull("mlist as? MutableCollection") { mlist as? MutableCollection<*> }
|
||||
safeAsReturnsNonNull("mlist as? MutableList") { mlist as? MutableList<*> }
|
||||
|
||||
val set = S() as Any
|
||||
val mset = MS()
|
||||
val hashSet = HashSet<String>()
|
||||
|
||||
safeAsReturnsNull("set as? MutableIterable") { set as? MutableIterable<*> }
|
||||
safeAsReturnsNull("set as? MutableCollection") { set as? MutableCollection<*> }
|
||||
safeAsReturnsNull("set as? MutableSet") { set as? MutableSet<*> }
|
||||
safeAsReturnsNonNull("mset as? MutableIterable") { mset as? MutableIterable<*> }
|
||||
safeAsReturnsNonNull("mset as? MutableCollection") { mset as? MutableCollection<*> }
|
||||
safeAsReturnsNonNull("mset as? MutableSet") { mset as? MutableSet<*> }
|
||||
safeAsReturnsNonNull("hashSet as? MutableSet") { hashSet as? MutableSet<*> }
|
||||
|
||||
val map = M() as Any
|
||||
val mmap = MM()
|
||||
val hashMap = HashMap<String, String>()
|
||||
|
||||
safeAsReturnsNull("map as? MutableMap") { map as? MutableMap<*, *> }
|
||||
safeAsReturnsNonNull("mmap as? MutableMap") { mmap as? MutableMap<*, *> }
|
||||
|
||||
val entry = ME() as Any
|
||||
val mentry = MME()
|
||||
|
||||
safeAsReturnsNull("entry as? MutableMap.MutableEntry") { entry as? MutableMap.MutableEntry<*, *> }
|
||||
safeAsReturnsNonNull("mentry as? MutableMap.MutableEntry") { mentry as? MutableMap.MutableEntry<*, *> }
|
||||
|
||||
hashMap[""] = ""
|
||||
val hashMapEntry = hashMap.entrySet().first()
|
||||
|
||||
safeAsReturnsNonNull("hashMapEntry as? MutableMap.MutableEntry") { hashMapEntry as? MutableMap.MutableEntry<*, *> }
|
||||
|
||||
safeAsReturnsNull("null as? MutableIterator") { null as? MutableIterator<*> }
|
||||
safeAsReturnsNull("null as? MutableListIterator") { null as? MutableListIterator<*> }
|
||||
safeAsReturnsNull("null as? MutableIterable") { null as? MutableIterable<*> }
|
||||
safeAsReturnsNull("null as? MutableCollection") { null as? MutableCollection<*> }
|
||||
safeAsReturnsNull("null as? MutableList") { null as? MutableList<*> }
|
||||
safeAsReturnsNull("null as? MutableSet") { null as? MutableSet<*> }
|
||||
safeAsReturnsNull("null as? MutableMap") { null as? MutableMap<*, *> }
|
||||
safeAsReturnsNull("null as? MutableMap.MutableEntry") { null as? MutableMap.MutableEntry<*, *> }
|
||||
|
||||
return "OK"
|
||||
}
|
||||
Reference in New Issue
Block a user