From 0c7756510497ffcb5bf4bde36043b5bdb919423f Mon Sep 17 00:00:00 2001 From: Mads Ager Date: Tue, 27 Apr 2021 17:19:07 +0200 Subject: [PATCH] JVM_IR: Add more local variable tests for finally code. --- .../backend/jvm/codegen/ExpressionCodegen.kt | 4 +- .../debug/localVariables/tryFinally11.kt | 37 ++++++++++++++++++ .../debug/localVariables/tryFinally12.kt | 38 +++++++++++++++++++ .../debug/localVariables/tryFinally13.kt | 36 ++++++++++++++++++ .../debug/localVariables/tryFinally14.kt | 26 +++++++++++++ .../debug/localVariables/tryFinally15.kt | 27 +++++++++++++ .../debug/localVariables/tryFinally16.kt | 29 ++++++++++++++ .../debug/localVariables/tryFinally7.kt | 2 +- .../IrLocalVariableTestGenerated.java | 36 ++++++++++++++++++ .../LocalVariableTestGenerated.java | 36 ++++++++++++++++++ 10 files changed, 268 insertions(+), 3 deletions(-) create mode 100644 compiler/testData/debug/localVariables/tryFinally11.kt create mode 100644 compiler/testData/debug/localVariables/tryFinally12.kt create mode 100644 compiler/testData/debug/localVariables/tryFinally13.kt create mode 100644 compiler/testData/debug/localVariables/tryFinally14.kt create mode 100644 compiler/testData/debug/localVariables/tryFinally15.kt create mode 100644 compiler/testData/debug/localVariables/tryFinally16.kt diff --git a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/codegen/ExpressionCodegen.kt b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/codegen/ExpressionCodegen.kt index 3ee31e14a47..78901359e27 100644 --- a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/codegen/ExpressionCodegen.kt +++ b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/codegen/ExpressionCodegen.kt @@ -423,7 +423,7 @@ class ExpressionCodegen( } } - private fun addLocalVariableGapsForFinallyBlocks( + private fun splitLocalVariableRangesByFinallyBlocks( info: BlockInfo, tryWithFinallyInfo: TryWithFinallyInfo, gapStart: Label, @@ -1276,7 +1276,7 @@ class ExpressionCodegen( // Split the local variables for the blocks on the way to the finally. Variables introduced in these blocks do not // cover the finally block code. val endOfFinallyCode = markNewLinkedLabel() - addLocalVariableGapsForFinallyBlocks(data, tryWithFinallyInfo, gapStart, endOfFinallyCode) + splitLocalVariableRangesByFinallyBlocks(data, tryWithFinallyInfo, gapStart, endOfFinallyCode) val gapEnd = afterJumpLabel ?: endOfFinallyCode tryWithFinallyInfo.gaps.add(gapStart to gapEnd) diff --git a/compiler/testData/debug/localVariables/tryFinally11.kt b/compiler/testData/debug/localVariables/tryFinally11.kt new file mode 100644 index 00000000000..19a0ea97eda --- /dev/null +++ b/compiler/testData/debug/localVariables/tryFinally11.kt @@ -0,0 +1,37 @@ +// FILE: test.kt + +fun box(): String { + try { + for (i in 0 until 1) { + try { + val x = "x" + throw RuntimeException(x) + } catch (e: Exception) { + val y = "y" + val z = "z" + break // TODO: why does the break not have a line number so we can stop on it? + } finally { + throw RuntimeException("$i") // TODO: `e` should not be visible here + } + } + } finally { + return "OK" + } // TODO: why do we go to this line before going to the line above for the finally block. + return "FAIL" +} + +// The local variables `z` and `y` are visible in the finally block with old backend. +// IGNORE_BACKEND: JVM + +// LOCAL VARIABLES +// test.kt:4 box: +// test.kt:5 box: +// test.kt:6 box: i:int=0:int +// test.kt:7 box: i:int=0:int +// test.kt:8 box: i:int=0:int, x:java.lang.String="x":java.lang.String +// test.kt:9 box: i:int=0:int +// test.kt:10 box: i:int=0:int, e:java.lang.Exception=java.lang.RuntimeException +// test.kt:11 box: i:int=0:int, e:java.lang.Exception=java.lang.RuntimeException, y:java.lang.String="y":java.lang.String +// test.kt:14 box: i:int=0:int, e:java.lang.Exception=java.lang.RuntimeException +// test.kt:19 box: +// test.kt:18 box: diff --git a/compiler/testData/debug/localVariables/tryFinally12.kt b/compiler/testData/debug/localVariables/tryFinally12.kt new file mode 100644 index 00000000000..ea8e8564424 --- /dev/null +++ b/compiler/testData/debug/localVariables/tryFinally12.kt @@ -0,0 +1,38 @@ +// FILE: test.kt + +fun box(): String { + try { + for (i in 0 until 1) { + try { + val x = "x" + throw RuntimeException(x) + } catch (e: Exception) { + val y = "y" + val z = "z" + continue // TODO: why does the continue not have a line number so we stop here? + } finally { + throw RuntimeException("$i") // TODO: `e` should not be visible here + } + } + } finally { + return "OK" + } // TODO: why stop here before the line above for the finally block? + return "FAIL" +} + +// The local variables `z` and `y` are visible in the finally block with old backend. +// IGNORE_BACKEND: JVM + +// LOCAL VARIABLES +// test.kt:4 box: +// test.kt:5 box: +// test.kt:6 box: i:int=0:int +// test.kt:7 box: i:int=0:int +// test.kt:8 box: i:int=0:int, x:java.lang.String="x":java.lang.String +// test.kt:9 box: i:int=0:int +// test.kt:10 box: i:int=0:int, e:java.lang.Exception=java.lang.RuntimeException +// test.kt:11 box: i:int=0:int, e:java.lang.Exception=java.lang.RuntimeException, y:java.lang.String="y":java.lang.String +// test.kt:14 box: i:int=0:int, e:java.lang.Exception=java.lang.RuntimeException +// test.kt:19 box: +// test.kt:18 box: + diff --git a/compiler/testData/debug/localVariables/tryFinally13.kt b/compiler/testData/debug/localVariables/tryFinally13.kt new file mode 100644 index 00000000000..770dc03e4f9 --- /dev/null +++ b/compiler/testData/debug/localVariables/tryFinally13.kt @@ -0,0 +1,36 @@ +// FILE: test.kt + +fun box(): String { + try { + for (i in 0 until 1) { + try { + val x = "x" + throw RuntimeException(x) + } catch (e: Exception) { + val y = "y" + return "FAIL1" + } finally { + return "FAIL2" // TODO: `e` should not be visible here. + } + } + } finally { + return "OK" // TODO: `e` should not be visible here. + } + return "FAIL3" +} + +// The local variables `y` and `i` are visible in finally blocks with old backend. +// IGNORE_BACKEND: JVM + +// LOCAL VARIABLES +// test.kt:4 box: +// test.kt:5 box: +// test.kt:6 box: i:int=0:int +// test.kt:7 box: i:int=0:int +// test.kt:8 box: i:int=0:int, x:java.lang.String="x":java.lang.String +// test.kt:9 box: i:int=0:int +// test.kt:10 box: i:int=0:int, e:java.lang.Exception=java.lang.RuntimeException +// test.kt:11 box: i:int=0:int, e:java.lang.Exception=java.lang.RuntimeException, y:java.lang.String="y":java.lang.String +// test.kt:13 box: i:int=0:int, e:java.lang.Exception=java.lang.RuntimeException +// test.kt:17 box: e:java.lang.Exception=java.lang.RuntimeException + diff --git a/compiler/testData/debug/localVariables/tryFinally14.kt b/compiler/testData/debug/localVariables/tryFinally14.kt new file mode 100644 index 00000000000..06680871fc3 --- /dev/null +++ b/compiler/testData/debug/localVariables/tryFinally14.kt @@ -0,0 +1,26 @@ +// FILE: test.kt + +fun box(): String { + try { + for (i in 0 until 1) { + try { + val x = "x" + var y = "y" + } finally { + break + } + } + } finally { + return "OK" + } + return "FAIL" +} + +// LOCAL VARIABLES +// test.kt:4 box: +// test.kt:5 box: +// test.kt:6 box: i:int=0:int +// test.kt:7 box: i:int=0:int +// test.kt:8 box: i:int=0:int, x:java.lang.String="x":java.lang.String +// test.kt:10 box: i:int=0:int +// test.kt:14 box: diff --git a/compiler/testData/debug/localVariables/tryFinally15.kt b/compiler/testData/debug/localVariables/tryFinally15.kt new file mode 100644 index 00000000000..3cbf8da4f4c --- /dev/null +++ b/compiler/testData/debug/localVariables/tryFinally15.kt @@ -0,0 +1,27 @@ +// FILE: test.kt + +fun box(): String { + try { + for (i in 0 until 1) { + try { + val x = "x" + val y = "y" + } finally { + continue + } + } + } finally { + return "OK" + } + return "FAIL" +} + +// LOCAL VARIABLES +// test.kt:4 box: +// test.kt:5 box: +// test.kt:6 box: i:int=0:int +// test.kt:7 box: i:int=0:int +// test.kt:8 box: i:int=0:int, x:java.lang.String="x":java.lang.String +// test.kt:10 box: i:int=0:int +// test.kt:5 box: i:int=0:int +// test.kt:14 box: diff --git a/compiler/testData/debug/localVariables/tryFinally16.kt b/compiler/testData/debug/localVariables/tryFinally16.kt new file mode 100644 index 00000000000..901b44b8765 --- /dev/null +++ b/compiler/testData/debug/localVariables/tryFinally16.kt @@ -0,0 +1,29 @@ +// FILE: test.kt + +fun box(): String { + try { + for (i in 0 until 1) { + try { + val x = "x" + val y = "y" + } finally { + return "FAIL1" + } + } + } finally { + return "OK" + } + return "FAIL2" +} + +// The local `i` is visible in the finally block with old backend. +// IGNORE_BACKEND: JVM + +// LOCAL VARIABLES +// test.kt:4 box: +// test.kt:5 box: +// test.kt:6 box: i:int=0:int +// test.kt:7 box: i:int=0:int +// test.kt:8 box: i:int=0:int, x:java.lang.String="x":java.lang.String +// test.kt:10 box: i:int=0:int +// test.kt:14 box: diff --git a/compiler/testData/debug/localVariables/tryFinally7.kt b/compiler/testData/debug/localVariables/tryFinally7.kt index b887318c03c..d69320960e1 100644 --- a/compiler/testData/debug/localVariables/tryFinally7.kt +++ b/compiler/testData/debug/localVariables/tryFinally7.kt @@ -20,7 +20,7 @@ fun compute(): String { } } } finally { - x = "OK" + x = "OK" // TODO: `e` should not be visible here. } return "FAIL" } diff --git a/compiler/tests-gen/org/jetbrains/kotlin/codegen/debugInformation/IrLocalVariableTestGenerated.java b/compiler/tests-gen/org/jetbrains/kotlin/codegen/debugInformation/IrLocalVariableTestGenerated.java index f4e9de550c8..39e75f8733d 100644 --- a/compiler/tests-gen/org/jetbrains/kotlin/codegen/debugInformation/IrLocalVariableTestGenerated.java +++ b/compiler/tests-gen/org/jetbrains/kotlin/codegen/debugInformation/IrLocalVariableTestGenerated.java @@ -98,6 +98,42 @@ public class IrLocalVariableTestGenerated extends AbstractIrLocalVariableTest { runTest("compiler/testData/debug/localVariables/tryFinally10.kt"); } + @Test + @TestMetadata("tryFinally11.kt") + public void testTryFinally11() throws Exception { + runTest("compiler/testData/debug/localVariables/tryFinally11.kt"); + } + + @Test + @TestMetadata("tryFinally12.kt") + public void testTryFinally12() throws Exception { + runTest("compiler/testData/debug/localVariables/tryFinally12.kt"); + } + + @Test + @TestMetadata("tryFinally13.kt") + public void testTryFinally13() throws Exception { + runTest("compiler/testData/debug/localVariables/tryFinally13.kt"); + } + + @Test + @TestMetadata("tryFinally14.kt") + public void testTryFinally14() throws Exception { + runTest("compiler/testData/debug/localVariables/tryFinally14.kt"); + } + + @Test + @TestMetadata("tryFinally15.kt") + public void testTryFinally15() throws Exception { + runTest("compiler/testData/debug/localVariables/tryFinally15.kt"); + } + + @Test + @TestMetadata("tryFinally16.kt") + public void testTryFinally16() throws Exception { + runTest("compiler/testData/debug/localVariables/tryFinally16.kt"); + } + @Test @TestMetadata("tryFinally2.kt") public void testTryFinally2() throws Exception { diff --git a/compiler/tests-gen/org/jetbrains/kotlin/codegen/debugInformation/LocalVariableTestGenerated.java b/compiler/tests-gen/org/jetbrains/kotlin/codegen/debugInformation/LocalVariableTestGenerated.java index 2e2010a460b..c8214976f78 100644 --- a/compiler/tests-gen/org/jetbrains/kotlin/codegen/debugInformation/LocalVariableTestGenerated.java +++ b/compiler/tests-gen/org/jetbrains/kotlin/codegen/debugInformation/LocalVariableTestGenerated.java @@ -98,6 +98,42 @@ public class LocalVariableTestGenerated extends AbstractLocalVariableTest { runTest("compiler/testData/debug/localVariables/tryFinally10.kt"); } + @Test + @TestMetadata("tryFinally11.kt") + public void testTryFinally11() throws Exception { + runTest("compiler/testData/debug/localVariables/tryFinally11.kt"); + } + + @Test + @TestMetadata("tryFinally12.kt") + public void testTryFinally12() throws Exception { + runTest("compiler/testData/debug/localVariables/tryFinally12.kt"); + } + + @Test + @TestMetadata("tryFinally13.kt") + public void testTryFinally13() throws Exception { + runTest("compiler/testData/debug/localVariables/tryFinally13.kt"); + } + + @Test + @TestMetadata("tryFinally14.kt") + public void testTryFinally14() throws Exception { + runTest("compiler/testData/debug/localVariables/tryFinally14.kt"); + } + + @Test + @TestMetadata("tryFinally15.kt") + public void testTryFinally15() throws Exception { + runTest("compiler/testData/debug/localVariables/tryFinally15.kt"); + } + + @Test + @TestMetadata("tryFinally16.kt") + public void testTryFinally16() throws Exception { + runTest("compiler/testData/debug/localVariables/tryFinally16.kt"); + } + @Test @TestMetadata("tryFinally2.kt") public void testTryFinally2() throws Exception {