JS: fix translation of && and || operators when their RHS declare temporary variables. See KT-16350
This commit is contained in:
@@ -2005,6 +2005,12 @@ public class BoxJsTestGenerated extends AbstractBoxJsTest {
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("andAndWithTmpVarRhs.kt")
|
||||
public void testAndAndWithTmpVarRhs() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("js/js.translator/testData/box/expression/evaluationOrder/andAndWithTmpVarRhs.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("assignToArrayElementWithSideEffect.kt")
|
||||
public void testAssignToArrayElementWithSideEffect() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("js/js.translator/testData/box/expression/evaluationOrder/assignToArrayElementWithSideEffect.kt");
|
||||
|
||||
+5
-2
@@ -21,6 +21,7 @@ import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.kotlin.descriptors.CallableDescriptor;
|
||||
import org.jetbrains.kotlin.descriptors.FunctionDescriptor;
|
||||
import org.jetbrains.kotlin.js.backend.ast.*;
|
||||
import org.jetbrains.kotlin.js.backend.ast.metadata.MetadataProperties;
|
||||
import org.jetbrains.kotlin.js.translate.callTranslator.CallTranslator;
|
||||
import org.jetbrains.kotlin.js.translate.context.TranslationContext;
|
||||
import org.jetbrains.kotlin.js.translate.general.AbstractTranslator;
|
||||
@@ -202,10 +203,12 @@ public final class BinaryOperationTranslator extends AbstractTranslator {
|
||||
if (rightExpression instanceof JsNameRef) {
|
||||
result = rightExpression; // Reuse tmp variable
|
||||
} else {
|
||||
result = context().defineTemporary(rightExpression);
|
||||
result = context().declareTemporary(null).reference();
|
||||
rightBlock.getStatements().add(JsAstUtils.asSyntheticStatement(JsAstUtils.assignment(result, rightExpression)));
|
||||
}
|
||||
JsStatement assignmentStatement = JsAstUtils.assignment(result, literalResult).makeStmt();
|
||||
JsStatement assignmentStatement = JsAstUtils.asSyntheticStatement(JsAstUtils.assignment(result, literalResult));
|
||||
ifStatement = JsAstUtils.newJsIf(leftExpression, rightBlock, assignmentStatement);
|
||||
MetadataProperties.setSynthetic(ifStatement, true);
|
||||
}
|
||||
else {
|
||||
ifStatement = JsAstUtils.newJsIf(leftExpression, rightBlock);
|
||||
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
fun foo(arg: Any): Boolean {
|
||||
return arg == "x"
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
val values = listOf(null, "x")
|
||||
return if (values[0] == null && foo(values[1]!!)) "OK" else "fail"
|
||||
}
|
||||
Reference in New Issue
Block a user