From 71cb65c064c7e94e6733baaca54fa8013629a8ef Mon Sep 17 00:00:00 2001 From: "victor.petukhov" Date: Wed, 11 Dec 2019 16:56:12 +0300 Subject: [PATCH] NI: Add flexible Nothing to trivial constraints to filter it --- .../fir/FirOldFrontendDiagnosticsTestGenerated.java | 5 +++++ .../TrivialConstraintTypeInferenceOracle.kt | 2 +- .../constraints/errorUpperBoundConstraint.kt | 2 +- .../implicitInferenceTToFlexibleNothing.kt | 11 +++++++++++ .../kotlin/checkers/DiagnosticsTestGenerated.java | 5 +++++ .../javac/DiagnosticsUsingJavacTestGenerated.java | 5 +++++ .../jetbrains/kotlin/types/model/TypeSystemContext.kt | 3 +++ 7 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 compiler/testData/diagnostics/tests/inference/nothingType/implicitInferenceTToFlexibleNothing.kt 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 3042c9493e3..3781ab6db50 100644 --- a/compiler/fir/resolve/tests/org/jetbrains/kotlin/fir/FirOldFrontendDiagnosticsTestGenerated.java +++ b/compiler/fir/resolve/tests/org/jetbrains/kotlin/fir/FirOldFrontendDiagnosticsTestGenerated.java @@ -10560,6 +10560,11 @@ public class FirOldFrontendDiagnosticsTestGenerated extends AbstractFirOldFronte runTest("compiler/testData/diagnostics/tests/inference/nothingType/generateConstraintWithInnerNothingType.kt"); } + @TestMetadata("implicitInferenceTToFlexibleNothing.kt") + public void testImplicitInferenceTToFlexibleNothing() throws Exception { + runTest("compiler/testData/diagnostics/tests/inference/nothingType/implicitInferenceTToFlexibleNothing.kt"); + } + @TestMetadata("implicitNothingConstraintFromReturn.kt") public void testImplicitNothingConstraintFromReturn() throws Exception { runTest("compiler/testData/diagnostics/tests/inference/nothingType/implicitNothingConstraintFromReturn.kt"); diff --git a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/components/TrivialConstraintTypeInferenceOracle.kt b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/components/TrivialConstraintTypeInferenceOracle.kt index f74512ef7d7..46b0f14ac2f 100644 --- a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/components/TrivialConstraintTypeInferenceOracle.kt +++ b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/components/TrivialConstraintTypeInferenceOracle.kt @@ -40,7 +40,7 @@ class TrivialConstraintTypeInferenceOracle(context: TypeSystemInferenceExtension generatedConstraintType: KotlinTypeMarker, isSubtype: Boolean ): Boolean { - if (isSubtype && generatedConstraintType.isNothing()) return true + if (isSubtype && (generatedConstraintType.isNothing() || generatedConstraintType.isFlexibleNothing())) return true if (!isSubtype && generatedConstraintType.isNullableAny()) return true // If types from constraints that will be used to generate new constraint already contains `Nothing(?)`, diff --git a/compiler/testData/diagnostics/tests/inference/constraints/errorUpperBoundConstraint.kt b/compiler/testData/diagnostics/tests/inference/constraints/errorUpperBoundConstraint.kt index 26de6fec138..187422bdd1c 100644 --- a/compiler/testData/diagnostics/tests/inference/constraints/errorUpperBoundConstraint.kt +++ b/compiler/testData/diagnostics/tests/inference/constraints/errorUpperBoundConstraint.kt @@ -20,7 +20,7 @@ public class Foo { // FILE: test.kt fun test(e: ErrorType) { - Foo.foo { + Foo.foo { Sam.Result.create(e) } } \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/inference/nothingType/implicitInferenceTToFlexibleNothing.kt b/compiler/testData/diagnostics/tests/inference/nothingType/implicitInferenceTToFlexibleNothing.kt new file mode 100644 index 00000000000..9a8ec6c3e18 --- /dev/null +++ b/compiler/testData/diagnostics/tests/inference/nothingType/implicitInferenceTToFlexibleNothing.kt @@ -0,0 +1,11 @@ +// !DIAGNOSTICS: -UNUSED_VARIABLE -UNCHECKED_CAST -UNUSED_PARAMETER +// !LANGUAGE: +NewInference +// SKIP_TXT + +import java.util.* + +fun foo (f: () -> List): T = null as T + +fun main() { + val x = foo { Collections.emptyList() } +} diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java index 38d145c70f9..7dfed2ce74e 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java @@ -10567,6 +10567,11 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest { runTest("compiler/testData/diagnostics/tests/inference/nothingType/generateConstraintWithInnerNothingType.kt"); } + @TestMetadata("implicitInferenceTToFlexibleNothing.kt") + public void testImplicitInferenceTToFlexibleNothing() throws Exception { + runTest("compiler/testData/diagnostics/tests/inference/nothingType/implicitInferenceTToFlexibleNothing.kt"); + } + @TestMetadata("implicitNothingConstraintFromReturn.kt") public void testImplicitNothingConstraintFromReturn() throws Exception { runTest("compiler/testData/diagnostics/tests/inference/nothingType/implicitNothingConstraintFromReturn.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java index f54f2f08b49..390f3296b8c 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java @@ -10562,6 +10562,11 @@ public class DiagnosticsUsingJavacTestGenerated extends AbstractDiagnosticsUsing runTest("compiler/testData/diagnostics/tests/inference/nothingType/generateConstraintWithInnerNothingType.kt"); } + @TestMetadata("implicitInferenceTToFlexibleNothing.kt") + public void testImplicitInferenceTToFlexibleNothing() throws Exception { + runTest("compiler/testData/diagnostics/tests/inference/nothingType/implicitInferenceTToFlexibleNothing.kt"); + } + @TestMetadata("implicitNothingConstraintFromReturn.kt") public void testImplicitNothingConstraintFromReturn() throws Exception { runTest("compiler/testData/diagnostics/tests/inference/nothingType/implicitNothingConstraintFromReturn.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 9af3760bcb9..aaca404a786 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 @@ -248,6 +248,9 @@ interface TypeSystemContext : TypeSystemOptimizationContext { fun KotlinTypeMarker.isNullableAny() = this.typeConstructor().isAnyConstructor() && this.isNullableType() fun KotlinTypeMarker.isNothing() = this.typeConstructor().isNothingConstructor() && !this.isNullableType() + fun KotlinTypeMarker.isFlexibleNothing() = + this is FlexibleTypeMarker && lowerBound().isNothing() && upperBound().isNullableNothing() + fun KotlinTypeMarker.isNullableNothing() = this.typeConstructor().isNothingConstructor() && this.isNullableType() fun SimpleTypeMarker.isClassType(): Boolean = typeConstructor().isClassTypeConstructor()