// WITH_STDLIB fun unsupported(): Nothing = throw UnsupportedOperationException() class Weird : Iterator, MutableIterable, MutableMap.MutableEntry { override fun next(): String = unsupported() override fun hasNext(): Boolean = unsupported() override val key: String get() = unsupported() override val value: String get() = unsupported() override fun setValue(value: String): String = unsupported() override fun iterator(): MutableIterator = unsupported() } inline fun asFailsWithCCE(operation: String, cast: () -> Unit) { try { cast() } 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, cast: () -> Unit) { try { cast() } catch (e: Throwable) { throw AssertionError("$operation: should not throw exceptions, got $e") } } inline fun safeAsReturnsNull(operation: String, cast: () -> Any?) { try { val x = cast() require(x == null) { "$operation: should return null, got $x" } } catch (e: Throwable) { throw AssertionError("$operation: should not throw exceptions, got $e") } } inline fun safeAsReturnsNonNull(operation: String, cast: () -> Any?) { try { val x = cast() require(x != null) { "$operation: should return non-null" } } catch (e: Throwable) { throw AssertionError("$operation: should not throw exceptions, got $e") } } inline fun reifiedIs(x: Any): Boolean = x is T inline fun reifiedIsNot(x: Any): Boolean = x !is T inline fun reifiedAsSucceeds(x: Any, operation: String) { try { x as T } catch (e: Throwable) { throw AssertionError("$operation: should not throw exceptions, got $e") } } inline fun reifiedAsFailsWithCCE(x: Any, operation: String) { try { x as T } catch (e: ClassCastException) { return } catch (e: Throwable) { throw AssertionError("$operation: should throw ClassCastException, got $e") } throw AssertionError("$operation: should fail with CCE, no exception thrown") } inline fun 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 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 w: Any = Weird() require(w is Iterator<*>) { "w is Iterator<*>" } require(w !is MutableIterator<*>) { "w !is MutableIterator<*>" } require(w is MutableIterable<*>) { "w is MutableIterable<*>" } require(w is MutableMap.MutableEntry<*, *>) { "w is MutableMap.MutableEntry<*, *>" } asSucceeds("w as Iterator<*>") { w as Iterator<*> } asFailsWithCCE("w as MutableIterator<*>") { w as MutableIterator<*> } asSucceeds("w as MutableIterable<*>") { w as MutableIterable<*> } asSucceeds("w as MutableMap.MutableEntry<*, *>") { w as MutableMap.MutableEntry<*, *> } safeAsReturnsNonNull("w as Iterator<*>") { w as? Iterator<*> } safeAsReturnsNull("w as? MutableIterator<*>") { w as? MutableIterator<*> } safeAsReturnsNonNull("w as? MutableIterable<*>") { w as? MutableIterable<*> } safeAsReturnsNonNull("w as? MutableMap.MutableEntry<*, *>") { w as? MutableMap.MutableEntry<*, *> } require(reifiedIs>(w)) { "reifiedIs>(w)" } require(reifiedIsNot>(w)) { "reifiedIsNot>(w)" } require(reifiedIs>(w)) { "reifiedIs>(w)" } require(reifiedIs>(w)) { "reifiedIs>(w)" } reifiedAsSucceeds>(w, "reified w as Iterator<*>") reifiedAsFailsWithCCE>(w, "reified w as MutableIterator<*>") reifiedAsSucceeds>(w, "reified w as MutableIterable<*>") reifiedAsSucceeds>(w, "reified w as MutableMap.MutableEntry<*, *>") reifiedSafeAsReturnsNonNull>(w, "reified w as? Iterator<*>") reifiedSafeAsReturnsNull>(w, "reified w as? MutableIterator<*>") reifiedSafeAsReturnsNonNull>(w, "reified w as? MutableIterable<*>") reifiedSafeAsReturnsNonNull>(w, "reified w as? MutableMap.MutableEntry<*, *>") return "OK" }