From fb6c7e20de47ba6f4c56f6dc3cf7444efa4318bd Mon Sep 17 00:00:00 2001 From: Ilya Gorbunov Date: Wed, 10 Jun 2015 21:25:58 +0300 Subject: [PATCH] Use identity equals in lazy unescape. #KT-7474 Fixed --- .../src/kotlin/properties/Delegation.kt | 2 +- .../delegation/lazy/LazyValuesTest.kt | 23 +++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/libraries/stdlib/src/kotlin/properties/Delegation.kt b/libraries/stdlib/src/kotlin/properties/Delegation.kt index 90e03b515c1..9398428f9d2 100644 --- a/libraries/stdlib/src/kotlin/properties/Delegation.kt +++ b/libraries/stdlib/src/kotlin/properties/Delegation.kt @@ -158,7 +158,7 @@ private fun escape(value: Any?): Any { } private fun unescape(value: Any?): Any? { - return if (value == NULL_VALUE) null else value + return if (value === NULL_VALUE) null else value } private class LazyVal(private val initializer: () -> T) : ReadOnlyProperty { diff --git a/libraries/stdlib/test/properties/delegation/lazy/LazyValuesTest.kt b/libraries/stdlib/test/properties/delegation/lazy/LazyValuesTest.kt index 21b0c1f121f..6bec3bd385f 100644 --- a/libraries/stdlib/test/properties/delegation/lazy/LazyValuesTest.kt +++ b/libraries/stdlib/test/properties/delegation/lazy/LazyValuesTest.kt @@ -30,6 +30,10 @@ class LazyValuesTest(): DelegationTestBase() { test fun testVolatileLazyVal() { doTest(TestVolatileLazyVal()) } + + test fun testIdentityEqualsIsUsedToUnescapeLazyVal() { + doTest(TestIdentityEqualsIsUsedToUnescapeLazyVal()) + } } class TestLazyVal: WithBox { @@ -142,3 +146,22 @@ class TestAtomicNullableLazyVal: WithBox { return null } } + +class TestIdentityEqualsIsUsedToUnescapeLazyVal: WithBox { + var equalsCalled = 0 + val a by Delegates.lazy { ClassWithCustomEquality { equalsCalled++ } } + + override fun box(): String { + a + a + if (equalsCalled > 0) return "fail: equals called $equalsCalled times." + return "OK" + } +} + +private class ClassWithCustomEquality(private val onEqualsCalled: () -> Unit) { + override fun equals(other: Any?): Boolean { + onEqualsCalled() + return super.equals(other) + } +} \ No newline at end of file