JS: support integer overflow semantics for unary minus operator

See KT-19290
This commit is contained in:
Alexey Andreev
2017-10-17 11:56:11 +03:00
parent f25a5b5177
commit d4ea4983d8
2 changed files with 24 additions and 1 deletions
@@ -61,6 +61,7 @@ public enum PrimitiveUnaryOperationFIF implements FunctionIntrinsicFactory {
private static final DescriptorPredicate DEC_OPERATION_FOR_BYTE = pattern("Byte.dec");
private static final DescriptorPredicate INC_OPERATION_FOR_SHORT = pattern("Short.inc");
private static final DescriptorPredicate DEC_OPERATION_FOR_SHORT = pattern("Short.dec");
private static final DescriptorPredicate NEG_OPERATION_FOR_INT = pattern("Int.unaryMinus");
@NotNull
private static final DescriptorPredicate INC_OPERATION_FOR_PRIMITIVE_NUMBER = pattern("Float|Double.inc()");
@@ -152,6 +153,20 @@ public enum PrimitiveUnaryOperationFIF implements FunctionIntrinsicFactory {
}
};
private static final FunctionIntrinsicWithReceiverComputed NUMBER_NEG_INTRINSIC = new FunctionIntrinsicWithReceiverComputed() {
@NotNull
@Override
public JsExpression apply(
@Nullable JsExpression receiver,
@NotNull List<? extends JsExpression> arguments,
@NotNull TranslationContext context
) {
assert receiver != null;
assert arguments.size() == 0;
return new JsPrefixOperation(JsUnaryOperator.NEG, receiver);
}
};
private static abstract class UnaryOperationInstrinsicBase extends FunctionIntrinsicWithReceiverComputed {
@NotNull
public abstract JsExpression doApply(@NotNull JsExpression receiver, @NotNull TranslationContext context);
@@ -265,6 +280,9 @@ public enum PrimitiveUnaryOperationFIF implements FunctionIntrinsicFactory {
return NUMBER_DEC_INTRINSIC;
}
if (NEG_OPERATION_FOR_INT.test(descriptor)) {
return new IntOverflowIntrinsic(NUMBER_NEG_INTRINSIC);
}
Name name = descriptor.getName();
+6 -1
View File
@@ -26,5 +26,10 @@ fun box(): String {
v = bigValue() * bigValue()
if (v != 16) return "fail6: $v"
v = -minInt()
if (v != Int.MIN_VALUE) return "fail7: $v"
return "OK"
}
}
fun minInt() = Int.MIN_VALUE