Skip to content

Commit dd61a5b

Browse files
committed
KT-16553 Underscore variable values shall not be evaluated
Do not generate 'componentN' calls for '_' in destructuring assignment.
1 parent 63b16e1 commit dd61a5b

File tree

4 files changed

+57
-0
lines changed

4 files changed

+57
-0
lines changed

compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/generators/StatementGenerator.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,10 @@ class StatementGenerator(
114114
componentSubstitutedCall.setExplicitReceiverValue(containerValue)
115115

116116
val componentVariable = getOrFail(BindingContext.VARIABLE, ktEntry)
117+
118+
// componentN for '_' SHOULD NOT be evaluated
119+
if (componentVariable.name.isSpecial) continue
120+
117121
val irComponentCall = callGenerator.generateCall(ktEntry.startOffset, ktEntry.endOffset, componentSubstitutedCall,
118122
IrStatementOrigin.COMPONENT_N.withIndex(index + 1))
119123
val irComponentVar = IrVariableImpl(ktEntry.startOffset, ktEntry.endOffset, IrDeclarationOrigin.DEFINED,
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
object A
2+
3+
object B {
4+
operator fun A.component1() = 1
5+
operator fun A.component2() = 2
6+
operator fun A.component3() = 3
7+
}
8+
9+
fun B.test() {
10+
val (x, _, z) = A
11+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
FILE /destructuringWithUnderscore.kt
2+
CLASS OBJECT A
3+
CONSTRUCTOR private constructor A()
4+
BLOCK_BODY
5+
DELEGATING_CONSTRUCTOR_CALL 'constructor Any()'
6+
INSTANCE_INITIALIZER_CALL classDescriptor='A'
7+
CLASS OBJECT B
8+
CONSTRUCTOR private constructor B()
9+
BLOCK_BODY
10+
DELEGATING_CONSTRUCTOR_CALL 'constructor Any()'
11+
INSTANCE_INITIALIZER_CALL classDescriptor='B'
12+
FUN public final operator fun A.component1(): kotlin.Int
13+
BLOCK_BODY
14+
RETURN type=kotlin.Nothing from='component1() on A: Int'
15+
CONST Int type=kotlin.Int value='1'
16+
FUN public final operator fun A.component2(): kotlin.Int
17+
BLOCK_BODY
18+
RETURN type=kotlin.Nothing from='component2() on A: Int'
19+
CONST Int type=kotlin.Int value='2'
20+
FUN public final operator fun A.component3(): kotlin.Int
21+
BLOCK_BODY
22+
RETURN type=kotlin.Nothing from='component3() on A: Int'
23+
CONST Int type=kotlin.Int value='3'
24+
FUN public fun B.test(): kotlin.Unit
25+
BLOCK_BODY
26+
COMPOSITE type=kotlin.Unit origin=DESTRUCTURING_DECLARATION
27+
VAR IR_TEMPORARY_VARIABLE val tmp0_container: A
28+
GET_OBJECT 'A' type=A
29+
VAR val x: kotlin.Int
30+
CALL 'component1() on A: Int' type=kotlin.Int origin=COMPONENT_N(index=1)
31+
$this: GET_VAR '<receiver: test() on B: Unit>' type=B origin=null
32+
$receiver: GET_VAR 'tmp0_container: A' type=A origin=null
33+
VAR val z: kotlin.Int
34+
CALL 'component3() on A: Int' type=kotlin.Int origin=COMPONENT_N(index=3)
35+
$this: GET_VAR '<receiver: test() on B: Unit>' type=B origin=null
36+
$receiver: GET_VAR 'tmp0_container: A' type=A origin=null

compiler/tests/org/jetbrains/kotlin/ir/IrTextTestCaseGenerated.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,12 @@ public void testDestructuring1() throws Exception {
482482
doTest(fileName);
483483
}
484484

485+
@TestMetadata("destructuringWithUnderscore.kt")
486+
public void testDestructuringWithUnderscore() throws Exception {
487+
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/ir/irText/expressions/destructuringWithUnderscore.kt");
488+
doTest(fileName);
489+
}
490+
485491
@TestMetadata("dotQualified.kt")
486492
public void testDotQualified() throws Exception {
487493
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/ir/irText/expressions/dotQualified.kt");

0 commit comments

Comments
 (0)