diff --git a/js/js.translator/src/org/jetbrains/kotlin/js/translate/intrinsic/functions/factories/PrimitiveUnaryOperationFIF.java b/js/js.translator/src/org/jetbrains/kotlin/js/translate/intrinsic/functions/factories/PrimitiveUnaryOperationFIF.java index 95e94faad97..10bd31ceb14 100644 --- a/js/js.translator/src/org/jetbrains/kotlin/js/translate/intrinsic/functions/factories/PrimitiveUnaryOperationFIF.java +++ b/js/js.translator/src/org/jetbrains/kotlin/js/translate/intrinsic/functions/factories/PrimitiveUnaryOperationFIF.java @@ -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 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(); diff --git a/js/js.translator/testData/box/number/intOverflow.kt b/js/js.translator/testData/box/number/intOverflow.kt index 4609a3b89ba..1165430f940 100644 --- a/js/js.translator/testData/box/number/intOverflow.kt +++ b/js/js.translator/testData/box/number/intOverflow.kt @@ -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" -} \ No newline at end of file +} + +fun minInt() = Int.MIN_VALUE \ No newline at end of file