Skip to content

Commit 66f5a12

Browse files
Alexey AndreevAlexey Andreev
authored andcommitted
JS: fix translation of && and || operators when their RHS declare temporary variables. See KT-16350
1 parent c6bfb02 commit 66f5a12

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/BoxJsTestGenerated.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2005,6 +2005,12 @@ public void testAndAndWithSideEffect() throws Exception {
20052005
doTest(fileName);
20062006
}
20072007

2008+
@TestMetadata("andAndWithTmpVarRhs.kt")
2009+
public void testAndAndWithTmpVarRhs() throws Exception {
2010+
String fileName = KotlinTestUtils.navigationMetadata("js/js.translator/testData/box/expression/evaluationOrder/andAndWithTmpVarRhs.kt");
2011+
doTest(fileName);
2012+
}
2013+
20082014
@TestMetadata("assignToArrayElementWithSideEffect.kt")
20092015
public void testAssignToArrayElementWithSideEffect() throws Exception {
20102016
String fileName = KotlinTestUtils.navigationMetadata("js/js.translator/testData/box/expression/evaluationOrder/assignToArrayElementWithSideEffect.kt");

js/js.translator/src/org/jetbrains/kotlin/js/translate/operation/BinaryOperationTranslator.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.jetbrains.kotlin.descriptors.CallableDescriptor;
2222
import org.jetbrains.kotlin.descriptors.FunctionDescriptor;
2323
import org.jetbrains.kotlin.js.backend.ast.*;
24+
import org.jetbrains.kotlin.js.backend.ast.metadata.MetadataProperties;
2425
import org.jetbrains.kotlin.js.translate.callTranslator.CallTranslator;
2526
import org.jetbrains.kotlin.js.translate.context.TranslationContext;
2627
import org.jetbrains.kotlin.js.translate.general.AbstractTranslator;
@@ -202,10 +203,12 @@ else if (OperatorConventions.IDENTITY_EQUALS_OPERATIONS.contains(operationToken)
202203
if (rightExpression instanceof JsNameRef) {
203204
result = rightExpression; // Reuse tmp variable
204205
} else {
205-
result = context().defineTemporary(rightExpression);
206+
result = context().declareTemporary(null).reference();
207+
rightBlock.getStatements().add(JsAstUtils.asSyntheticStatement(JsAstUtils.assignment(result, rightExpression)));
206208
}
207-
JsStatement assignmentStatement = JsAstUtils.assignment(result, literalResult).makeStmt();
209+
JsStatement assignmentStatement = JsAstUtils.asSyntheticStatement(JsAstUtils.assignment(result, literalResult));
208210
ifStatement = JsAstUtils.newJsIf(leftExpression, rightBlock, assignmentStatement);
211+
MetadataProperties.setSynthetic(ifStatement, true);
209212
}
210213
else {
211214
ifStatement = JsAstUtils.newJsIf(leftExpression, rightBlock);
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
fun foo(arg: Any): Boolean {
2+
return arg == "x"
3+
}
4+
5+
fun box(): String {
6+
val values = listOf(null, "x")
7+
return if (values[0] == null && foo(values[1]!!)) "OK" else "fail"
8+
}

0 commit comments

Comments
 (0)