From d804e73c22faec12746ccce8a0543b7cff7f45e4 Mon Sep 17 00:00:00 2001 From: Victor Petukhov Date: Sat, 10 Apr 2021 13:04:51 +0300 Subject: [PATCH] Avoid subtype checking between type variable of self type and captured star projection only for invariant positions Subtyping with non-invariant positions may produce useful constraints ^KT-46001 Fixed --- .../FirOldFrontendDiagnosticsTestGenerated.java | 12 ++++++++++++ ...napplicableCandidateWithNotSatisfyingSelfType.kt | 13 +++++++++++++ ...applicableCandidateWithNotSatisfyingSelfType.txt | 11 +++++++++++ .../inference/violatingUpperBoundForSelfType.fir.kt | 9 +++++++++ .../inference/violatingUpperBoundForSelfType.kt | 9 +++++++++ .../inference/violatingUpperBoundForSelfType.txt | 4 ++++ .../test/runners/DiagnosticTestGenerated.java | 12 ++++++++++++ .../jetbrains/kotlin/types/AbstractTypeChecker.kt | 12 +++++++----- 8 files changed, 77 insertions(+), 5 deletions(-) create mode 100644 compiler/testData/diagnostics/tests/inference/discardInapplicableCandidateWithNotSatisfyingSelfType.kt create mode 100644 compiler/testData/diagnostics/tests/inference/discardInapplicableCandidateWithNotSatisfyingSelfType.txt create mode 100644 compiler/testData/diagnostics/tests/inference/violatingUpperBoundForSelfType.fir.kt create mode 100644 compiler/testData/diagnostics/tests/inference/violatingUpperBoundForSelfType.kt create mode 100644 compiler/testData/diagnostics/tests/inference/violatingUpperBoundForSelfType.txt diff --git a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsTestGenerated.java b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsTestGenerated.java index 4f277f7ced1..cfb56f00b0c 100644 --- a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsTestGenerated.java +++ b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsTestGenerated.java @@ -11913,6 +11913,12 @@ public class FirOldFrontendDiagnosticsTestGenerated extends AbstractFirDiagnosti runTest("compiler/testData/diagnostics/tests/inference/dependantOnVarianceNullable.kt"); } + @Test + @TestMetadata("discardInapplicableCandidateWithNotSatisfyingSelfType.kt") + public void testDiscardInapplicableCandidateWithNotSatisfyingSelfType() throws Exception { + runTest("compiler/testData/diagnostics/tests/inference/discardInapplicableCandidateWithNotSatisfyingSelfType.kt"); + } + @Test @TestMetadata("equalitySubstitutionInsideNonInvariantType.kt") public void testEqualitySubstitutionInsideNonInvariantType() throws Exception { @@ -12387,6 +12393,12 @@ public class FirOldFrontendDiagnosticsTestGenerated extends AbstractFirDiagnosti runTest("compiler/testData/diagnostics/tests/inference/useFunctionLiteralsToInferType.kt"); } + @Test + @TestMetadata("violatingUpperBoundForSelfType.kt") + public void testViolatingUpperBoundForSelfType() throws Exception { + runTest("compiler/testData/diagnostics/tests/inference/violatingUpperBoundForSelfType.kt"); + } + @Nested @TestMetadata("compiler/testData/diagnostics/tests/inference/builderInference") @TestDataPath("$PROJECT_ROOT") diff --git a/compiler/testData/diagnostics/tests/inference/discardInapplicableCandidateWithNotSatisfyingSelfType.kt b/compiler/testData/diagnostics/tests/inference/discardInapplicableCandidateWithNotSatisfyingSelfType.kt new file mode 100644 index 00000000000..252306b9d32 --- /dev/null +++ b/compiler/testData/diagnostics/tests/inference/discardInapplicableCandidateWithNotSatisfyingSelfType.kt @@ -0,0 +1,13 @@ +// FIR_IDENTICAL +// WITH_RUNTIME + +interface WithChildren + +fun > WithChildren>.test() { + withDescendants() +} + +fun > T.withDescendants() {} + +@JvmName("foo") +fun WithChildren<*>.withDescendants() {} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/inference/discardInapplicableCandidateWithNotSatisfyingSelfType.txt b/compiler/testData/diagnostics/tests/inference/discardInapplicableCandidateWithNotSatisfyingSelfType.txt new file mode 100644 index 00000000000..035b81cf7c7 --- /dev/null +++ b/compiler/testData/diagnostics/tests/inference/discardInapplicableCandidateWithNotSatisfyingSelfType.txt @@ -0,0 +1,11 @@ +package + +public fun > WithChildren>.test(): kotlin.Unit +public fun > T.withDescendants(): kotlin.Unit +@kotlin.jvm.JvmName(name = "foo") public fun WithChildren<*>.withDescendants(): kotlin.Unit + +public interface WithChildren { + 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 +} diff --git a/compiler/testData/diagnostics/tests/inference/violatingUpperBoundForSelfType.fir.kt b/compiler/testData/diagnostics/tests/inference/violatingUpperBoundForSelfType.fir.kt new file mode 100644 index 00000000000..3b5cad23443 --- /dev/null +++ b/compiler/testData/diagnostics/tests/inference/violatingUpperBoundForSelfType.fir.kt @@ -0,0 +1,9 @@ +// WITH_RUNTIME +// !DIAGNOSTICS: -UNUSED_PARAMETER -CAST_NEVER_SUCCEEDS + +fun > createMap(enumClass: Class) {} + +fun reproduce() { + val enumClass: Class> = "any" as Class> + createMap(enumClass) +} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/inference/violatingUpperBoundForSelfType.kt b/compiler/testData/diagnostics/tests/inference/violatingUpperBoundForSelfType.kt new file mode 100644 index 00000000000..f1b000219af --- /dev/null +++ b/compiler/testData/diagnostics/tests/inference/violatingUpperBoundForSelfType.kt @@ -0,0 +1,9 @@ +// WITH_RUNTIME +// !DIAGNOSTICS: -UNUSED_PARAMETER -CAST_NEVER_SUCCEEDS + +fun > createMap(enumClass: Class) {} + +fun reproduce() { + val enumClass: Class> = "any" as Class> + createMap(enumClass) +} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/inference/violatingUpperBoundForSelfType.txt b/compiler/testData/diagnostics/tests/inference/violatingUpperBoundForSelfType.txt new file mode 100644 index 00000000000..fb1669a9433 --- /dev/null +++ b/compiler/testData/diagnostics/tests/inference/violatingUpperBoundForSelfType.txt @@ -0,0 +1,4 @@ +package + +public fun > createMap(/*0*/ enumClass: java.lang.Class): kotlin.Unit +public fun reproduce(): kotlin.Unit diff --git a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/DiagnosticTestGenerated.java b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/DiagnosticTestGenerated.java index 9143d6fc5ed..6b0b2b28bb0 100644 --- a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/DiagnosticTestGenerated.java +++ b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/DiagnosticTestGenerated.java @@ -11919,6 +11919,12 @@ public class DiagnosticTestGenerated extends AbstractDiagnosticTest { runTest("compiler/testData/diagnostics/tests/inference/dependantOnVarianceNullable.kt"); } + @Test + @TestMetadata("discardInapplicableCandidateWithNotSatisfyingSelfType.kt") + public void testDiscardInapplicableCandidateWithNotSatisfyingSelfType() throws Exception { + runTest("compiler/testData/diagnostics/tests/inference/discardInapplicableCandidateWithNotSatisfyingSelfType.kt"); + } + @Test @TestMetadata("equalitySubstitutionInsideNonInvariantType.kt") public void testEqualitySubstitutionInsideNonInvariantType() throws Exception { @@ -12393,6 +12399,12 @@ public class DiagnosticTestGenerated extends AbstractDiagnosticTest { runTest("compiler/testData/diagnostics/tests/inference/useFunctionLiteralsToInferType.kt"); } + @Test + @TestMetadata("violatingUpperBoundForSelfType.kt") + public void testViolatingUpperBoundForSelfType() throws Exception { + runTest("compiler/testData/diagnostics/tests/inference/violatingUpperBoundForSelfType.kt"); + } + @Nested @TestMetadata("compiler/testData/diagnostics/tests/inference/builderInference") @TestDataPath("$PROJECT_ROOT") diff --git a/core/compiler.common/src/org/jetbrains/kotlin/types/AbstractTypeChecker.kt b/core/compiler.common/src/org/jetbrains/kotlin/types/AbstractTypeChecker.kt index e3248f931b1..18edeb9aa18 100644 --- a/core/compiler.common/src/org/jetbrains/kotlin/types/AbstractTypeChecker.kt +++ b/core/compiler.common/src/org/jetbrains/kotlin/types/AbstractTypeChecker.kt @@ -406,9 +406,13 @@ object AbstractTypeChecker { it.getType() } - val isTypeVariableAgainstStarProjectionForSelfType = + val variance = effectiveVariance(superTypeConstructor.getParameter(index).getVariance(), superProjection.getVariance()) + ?: return isErrorTypeEqualsToAnything // todo exception? + + val isTypeVariableAgainstStarProjectionForSelfType = if (variance == TypeVariance.INV) { isTypeVariableAgainstStarProjectionForSelfType(subArgumentType, superArgumentType, superTypeConstructor) || isTypeVariableAgainstStarProjectionForSelfType(superArgumentType, subArgumentType, superTypeConstructor) + } else false /* * We don't check subtyping between types like CapturedType(*) and TypeVariable(E) if the corresponding type parameter forms self type, for instance, Enum>. @@ -417,10 +421,8 @@ object AbstractTypeChecker { * Instead this type check we move on self-type level anyway: checking CapturedType(out Enum<*>) against TypeVariable(E). * This subtyping can already be successful and not add unwanted constraints in the type inference context. */ - if (isTypeVariableAgainstStarProjectionForSelfType) continue - - val variance = effectiveVariance(superTypeConstructor.getParameter(index).getVariance(), superProjection.getVariance()) - ?: return isErrorTypeEqualsToAnything // todo exception? + if (isTypeVariableAgainstStarProjectionForSelfType) + continue val correctArgument = runWithArgumentsSettings(subArgumentType) { when (variance) {