KT-817 Wrong code on prefix increment operators.
This commit is contained in:
@@ -81,6 +81,10 @@ public final class MiscTest extends AbstractExpressionTest {
|
||||
checkFooBoxIsTrue("KT-1361-2.kt");
|
||||
}
|
||||
|
||||
public void testKt817() throws Exception {
|
||||
checkFooBoxIsTrue("KT-817.kt");
|
||||
}
|
||||
|
||||
//TODO: depends on KT-1198
|
||||
//TODO: look into BindingContext.VARIABLE_REASSIGNMENT
|
||||
// public void testKt740_3() throws Exception {
|
||||
|
||||
+18
-41
@@ -31,8 +31,6 @@ import org.jetbrains.k2js.translate.reference.CachedAccessTranslator;
|
||||
import java.util.List;
|
||||
|
||||
import static org.jetbrains.k2js.translate.reference.AccessTranslationUtils.getCachedAccessTranslator;
|
||||
import static org.jetbrains.k2js.translate.utils.BindingUtils.isStatement;
|
||||
import static org.jetbrains.k2js.translate.utils.BindingUtils.isVariableReassignment;
|
||||
import static org.jetbrains.k2js.translate.utils.JsAstUtils.newSequence;
|
||||
import static org.jetbrains.k2js.translate.utils.PsiUtils.*;
|
||||
import static org.jetbrains.k2js.translate.utils.TemporariesUtils.temporariesInitialization;
|
||||
@@ -54,58 +52,49 @@ public abstract class IncrementTranslator extends AbstractTranslator {
|
||||
if (isIntrinsicOperation(context, expression)) {
|
||||
return IntrinsicIncrementTranslator.doTranslate(expression, context);
|
||||
}
|
||||
return OverloadedIncrementTranslator.doTranslate(expression, context);
|
||||
return (new OverloadedIncrementTranslator(expression, context)).translateIncrementExpression();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
protected final JetUnaryExpression expression;
|
||||
@NotNull
|
||||
protected final CachedAccessTranslator accessTranslator;
|
||||
private final boolean isVariableReassignment;
|
||||
|
||||
protected IncrementTranslator(@NotNull JetUnaryExpression expression,
|
||||
@NotNull TranslationContext context) {
|
||||
super(context);
|
||||
this.expression = expression;
|
||||
this.isVariableReassignment = isVariableReassignment(context.bindingContext(), expression);
|
||||
JetExpression baseExpression = getBaseExpression(expression);
|
||||
this.accessTranslator = getCachedAccessTranslator(baseExpression, context());
|
||||
}
|
||||
|
||||
@NotNull
|
||||
protected JsExpression translateAsMethodCall() {
|
||||
return withTemporariesInitialized(doTranslateAsMethodCall());
|
||||
protected JsExpression translateIncrementExpression() {
|
||||
return withTemporariesInitialized(doTranslateIncrementExpression());
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private JsExpression doTranslateAsMethodCall() {
|
||||
if (returnValueIgnored() || isPrefix(expression)) {
|
||||
private JsExpression doTranslateIncrementExpression() {
|
||||
if (isPrefix(expression)) {
|
||||
return asPrefix();
|
||||
}
|
||||
if (isVariableReassignment) {
|
||||
return asPostfixWithReassignment();
|
||||
}
|
||||
else {
|
||||
return asPostfixWithNoReassignment();
|
||||
}
|
||||
}
|
||||
|
||||
private boolean returnValueIgnored() {
|
||||
return isStatement(bindingContext(), expression);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private JsExpression asPrefix() {
|
||||
JsExpression getExpression = accessTranslator.translateAsGet();
|
||||
if (isVariableReassignment) {
|
||||
return variableReassignment(getExpression);
|
||||
}
|
||||
return operationExpression(getExpression);
|
||||
return asPostfix();
|
||||
}
|
||||
|
||||
//TODO: decide if this expression can be optimised in case of direct access (not property)
|
||||
@NotNull
|
||||
private JsExpression asPostfixWithReassignment() {
|
||||
private JsExpression asPrefix() {
|
||||
// code fragment: expr(a++)
|
||||
// generate: expr(a = a.inc(), a)
|
||||
JsExpression getExpression = accessTranslator.translateAsGet();
|
||||
JsExpression reassignment = variableReassignment(getExpression);
|
||||
JsExpression getNewValue = accessTranslator.translateAsGet();
|
||||
return AstUtil.newSequence(reassignment, getNewValue);
|
||||
}
|
||||
|
||||
//TODO: decide if this expression can be optimised in case of direct access (not property)
|
||||
@NotNull
|
||||
private JsExpression asPostfix() {
|
||||
// code fragment: expr(a++)
|
||||
// generate: expr( (t1 = a, t2 = t1, a = t1.inc(), t2) )
|
||||
TemporaryVariable t1 = context().declareTemporary(accessTranslator.translateAsGet());
|
||||
@@ -115,18 +104,6 @@ public abstract class IncrementTranslator extends AbstractTranslator {
|
||||
variableReassignment, t2.reference());
|
||||
}
|
||||
|
||||
//TODO: TEST
|
||||
@NotNull
|
||||
private JsExpression asPostfixWithNoReassignment() {
|
||||
// code fragment: expr(a++)
|
||||
// generate: expr( (t1 = a, t2 = t1, t2.inc(), t1) )
|
||||
TemporaryVariable t1 = context().declareTemporary(accessTranslator.translateAsGet());
|
||||
TemporaryVariable t2 = context().declareTemporary(t1.reference());
|
||||
JsExpression methodCall = operationExpression(t2.reference());
|
||||
JsExpression returnedValue = t1.reference();
|
||||
return AstUtil.newSequence(t1.assignmentExpression(), t2.assignmentExpression(), methodCall, returnedValue);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private JsExpression variableReassignment(@NotNull JsExpression toCallMethodUpon) {
|
||||
JsExpression overloadedMethodCallOnPropertyGetter = operationExpression(toCallMethodUpon);
|
||||
|
||||
+5
-5
@@ -33,7 +33,6 @@ import static org.jetbrains.k2js.translate.utils.PsiUtils.isPrefix;
|
||||
*/
|
||||
public final class IntrinsicIncrementTranslator extends IncrementTranslator {
|
||||
|
||||
|
||||
@NotNull
|
||||
public static JsExpression doTranslate(@NotNull JetUnaryExpression expression,
|
||||
@NotNull TranslationContext context) {
|
||||
@@ -49,9 +48,9 @@ public final class IntrinsicIncrementTranslator extends IncrementTranslator {
|
||||
@NotNull
|
||||
private JsExpression translate() {
|
||||
if (isPrimitiveExpressionIncrement()) {
|
||||
return jsUnaryExpression();
|
||||
return primitiveExpressionIncrement();
|
||||
}
|
||||
return translateAsMethodCall();
|
||||
return translateIncrementExpression();
|
||||
}
|
||||
|
||||
private boolean isPrimitiveExpressionIncrement() {
|
||||
@@ -59,7 +58,7 @@ public final class IntrinsicIncrementTranslator extends IncrementTranslator {
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private JsExpression jsUnaryExpression() {
|
||||
private JsExpression primitiveExpressionIncrement() {
|
||||
JsUnaryOperator operator = OperatorTable.getUnaryOperator(getOperationToken(expression));
|
||||
JsExpression getExpression = accessTranslator.translateAsGet();
|
||||
if (isPrefix(expression)) {
|
||||
@@ -76,7 +75,8 @@ public final class IntrinsicIncrementTranslator extends IncrementTranslator {
|
||||
return unaryAsBinary(receiver);
|
||||
}
|
||||
|
||||
public JsBinaryOperation unaryAsBinary(@NotNull JsExpression leftExpression) {
|
||||
@NotNull
|
||||
private JsBinaryOperation unaryAsBinary(@NotNull JsExpression leftExpression) {
|
||||
JsNumberLiteral oneLiteral = program().getNumberLiteral(1);
|
||||
JetToken token = getOperationToken(expression);
|
||||
if (token.equals(JetTokens.PLUSPLUS)) {
|
||||
|
||||
+2
-13
@@ -31,26 +31,15 @@ import static org.jetbrains.k2js.translate.utils.TranslationUtils.getMethodRefer
|
||||
*/
|
||||
public final class OverloadedIncrementTranslator extends IncrementTranslator {
|
||||
|
||||
@NotNull
|
||||
public static JsExpression doTranslate(@NotNull JetUnaryExpression expression,
|
||||
@NotNull TranslationContext context) {
|
||||
return (new OverloadedIncrementTranslator(expression, context))
|
||||
.translate();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private final JsNameRef operationReference;
|
||||
|
||||
private OverloadedIncrementTranslator(@NotNull JetUnaryExpression expression,
|
||||
@NotNull TranslationContext context) {
|
||||
/*package*/ OverloadedIncrementTranslator(@NotNull JetUnaryExpression expression,
|
||||
@NotNull TranslationContext context) {
|
||||
super(expression, context);
|
||||
this.operationReference = getMethodReferenceForOverloadedOperation(context, expression);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private JsExpression translate() {
|
||||
return translateAsMethodCall();
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
|
||||
@@ -36,7 +36,7 @@ fun box() : String {
|
||||
if (c0 != 2) {
|
||||
return "3"
|
||||
}
|
||||
if (c1 != 2) {
|
||||
if (c1 != 3) {
|
||||
return "4"
|
||||
}
|
||||
if (c2 != 1) {
|
||||
@@ -46,14 +46,11 @@ fun box() : String {
|
||||
if (c0 != 3) {
|
||||
return "6"
|
||||
}
|
||||
if (c1 != 3) {
|
||||
if (c1 != 5) {
|
||||
return "7"
|
||||
}
|
||||
if (c2 != 2) {
|
||||
return "8"
|
||||
}
|
||||
|
||||
|
||||
return "OK"
|
||||
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package foo
|
||||
|
||||
class Range() {
|
||||
|
||||
val reversed = false;
|
||||
val start = 0;
|
||||
var count = 10;
|
||||
|
||||
fun next() = start + if (reversed) -(--count) else (--count);
|
||||
}
|
||||
|
||||
fun box() : Boolean {
|
||||
val r = Range()
|
||||
if (r.next() != 9) {
|
||||
return false;
|
||||
}
|
||||
if (r.next() != 8) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
Reference in New Issue
Block a user