From 4f745155087bbb8cd4e0a4da0953d3fa867d1383 Mon Sep 17 00:00:00 2001 From: Mikhail Zarechenskiy Date: Thu, 16 Jan 2020 13:34:34 +0300 Subject: [PATCH] [NI] Fix CST calculation for covariant type projections --- .../calls/NewCommonSuperTypeCalculator.kt | 3 ++- .../nonFixedVariableFromBothBranches.kt | 2 +- .../elvisOperatorAgainstFlexibleType.kt | 20 +++++++++++++++++++ .../elvisOperatorAgainstFlexibleType.txt | 15 ++++++++++++++ .../DiagnosticsTestWithStdLibGenerated.java | 5 +++++ ...ticsTestWithStdLibUsingJavacGenerated.java | 5 +++++ 6 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 compiler/testData/diagnostics/testsWithStdLib/coroutines/inference/elvisOperatorAgainstFlexibleType.kt create mode 100644 compiler/testData/diagnostics/testsWithStdLib/coroutines/inference/elvisOperatorAgainstFlexibleType.txt 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 d0e41539851..1ca3cd7c2d4 100644 --- a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/NewCommonSuperTypeCalculator.kt +++ b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/NewCommonSuperTypeCalculator.kt @@ -388,7 +388,8 @@ object NewCommonSuperTypeCalculator { return if (equalToEachOtherType == null) { createTypeArgument(commonSuperType(argumentTypes, depth + 1), TypeVariance.OUT) } else { - createTypeArgument(equalToEachOtherType.getType(), TypeVariance.INV) + val thereIsNotInv = arguments.any { it.getVariance() != TypeVariance.INV } + createTypeArgument(equalToEachOtherType.getType(), if (thereIsNotInv) TypeVariance.OUT else TypeVariance.INV) } } else { val type = intersectTypes(arguments.map { it.getType() }) diff --git a/compiler/testData/diagnostics/tests/inference/commonSystem/nonFixedVariableFromBothBranches.kt b/compiler/testData/diagnostics/tests/inference/commonSystem/nonFixedVariableFromBothBranches.kt index 03de747369f..3b672bd96c7 100644 --- a/compiler/testData/diagnostics/tests/inference/commonSystem/nonFixedVariableFromBothBranches.kt +++ b/compiler/testData/diagnostics/tests/inference/commonSystem/nonFixedVariableFromBothBranches.kt @@ -16,5 +16,5 @@ fun foo(f: () -> R): R = f() fun test(n: Number) { val a = select(foo { JavaTest.createNumberArray() }, emptyArray()) - ..kotlin.Array<(kotlin.Number..kotlin.Number?)>?)")!>a + ..kotlin.Array?)")!>a } \ No newline at end of file diff --git a/compiler/testData/diagnostics/testsWithStdLib/coroutines/inference/elvisOperatorAgainstFlexibleType.kt b/compiler/testData/diagnostics/testsWithStdLib/coroutines/inference/elvisOperatorAgainstFlexibleType.kt new file mode 100644 index 00000000000..1c3cd886044 --- /dev/null +++ b/compiler/testData/diagnostics/testsWithStdLib/coroutines/inference/elvisOperatorAgainstFlexibleType.kt @@ -0,0 +1,20 @@ +// !LANGUAGE: +NewInference +// !DIAGNOSTICS: -UNUSED_VARIABLE -UNUSED_PARAMETER + +// FILE: Foo.java + +public class Foo { + public static void create(java.util.Map m) { return null; } +} + +// FILE: test.kt + +fun test(properties: Map, nullableProperties: Map?) { + val f1 = Foo.create(select1(properties, myEmptyMap())) + val f2 = Foo.create(nullableProperties ?: myEmptyMap()) +} + +fun myEmptyMap(): Map = TODO() + +@Suppress("INVISIBLE_REFERENCE") +fun select1(x: S, y: S): @kotlin.internal.Exact S = y \ No newline at end of file diff --git a/compiler/testData/diagnostics/testsWithStdLib/coroutines/inference/elvisOperatorAgainstFlexibleType.txt b/compiler/testData/diagnostics/testsWithStdLib/coroutines/inference/elvisOperatorAgainstFlexibleType.txt new file mode 100644 index 00000000000..35640e14375 --- /dev/null +++ b/compiler/testData/diagnostics/testsWithStdLib/coroutines/inference/elvisOperatorAgainstFlexibleType.txt @@ -0,0 +1,15 @@ +package + +public fun myEmptyMap(): kotlin.collections.Map +@kotlin.Suppress(names = {"INVISIBLE_REFERENCE"}) public fun select1(/*0*/ x: S, /*1*/ y: S): S +public fun test(/*0*/ properties: kotlin.collections.Map, /*1*/ nullableProperties: kotlin.collections.Map?): kotlin.Unit + +public open class Foo { + public constructor Foo() + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String + + // Static members + public open fun create(/*0*/ m: (kotlin.collections.MutableMap..kotlin.collections.Map?)): kotlin.Unit +} diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestWithStdLibGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestWithStdLibGenerated.java index 5d33b4ed007..c81ef793617 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestWithStdLibGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestWithStdLibGenerated.java @@ -1902,6 +1902,11 @@ public class DiagnosticsTestWithStdLibGenerated extends AbstractDiagnosticsTestW runTest("compiler/testData/diagnostics/testsWithStdLib/coroutines/inference/correctMember.kt"); } + @TestMetadata("elvisOperatorAgainstFlexibleType.kt") + public void testElvisOperatorAgainstFlexibleType() throws Exception { + runTest("compiler/testData/diagnostics/testsWithStdLib/coroutines/inference/elvisOperatorAgainstFlexibleType.kt"); + } + @TestMetadata("extensionPriority.kt") public void testExtensionPriority() throws Exception { runTest("compiler/testData/diagnostics/testsWithStdLib/coroutines/inference/extensionPriority.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsTestWithStdLibUsingJavacGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsTestWithStdLibUsingJavacGenerated.java index 69a56811d24..4a6da1c3459 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsTestWithStdLibUsingJavacGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsTestWithStdLibUsingJavacGenerated.java @@ -1902,6 +1902,11 @@ public class DiagnosticsTestWithStdLibUsingJavacGenerated extends AbstractDiagno runTest("compiler/testData/diagnostics/testsWithStdLib/coroutines/inference/correctMember.kt"); } + @TestMetadata("elvisOperatorAgainstFlexibleType.kt") + public void testElvisOperatorAgainstFlexibleType() throws Exception { + runTest("compiler/testData/diagnostics/testsWithStdLib/coroutines/inference/elvisOperatorAgainstFlexibleType.kt"); + } + @TestMetadata("extensionPriority.kt") public void testExtensionPriority() throws Exception { runTest("compiler/testData/diagnostics/testsWithStdLib/coroutines/inference/extensionPriority.kt");