From 84e49e5b57baeed8c0abbc3c2cb2698f587dc24c Mon Sep 17 00:00:00 2001 From: Toshiaki Kameyama Date: Tue, 5 Jun 2018 10:04:57 +0900 Subject: [PATCH] JS: Fix ClassCastException that occurs when 'when' clause has map range #KT-23458 Fixed --- .../js/test/semantics/BoxJsTestGenerated.java | 5 +++++ .../js/test/semantics/IrBoxJsTestGenerated.java | 5 +++++ .../js/translate/callTranslator/CallTranslator.kt | 6 ++++-- .../box/expression/when/whenWithMapRangeClause.kt | 13 +++++++++++++ 4 files changed, 27 insertions(+), 2 deletions(-) create mode 100644 js/js.translator/testData/box/expression/when/whenWithMapRangeClause.kt diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/BoxJsTestGenerated.java b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/BoxJsTestGenerated.java index 6a779bb9476..27b02bde55f 100644 --- a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/BoxJsTestGenerated.java +++ b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/BoxJsTestGenerated.java @@ -3202,6 +3202,11 @@ public class BoxJsTestGenerated extends AbstractBoxJsTest { runTest("js/js.translator/testData/box/expression/when/whenWithLongRangeClause.kt"); } + @TestMetadata("whenWithMapRangeClause.kt") + public void testWhenWithMapRangeClause() throws Exception { + runTest("js/js.translator/testData/box/expression/when/whenWithMapRangeClause.kt"); + } + @TestMetadata("whenWithOneStmWhen.kt") public void testWhenWithOneStmWhen() throws Exception { runTest("js/js.translator/testData/box/expression/when/whenWithOneStmWhen.kt"); diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/IrBoxJsTestGenerated.java b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/IrBoxJsTestGenerated.java index 43d6440803c..aa96bac641d 100644 --- a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/IrBoxJsTestGenerated.java +++ b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/IrBoxJsTestGenerated.java @@ -3202,6 +3202,11 @@ public class IrBoxJsTestGenerated extends AbstractIrBoxJsTest { runTest("js/js.translator/testData/box/expression/when/whenWithLongRangeClause.kt"); } + @TestMetadata("whenWithMapRangeClause.kt") + public void testWhenWithMapRangeClause() throws Exception { + runTest("js/js.translator/testData/box/expression/when/whenWithMapRangeClause.kt"); + } + @TestMetadata("whenWithOneStmWhen.kt") public void testWhenWithOneStmWhen() throws Exception { runTest("js/js.translator/testData/box/expression/when/whenWithOneStmWhen.kt"); diff --git a/js/js.translator/src/org/jetbrains/kotlin/js/translate/callTranslator/CallTranslator.kt b/js/js.translator/src/org/jetbrains/kotlin/js/translate/callTranslator/CallTranslator.kt index dd846067cee..15c4bd7acd2 100644 --- a/js/js.translator/src/org/jetbrains/kotlin/js/translate/callTranslator/CallTranslator.kt +++ b/js/js.translator/src/org/jetbrains/kotlin/js/translate/callTranslator/CallTranslator.kt @@ -31,6 +31,7 @@ import org.jetbrains.kotlin.js.translate.utils.* import org.jetbrains.kotlin.name.FqName import org.jetbrains.kotlin.psi.Call.CallType import org.jetbrains.kotlin.psi.KtExpression +import org.jetbrains.kotlin.psi.KtWhenConditionInRange import org.jetbrains.kotlin.resolve.calls.callResolverUtil.isInvokeCallOnVariable import org.jetbrains.kotlin.resolve.calls.callUtil.isSafeCall import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall @@ -148,8 +149,9 @@ private fun translateFunctionCall( var callExpression = callInfo.translateFunctionCall() if (CallExpressionTranslator.shouldBeInlined(inlineResolvedCall.resultingDescriptor, context)) { - setInlineCallMetadata(callExpression, resolvedCall.call.callElement as KtExpression, - inlineResolvedCall.resultingDescriptor, context) + val callElement = resolvedCall.call.callElement + val ktExpression = (callElement as? KtWhenConditionInRange)?.rangeExpression ?: callElement as KtExpression + setInlineCallMetadata(callExpression, ktExpression, inlineResolvedCall.resultingDescriptor, context) } if (resolvedCall.resultingDescriptor.isSuspend) { diff --git a/js/js.translator/testData/box/expression/when/whenWithMapRangeClause.kt b/js/js.translator/testData/box/expression/when/whenWithMapRangeClause.kt new file mode 100644 index 00000000000..8c99585d1cc --- /dev/null +++ b/js/js.translator/testData/box/expression/when/whenWithMapRangeClause.kt @@ -0,0 +1,13 @@ +// IGNORE_BACKEND: JS_IR +// EXPECTED_REACHABLE_NODES: 1096 + +package foo + +fun box(): String { + val map = mapOf(1 to "") + val i = 1 + return when (i) { + in map -> "OK" + else -> "fail" + } +} \ No newline at end of file