diff --git a/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/ir/Fir2IrTextTestGenerated.java b/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/ir/Fir2IrTextTestGenerated.java index 901bf858235..45d5d39ee04 100644 --- a/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/ir/Fir2IrTextTestGenerated.java +++ b/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/ir/Fir2IrTextTestGenerated.java @@ -1454,6 +1454,12 @@ public class Fir2IrTextTestGenerated extends AbstractFir2IrTextTest { runTest("compiler/testData/ir/irText/expressions/kt47450.kt"); } + @Test + @TestMetadata("kt48708.kt") + public void testKt48708() throws Exception { + runTest("compiler/testData/ir/irText/expressions/kt48708.kt"); + } + @Test @TestMetadata("lambdaInCAO.kt") public void testLambdaInCAO() throws Exception { diff --git a/compiler/frontend/cfg/src/org/jetbrains/kotlin/cfg/ControlFlowInformationProviderImpl.kt b/compiler/frontend/cfg/src/org/jetbrains/kotlin/cfg/ControlFlowInformationProviderImpl.kt index a4830fce4fd..83714186531 100644 --- a/compiler/frontend/cfg/src/org/jetbrains/kotlin/cfg/ControlFlowInformationProviderImpl.kt +++ b/compiler/frontend/cfg/src/org/jetbrains/kotlin/cfg/ControlFlowInformationProviderImpl.kt @@ -797,15 +797,17 @@ class ControlFlowInformationProviderImpl private constructor( //////////////////////////////////////////////////////////////////////////////// // Statements - private fun markStatements() = pseudocode.traverse(TraversalOrder.FORWARD) { instruction -> - val value = (instruction as? InstructionWithValue)?.outputValue - val pseudocode = instruction.owner - val usages = pseudocode.getUsages(value) - val isUsedAsExpression = usages.isNotEmpty() - val isUsedAsResultOfLambda = isUsedAsResultOfLambda(usages) - for (element in pseudocode.getValueElements(value)) { - trace.record(USED_AS_EXPRESSION, element, isUsedAsExpression) - trace.record(USED_AS_RESULT_OF_LAMBDA, element, isUsedAsResultOfLambda) + private fun markStatements() { + pseudocode.traverseIncludingDeadCode { instruction -> + val value = (instruction as? InstructionWithValue)?.outputValue + val pseudocode = instruction.owner + val usages = pseudocode.getUsages(value) + val isUsedAsExpression = usages.isNotEmpty() + val isUsedAsResultOfLambda = isUsedAsResultOfLambda(usages) + for (element in pseudocode.getValueElements(value)) { + trace.record(USED_AS_EXPRESSION, element, isUsedAsExpression) + trace.record(USED_AS_RESULT_OF_LAMBDA, element, isUsedAsResultOfLambda) + } } } diff --git a/compiler/frontend/cfg/src/org/jetbrains/kotlin/cfg/PseudocodeTraverser.kt b/compiler/frontend/cfg/src/org/jetbrains/kotlin/cfg/PseudocodeTraverser.kt index d2be8fb0102..01938b90412 100644 --- a/compiler/frontend/cfg/src/org/jetbrains/kotlin/cfg/PseudocodeTraverser.kt +++ b/compiler/frontend/cfg/src/org/jetbrains/kotlin/cfg/PseudocodeTraverser.kt @@ -54,6 +54,15 @@ fun Pseudocode.traverse( } } +fun Pseudocode.traverseIncludingDeadCode(analyzeInstruction: (Instruction) -> Unit) { + for (instruction in instructionsIncludingDeadCode) { + if (instruction is LocalFunctionDeclarationInstruction) { + instruction.body.traverseIncludingDeadCode(analyzeInstruction) + } + analyzeInstruction(instruction) + } +} + fun > Pseudocode.collectData( traversalOrder: TraversalOrder, mergeEdges: (Instruction, Collection) -> Edges, diff --git a/compiler/testData/ir/irText/expressions/kt48708.fir.kt.txt b/compiler/testData/ir/irText/expressions/kt48708.fir.kt.txt new file mode 100644 index 00000000000..d90d76d152a --- /dev/null +++ b/compiler/testData/ir/irText/expressions/kt48708.fir.kt.txt @@ -0,0 +1,13 @@ +fun test(b: Boolean) { + val x: Int = when { + b -> 3 + else -> { // BLOCK + throw Exception() + 0 + } + } + takeInt(x = x) +} + +fun takeInt(x: Int) { +} diff --git a/compiler/testData/ir/irText/expressions/kt48708.fir.txt b/compiler/testData/ir/irText/expressions/kt48708.fir.txt new file mode 100644 index 00000000000..d743bf64dfa --- /dev/null +++ b/compiler/testData/ir/irText/expressions/kt48708.fir.txt @@ -0,0 +1,20 @@ +FILE fqName: fileName:/kt48708.kt + FUN name:test visibility:public modality:FINAL <> (b:kotlin.Boolean) returnType:kotlin.Unit + VALUE_PARAMETER name:b index:0 type:kotlin.Boolean + BLOCK_BODY + VAR name:x type:kotlin.Int [val] + WHEN type=kotlin.Int origin=IF + BRANCH + if: GET_VAR 'b: kotlin.Boolean declared in .test' type=kotlin.Boolean origin=null + then: CONST Int type=kotlin.Int value=3 + BRANCH + if: CONST Boolean type=kotlin.Boolean value=true + then: BLOCK type=kotlin.Int origin=null + THROW type=kotlin.Nothing + CONSTRUCTOR_CALL 'public constructor () declared in java.lang.Exception' type=java.lang.Exception origin=null + CONST Int type=kotlin.Int value=0 + CALL 'public final fun takeInt (x: kotlin.Int): kotlin.Unit declared in ' type=kotlin.Unit origin=null + x: GET_VAR 'val x: kotlin.Int [val] declared in .test' type=kotlin.Int origin=null + FUN name:takeInt visibility:public modality:FINAL <> (x:kotlin.Int) returnType:kotlin.Unit + VALUE_PARAMETER name:x index:0 type:kotlin.Int + BLOCK_BODY diff --git a/compiler/testData/ir/irText/expressions/kt48708.kt b/compiler/testData/ir/irText/expressions/kt48708.kt new file mode 100644 index 00000000000..5bc3d1b313e --- /dev/null +++ b/compiler/testData/ir/irText/expressions/kt48708.kt @@ -0,0 +1,13 @@ +// ISSUE: KT-48708 + +fun test(b: Boolean) { + val x = if (b) { + 3 + } else { + throw Exception() + 0 + } + takeInt(x) +} + +fun takeInt(x: Int) {} diff --git a/compiler/testData/ir/irText/expressions/kt48708.kt.txt b/compiler/testData/ir/irText/expressions/kt48708.kt.txt new file mode 100644 index 00000000000..0a05a243d5d --- /dev/null +++ b/compiler/testData/ir/irText/expressions/kt48708.kt.txt @@ -0,0 +1,15 @@ +fun test(b: Boolean) { + val x: Int = when { + b -> { // BLOCK + 3 + } + else -> { // BLOCK + throw Exception() + 0 + } + } + takeInt(x = x) +} + +fun takeInt(x: Int) { +} diff --git a/compiler/testData/ir/irText/expressions/kt48708.txt b/compiler/testData/ir/irText/expressions/kt48708.txt new file mode 100644 index 00000000000..85a887c41a6 --- /dev/null +++ b/compiler/testData/ir/irText/expressions/kt48708.txt @@ -0,0 +1,21 @@ +FILE fqName: fileName:/kt48708.kt + FUN name:test visibility:public modality:FINAL <> (b:kotlin.Boolean) returnType:kotlin.Unit + VALUE_PARAMETER name:b index:0 type:kotlin.Boolean + BLOCK_BODY + VAR name:x type:kotlin.Int [val] + WHEN type=kotlin.Int origin=IF + BRANCH + if: GET_VAR 'b: kotlin.Boolean declared in .test' type=kotlin.Boolean origin=null + then: BLOCK type=kotlin.Int origin=null + CONST Int type=kotlin.Int value=3 + BRANCH + if: CONST Boolean type=kotlin.Boolean value=true + then: BLOCK type=kotlin.Int origin=null + THROW type=kotlin.Nothing + CONSTRUCTOR_CALL 'public constructor () declared in java.lang.Exception' type=java.lang.Exception origin=null + CONST Int type=kotlin.Int value=0 + CALL 'public final fun takeInt (x: kotlin.Int): kotlin.Unit declared in ' type=kotlin.Unit origin=null + x: GET_VAR 'val x: kotlin.Int [val] declared in .test' type=kotlin.Int origin=null + FUN name:takeInt visibility:public modality:FINAL <> (x:kotlin.Int) returnType:kotlin.Unit + VALUE_PARAMETER name:x index:0 type:kotlin.Int + BLOCK_BODY diff --git a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/ir/IrTextTestGenerated.java b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/ir/IrTextTestGenerated.java index 661f0de3980..418ba624ec3 100644 --- a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/ir/IrTextTestGenerated.java +++ b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/ir/IrTextTestGenerated.java @@ -1454,6 +1454,12 @@ public class IrTextTestGenerated extends AbstractIrTextTest { runTest("compiler/testData/ir/irText/expressions/kt47450.kt"); } + @Test + @TestMetadata("kt48708.kt") + public void testKt48708() throws Exception { + runTest("compiler/testData/ir/irText/expressions/kt48708.kt"); + } + @Test @TestMetadata("lambdaInCAO.kt") public void testLambdaInCAO() throws Exception { diff --git a/compiler/tests-gen/org/jetbrains/kotlin/ir/KlibTextTestCaseGenerated.java b/compiler/tests-gen/org/jetbrains/kotlin/ir/KlibTextTestCaseGenerated.java index 79c6fc43e0e..8c274e48c83 100644 --- a/compiler/tests-gen/org/jetbrains/kotlin/ir/KlibTextTestCaseGenerated.java +++ b/compiler/tests-gen/org/jetbrains/kotlin/ir/KlibTextTestCaseGenerated.java @@ -1128,6 +1128,11 @@ public class KlibTextTestCaseGenerated extends AbstractKlibTextTestCase { runTest("compiler/testData/ir/irText/expressions/kt47450.kt"); } + @TestMetadata("kt48708.kt") + public void testKt48708() throws Exception { + runTest("compiler/testData/ir/irText/expressions/kt48708.kt"); + } + @TestMetadata("lambdaInCAO.kt") public void testLambdaInCAO() throws Exception { runTest("compiler/testData/ir/irText/expressions/lambdaInCAO.kt");