JVM IR: fix operand type for CompareTo intrinsic
It's incorrect to take the first parameter type from the expression itself because it can be nullable if smart casts are used. And if it's nullable, it's mapped to the wrapper type and calling `comparisonOperandType` for it makes no sense. Instead, take the type from the callee function, as it's guaranteed to be mapped to a JVM primitive type. E.g. in `test1` function in the added test, the problem was that the dispatch receiver type of the call expression is `Double?`, which is mapped to `java/lang/Double`, whereas we clearly wanted to obtain the primitive `D` (double) type. #KT-52163 Fixed
This commit is contained in:
@@ -19,16 +19,23 @@ package org.jetbrains.kotlin.codegen.intrinsics
|
||||
import org.jetbrains.kotlin.codegen.AsmUtil.comparisonOperandType
|
||||
import org.jetbrains.kotlin.codegen.Callable
|
||||
import org.jetbrains.kotlin.codegen.CallableMethod
|
||||
import org.jetbrains.kotlin.config.JvmTarget
|
||||
import org.jetbrains.org.objectweb.asm.Type
|
||||
import org.jetbrains.org.objectweb.asm.commons.InstructionAdapter
|
||||
|
||||
class CompareTo : IntrinsicMethod() {
|
||||
class CompareTo(private val jvmTarget: JvmTarget) : IntrinsicMethod() {
|
||||
private fun genInvoke(type: Type?, v: InstructionAdapter) {
|
||||
when (type) {
|
||||
Type.INT_TYPE, Type.CHAR_TYPE -> v.invokestatic(IntrinsicMethods.INTRINSICS_CLASS_NAME, "compare", "(II)I", false)
|
||||
Type.LONG_TYPE -> v.lcmp()
|
||||
Type.FLOAT_TYPE -> v.invokestatic("java/lang/Float", "compare", "(FF)I", false)
|
||||
Type.DOUBLE_TYPE -> v.invokestatic("java/lang/Double", "compare", "(DD)I", false)
|
||||
Type.BOOLEAN_TYPE -> {
|
||||
check(jvmTarget >= JvmTarget.JVM_1_8) {
|
||||
"Cannot generate boolean comparison for JVM target 1.6"
|
||||
}
|
||||
v.invokestatic("java/lang/Boolean", "compare", "(ZZ)I", false)
|
||||
}
|
||||
else -> throw UnsupportedOperationException()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -149,7 +149,7 @@ public class IntrinsicMethods {
|
||||
intrinsicsMap.registerIntrinsic(BUILT_INS_PACKAGE_FQ_NAME, null, "arrayOfNulls", 1, new NewArray());
|
||||
|
||||
for (PrimitiveType type : PrimitiveType.values()) {
|
||||
declareIntrinsicFunction(type.getTypeFqName(), "compareTo", 1, new CompareTo());
|
||||
declareIntrinsicFunction(type.getTypeFqName(), "compareTo", 1, new CompareTo(jvmTarget));
|
||||
declareIntrinsicFunction(COLLECTIONS_PACKAGE_FQ_NAME.child(Name.identifier(type.getTypeName().asString() + "Iterator")), "next", 0, ITERATOR_NEXT);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user