From 55bbcc46014cee75290bfd45f51b554fd9a0aaf0 Mon Sep 17 00:00:00 2001 From: Yan Zhulanow Date: Tue, 19 Mar 2019 21:46:15 +0300 Subject: [PATCH] Generate line numbers for closing '}' in 'init {}' blocks (KT-12787) --- .../kotlin/codegen/MemberCodegen.java | 19 ++++++++++- .../testData/lineNumber/custom/initBlocks.kt | 34 +++++++++++++++++++ .../codegen/LineNumberTestGenerated.java | 5 +++ .../codegen/ir/IrLineNumberTestGenerated.java | 5 +++ .../tinyApp/src/stepping/custom/initBlocks.kt | 18 ++++++++++ .../src/stepping/custom/initBlocks.out | 12 +++++++ .../filters/doNotSkipConstructors.out | 2 +- .../debugger/KotlinSteppingTestGenerated.java | 5 +++ 8 files changed, 98 insertions(+), 2 deletions(-) create mode 100644 compiler/testData/lineNumber/custom/initBlocks.kt create mode 100644 idea/testData/debugger/tinyApp/src/stepping/custom/initBlocks.kt create mode 100644 idea/testData/debugger/tinyApp/src/stepping/custom/initBlocks.out diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/MemberCodegen.java b/compiler/backend/src/org/jetbrains/kotlin/codegen/MemberCodegen.java index 301c8c0e995..2703ca7577d 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/MemberCodegen.java +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/MemberCodegen.java @@ -472,21 +472,38 @@ public abstract class MemberCodegen codegen) { + if (nopSeparatorNeeded) { + nopSeparatorNeeded = false; + codegen.invoke().v.nop(); + } + } + protected void generateInitializers(@NotNull Function0 createCodegen) { NotNullLazyValue codegen = LockBasedStorageManager.NO_LOCKS.createLazyValue(createCodegen); + for (KtDeclaration declaration : ((KtDeclarationContainer) element).getDeclarations()) { if (declaration instanceof KtProperty) { if (shouldInitializeProperty((KtProperty) declaration)) { + generateNopSeparatorIfNeeded(codegen); initializeProperty(codegen.invoke(), (KtProperty) declaration); } } else if (declaration instanceof KtDestructuringDeclaration) { + generateNopSeparatorIfNeeded(codegen); codegen.invoke().initializeDestructuringDeclaration((KtDestructuringDeclaration) declaration, true); } else if (declaration instanceof KtAnonymousInitializer) { KtExpression body = ((KtAnonymousInitializer) declaration).getBody(); if (body != null) { - codegen.invoke().gen(body, Type.VOID_TYPE); + generateNopSeparatorIfNeeded(codegen); + + ExpressionCodegen expressionCodegen = codegen.invoke(); + expressionCodegen.gen(body, Type.VOID_TYPE); + expressionCodegen.markLineNumber(declaration, true); + nopSeparatorNeeded = true; } } } diff --git a/compiler/testData/lineNumber/custom/initBlocks.kt b/compiler/testData/lineNumber/custom/initBlocks.kt new file mode 100644 index 00000000000..35c10efe2d1 --- /dev/null +++ b/compiler/testData/lineNumber/custom/initBlocks.kt @@ -0,0 +1,34 @@ +class Foo { + var a: String + + init { + a = x() + } +} + +class Bar { + init { + val a = 5 + } + + init { + val b = 2 + } +} + +class Boo { + init { + val a = 5 + } + + val x = x() + + init { + val b = 2 + } +} + +fun x() = "" + +// IGNORE_BACKEND: JVM_IR +// 2 2 1 5 6 9 11 12 15 16 24 19 21 22 24 27 28 31 \ No newline at end of file diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/LineNumberTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/LineNumberTestGenerated.java index 0dee935ce00..57321a479bd 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/LineNumberTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/LineNumberTestGenerated.java @@ -191,6 +191,11 @@ public class LineNumberTestGenerated extends AbstractLineNumberTest { runTest("compiler/testData/lineNumber/custom/inTheEndOfLambdaArgumentOfInlineCall.kt"); } + @TestMetadata("initBlocks.kt") + public void testInitBlocks() throws Exception { + runTest("compiler/testData/lineNumber/custom/initBlocks.kt"); + } + @TestMetadata("multilineFunctionCall.kt") public void testMultilineFunctionCall() throws Exception { runTest("compiler/testData/lineNumber/custom/multilineFunctionCall.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrLineNumberTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrLineNumberTestGenerated.java index 0b345a29741..559b1990b24 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrLineNumberTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrLineNumberTestGenerated.java @@ -191,6 +191,11 @@ public class IrLineNumberTestGenerated extends AbstractIrLineNumberTest { runTest("compiler/testData/lineNumber/custom/inTheEndOfLambdaArgumentOfInlineCall.kt"); } + @TestMetadata("initBlocks.kt") + public void testInitBlocks() throws Exception { + runTest("compiler/testData/lineNumber/custom/initBlocks.kt"); + } + @TestMetadata("multilineFunctionCall.kt") public void testMultilineFunctionCall() throws Exception { runTest("compiler/testData/lineNumber/custom/multilineFunctionCall.kt"); diff --git a/idea/testData/debugger/tinyApp/src/stepping/custom/initBlocks.kt b/idea/testData/debugger/tinyApp/src/stepping/custom/initBlocks.kt new file mode 100644 index 00000000000..f7f56e16011 --- /dev/null +++ b/idea/testData/debugger/tinyApp/src/stepping/custom/initBlocks.kt @@ -0,0 +1,18 @@ +package initBlocks + +class Foo { + init { + //Breakpoint! + val a = 5 + } + + init { + val b = 7 + } +} + +fun main() { + Foo() +} + +// STEP_OVER: 5 \ No newline at end of file diff --git a/idea/testData/debugger/tinyApp/src/stepping/custom/initBlocks.out b/idea/testData/debugger/tinyApp/src/stepping/custom/initBlocks.out new file mode 100644 index 00000000000..f168dabb684 --- /dev/null +++ b/idea/testData/debugger/tinyApp/src/stepping/custom/initBlocks.out @@ -0,0 +1,12 @@ +LineBreakpoint created at initBlocks.kt:6 +Run Java +Connected to the target VM +initBlocks.kt:6 +initBlocks.kt:7 +initBlocks.kt:10 +initBlocks.kt:11 +initBlocks.kt:15 +initBlocks.kt:16 +Disconnected from the target VM + +Process finished with exit code 0 diff --git a/idea/testData/debugger/tinyApp/src/stepping/filters/doNotSkipConstructors.out b/idea/testData/debugger/tinyApp/src/stepping/filters/doNotSkipConstructors.out index ba3c2acd517..6d006f7baf8 100644 --- a/idea/testData/debugger/tinyApp/src/stepping/filters/doNotSkipConstructors.out +++ b/idea/testData/debugger/tinyApp/src/stepping/filters/doNotSkipConstructors.out @@ -4,7 +4,7 @@ Connected to the target VM doNotSkipConstructors.kt:5 doNotSkipConstructors.kt:9 doNotSkipConstructors.kt:11 -doNotSkipConstructors.kt:5 +doNotSkipConstructors.kt:12 Disconnected from the target VM Process finished with exit code 0 diff --git a/idea/tests/org/jetbrains/kotlin/idea/debugger/KotlinSteppingTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/debugger/KotlinSteppingTestGenerated.java index 94a98734fe2..a059d66a456 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/debugger/KotlinSteppingTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/debugger/KotlinSteppingTestGenerated.java @@ -1077,6 +1077,11 @@ public class KotlinSteppingTestGenerated extends AbstractKotlinSteppingTest { runTest("idea/testData/debugger/tinyApp/src/stepping/custom/fwPropertyInInterface.kt"); } + @TestMetadata("initBlocks.kt") + public void testInitBlocks() throws Exception { + runTest("idea/testData/debugger/tinyApp/src/stepping/custom/initBlocks.kt"); + } + @TestMetadata("inlineInObject.kt") public void testInlineInObject() throws Exception { runTest("idea/testData/debugger/tinyApp/src/stepping/custom/inlineInObject.kt");