From 8ccf56415a33dcc9239c7065a954933ee504f55d Mon Sep 17 00:00:00 2001 From: Valentin Kipyatkov Date: Fri, 21 Nov 2014 12:47:01 +0300 Subject: [PATCH] Partial body resolve filter: fixed nested loops case --- .../resolve/lazy/PartialBodyResolveFilter.kt | 14 +++++++------- .../resolve/partialBodyResolve/NestedLoop.dump | 12 ++++++++++++ .../resolve/partialBodyResolve/NestedLoop.kt | 17 +++++++++++++++++ .../PartialBodyResolveTestGenerated.java | 12 ++++++------ 4 files changed, 42 insertions(+), 13 deletions(-) create mode 100644 idea/testData/resolve/partialBodyResolve/NestedLoop.dump create mode 100644 idea/testData/resolve/partialBodyResolve/NestedLoop.kt diff --git a/idea/ide-common/src/org/jetbrains/jet/lang/resolve/lazy/PartialBodyResolveFilter.kt b/idea/ide-common/src/org/jetbrains/jet/lang/resolve/lazy/PartialBodyResolveFilter.kt index cfef78cccfc..8bf9ca15841 100644 --- a/idea/ide-common/src/org/jetbrains/jet/lang/resolve/lazy/PartialBodyResolveFilter.kt +++ b/idea/ide-common/src/org/jetbrains/jet/lang/resolve/lazy/PartialBodyResolveFilter.kt @@ -268,7 +268,7 @@ class PartialBodyResolveFilter( private fun collectAlwaysExitPoints(statement: JetExpression?): Collection { val result = ArrayList() statement?.accept(object : ControlFlowVisitor() { - var insideLoop = false + var insideLoopLevel: Int = 0 override fun visitReturnExpression(expression: JetReturnExpression) { result.add(expression) @@ -303,9 +303,9 @@ class PartialBodyResolveFilter( override fun visitWhileExpression(loop: JetWhileExpression) { val condition = loop.getCondition() if (condition.isTrueConstant()) { - insideLoop = true + insideLoopLevel++ loop.getBody()?.accept(this) - insideLoop = false + insideLoopLevel-- } else { // do not make sense to search exits inside while-loop as not necessary enter it at all @@ -315,19 +315,19 @@ class PartialBodyResolveFilter( override fun visitDoWhileExpression(loop: JetDoWhileExpression) { loop.getCondition()?.accept(this) - insideLoop = true + insideLoopLevel++ loop.getBody()?.accept(this) - insideLoop = false + insideLoopLevel-- } override fun visitBreakExpression(expression: JetBreakExpression) { - if (!insideLoop || expression.getLabelName() != null) { + if (insideLoopLevel == 0 || expression.getLabelName() != null) { result.add(expression) } } override fun visitContinueExpression(expression: JetContinueExpression) { - if (!insideLoop || expression.getLabelName() != null) { + if (insideLoopLevel == 0 || expression.getLabelName() != null) { result.add(expression) } } diff --git a/idea/testData/resolve/partialBodyResolve/NestedLoop.dump b/idea/testData/resolve/partialBodyResolve/NestedLoop.dump new file mode 100644 index 00000000000..3238ce9be7e --- /dev/null +++ b/idea/testData/resolve/partialBodyResolve/NestedLoop.dump @@ -0,0 +1,12 @@ +Resolve target: val x: kotlin.Any? +---------------------------------------------- +fun foo() { + for (i in 1..10) { + val x = take() + // STATEMENT DELETED: if (x == null) { while (true) { do { println() } while(f()) break } } + x.hashCode() + } +} + +fun take(): Any? = null +fun f(): Boolean{} diff --git a/idea/testData/resolve/partialBodyResolve/NestedLoop.kt b/idea/testData/resolve/partialBodyResolve/NestedLoop.kt new file mode 100644 index 00000000000..0f4a1cd9b15 --- /dev/null +++ b/idea/testData/resolve/partialBodyResolve/NestedLoop.kt @@ -0,0 +1,17 @@ +fun foo() { + for (i in 1..10) { + val x = take() + if (x == null) { + while (true) { + do { + println() + } while(f()) + break + } + } + x.hashCode() + } +} + +fun take(): Any? = null +fun f(): Boolean{} diff --git a/idea/tests/org/jetbrains/jet/resolve/PartialBodyResolveTestGenerated.java b/idea/tests/org/jetbrains/jet/resolve/PartialBodyResolveTestGenerated.java index 136d31a621f..138b85f116c 100644 --- a/idea/tests/org/jetbrains/jet/resolve/PartialBodyResolveTestGenerated.java +++ b/idea/tests/org/jetbrains/jet/resolve/PartialBodyResolveTestGenerated.java @@ -72,12 +72,6 @@ public class PartialBodyResolveTestGenerated extends AbstractPartialBodyResolveT doTest(fileName); } - @TestMetadata("ExpressionBody.kt") - public void testExpressionBody() throws Exception { - String fileName = JetTestUtils.navigationMetadata("idea/testData/resolve/partialBodyResolve/ExpressionBody.kt"); - doTest(fileName); - } - @TestMetadata("ExpressionBodyExplicitType.kt") public void testExpressionBodyExplicitType() throws Exception { String fileName = JetTestUtils.navigationMetadata("idea/testData/resolve/partialBodyResolve/ExpressionBodyExplicitType.kt"); @@ -306,6 +300,12 @@ public class PartialBodyResolveTestGenerated extends AbstractPartialBodyResolveT doTest(fileName); } + @TestMetadata("NestedLoop.kt") + public void testNestedLoop() throws Exception { + String fileName = JetTestUtils.navigationMetadata("idea/testData/resolve/partialBodyResolve/NestedLoop.kt"); + doTest(fileName); + } + @TestMetadata("OutOfBodyResolve.kt") public void testOutOfBodyResolve() throws Exception { String fileName = JetTestUtils.navigationMetadata("idea/testData/resolve/partialBodyResolve/OutOfBodyResolve.kt");