diff --git a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/ConversionUtils.kt b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/ConversionUtils.kt index 0eb9184d020..1c91ab3482b 100644 --- a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/ConversionUtils.kt +++ b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/ConversionUtils.kt @@ -229,7 +229,14 @@ private fun FirCallableSymbol<*>.toSymbolForCall( } // Member fake override or bound callable reference dispatchReceiver !is FirNoReceiverExpression -> { - dispatchReceiver.typeRef.coneType.let { it.findClassRepresentation(it, declarationStorage.session) } + val callSiteDispatchReceiverType = dispatchReceiver.typeRef.coneType + val declarationSiteDispatchReceiverType = dispatchReceiverType + val type = if (callSiteDispatchReceiverType is ConeDynamicType && declarationSiteDispatchReceiverType != null) { + declarationSiteDispatchReceiverType + } else { + callSiteDispatchReceiverType + } + type.findClassRepresentation(type, declarationStorage.session) } // Unbound callable reference to member (non-extension) isReference && fir.receiverParameter == null -> { @@ -752,4 +759,4 @@ fun FirCallableDeclaration.contextReceiversForFunctionOrContainingProperty(): Li fun IrActualizationResult?.extractFirDeclarations(): Set? { return this?.actualizedExpectDeclarations?.mapNotNullTo(mutableSetOf()) { ((it as IrMetadataSourceOwner).metadata as FirMetadataSource).fir } -} \ No newline at end of file +} diff --git a/compiler/testData/codegen/box/smartCasts/smartcastOnDynamic.kt b/compiler/testData/codegen/box/smartCasts/smartcastOnDynamic.kt new file mode 100644 index 00000000000..20f9d815559 --- /dev/null +++ b/compiler/testData/codegen/box/smartCasts/smartcastOnDynamic.kt @@ -0,0 +1,13 @@ +// TARGET_BACKEND: JS_IR +// ISSUE: KT-57682 +fun test_1(x: String) = x.length + +fun test_2(x: dynamic): Int = when (x) { + is String -> x.length + else -> x.something +} + +fun box(): String { + val result = test_1("a") + test_2("ab") + return if (result == 3) "OK" else "fail" +} diff --git a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/fir/FirJsCodegenBoxTestGenerated.java b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/fir/FirJsCodegenBoxTestGenerated.java index 7523c434fa7..736e6f3a499 100644 --- a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/fir/FirJsCodegenBoxTestGenerated.java +++ b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/fir/FirJsCodegenBoxTestGenerated.java @@ -35055,6 +35055,12 @@ public class FirJsCodegenBoxTestGenerated extends AbstractFirJsCodegenBoxTest { runTest("compiler/testData/codegen/box/smartCasts/smartCastInsideIf.kt"); } + @Test + @TestMetadata("smartcastOnDynamic.kt") + public void testSmartcastOnDynamic() throws Exception { + runTest("compiler/testData/codegen/box/smartCasts/smartcastOnDynamic.kt"); + } + @Test @TestMetadata("whenSmartCast.kt") public void testWhenSmartCast() throws Exception { diff --git a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/IrJsCodegenBoxTestGenerated.java b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/IrJsCodegenBoxTestGenerated.java index 7900ea46ea1..2eaa51c2186 100644 --- a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/IrJsCodegenBoxTestGenerated.java +++ b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/IrJsCodegenBoxTestGenerated.java @@ -35055,6 +35055,12 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest { runTest("compiler/testData/codegen/box/smartCasts/smartCastInsideIf.kt"); } + @Test + @TestMetadata("smartcastOnDynamic.kt") + public void testSmartcastOnDynamic() throws Exception { + runTest("compiler/testData/codegen/box/smartCasts/smartcastOnDynamic.kt"); + } + @Test @TestMetadata("whenSmartCast.kt") public void testWhenSmartCast() throws Exception { diff --git a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/IrJsES6CodegenBoxTestGenerated.java b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/IrJsES6CodegenBoxTestGenerated.java index 7762c2a8dbc..2efe308ced9 100644 --- a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/IrJsES6CodegenBoxTestGenerated.java +++ b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/IrJsES6CodegenBoxTestGenerated.java @@ -35055,6 +35055,12 @@ public class IrJsES6CodegenBoxTestGenerated extends AbstractIrJsES6CodegenBoxTes runTest("compiler/testData/codegen/box/smartCasts/smartCastInsideIf.kt"); } + @Test + @TestMetadata("smartcastOnDynamic.kt") + public void testSmartcastOnDynamic() throws Exception { + runTest("compiler/testData/codegen/box/smartCasts/smartcastOnDynamic.kt"); + } + @Test @TestMetadata("whenSmartCast.kt") public void testWhenSmartCast() throws Exception {