From cc150ca832583d4c50aa552cb46a6fb6925f547d Mon Sep 17 00:00:00 2001 From: Ilya Gorbunov Date: Tue, 26 Sep 2017 21:00:37 +0300 Subject: [PATCH] Make Closeable.use call addSuppressed Call addSuppressed when it's provided by the supplementary artifact for jdk7, and only when targeting apiVersion > 1.1 #KT-18961 Fixed --- .../test/TryWithResourcesCloseableTest.kt | 2 -- .../test/TryWithResourcesCloseableTest.kt | 4 +--- libraries/stdlib/src/kotlin/io/Closeable.kt | 22 +++++++++++-------- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/libraries/stdlib/jdk7/test/TryWithResourcesCloseableTest.kt b/libraries/stdlib/jdk7/test/TryWithResourcesCloseableTest.kt index 46425c363ab..022170704bf 100644 --- a/libraries/stdlib/jdk7/test/TryWithResourcesCloseableTest.kt +++ b/libraries/stdlib/jdk7/test/TryWithResourcesCloseableTest.kt @@ -60,7 +60,6 @@ class TryWithResourcesCloseableTest { assertTrue(e.suppressed.isEmpty()) } - @Ignore @Test fun opFailsCloseFails() { val e = assertFails { Resource(faultyClose = true).use { error("op fail") } @@ -69,7 +68,6 @@ class TryWithResourcesCloseableTest { assertTrue(e.suppressed.single() is IOException) } - @Ignore @Test fun opFailsCloseFailsTwice() { val e = assertFails { Resource(faultyClose = true).use { _ -> diff --git a/libraries/stdlib/jre7/test/TryWithResourcesCloseableTest.kt b/libraries/stdlib/jre7/test/TryWithResourcesCloseableTest.kt index 46425c363ab..679137f20bd 100644 --- a/libraries/stdlib/jre7/test/TryWithResourcesCloseableTest.kt +++ b/libraries/stdlib/jre7/test/TryWithResourcesCloseableTest.kt @@ -14,7 +14,7 @@ * limitations under the License. */ -package kotlin.jdk7.test +package test.jdk7 import org.junit.Ignore import java.io.* @@ -60,7 +60,6 @@ class TryWithResourcesCloseableTest { assertTrue(e.suppressed.isEmpty()) } - @Ignore @Test fun opFailsCloseFails() { val e = assertFails { Resource(faultyClose = true).use { error("op fail") } @@ -69,7 +68,6 @@ class TryWithResourcesCloseableTest { assertTrue(e.suppressed.single() is IOException) } - @Ignore @Test fun opFailsCloseFailsTwice() { val e = assertFails { Resource(faultyClose = true).use { _ -> diff --git a/libraries/stdlib/src/kotlin/io/Closeable.kt b/libraries/stdlib/src/kotlin/io/Closeable.kt index 914c6fad02f..6779df8591e 100644 --- a/libraries/stdlib/src/kotlin/io/Closeable.kt +++ b/libraries/stdlib/src/kotlin/io/Closeable.kt @@ -30,19 +30,23 @@ import kotlin.internal.* */ @InlineOnly public inline fun T.use(block: (T) -> R): R { - var closed = false + var exception: Throwable? = null try { return block(this) - } catch (e: Exception) { - closed = true - try { - this?.close() - } catch (closeException: Exception) { - } + } catch (e: Throwable) { + exception = e throw e } finally { - if (!closed) { - this?.close() + when { + apiVersionIsAtLeast(1, 1, 0) -> this.closeFinally(exception) + this == null -> {} + exception == null -> close() + else -> + try { + close() + } catch (closeException: Throwable) { + // cause.addSuppressed(closeException) // ignored here + } } } }