[JS IR BE] Don't transform toString with extension receiver

This commit is contained in:
Svyatoslav Kuzmich
2019-05-19 20:05:53 +03:00
parent 3153a7dd7e
commit e9ceee388e
4 changed files with 26 additions and 10 deletions
@@ -64,20 +64,17 @@ class MethodsOfAnyCallsTransformer(context: JsIrBackendContext) : CallsTransform
}
private fun shouldReplaceToStringWithRuntimeCall(call: IrFunctionAccessExpression): Boolean {
// TODO: (KOTLIN-CR-2079)
// - User defined extension functions Any?.toString() call can be lost during lowering.
// - Use direct method call for dynamic types???
// - Define Any?.toString() in runtime library and stop intrinsicifying extensions
if (call.valueArgumentsCount > 0)
val function = call.symbol.owner
if (function.valueParameters.size != 0 && function.name.asString() != "toString" )
return false
val receiverParameterType = with(call.symbol.owner) {
dispatchReceiverParameter ?: extensionReceiverParameter
}?.type ?: return false
if (function.extensionReceiverParameter != null)
return false
val receiverParameterType = function.dispatchReceiverParameter?.type ?: return false
return receiverParameterType.run {
isArray() || isAny() || isNullable() || this is IrDynamicType || isString()
isArray() || isAny() || this is IrDynamicType || isString()
}
}
}
@@ -6805,6 +6805,11 @@ public class IrBoxJsTestGenerated extends AbstractIrBoxJsTest {
public void testTaggedArrayCopy() throws Exception {
runTest("js/js.translator/testData/box/regression/typeChecks/taggedArrayCopy.kt");
}
@TestMetadata("toStringExtension.kt")
public void testToStringExtension() throws Exception {
runTest("js/js.translator/testData/box/regression/typeChecks/toStringExtension.kt");
}
}
}
@@ -6840,6 +6840,11 @@ public class BoxJsTestGenerated extends AbstractBoxJsTest {
public void testTaggedArrayCopy() throws Exception {
runTest("js/js.translator/testData/box/regression/typeChecks/taggedArrayCopy.kt");
}
@TestMetadata("toStringExtension.kt")
public void testToStringExtension() throws Exception {
runTest("js/js.translator/testData/box/regression/typeChecks/toStringExtension.kt");
}
}
}
@@ -0,0 +1,9 @@
// EXPECTED_REACHABLE_NODES: 1281
package foo
class A
fun A?.toString() = "OK"
fun box() = (null as A?).toString()