From 748a326104eb79c1cb8fda5e5d3c57c81f38bfb3 Mon Sep 17 00:00:00 2001 From: Victor Petukhov Date: Tue, 18 Feb 2020 19:09:46 +0300 Subject: [PATCH] NI: Discard definitely not-null types inside invariant positions during inference process ^KT-36816 Fixed --- ...irOldFrontendDiagnosticsTestGenerated.java | 5 +++ .../kotlin/types/TypeApproximator.kt | 5 ++- ...NotNullInInvPositionDuringInference.fir.kt | 24 ++++++++++++++ ...hDefNotNullInInvPositionDuringInference.kt | 24 ++++++++++++++ ...DefNotNullInInvPositionDuringInference.txt | 33 +++++++++++++++++++ ...xibilityInCommonSuperTypeCalculation.ni.kt | 4 +-- .../checkers/DiagnosticsTestGenerated.java | 5 +++ .../DiagnosticsUsingJavacTestGenerated.java | 5 +++ 8 files changed, 102 insertions(+), 3 deletions(-) create mode 100644 compiler/testData/diagnostics/tests/inference/constraints/approximationWithDefNotNullInInvPositionDuringInference.fir.kt create mode 100644 compiler/testData/diagnostics/tests/inference/constraints/approximationWithDefNotNullInInvPositionDuringInference.kt create mode 100644 compiler/testData/diagnostics/tests/inference/constraints/approximationWithDefNotNullInInvPositionDuringInference.txt diff --git a/compiler/fir/resolve/tests/org/jetbrains/kotlin/fir/FirOldFrontendDiagnosticsTestGenerated.java b/compiler/fir/resolve/tests/org/jetbrains/kotlin/fir/FirOldFrontendDiagnosticsTestGenerated.java index c0f56f30534..1cdaba2c5e9 100644 --- a/compiler/fir/resolve/tests/org/jetbrains/kotlin/fir/FirOldFrontendDiagnosticsTestGenerated.java +++ b/compiler/fir/resolve/tests/org/jetbrains/kotlin/fir/FirOldFrontendDiagnosticsTestGenerated.java @@ -10695,6 +10695,11 @@ public class FirOldFrontendDiagnosticsTestGenerated extends AbstractFirOldFronte KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/diagnostics/tests/inference/constraints"), Pattern.compile("^(.+)\\.kt$"), Pattern.compile("^(.+)\\.fir\\.kts?$"), true); } + @TestMetadata("approximationWithDefNotNullInInvPositionDuringInference.kt") + public void testApproximationWithDefNotNullInInvPositionDuringInference() throws Exception { + runTest("compiler/testData/diagnostics/tests/inference/constraints/approximationWithDefNotNullInInvPositionDuringInference.kt"); + } + @TestMetadata("constraintFromVariantTypeWithNestedProjection.kt") public void testConstraintFromVariantTypeWithNestedProjection() throws Exception { runTest("compiler/testData/diagnostics/tests/inference/constraints/constraintFromVariantTypeWithNestedProjection.kt"); diff --git a/compiler/resolution/src/org/jetbrains/kotlin/types/TypeApproximator.kt b/compiler/resolution/src/org/jetbrains/kotlin/types/TypeApproximator.kt index 872f35a29ee..44b048875a6 100644 --- a/compiler/resolution/src/org/jetbrains/kotlin/types/TypeApproximator.kt +++ b/compiler/resolution/src/org/jetbrains/kotlin/types/TypeApproximator.kt @@ -83,7 +83,10 @@ open class TypeApproximatorConfiguration { override val typeVariable: (TypeVariableTypeConstructorMarker) -> Boolean get() = { true } } - object IncorporationConfiguration : TypeApproximatorConfiguration.AbstractCapturedTypesApproximation(FOR_INCORPORATION) + object IncorporationConfiguration : TypeApproximatorConfiguration.AbstractCapturedTypesApproximation(FOR_INCORPORATION) { + override val definitelyNotNullTypeInInvariantPosition: Boolean get() = false + } + object SubtypeCapturedTypesApproximation : TypeApproximatorConfiguration.AbstractCapturedTypesApproximation(FOR_SUBTYPING) object CapturedAndIntegerLiteralsTypesApproximation : TypeApproximatorConfiguration.AbstractCapturedTypesApproximation(FROM_EXPRESSION) { override val integerLiteralType: Boolean get() = true diff --git a/compiler/testData/diagnostics/tests/inference/constraints/approximationWithDefNotNullInInvPositionDuringInference.fir.kt b/compiler/testData/diagnostics/tests/inference/constraints/approximationWithDefNotNullInInvPositionDuringInference.fir.kt new file mode 100644 index 00000000000..25dd9342868 --- /dev/null +++ b/compiler/testData/diagnostics/tests/inference/constraints/approximationWithDefNotNullInInvPositionDuringInference.fir.kt @@ -0,0 +1,24 @@ +// !LANGUAGE: +NewInference +// !DIAGNOSTICS: -UNUSED_PARAMETER +// Issues: KT-36816 + +interface Parent + +class Foo(x: K?): Parent {} +class Bar(x: T): Parent {} + +fun select(vararg x: R) = x[0] + +fun main(x: S) { + select(Foo(x), Bar(x)) +} + + + +inline fun test(transform: () -> R) {} + +class Inv(x: T?) {} + +fun foo(x: K) { + test { Inv(x) } +} diff --git a/compiler/testData/diagnostics/tests/inference/constraints/approximationWithDefNotNullInInvPositionDuringInference.kt b/compiler/testData/diagnostics/tests/inference/constraints/approximationWithDefNotNullInInvPositionDuringInference.kt new file mode 100644 index 00000000000..f8a28e8c942 --- /dev/null +++ b/compiler/testData/diagnostics/tests/inference/constraints/approximationWithDefNotNullInInvPositionDuringInference.kt @@ -0,0 +1,24 @@ +// !LANGUAGE: +NewInference +// !DIAGNOSTICS: -UNUSED_PARAMETER +// Issues: KT-36816 + +interface Parent + +class Foo(x: K?): Parent {} +class Bar(x: T): Parent {} + +fun select(vararg x: R) = x[0] + +fun main(x: S) { + ")!>select(Foo(x), Bar(x)) +} + + + +inline fun test(transform: () -> R) {} + +class Inv(x: T?) {} + +fun foo(x: K) { + test { ")!>Inv(x) } +} diff --git a/compiler/testData/diagnostics/tests/inference/constraints/approximationWithDefNotNullInInvPositionDuringInference.txt b/compiler/testData/diagnostics/tests/inference/constraints/approximationWithDefNotNullInInvPositionDuringInference.txt new file mode 100644 index 00000000000..4341d6ebf3e --- /dev/null +++ b/compiler/testData/diagnostics/tests/inference/constraints/approximationWithDefNotNullInInvPositionDuringInference.txt @@ -0,0 +1,33 @@ +package + +public fun foo(/*0*/ x: K): kotlin.Unit +public fun main(/*0*/ x: S): kotlin.Unit +public fun select(/*0*/ vararg x: R /*kotlin.Array*/): R +public inline fun test(/*0*/ transform: () -> R): kotlin.Unit + +public final class Bar : Parent { + public constructor Bar(/*0*/ x: T) + 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 +} + +public final class Foo : Parent { + public constructor Foo(/*0*/ x: K?) + 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 +} + +public final class Inv { + public constructor Inv(/*0*/ x: T?) + 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 +} + +public interface Parent { + 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/upperBounds/flexibilityInCommonSuperTypeCalculation.ni.kt b/compiler/testData/diagnostics/tests/inference/upperBounds/flexibilityInCommonSuperTypeCalculation.ni.kt index 2ea53605e6f..9d3943e6ef5 100644 --- a/compiler/testData/diagnostics/tests/inference/upperBounds/flexibilityInCommonSuperTypeCalculation.ni.kt +++ b/compiler/testData/diagnostics/tests/inference/upperBounds/flexibilityInCommonSuperTypeCalculation.ni.kt @@ -169,8 +169,8 @@ fun case_11(y: T) { x3 y - ")!>result_1 - ")!>result_2 + ")!>result_1 + ")!>result_2 ")!>result_3 ")!>result_4 ")!>result_5 diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java index 980944534d9..9ecfe94603e 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java @@ -10702,6 +10702,11 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest { KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/diagnostics/tests/inference/constraints"), Pattern.compile("^(.*)\\.kts?$"), Pattern.compile("^(.+)\\.fir\\.kts?$"), true); } + @TestMetadata("approximationWithDefNotNullInInvPositionDuringInference.kt") + public void testApproximationWithDefNotNullInInvPositionDuringInference() throws Exception { + runTest("compiler/testData/diagnostics/tests/inference/constraints/approximationWithDefNotNullInInvPositionDuringInference.kt"); + } + @TestMetadata("constraintFromVariantTypeWithNestedProjection.kt") public void testConstraintFromVariantTypeWithNestedProjection() throws Exception { runTest("compiler/testData/diagnostics/tests/inference/constraints/constraintFromVariantTypeWithNestedProjection.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java index d057b5110a8..d33f5a1ae89 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java @@ -10697,6 +10697,11 @@ public class DiagnosticsUsingJavacTestGenerated extends AbstractDiagnosticsUsing KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/diagnostics/tests/inference/constraints"), Pattern.compile("^(.+)\\.kt$"), Pattern.compile("^(.+)\\.fir\\.kts?$"), true); } + @TestMetadata("approximationWithDefNotNullInInvPositionDuringInference.kt") + public void testApproximationWithDefNotNullInInvPositionDuringInference() throws Exception { + runTest("compiler/testData/diagnostics/tests/inference/constraints/approximationWithDefNotNullInInvPositionDuringInference.kt"); + } + @TestMetadata("constraintFromVariantTypeWithNestedProjection.kt") public void testConstraintFromVariantTypeWithNestedProjection() throws Exception { runTest("compiler/testData/diagnostics/tests/inference/constraints/constraintFromVariantTypeWithNestedProjection.kt");