diff --git a/compiler/testData/codegen/box/delegatedProperty/genericDelegateUncheckedCast1.kt b/compiler/testData/codegen/box/delegatedProperty/genericDelegateUncheckedCast1.kt new file mode 100644 index 00000000000..e122e11cd6e --- /dev/null +++ b/compiler/testData/codegen/box/delegatedProperty/genericDelegateUncheckedCast1.kt @@ -0,0 +1,38 @@ +// IGNORE_BACKEND: JS + +import kotlin.reflect.KProperty + +class Delegate(var inner: T) { + operator fun getValue(t: Any?, p: KProperty<*>): T = inner + operator fun setValue(t: Any?, p: KProperty<*>, i: T) { inner = i } +} + +val del = Delegate("zzz") + +class A { + inner class B { + var prop: String by del + } +} + +inline fun asFailsWithCCE(block: () -> Unit) { + try { + block() + } + catch (e: ClassCastException) { + return + } + catch (e: Throwable) { + throw AssertionError("Should throw ClassCastException, got $e") + } + throw AssertionError("Should throw ClassCastException, no exception thrown") +} + +fun box(): String { + val c = A().B() + + (del as Delegate).inner = 10 + asFailsWithCCE { c.prop } // does not fail in JS due KT-8135. + + return "OK" +} diff --git a/compiler/testData/codegen/box/delegatedProperty/genericDelegateUncheckedCast2.kt b/compiler/testData/codegen/box/delegatedProperty/genericDelegateUncheckedCast2.kt new file mode 100644 index 00000000000..14e4970fa59 --- /dev/null +++ b/compiler/testData/codegen/box/delegatedProperty/genericDelegateUncheckedCast2.kt @@ -0,0 +1,38 @@ +// IGNORE_BACKEND: JVM, JS + +import kotlin.reflect.KProperty + +class Delegate(var inner: T) { + operator fun getValue(t: Any?, p: KProperty<*>): T = inner + operator fun setValue(t: Any?, p: KProperty<*>, i: T) { inner = i } +} + +val del = Delegate("zzz") + +class A { + inner class B { + var prop: String by del + } +} + +inline fun asFailsWithCCE(block: () -> Unit) { + try { + block() + } + catch (e: ClassCastException) { + return + } + catch (e: Throwable) { + throw AssertionError("Should throw ClassCastException, got $e") + } + throw AssertionError("Should throw ClassCastException, no exception thrown") +} + +fun box(): String { + val c = A().B() + + (del as Delegate).inner = null + asFailsWithCCE { c.prop } // does not fail in JVM, JS due KT-8135. + + return "OK" +} \ No newline at end of file diff --git a/libraries/stdlib/test/properties/delegation/MapAccessorsTest.kt b/libraries/stdlib/test/properties/delegation/MapAccessorsTest.kt index 0771be8081d..4a40c82d36d 100644 --- a/libraries/stdlib/test/properties/delegation/MapAccessorsTest.kt +++ b/libraries/stdlib/test/properties/delegation/MapAccessorsTest.kt @@ -32,7 +32,6 @@ class ValByMapExtensionsTest { class VarByMapExtensionsTest { -// IGNORE_BACKEND: NATIVE val map = hashMapOf("a" to "all", "b" to null, "c" to 1, "xProperty" to 1.0) val map2: MutableMap = hashMapOf("a2" to "all") @@ -55,49 +54,8 @@ class VarByMapExtensionsTest { map2.remove("a2") assertEquals("empty", a2) - map["c"] = "string" - // fails { c } // does not fail in JS due to KT-8135 - - map["a"] = null - a // fails { a } // does not fail due to KT-8135 - assertFailsWith { d } map["d"] = null assertEquals(null, d) } -} - -class VarByMapExtensionsTest_ForNative { - val map = hashMapOf("a" to "all", "b" to null, "c" to 1, "xProperty" to 1.0) - val map2: MutableMap = hashMapOf("a2" to "all") - - var a: String by map - var b: Any? by map - var c: Int by map - var d: String? by map - var a2: String by map2.withDefault { "empty" } - //var x: Int by map2 // prohibited by type system - - @Test fun doTest() { - assertEquals("all", a) - assertEquals(null, b) - assertEquals(1, c) - c = 2 - assertEquals(2, c) - assertEquals(2, map["c"]) - - assertEquals("all", a2) - map2.remove("a2") - assertEquals("empty", a2) - - map["c"] = "string" - // fails { c } // does not fail in JS due to KT-8135 - - map["a"] = null - //a Fails in Native, KT-8135 is already fixed in Native. // fails { a } // does not fail due to KT-8135 - - assertFailsWith { d } - map["d"] = null - assertEquals(null, d) - } -} +} \ No newline at end of file