diff --git a/js/js.tests/test/org/jetbrains/k2js/test/semantics/NumberTest.java b/js/js.tests/test/org/jetbrains/k2js/test/semantics/NumberTest.java index 4e12d5dbe11..1eeebca948c 100644 --- a/js/js.tests/test/org/jetbrains/k2js/test/semantics/NumberTest.java +++ b/js/js.tests/test/org/jetbrains/k2js/test/semantics/NumberTest.java @@ -50,6 +50,11 @@ public final class NumberTest extends SingleFileTranslationTest { checkFooBoxIsOk(); } + // KT-5345 Type mismatch on Int / Float division + public void testIntDivFloat() throws Exception { + checkFooBoxIsOk(); + } + public void testHexademicalConstant() throws Exception { try { fooBoxTest(); diff --git a/js/js.translator/src/org/jetbrains/k2js/translate/intrinsic/functions/factories/PrimitiveBinaryOperationFIF.java b/js/js.translator/src/org/jetbrains/k2js/translate/intrinsic/functions/factories/PrimitiveBinaryOperationFIF.java index 067aaebf2dc..0b728cc253a 100644 --- a/js/js.translator/src/org/jetbrains/k2js/translate/intrinsic/functions/factories/PrimitiveBinaryOperationFIF.java +++ b/js/js.translator/src/org/jetbrains/k2js/translate/intrinsic/functions/factories/PrimitiveBinaryOperationFIF.java @@ -24,7 +24,9 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.jetbrains.jet.lang.descriptors.FunctionDescriptor; import org.jetbrains.jet.lang.resolve.name.Name; +import org.jetbrains.jet.lang.types.JetType; import org.jetbrains.jet.lang.types.expressions.OperatorConventions; +import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns; import org.jetbrains.jet.lexer.JetToken; import org.jetbrains.k2js.translate.context.TranslationContext; import org.jetbrains.k2js.translate.intrinsic.functions.basic.FunctionIntrinsic; @@ -101,7 +103,10 @@ public enum PrimitiveBinaryOperationFIF implements FunctionIntrinsicFactory { } if (pattern(INTEGER_NUMBER_TYPES + ".div").apply(descriptor)) { - return INTEGER_DIVISION_INTRINSIC; + JetType resultType = descriptor.getReturnType(); + if (!KotlinBuiltIns.getInstance().getFloatType().equals(resultType) && + !KotlinBuiltIns.getInstance().getDoubleType().equals(resultType)) + return INTEGER_DIVISION_INTRINSIC; } if (descriptor.getName().equals(Name.identifier("rangeTo"))) { return RANGE_TO_INTRINSIC; diff --git a/js/js.translator/testData/number/cases/intDivFloat.kt b/js/js.translator/testData/number/cases/intDivFloat.kt new file mode 100644 index 00000000000..de57a180b30 --- /dev/null +++ b/js/js.translator/testData/number/cases/intDivFloat.kt @@ -0,0 +1,18 @@ +// http://youtrack.jetbrains.com/issue/KT-5345 +// KT-5345 (Javascript) Type mismatch on Int / Float division +// If any of Number operands is floating-point, the result should be float too. + +package foo + +fun box(): String { + assertEquals(0.5f, 1 / 2.0f, "Int / Float") + assertEquals(0.5, 1 / 2.0, "Int / Double") + + assertEquals(0.5f, (1: Short) / 2.0f, "Short / Float") + assertEquals(0.5, (1: Short) / 2.0, "Short / Double") + + assertEquals(0.5f, (1: Byte) / 2.0f, "Byte / Float") + assertEquals(0.5, (1: Byte) / 2.0, "Byte / Double") + + return "OK" +} \ No newline at end of file