From 4f20d2dccf888c84e3db859af1ae60de197bb58f Mon Sep 17 00:00:00 2001 From: Jinseong Jeon Date: Tue, 9 Mar 2021 22:31:10 -0800 Subject: [PATCH] Reproduce KT-45385: false positive MUST_BE_INITIALIZED_* after rethrow --- ...irOldFrontendDiagnosticsTestGenerated.java | 6 + .../tests/initializedAfterRethrow.fir.kt | 222 ++++++++++++++++++ .../tests/initializedAfterRethrow.kt | 222 ++++++++++++++++++ .../tests/initializedAfterRethrow.txt | 116 +++++++++ .../test/runners/DiagnosticTestGenerated.java | 6 + 5 files changed, 572 insertions(+) create mode 100644 compiler/testData/diagnostics/tests/initializedAfterRethrow.fir.kt create mode 100644 compiler/testData/diagnostics/tests/initializedAfterRethrow.kt create mode 100644 compiler/testData/diagnostics/tests/initializedAfterRethrow.txt diff --git a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsTestGenerated.java b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsTestGenerated.java index 5a68ead15c6..485f7919e2c 100644 --- a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsTestGenerated.java +++ b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsTestGenerated.java @@ -362,6 +362,12 @@ public class FirOldFrontendDiagnosticsTestGenerated extends AbstractFirDiagnosti runTest("compiler/testData/diagnostics/tests/InfixModifierApplicability.kt"); } + @Test + @TestMetadata("initializedAfterRethrow.kt") + public void testInitializedAfterRethrow() throws Exception { + runTest("compiler/testData/diagnostics/tests/initializedAfterRethrow.kt"); + } + @Test @TestMetadata("InvokeAndRecursiveResolve.kt") public void testInvokeAndRecursiveResolve() throws Exception { diff --git a/compiler/testData/diagnostics/tests/initializedAfterRethrow.fir.kt b/compiler/testData/diagnostics/tests/initializedAfterRethrow.fir.kt new file mode 100644 index 00000000000..0526483c9af --- /dev/null +++ b/compiler/testData/diagnostics/tests/initializedAfterRethrow.fir.kt @@ -0,0 +1,222 @@ +fun foo(): Int = 42 + +object ThrowInTryWithCatch { + private val p: String + + init { + try { + throw Exception() + } catch (e: Exception) { + } + p = "OK" + } +} + +object ThrowInTryWithCatchAndFinally { + private val p: String + + init { + try { + throw Exception() + } catch (e: Exception) { + } finally { + } + p = "OK" + } +} + +object ThrowInFinally { + private val p: String + + init { + try { + foo() + } catch (e: Exception) { + } finally { + throw Exception() + } + p = "OK" + } +} + +object RethrowInCatch { + private val p: String + + init { + try { + foo() + } catch (e: Exception) { + throw e + } + p = "OK" + } +} + +object RethrowInCatchWithFinally { + private val p: String + + init { + try { + foo() + } catch (e: Exception) { + throw e + } finally { + } + p = "OK" + } +} + +object InnerTryWithCatch { + private val p: String + + init { + try { + foo() + } catch (e: Exception) { + try { + throw e + } catch (ee: Exception) { + } + } + p = "OK" + } +} + +object InnerTryWithFinally { + private val p: String + + init { + try { + foo() + } catch (e: Exception) { + try { + throw e + } finally { + } + } + p = "OK" + } +} + + +object InnerTryWithCatchAndFinally { + private val p: String + + init { + try { + foo() + } catch (e: Exception) { + try { + throw e + } catch (ee: Exception) { + } finally { + } + } + p = "OK" + } +} + +object InnerCatch { + private val p: String + + init { + try { + foo() + } catch (e: Exception) { + try { + foo() + } catch (ee: Exception) { + throw ee + } + } + p = "OK" + } +} + +object InnerCatchWithFinally { + private val p: String + + init { + try { + foo() + } catch (e: Exception) { + try { + foo() + } catch (ee: Exception) { + throw ee + } finally { + } + } + p = "OK" + } +} + +object InnerCatchOuterRethrow { + private val p: String + + init { + try { + foo() + } catch (e: Exception) { + try { + foo() + } catch (ee: Exception) { + throw e + } + } + p = "OK" + } +} + +object InnerCatchOuterRethrowWithFinally { + private val p: String + + init { + try { + foo() + } catch (e: Exception) { + try { + foo() + } catch (ee: Exception) { + throw e + } finally { + } + } + p = "OK" + } +} + +object InnerFinally { + private val p: String + + init { + try { + foo() + } catch (e: Exception) { + try { + foo() + } finally { + throw e + } + } + p = "OK" + } +} + +object InnerFinallyWithCatch { + private val p: String + + init { + try { + foo() + } catch (e: Exception) { + try { + foo() + } catch (ee: Exception) { + } finally { + throw e + } + } + p = "OK" + } +} diff --git a/compiler/testData/diagnostics/tests/initializedAfterRethrow.kt b/compiler/testData/diagnostics/tests/initializedAfterRethrow.kt new file mode 100644 index 00000000000..af394e1fa19 --- /dev/null +++ b/compiler/testData/diagnostics/tests/initializedAfterRethrow.kt @@ -0,0 +1,222 @@ +fun foo(): Int = 42 + +object ThrowInTryWithCatch { + private val p: String + + init { + try { + throw Exception() + } catch (e: Exception) { + } + p = "OK" + } +} + +object ThrowInTryWithCatchAndFinally { + private val p: String + + init { + try { + throw Exception() + } catch (e: Exception) { + } finally { + } + p = "OK" + } +} + +object ThrowInFinally { + private val p: String + + init { + try { + foo() + } catch (e: Exception) { + } finally { + throw Exception() + } + p = "OK" + } +} + +object RethrowInCatch { + private val p: String + + init { + try { + foo() + } catch (e: Exception) { + throw e + } + p = "OK" + } +} + +object RethrowInCatchWithFinally { + private val p: String + + init { + try { + foo() + } catch (e: Exception) { + throw e + } finally { + } + p = "OK" + } +} + +object InnerTryWithCatch { + private val p: String + + init { + try { + foo() + } catch (e: Exception) { + try { + throw e + } catch (ee: Exception) { + } + } + p = "OK" + } +} + +object InnerTryWithFinally { + private val p: String + + init { + try { + foo() + } catch (e: Exception) { + try { + throw e + } finally { + } + } + p = "OK" + } +} + + +object InnerTryWithCatchAndFinally { + private val p: String + + init { + try { + foo() + } catch (e: Exception) { + try { + throw e + } catch (ee: Exception) { + } finally { + } + } + p = "OK" + } +} + +object InnerCatch { + private val p: String + + init { + try { + foo() + } catch (e: Exception) { + try { + foo() + } catch (ee: Exception) { + throw ee + } + } + p = "OK" + } +} + +object InnerCatchWithFinally { + private val p: String + + init { + try { + foo() + } catch (e: Exception) { + try { + foo() + } catch (ee: Exception) { + throw ee + } finally { + } + } + p = "OK" + } +} + +object InnerCatchOuterRethrow { + private val p: String + + init { + try { + foo() + } catch (e: Exception) { + try { + foo() + } catch (ee: Exception) { + throw e + } + } + p = "OK" + } +} + +object InnerCatchOuterRethrowWithFinally { + private val p: String + + init { + try { + foo() + } catch (e: Exception) { + try { + foo() + } catch (ee: Exception) { + throw e + } finally { + } + } + p = "OK" + } +} + +object InnerFinally { + private val p: String + + init { + try { + foo() + } catch (e: Exception) { + try { + foo() + } finally { + throw e + } + } + p = "OK" + } +} + +object InnerFinallyWithCatch { + private val p: String + + init { + try { + foo() + } catch (e: Exception) { + try { + foo() + } catch (ee: Exception) { + } finally { + throw e + } + } + p = "OK" + } +} diff --git a/compiler/testData/diagnostics/tests/initializedAfterRethrow.txt b/compiler/testData/diagnostics/tests/initializedAfterRethrow.txt new file mode 100644 index 00000000000..d0ef3c4a075 --- /dev/null +++ b/compiler/testData/diagnostics/tests/initializedAfterRethrow.txt @@ -0,0 +1,116 @@ +package + +public fun foo(): kotlin.Int + +public object InnerCatch { + private constructor InnerCatch() + private final val p: kotlin.String + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String +} + +public object InnerCatchOuterRethrow { + private constructor InnerCatchOuterRethrow() + private final val p: kotlin.String + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String +} + +public object InnerCatchOuterRethrowWithFinally { + private constructor InnerCatchOuterRethrowWithFinally() + private final val p: kotlin.String + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String +} + +public object InnerCatchWithFinally { + private constructor InnerCatchWithFinally() + private final val p: kotlin.String + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String +} + +public object InnerFinally { + private constructor InnerFinally() + private final val p: kotlin.String + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String +} + +public object InnerFinallyWithCatch { + private constructor InnerFinallyWithCatch() + private final val p: kotlin.String + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String +} + +public object InnerTryWithCatch { + private constructor InnerTryWithCatch() + private final val p: kotlin.String + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String +} + +public object InnerTryWithCatchAndFinally { + private constructor InnerTryWithCatchAndFinally() + private final val p: kotlin.String + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String +} + +public object InnerTryWithFinally { + private constructor InnerTryWithFinally() + private final val p: kotlin.String + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String +} + +public object RethrowInCatch { + private constructor RethrowInCatch() + private final val p: kotlin.String + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String +} + +public object RethrowInCatchWithFinally { + private constructor RethrowInCatchWithFinally() + private final val p: kotlin.String + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String +} + +public object ThrowInFinally { + private constructor ThrowInFinally() + private final val p: kotlin.String + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String +} + +public object ThrowInTryWithCatch { + private constructor ThrowInTryWithCatch() + private final val p: kotlin.String + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String +} + +public object ThrowInTryWithCatchAndFinally { + private constructor ThrowInTryWithCatchAndFinally() + private final val p: kotlin.String + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String +} + diff --git a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/DiagnosticTestGenerated.java b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/DiagnosticTestGenerated.java index 6c2f4c9a2e7..215f7d5612b 100644 --- a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/DiagnosticTestGenerated.java +++ b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/DiagnosticTestGenerated.java @@ -362,6 +362,12 @@ public class DiagnosticTestGenerated extends AbstractDiagnosticTest { runTest("compiler/testData/diagnostics/tests/InfixModifierApplicability.kt"); } + @Test + @TestMetadata("initializedAfterRethrow.kt") + public void testInitializedAfterRethrow() throws Exception { + runTest("compiler/testData/diagnostics/tests/initializedAfterRethrow.kt"); + } + @Test @TestMetadata("InvokeAndRecursiveResolve.kt") public void testInvokeAndRecursiveResolve() throws Exception {