From 69723911858e7502fef7f7638a92ce7a444e3bb9 Mon Sep 17 00:00:00 2001 From: Kirill Rakhman Date: Wed, 19 Apr 2023 15:52:18 +0200 Subject: [PATCH] [FE] Make dynamic a suitable resulted type This fixes a FIR-specific issue where a type variable is inferred to Any instead of dynamic. K1 wasn't affected because of a FIR-specific condition in ResultTypeResolver. #KT-57962 Fixed --- .../TrivialConstraintTypeInferenceOracle.kt | 2 +- .../dynamicTypes/setOperatorOnDynamic.fir.kt | 13 +++++++++++++ .../dynamicTypes/setOperatorOnDynamic.fir.txt | 13 +++++++++++++ .../dynamicTypes/setOperatorOnDynamic.kt | 13 +++++++++++++ .../DiagnosticsTestWithJsStdLibGenerated.java | 6 ++++++ ...FirPsiJsOldFrontendDiagnosticsTestGenerated.java | 6 ++++++ 6 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 compiler/testData/diagnostics/testsWithJsStdLib/dynamicTypes/setOperatorOnDynamic.fir.kt create mode 100644 compiler/testData/diagnostics/testsWithJsStdLib/dynamicTypes/setOperatorOnDynamic.fir.txt create mode 100644 compiler/testData/diagnostics/testsWithJsStdLib/dynamicTypes/setOperatorOnDynamic.kt diff --git a/compiler/resolution.common/src/org/jetbrains/kotlin/resolve/calls/inference/components/TrivialConstraintTypeInferenceOracle.kt b/compiler/resolution.common/src/org/jetbrains/kotlin/resolve/calls/inference/components/TrivialConstraintTypeInferenceOracle.kt index 7fb0aee3d76..ef57c90b688 100644 --- a/compiler/resolution.common/src/org/jetbrains/kotlin/resolve/calls/inference/components/TrivialConstraintTypeInferenceOracle.kt +++ b/compiler/resolution.common/src/org/jetbrains/kotlin/resolve/calls/inference/components/TrivialConstraintTypeInferenceOracle.kt @@ -30,7 +30,7 @@ class TrivialConstraintTypeInferenceOracle private constructor(context: TypeSyst fun isSuitableResultedType( resultType: KotlinTypeMarker ): Boolean { - return !resultType.typeConstructor().isNothingConstructor() + return !resultType.typeConstructor().isNothingConstructor() || (isK2 && resultType.isDynamic()) } // It's possible to generate Nothing-like constraints inside incorporation mechanism: diff --git a/compiler/testData/diagnostics/testsWithJsStdLib/dynamicTypes/setOperatorOnDynamic.fir.kt b/compiler/testData/diagnostics/testsWithJsStdLib/dynamicTypes/setOperatorOnDynamic.fir.kt new file mode 100644 index 00000000000..67953df37b8 --- /dev/null +++ b/compiler/testData/diagnostics/testsWithJsStdLib/dynamicTypes/setOperatorOnDynamic.fir.kt @@ -0,0 +1,13 @@ +// FIR_DUMP +// DIAGNOSTICS: -UNUSED_ANONYMOUS_PARAMETER, -UNUSED_VARIABLE + +fun foo() { + val x1: Any = arrayOf().fold("") { res, key -> + res.length + res + } + val x3: Any = arrayOf().fold(js("({})")) { res, key -> + res[key] = "hello" + res + } +} diff --git a/compiler/testData/diagnostics/testsWithJsStdLib/dynamicTypes/setOperatorOnDynamic.fir.txt b/compiler/testData/diagnostics/testsWithJsStdLib/dynamicTypes/setOperatorOnDynamic.fir.txt new file mode 100644 index 00000000000..0780b438129 --- /dev/null +++ b/compiler/testData/diagnostics/testsWithJsStdLib/dynamicTypes/setOperatorOnDynamic.fir.txt @@ -0,0 +1,13 @@ +FILE: setOperatorOnDynamic.fir.kt + public final fun foo(): R|kotlin/Unit| { + lval x1: R|kotlin/Any| = R|kotlin/arrayOf|().R|kotlin/collections/fold|(String(), = fold@fun (res: R|kotlin/String|, key: R|kotlin/String|): R|kotlin/String| { + R|/res|.R|kotlin/String.length| + ^ R|/res| + } + ) + lval x3: R|kotlin/Any| = R|kotlin/arrayOf|().R|kotlin/collections/fold|(R|kotlin/js/js|(String(({}))), = fold@fun (res: R|dynamic|, key: R|kotlin/String|): R|dynamic| { + R|/res|.R|/set|(vararg(R|/key|, String(hello))) + ^ R|/res| + } + ) + } diff --git a/compiler/testData/diagnostics/testsWithJsStdLib/dynamicTypes/setOperatorOnDynamic.kt b/compiler/testData/diagnostics/testsWithJsStdLib/dynamicTypes/setOperatorOnDynamic.kt new file mode 100644 index 00000000000..6b16e89dbbf --- /dev/null +++ b/compiler/testData/diagnostics/testsWithJsStdLib/dynamicTypes/setOperatorOnDynamic.kt @@ -0,0 +1,13 @@ +// FIR_DUMP +// DIAGNOSTICS: -UNUSED_ANONYMOUS_PARAMETER, -UNUSED_VARIABLE + +fun foo() { + val x1: Any = arrayOf().fold("") { res, key -> + res.length + res + } + val x3: Any = arrayOf().fold(js("({})")) { res, key -> + res[key] = "hello" + res + } +} diff --git a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/DiagnosticsTestWithJsStdLibGenerated.java b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/DiagnosticsTestWithJsStdLibGenerated.java index c80e2b6b2c9..711e786807d 100644 --- a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/DiagnosticsTestWithJsStdLibGenerated.java +++ b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/DiagnosticsTestWithJsStdLibGenerated.java @@ -373,6 +373,12 @@ public class DiagnosticsTestWithJsStdLibGenerated extends AbstractDiagnosticsTes runTest("compiler/testData/diagnostics/testsWithJsStdLib/dynamicTypes/reified.kt"); } + @Test + @TestMetadata("setOperatorOnDynamic.kt") + public void testSetOperatorOnDynamic() throws Exception { + runTest("compiler/testData/diagnostics/testsWithJsStdLib/dynamicTypes/setOperatorOnDynamic.kt"); + } + @Test @TestMetadata("smartCast.kt") public void testSmartCast() throws Exception { diff --git a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/fir/FirPsiJsOldFrontendDiagnosticsTestGenerated.java b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/fir/FirPsiJsOldFrontendDiagnosticsTestGenerated.java index 99f36d0f4aa..4096faf723c 100644 --- a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/fir/FirPsiJsOldFrontendDiagnosticsTestGenerated.java +++ b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/fir/FirPsiJsOldFrontendDiagnosticsTestGenerated.java @@ -374,6 +374,12 @@ public class FirPsiJsOldFrontendDiagnosticsTestGenerated extends AbstractFirPsiJ runTest("compiler/testData/diagnostics/testsWithJsStdLib/dynamicTypes/reified.kt"); } + @Test + @TestMetadata("setOperatorOnDynamic.kt") + public void testSetOperatorOnDynamic() throws Exception { + runTest("compiler/testData/diagnostics/testsWithJsStdLib/dynamicTypes/setOperatorOnDynamic.kt"); + } + @Test @TestMetadata("smartCast.kt") public void testSmartCast() throws Exception {