From dd61a5b2c6d300e620028520283713413ba21760 Mon Sep 17 00:00:00 2001 From: Dmitry Petrov Date: Tue, 28 Feb 2017 17:55:48 +0300 Subject: [PATCH] KT-16553 Underscore variable values shall not be evaluated Do not generate 'componentN' calls for '_' in destructuring assignment. --- .../psi2ir/generators/StatementGenerator.kt | 4 +++ .../destructuringWithUnderscore.kt | 11 ++++++ .../destructuringWithUnderscore.txt | 36 +++++++++++++++++++ .../kotlin/ir/IrTextTestCaseGenerated.java | 6 ++++ 4 files changed, 57 insertions(+) create mode 100644 compiler/testData/ir/irText/expressions/destructuringWithUnderscore.kt create mode 100644 compiler/testData/ir/irText/expressions/destructuringWithUnderscore.txt 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 3013f3acc65..bf42482c212 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 @@ -114,6 +114,10 @@ class StatementGenerator( componentSubstitutedCall.setExplicitReceiverValue(containerValue) val componentVariable = getOrFail(BindingContext.VARIABLE, ktEntry) + + // componentN for '_' SHOULD NOT be evaluated + if (componentVariable.name.isSpecial) continue + val irComponentCall = callGenerator.generateCall(ktEntry.startOffset, ktEntry.endOffset, componentSubstitutedCall, IrStatementOrigin.COMPONENT_N.withIndex(index + 1)) val irComponentVar = IrVariableImpl(ktEntry.startOffset, ktEntry.endOffset, IrDeclarationOrigin.DEFINED, diff --git a/compiler/testData/ir/irText/expressions/destructuringWithUnderscore.kt b/compiler/testData/ir/irText/expressions/destructuringWithUnderscore.kt new file mode 100644 index 00000000000..1c43c38b9cf --- /dev/null +++ b/compiler/testData/ir/irText/expressions/destructuringWithUnderscore.kt @@ -0,0 +1,11 @@ +object A + +object B { + operator fun A.component1() = 1 + operator fun A.component2() = 2 + operator fun A.component3() = 3 +} + +fun B.test() { + val (x, _, z) = A +} \ No newline at end of file diff --git a/compiler/testData/ir/irText/expressions/destructuringWithUnderscore.txt b/compiler/testData/ir/irText/expressions/destructuringWithUnderscore.txt new file mode 100644 index 00000000000..af7780dfa4a --- /dev/null +++ b/compiler/testData/ir/irText/expressions/destructuringWithUnderscore.txt @@ -0,0 +1,36 @@ +FILE /destructuringWithUnderscore.kt + CLASS OBJECT A + CONSTRUCTOR private constructor A() + BLOCK_BODY + DELEGATING_CONSTRUCTOR_CALL 'constructor Any()' + INSTANCE_INITIALIZER_CALL classDescriptor='A' + CLASS OBJECT B + CONSTRUCTOR private constructor B() + BLOCK_BODY + DELEGATING_CONSTRUCTOR_CALL 'constructor Any()' + INSTANCE_INITIALIZER_CALL classDescriptor='B' + FUN public final operator fun A.component1(): kotlin.Int + BLOCK_BODY + RETURN type=kotlin.Nothing from='component1() on A: Int' + CONST Int type=kotlin.Int value='1' + FUN public final operator fun A.component2(): kotlin.Int + BLOCK_BODY + RETURN type=kotlin.Nothing from='component2() on A: Int' + CONST Int type=kotlin.Int value='2' + FUN public final operator fun A.component3(): kotlin.Int + BLOCK_BODY + RETURN type=kotlin.Nothing from='component3() on A: Int' + CONST Int type=kotlin.Int value='3' + FUN public fun B.test(): kotlin.Unit + BLOCK_BODY + COMPOSITE type=kotlin.Unit origin=DESTRUCTURING_DECLARATION + VAR IR_TEMPORARY_VARIABLE val tmp0_container: A + GET_OBJECT 'A' type=A + VAR val x: kotlin.Int + CALL 'component1() on A: Int' type=kotlin.Int origin=COMPONENT_N(index=1) + $this: GET_VAR '' type=B origin=null + $receiver: GET_VAR 'tmp0_container: A' type=A origin=null + VAR val z: kotlin.Int + CALL 'component3() on A: Int' type=kotlin.Int origin=COMPONENT_N(index=3) + $this: GET_VAR '' type=B origin=null + $receiver: GET_VAR 'tmp0_container: A' type=A origin=null diff --git a/compiler/tests/org/jetbrains/kotlin/ir/IrTextTestCaseGenerated.java b/compiler/tests/org/jetbrains/kotlin/ir/IrTextTestCaseGenerated.java index 3b8cd1a7e5e..c83f46b55b4 100644 --- a/compiler/tests/org/jetbrains/kotlin/ir/IrTextTestCaseGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/ir/IrTextTestCaseGenerated.java @@ -482,6 +482,12 @@ public class IrTextTestCaseGenerated extends AbstractIrTextTestCase { doTest(fileName); } + @TestMetadata("destructuringWithUnderscore.kt") + public void testDestructuringWithUnderscore() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/ir/irText/expressions/destructuringWithUnderscore.kt"); + doTest(fileName); + } + @TestMetadata("dotQualified.kt") public void testDotQualified() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/ir/irText/expressions/dotQualified.kt");