From dbacae94d0b7faccd467c816855b8f05c0bae895 Mon Sep 17 00:00:00 2001 From: "victor.petukhov" Date: Mon, 25 Nov 2019 19:03:43 +0300 Subject: [PATCH] Fix common super type calculation for captured dynamic types ^KT-32499 Fixed --- .../calls/NewCommonSuperTypeCalculator.kt | 6 +++- .../dynamicTypes/capturedDynamic.kt | 27 ++++++++++++++++++ .../dynamicTypes/capturedDynamicNI.kt | 28 +++++++++++++++++++ .../DiagnosticsTestWithJsStdLibGenerated.java | 10 +++++++ .../kotlin/types/model/TypeSystemContext.kt | 3 ++ 5 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 compiler/testData/diagnostics/testsWithJsStdLib/dynamicTypes/capturedDynamic.kt create mode 100644 compiler/testData/diagnostics/testsWithJsStdLib/dynamicTypes/capturedDynamicNI.kt diff --git a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/NewCommonSuperTypeCalculator.kt b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/NewCommonSuperTypeCalculator.kt index a2ccaad048a..5813af7618c 100644 --- a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/NewCommonSuperTypeCalculator.kt +++ b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/NewCommonSuperTypeCalculator.kt @@ -41,7 +41,11 @@ object NewCommonSuperTypeCalculator { val lowers = types.map { when (it) { - is SimpleTypeMarker -> it + is SimpleTypeMarker -> { + if (it.isCapturedDynamic()) return it + + it + } is FlexibleTypeMarker -> { if (it.isDynamic()) return it // raw types are allowed here and will be transformed to FlexibleTypes diff --git a/compiler/testData/diagnostics/testsWithJsStdLib/dynamicTypes/capturedDynamic.kt b/compiler/testData/diagnostics/testsWithJsStdLib/dynamicTypes/capturedDynamic.kt new file mode 100644 index 00000000000..211c5a84bac --- /dev/null +++ b/compiler/testData/diagnostics/testsWithJsStdLib/dynamicTypes/capturedDynamic.kt @@ -0,0 +1,27 @@ +// SKIP_TXT + +fun case_1(vararg args: dynamic) { + (")!>listOf(null) + args).toTypedArray() +} + +fun case_2(vararg args: dynamic) { + (")!>listOf() + args).toTypedArray() +} + +fun case_3(vararg args: dynamic) { + (")!>listOf(1) + args).toTypedArray() +} + +fun case_4(vararg args: dynamic) { + (")!>listOf() + args).toTypedArray() +} + +fun case_5(x: Any?) { + fun foo(x: List) = x + + foo(")!>listOf(null) + x as MutableList) +} + +fun case_6(x: Any?) { + (")!>listOf(null) + x as MutableList).toTypedArray() +} diff --git a/compiler/testData/diagnostics/testsWithJsStdLib/dynamicTypes/capturedDynamicNI.kt b/compiler/testData/diagnostics/testsWithJsStdLib/dynamicTypes/capturedDynamicNI.kt new file mode 100644 index 00000000000..1cd9bcb521f --- /dev/null +++ b/compiler/testData/diagnostics/testsWithJsStdLib/dynamicTypes/capturedDynamicNI.kt @@ -0,0 +1,28 @@ +// !LANGUAGE: +NewInference +// SKIP_TXT + +fun case_1(vararg args: dynamic) { + (")!>listOf(null) + args).toTypedArray() +} + +fun case_2(vararg args: dynamic) { + (")!>listOf() + args).toTypedArray() +} + +fun case_3(vararg args: dynamic) { + (")!>listOf(1) + args).toTypedArray() +} + +fun case_4(vararg args: dynamic) { + (")!>listOf() + args).toTypedArray() +} + +fun case_5(x: Any?) { + fun foo(x: List) = x + + foo(")!>listOf(null) + x as MutableList) +} + +fun case_6(x: Any?) { + (")!>listOf(null) + x as MutableList).toTypedArray() +} diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestWithJsStdLibGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestWithJsStdLibGenerated.java index dc82450c365..0114a50c77c 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestWithJsStdLibGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestWithJsStdLibGenerated.java @@ -113,6 +113,16 @@ public class DiagnosticsTestWithJsStdLibGenerated extends AbstractDiagnosticsTes runTest("compiler/testData/diagnostics/testsWithJsStdLib/dynamicTypes/callableReferences.kt"); } + @TestMetadata("capturedDynamic.kt") + public void testДapturedDynamic() throws Exception { + runTest("compiler/testData/diagnostics/testsWithJsStdLib/dynamicTypes/capturedDynamic.kt"); + } + + @TestMetadata("capturedDynamicNI.kt") + public void testCapturedDynamicNI() throws Exception { + runTest("compiler/testData/diagnostics/testsWithJsStdLib/dynamicTypes/capturedDynamicNI.kt"); + } + @TestMetadata("classDelegateBy.kt") public void testClassDelegateBy() throws Exception { runTest("compiler/testData/diagnostics/testsWithJsStdLib/dynamicTypes/classDelegateBy.kt"); diff --git a/core/type-system/src/org/jetbrains/kotlin/types/model/TypeSystemContext.kt b/core/type-system/src/org/jetbrains/kotlin/types/model/TypeSystemContext.kt index 496e7afd70f..e692bd08400 100644 --- a/core/type-system/src/org/jetbrains/kotlin/types/model/TypeSystemContext.kt +++ b/core/type-system/src/org/jetbrains/kotlin/types/model/TypeSystemContext.kt @@ -232,6 +232,9 @@ interface TypeSystemContext : TypeSystemOptimizationContext { fun KotlinTypeMarker.isFlexible(): Boolean = asFlexibleType() != null fun KotlinTypeMarker.isDynamic(): Boolean = asFlexibleType()?.asDynamicType() != null + fun KotlinTypeMarker.isCapturedDynamic(): Boolean = + asSimpleType()?.asCapturedType()?.typeConstructor()?.projection()?.getType()?.isDynamic() == true + fun KotlinTypeMarker.isDefinitelyNotNullType(): Boolean = asSimpleType()?.asDefinitelyNotNullType() != null fun KotlinTypeMarker.hasFlexibleNullability() =