From bf077b893cb446b6a569ea971cf146d3f41346d0 Mon Sep 17 00:00:00 2001 From: Dmitry Petrov Date: Fri, 11 Jun 2021 13:24:35 +0300 Subject: [PATCH] PSI2IR KT-47245 handle declaration statements in control structures --- .../FirBlackBoxCodegenTestGenerated.java | 6 ++++++ .../runners/ir/Fir2IrTextTestGenerated.java | 6 ++++++ .../psi2ir/generators/StatementGenerator.kt | 18 ++++++++++++++++-- .../codegen/box/controlStructures/kt47245.kt | 6 ++++++ .../ir/irText/expressions/kt47245.fir.txt | 19 +++++++++++++++++++ .../testData/ir/irText/expressions/kt47245.kt | 5 +++++ .../ir/irText/expressions/kt47245.txt | 19 +++++++++++++++++++ .../codegen/BlackBoxCodegenTestGenerated.java | 6 ++++++ .../IrBlackBoxCodegenTestGenerated.java | 6 ++++++ .../test/runners/ir/IrTextTestGenerated.java | 6 ++++++ .../LightAnalysisModeTestGenerated.java | 5 +++++ .../IrJsCodegenBoxES6TestGenerated.java | 5 +++++ .../IrJsCodegenBoxTestGenerated.java | 5 +++++ .../semantics/JsCodegenBoxTestGenerated.java | 5 +++++ .../IrCodegenBoxWasmTestGenerated.java | 5 +++++ 15 files changed, 120 insertions(+), 2 deletions(-) create mode 100644 compiler/testData/codegen/box/controlStructures/kt47245.kt create mode 100644 compiler/testData/ir/irText/expressions/kt47245.fir.txt create mode 100644 compiler/testData/ir/irText/expressions/kt47245.kt create mode 100644 compiler/testData/ir/irText/expressions/kt47245.txt diff --git a/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirBlackBoxCodegenTestGenerated.java b/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirBlackBoxCodegenTestGenerated.java index 8faf2c7eca4..d7da89e1b75 100644 --- a/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirBlackBoxCodegenTestGenerated.java +++ b/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirBlackBoxCodegenTestGenerated.java @@ -7944,6 +7944,12 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT runTest("compiler/testData/codegen/box/controlStructures/kt42455.kt"); } + @Test + @TestMetadata("kt47245.kt") + public void testKt47245() throws Exception { + runTest("compiler/testData/codegen/box/controlStructures/kt47245.kt"); + } + @Test @TestMetadata("kt513.kt") public void testKt513() throws Exception { 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 176ee5004b6..f478ef65379 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 @@ -1406,6 +1406,12 @@ public class Fir2IrTextTestGenerated extends AbstractFir2IrTextTest { runTest("compiler/testData/ir/irText/expressions/kt47082.kt"); } + @Test + @TestMetadata("kt47245.kt") + public void testKt47245() throws Exception { + runTest("compiler/testData/ir/irText/expressions/kt47245.kt"); + } + @Test @TestMetadata("lambdaInCAO.kt") public void testLambdaInCAO() throws Exception { diff --git a/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/generators/StatementGenerator.kt b/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/generators/StatementGenerator.kt index dbe3a0d34fe..83e74482e03 100644 --- a/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/generators/StatementGenerator.kt +++ b/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/generators/StatementGenerator.kt @@ -22,13 +22,14 @@ import org.jetbrains.kotlin.descriptors.ClassDescriptor import org.jetbrains.kotlin.descriptors.DeclarationDescriptor import org.jetbrains.kotlin.descriptors.VariableDescriptorWithAccessors import org.jetbrains.kotlin.ir.IrStatement -import org.jetbrains.kotlin.ir.assertCast import org.jetbrains.kotlin.ir.builders.Scope +import org.jetbrains.kotlin.ir.declarations.IrDeclaration import org.jetbrains.kotlin.ir.declarations.IrDeclarationOrigin import org.jetbrains.kotlin.ir.expressions.* import org.jetbrains.kotlin.ir.expressions.impl.* import org.jetbrains.kotlin.ir.symbols.IrSymbol import org.jetbrains.kotlin.ir.util.referenceFunction +import org.jetbrains.kotlin.ir.util.render import org.jetbrains.kotlin.psi.* import org.jetbrains.kotlin.psi.psiUtil.endOffset import org.jetbrains.kotlin.psi.psiUtil.startOffsetSkippingComments @@ -84,7 +85,20 @@ class StatementGenerator( } private fun KtElement.genExpr(): IrExpression = - genStmt().assertCast() + when (val irStatement = genStmt()) { + is IrExpression -> + irStatement + is IrDeclaration -> + IrBlockImpl( + irStatement.startOffset, + irStatement.endOffset, + this@StatementGenerator.context.irBuiltIns.unitType, + null, + listOf(irStatement) + ) + else -> + throw AssertionError("Unexpected statement: ${irStatement.render()}") + } override fun visitExpression(expression: KtExpression, data: Nothing?): IrStatement = IrErrorExpressionImpl( diff --git a/compiler/testData/codegen/box/controlStructures/kt47245.kt b/compiler/testData/codegen/box/controlStructures/kt47245.kt new file mode 100644 index 00000000000..adc95f286b4 --- /dev/null +++ b/compiler/testData/codegen/box/controlStructures/kt47245.kt @@ -0,0 +1,6 @@ +// IGNORE_BACKEND: JVM + +fun box(): String { + for (i in 0..0) fun x() {} + return "OK" +} diff --git a/compiler/testData/ir/irText/expressions/kt47245.fir.txt b/compiler/testData/ir/irText/expressions/kt47245.fir.txt new file mode 100644 index 00000000000..5483d05a1b5 --- /dev/null +++ b/compiler/testData/ir/irText/expressions/kt47245.fir.txt @@ -0,0 +1,19 @@ +FILE fqName: fileName:/kt47245.kt + FUN name:test visibility:public modality:FINAL <> () returnType:kotlin.Unit + BLOCK_BODY + BLOCK type=kotlin.Unit origin=FOR_LOOP + VAR FOR_LOOP_ITERATOR name:tmp_0 type:kotlin.collections.IntIterator [val] + CALL 'public open fun iterator (): kotlin.collections.IntIterator [operator] declared in kotlin.ranges.IntProgression' type=kotlin.collections.IntIterator origin=FOR_LOOP_ITERATOR + $this: CALL 'public final fun rangeTo (other: kotlin.Int): kotlin.ranges.IntRange [operator] declared in kotlin.Int' type=kotlin.ranges.IntRange origin=RANGE + $this: CONST Int type=kotlin.Int value=0 + other: CONST Int type=kotlin.Int value=0 + WHILE label=null origin=FOR_LOOP_INNER_WHILE + condition: CALL 'public abstract fun hasNext (): kotlin.Boolean [operator] declared in kotlin.collections.Iterator' type=kotlin.Boolean origin=FOR_LOOP_HAS_NEXT + $this: GET_VAR 'val tmp_0: kotlin.collections.IntIterator [val] declared in .test' type=kotlin.collections.IntIterator origin=null + body: BLOCK type=kotlin.Unit origin=FOR_LOOP_INNER_WHILE + VAR FOR_LOOP_VARIABLE name:i type:kotlin.Int [val] + CALL 'public final fun next (): kotlin.Int [operator] declared in kotlin.collections.IntIterator' type=kotlin.Int origin=FOR_LOOP_NEXT + $this: GET_VAR 'val tmp_0: kotlin.collections.IntIterator [val] declared in .test' type=kotlin.collections.IntIterator origin=null + BLOCK type=kotlin.Unit origin=null + FUN name:x visibility:local modality:FINAL <> () returnType:kotlin.Unit + BLOCK_BODY diff --git a/compiler/testData/ir/irText/expressions/kt47245.kt b/compiler/testData/ir/irText/expressions/kt47245.kt new file mode 100644 index 00000000000..c6fc980d86a --- /dev/null +++ b/compiler/testData/ir/irText/expressions/kt47245.kt @@ -0,0 +1,5 @@ +// SKIP_KT_DUMP + +fun test() { + for (i in 0..0) fun x() {} +} \ No newline at end of file diff --git a/compiler/testData/ir/irText/expressions/kt47245.txt b/compiler/testData/ir/irText/expressions/kt47245.txt new file mode 100644 index 00000000000..d964b6470c3 --- /dev/null +++ b/compiler/testData/ir/irText/expressions/kt47245.txt @@ -0,0 +1,19 @@ +FILE fqName: fileName:/kt47245.kt + FUN name:test visibility:public modality:FINAL <> () returnType:kotlin.Unit + BLOCK_BODY + BLOCK type=kotlin.Unit origin=FOR_LOOP + VAR FOR_LOOP_ITERATOR name:tmp_0 type:kotlin.collections.IntIterator [val] + CALL 'public open fun iterator (): kotlin.collections.IntIterator [fake_override,operator] declared in kotlin.ranges.IntRange' type=kotlin.collections.IntIterator origin=FOR_LOOP_ITERATOR + $this: CALL 'public final fun rangeTo (other: kotlin.Int): kotlin.ranges.IntRange [operator] declared in kotlin.Int' type=kotlin.ranges.IntRange origin=RANGE + $this: CONST Int type=kotlin.Int value=0 + other: CONST Int type=kotlin.Int value=0 + WHILE label=null origin=FOR_LOOP_INNER_WHILE + condition: CALL 'public abstract fun hasNext (): kotlin.Boolean [fake_override,operator] declared in kotlin.collections.IntIterator' type=kotlin.Boolean origin=FOR_LOOP_HAS_NEXT + $this: GET_VAR 'val tmp_0: kotlin.collections.IntIterator [val] declared in .test' type=kotlin.collections.IntIterator origin=null + body: BLOCK type=kotlin.Unit origin=FOR_LOOP_INNER_WHILE + VAR FOR_LOOP_VARIABLE name:i type:kotlin.Int [val] + CALL 'public final fun next (): kotlin.Int [operator] declared in kotlin.collections.IntIterator' type=kotlin.Int origin=FOR_LOOP_NEXT + $this: GET_VAR 'val tmp_0: kotlin.collections.IntIterator [val] declared in .test' type=kotlin.collections.IntIterator origin=null + BLOCK type=kotlin.Unit origin=null + FUN name:x visibility:local modality:FINAL <> () returnType:kotlin.Unit + BLOCK_BODY diff --git a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/BlackBoxCodegenTestGenerated.java b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/BlackBoxCodegenTestGenerated.java index 6ff73a672c6..003743f77c5 100644 --- a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/BlackBoxCodegenTestGenerated.java +++ b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/BlackBoxCodegenTestGenerated.java @@ -7944,6 +7944,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { runTest("compiler/testData/codegen/box/controlStructures/kt42455.kt"); } + @Test + @TestMetadata("kt47245.kt") + public void testKt47245() throws Exception { + runTest("compiler/testData/codegen/box/controlStructures/kt47245.kt"); + } + @Test @TestMetadata("kt513.kt") public void testKt513() throws Exception { diff --git a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenTestGenerated.java b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenTestGenerated.java index 7a79b9a8c48..f33219c3921 100644 --- a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenTestGenerated.java +++ b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenTestGenerated.java @@ -7944,6 +7944,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes runTest("compiler/testData/codegen/box/controlStructures/kt42455.kt"); } + @Test + @TestMetadata("kt47245.kt") + public void testKt47245() throws Exception { + runTest("compiler/testData/codegen/box/controlStructures/kt47245.kt"); + } + @Test @TestMetadata("kt513.kt") public void testKt513() throws Exception { 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 2ff19e04bd8..283d2d8d053 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 @@ -1406,6 +1406,12 @@ public class IrTextTestGenerated extends AbstractIrTextTest { runTest("compiler/testData/ir/irText/expressions/kt47082.kt"); } + @Test + @TestMetadata("kt47245.kt") + public void testKt47245() throws Exception { + runTest("compiler/testData/ir/irText/expressions/kt47245.kt"); + } + @Test @TestMetadata("lambdaInCAO.kt") public void testLambdaInCAO() throws Exception { diff --git a/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java b/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java index 0331460182d..5e3bcfef4ea 100644 --- a/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java +++ b/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java @@ -5769,6 +5769,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class) public static class ControlStructures extends AbstractLightAnalysisModeTest { + @TestMetadata("kt47245.kt") + public void ignoreKt47245() throws Exception { + runTest("compiler/testData/codegen/box/controlStructures/kt47245.kt"); + } + private void runTest(String testDataFilePath) throws Exception { KotlinTestUtils.runTest(this::doTest, TargetBackend.JVM, testDataFilePath); } diff --git a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/es6/semantics/IrJsCodegenBoxES6TestGenerated.java b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/es6/semantics/IrJsCodegenBoxES6TestGenerated.java index 344afa3981e..eb26572c7f6 100644 --- a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/es6/semantics/IrJsCodegenBoxES6TestGenerated.java +++ b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/es6/semantics/IrJsCodegenBoxES6TestGenerated.java @@ -5351,6 +5351,11 @@ public class IrJsCodegenBoxES6TestGenerated extends AbstractIrJsCodegenBoxES6Tes runTest("compiler/testData/codegen/box/controlStructures/kt42455.kt"); } + @TestMetadata("kt47245.kt") + public void testKt47245() throws Exception { + runTest("compiler/testData/codegen/box/controlStructures/kt47245.kt"); + } + @TestMetadata("kt513.kt") public void testKt513() throws Exception { runTest("compiler/testData/codegen/box/controlStructures/kt513.kt"); diff --git a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/semantics/IrJsCodegenBoxTestGenerated.java b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/semantics/IrJsCodegenBoxTestGenerated.java index 96d203a86f7..d2a0ab20687 100644 --- a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/semantics/IrJsCodegenBoxTestGenerated.java +++ b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/semantics/IrJsCodegenBoxTestGenerated.java @@ -4757,6 +4757,11 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest { runTest("compiler/testData/codegen/box/controlStructures/kt42455.kt"); } + @TestMetadata("kt47245.kt") + public void testKt47245() throws Exception { + runTest("compiler/testData/codegen/box/controlStructures/kt47245.kt"); + } + @TestMetadata("kt513.kt") public void testKt513() throws Exception { runTest("compiler/testData/codegen/box/controlStructures/kt513.kt"); diff --git a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java index 88bfb463b6d..a5ae9443a52 100644 --- a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java +++ b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java @@ -4757,6 +4757,11 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest { runTest("compiler/testData/codegen/box/controlStructures/kt42455.kt"); } + @TestMetadata("kt47245.kt") + public void testKt47245() throws Exception { + runTest("compiler/testData/codegen/box/controlStructures/kt47245.kt"); + } + @TestMetadata("kt513.kt") public void testKt513() throws Exception { runTest("compiler/testData/codegen/box/controlStructures/kt513.kt"); diff --git a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/wasm/semantics/IrCodegenBoxWasmTestGenerated.java b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/wasm/semantics/IrCodegenBoxWasmTestGenerated.java index ae63802098e..5d375a8c969 100644 --- a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/wasm/semantics/IrCodegenBoxWasmTestGenerated.java +++ b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/wasm/semantics/IrCodegenBoxWasmTestGenerated.java @@ -3365,6 +3365,11 @@ public class IrCodegenBoxWasmTestGenerated extends AbstractIrCodegenBoxWasmTest runTest("compiler/testData/codegen/box/controlStructures/kt42455.kt"); } + @TestMetadata("kt47245.kt") + public void testKt47245() throws Exception { + runTest("compiler/testData/codegen/box/controlStructures/kt47245.kt"); + } + @TestMetadata("kt772.kt") public void testKt772() throws Exception { runTest("compiler/testData/codegen/box/controlStructures/kt772.kt");