From 7195e26ae24ec11d132784b3af3b1b21cdf362cb Mon Sep 17 00:00:00 2001 From: Ilya Gorbunov Date: Sat, 28 Jan 2017 00:36:49 +0300 Subject: [PATCH] Rollback Closeable.use implementation as it was in 1.0 in order not to call functions that were introduced in 1.1 from this inline function. #KT-16026 --- .../jre7/test/TryWithResourcesCloseableTest.kt | 3 +++ libraries/stdlib/src/kotlin/io/Closeable.kt | 14 ++++++++++---- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/libraries/stdlib/jre7/test/TryWithResourcesCloseableTest.kt b/libraries/stdlib/jre7/test/TryWithResourcesCloseableTest.kt index d26101a09d4..d67583cc202 100644 --- a/libraries/stdlib/jre7/test/TryWithResourcesCloseableTest.kt +++ b/libraries/stdlib/jre7/test/TryWithResourcesCloseableTest.kt @@ -1,5 +1,6 @@ package kotlin.jdk7.test +import org.junit.Ignore import java.io.* import org.junit.Test import java.util.* @@ -43,6 +44,7 @@ class TryWithResourcesCloseableTest { assertTrue(e.suppressed.isEmpty()) } + @Ignore @Test fun opFailsCloseFails() { val e = assertFails { Resource(faultyClose = true).use { error("op fail") } @@ -51,6 +53,7 @@ class TryWithResourcesCloseableTest { assertTrue(e.suppressed.single() is IOException) } + @Ignore @Test fun opFailsCloseFailsTwice() { val e = assertFails { Resource(faultyClose = true).use { res1 -> diff --git a/libraries/stdlib/src/kotlin/io/Closeable.kt b/libraries/stdlib/src/kotlin/io/Closeable.kt index e0b5aaa30b0..914c6fad02f 100644 --- a/libraries/stdlib/src/kotlin/io/Closeable.kt +++ b/libraries/stdlib/src/kotlin/io/Closeable.kt @@ -30,14 +30,20 @@ import kotlin.internal.* */ @InlineOnly public inline fun T.use(block: (T) -> R): R { - var exception: Throwable? = null + var closed = false try { return block(this) - } catch (e: Throwable) { - exception = e + } catch (e: Exception) { + closed = true + try { + this?.close() + } catch (closeException: Exception) { + } throw e } finally { - this.closeFinally(exception) + if (!closed) { + this?.close() + } } }