// WITH_RUNTIME class Itr : Iterator by ArrayList().iterator() class MItr : MutableIterator by ArrayList().iterator() class LItr : ListIterator by ArrayList().listIterator() class MLItr : MutableListIterator by ArrayList().listIterator() class It : Iterable by ArrayList() class MIt : MutableIterable by ArrayList() class C : Collection by ArrayList() class MC : MutableCollection by ArrayList() class L : List by ArrayList() class ML : MutableList by ArrayList() class S : Set by HashSet() class MS : MutableSet by HashSet() class M : Map by HashMap() class MM : MutableMap by HashMap() class ME : Map.Entry { override val key: String get() = throw UnsupportedOperationException() override val value: String get() = throw UnsupportedOperationException() } class MME : MutableMap.MutableEntry { override val key: String get() = throw UnsupportedOperationException() override val value: String get() = throw UnsupportedOperationException() override fun setValue(value: String): String = throw UnsupportedOperationException() } inline fun asFailsWithCCE(operation: String, block: () -> Unit) { try { block() } catch (e: 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() 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() 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() 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.entries.first() asSucceeds("hashMapEntry as MutableMap.MutableEntry") { hashMapEntry as MutableMap.MutableEntry<*, *> } return "OK" }