diff --git a/compiler/frontend/src/org/jetbrains/kotlin/cfg/PseudocodeVariablesData.kt b/compiler/frontend/src/org/jetbrains/kotlin/cfg/PseudocodeVariablesData.kt index ead8c09633a..7acc068c07a 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/cfg/PseudocodeVariablesData.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/cfg/PseudocodeVariablesData.kt @@ -38,6 +38,7 @@ private typealias ImmutableSet = javaslang.collection.Set private typealias ImmutableHashSet = javaslang.collection.HashSet class PseudocodeVariablesData(val pseudocode: Pseudocode, private val bindingContext: BindingContext) { + private val containsDoWhile = pseudocode.rootPseudocode.containsDoWhile private val pseudocodeVariableDataCollector = PseudocodeVariableDataCollector(bindingContext, pseudocode) private class VariablesForDeclaration( @@ -107,7 +108,7 @@ class PseudocodeVariablesData(val pseudocode: Pseudocode, private val bindingCon bindingContext.get(BindingContext.DECLARATION_TO_DESCRIPTOR, variableDeclarationElement) ) ?: continue - if (isValWithTrivialInitializer(variableDeclarationElement, descriptor)) { + if (!containsDoWhile && isValWithTrivialInitializer(variableDeclarationElement, descriptor)) { valsWithTrivialInitializer.add(descriptor) } else { diff --git a/compiler/frontend/src/org/jetbrains/kotlin/cfg/pseudocode/ControlFlowInstructionsGenerator.kt b/compiler/frontend/src/org/jetbrains/kotlin/cfg/pseudocode/ControlFlowInstructionsGenerator.kt index e86c81cc3a8..487edc5d865 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/cfg/pseudocode/ControlFlowInstructionsGenerator.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/cfg/pseudocode/ControlFlowInstructionsGenerator.kt @@ -19,8 +19,8 @@ package org.jetbrains.kotlin.cfg.pseudocode import com.intellij.util.containers.Stack import org.jetbrains.kotlin.builtins.KotlinBuiltIns import org.jetbrains.kotlin.cfg.* -import org.jetbrains.kotlin.cfg.pseudocode.instructions.Instruction import org.jetbrains.kotlin.cfg.pseudocode.instructions.BlockScope +import org.jetbrains.kotlin.cfg.pseudocode.instructions.Instruction import org.jetbrains.kotlin.cfg.pseudocode.instructions.eval.* import org.jetbrains.kotlin.cfg.pseudocode.instructions.jumps.* import org.jetbrains.kotlin.cfg.pseudocode.instructions.special.* @@ -29,7 +29,6 @@ import org.jetbrains.kotlin.psi.* import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall import org.jetbrains.kotlin.resolve.constants.CompileTimeConstant import org.jetbrains.kotlin.resolve.scopes.receivers.ReceiverValue - import java.util.* class ControlFlowInstructionsGenerator : ControlFlowBuilderAdapter() { @@ -113,6 +112,10 @@ class ControlFlowInstructionsGenerator : ControlFlowBuilderAdapter() { override fun createUnboundLabel(name: String): Label = pseudocode.createLabel("L" + labelCount++, name) override fun enterLoop(expression: KtLoopExpression): LoopInfo { + if (expression is KtDoWhileExpression) { + (pseudocode.rootPseudocode as PseudocodeImpl).containsDoWhile = true + } + val info = LoopInfo( expression, createUnboundLabel("loop entry point"), diff --git a/compiler/frontend/src/org/jetbrains/kotlin/cfg/pseudocode/Pseudocode.kt b/compiler/frontend/src/org/jetbrains/kotlin/cfg/pseudocode/Pseudocode.kt index b387e2360d6..7ab4903baf6 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/cfg/pseudocode/Pseudocode.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/cfg/pseudocode/Pseudocode.kt @@ -45,6 +45,9 @@ interface Pseudocode { val enterInstruction: SubroutineEnterInstruction + val containsDoWhile: Boolean + val rootPseudocode: Pseudocode + fun getElementValue(element: KtElement?): PseudoValue? fun getValueElements(value: PseudoValue?): List diff --git a/compiler/frontend/src/org/jetbrains/kotlin/cfg/pseudocode/PseudocodeImpl.kt b/compiler/frontend/src/org/jetbrains/kotlin/cfg/pseudocode/PseudocodeImpl.kt index f3b53f2fde1..cb7972476cf 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/cfg/pseudocode/PseudocodeImpl.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/cfg/pseudocode/PseudocodeImpl.kt @@ -77,6 +77,9 @@ class PseudocodeImpl(override val correspondingElement: KtElement) : Pseudocode private var postPrecessed = false + override var containsDoWhile: Boolean = false + internal set + private fun getLocalDeclarations(pseudocode: Pseudocode): Set { val localDeclarations = linkedSetOf() for (instruction in (pseudocode as PseudocodeImpl).mutableInstructionList) { @@ -88,7 +91,7 @@ class PseudocodeImpl(override val correspondingElement: KtElement) : Pseudocode return localDeclarations } - val rootPseudocode: Pseudocode + override val rootPseudocode: Pseudocode get() { var parent = parent while (parent != null) { diff --git a/compiler/testData/diagnostics/tests/controlFlowAnalysis/doWhileNotDefined.kt b/compiler/testData/diagnostics/tests/controlFlowAnalysis/doWhileNotDefined.kt index 92c4f436540..e41bcd1a76e 100644 --- a/compiler/testData/diagnostics/tests/controlFlowAnalysis/doWhileNotDefined.kt +++ b/compiler/testData/diagnostics/tests/controlFlowAnalysis/doWhileNotDefined.kt @@ -2,5 +2,5 @@ fun test(cond1: Boolean) { do { if (cond1) continue val cond2 = false - } while (cond2) // cond2 may be not defined here + } while (cond2) // cond2 may be not defined here }