From 5fc1e2e0cbcc2c0f3c6c99bb9a1a0acb7e2c4431 Mon Sep 17 00:00:00 2001 From: Dmitriy Novozhilov Date: Tue, 18 Jan 2022 10:42:05 +0300 Subject: [PATCH] [FIR] Don't create DNN types for flexible simple types ^KT-50788 --- ...nosisCompilerFirTestdataTestGenerated.java | 6 +++ ...TouchedTilContractsPhaseTestGenerated.java | 5 +++ .../inference/dnnThrowJavaCall.fir.txt | 4 ++ .../resolve/inference/dnnThrowJavaCall.kt | 37 +++++++++++++++++++ .../runners/FirDiagnosticTestGenerated.java | 6 +++ ...DiagnosticsWithLightTreeTestGenerated.java | 6 +++ .../jetbrains/kotlin/fir/types/TypeUtils.kt | 24 ++++++------ .../commonSuperTypeContravariant.fir.kt | 4 +- 8 files changed, 79 insertions(+), 13 deletions(-) create mode 100644 compiler/fir/analysis-tests/testData/resolve/inference/dnnThrowJavaCall.fir.txt create mode 100644 compiler/fir/analysis-tests/testData/resolve/inference/dnnThrowJavaCall.kt diff --git a/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosisCompilerFirTestdataTestGenerated.java b/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosisCompilerFirTestdataTestGenerated.java index 5f3728b7ecf..a584654657a 100644 --- a/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosisCompilerFirTestdataTestGenerated.java +++ b/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosisCompilerFirTestdataTestGenerated.java @@ -2799,6 +2799,12 @@ public class DiagnosisCompilerFirTestdataTestGenerated extends AbstractDiagnosis runTest("compiler/fir/analysis-tests/testData/resolve/inference/definitelyNotNullIntersectionType.kt"); } + @Test + @TestMetadata("dnnThrowJavaCall.kt") + public void testDnnThrowJavaCall() throws Exception { + runTest("compiler/fir/analysis-tests/testData/resolve/inference/dnnThrowJavaCall.kt"); + } + @Test @TestMetadata("elvisAndUnit.kt") public void testElvisAndUnit() throws Exception { diff --git a/compiler/fir/analysis-tests/legacy-fir-tests/tests-gen/org/jetbrains/kotlin/fir/LazyBodyIsNotTouchedTilContractsPhaseTestGenerated.java b/compiler/fir/analysis-tests/legacy-fir-tests/tests-gen/org/jetbrains/kotlin/fir/LazyBodyIsNotTouchedTilContractsPhaseTestGenerated.java index cb7afdc6d1d..5dfe3a3c695 100644 --- a/compiler/fir/analysis-tests/legacy-fir-tests/tests-gen/org/jetbrains/kotlin/fir/LazyBodyIsNotTouchedTilContractsPhaseTestGenerated.java +++ b/compiler/fir/analysis-tests/legacy-fir-tests/tests-gen/org/jetbrains/kotlin/fir/LazyBodyIsNotTouchedTilContractsPhaseTestGenerated.java @@ -2461,6 +2461,11 @@ public class LazyBodyIsNotTouchedTilContractsPhaseTestGenerated extends Abstract runTest("compiler/fir/analysis-tests/testData/resolve/inference/definitelyNotNullIntersectionType.kt"); } + @TestMetadata("dnnThrowJavaCall.kt") + public void testDnnThrowJavaCall() throws Exception { + runTest("compiler/fir/analysis-tests/testData/resolve/inference/dnnThrowJavaCall.kt"); + } + @TestMetadata("elvisAndUnit.kt") public void testElvisAndUnit() throws Exception { runTest("compiler/fir/analysis-tests/testData/resolve/inference/elvisAndUnit.kt"); diff --git a/compiler/fir/analysis-tests/testData/resolve/inference/dnnThrowJavaCall.fir.txt b/compiler/fir/analysis-tests/testData/resolve/inference/dnnThrowJavaCall.fir.txt new file mode 100644 index 00000000000..d9c34cb46b6 --- /dev/null +++ b/compiler/fir/analysis-tests/testData/resolve/inference/dnnThrowJavaCall.fir.txt @@ -0,0 +1,4 @@ +FILE: main.kt + public final fun getExcludedDirs(project: R|Project|, excludedProjects: R|kotlin/collections/List|): R|kotlin/collections/List| { + ^getExcludedDirs R|/project|.R|/Project.plugins|.R|/PluginContainer.findPlugin||>((Q|IdeaPlugin|).R|kotlin/jvm/java|)?.{ $subj$.R|/IdeaPlugin.excludeDirs| }?.{ $subj$.R|kotlin/collections/toList||>() } ?: R|kotlin/collections/emptyList|() + } diff --git a/compiler/fir/analysis-tests/testData/resolve/inference/dnnThrowJavaCall.kt b/compiler/fir/analysis-tests/testData/resolve/inference/dnnThrowJavaCall.kt new file mode 100644 index 00000000000..457914bbaf2 --- /dev/null +++ b/compiler/fir/analysis-tests/testData/resolve/inference/dnnThrowJavaCall.kt @@ -0,0 +1,37 @@ +// WITH_STDLIB +// FULL_JDK + +// FILE: Plugin.java +public interface Plugin {} + +// FILE: IdeaPlugin.java +import java.io.File +import java.util.Set + +public interface IdeaPlugin extends Plugin { + Set getExcludeDirs() +} + +// FILE: PluginContainer.java +import javax.annotation.Nullable + +public interface PluginContainer { + @Nullable + T findPlugin(Class type); +} + +// FILE: Project.java +public interface Project { + PluginContainer getPlugins(); + + Project getRootProject(); + + File getBuildDir(); +} + +// FILE: main.kt +import java.io.File + +fun getExcludedDirs(project: Project, excludedProjects: List): List { + return project.plugins.findPlugin(IdeaPlugin::class.java)?.excludeDirs?.toList() ?: emptyList() +} diff --git a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirDiagnosticTestGenerated.java b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirDiagnosticTestGenerated.java index eef6ec83f65..278bae9231a 100644 --- a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirDiagnosticTestGenerated.java +++ b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirDiagnosticTestGenerated.java @@ -2799,6 +2799,12 @@ public class FirDiagnosticTestGenerated extends AbstractFirDiagnosticTest { runTest("compiler/fir/analysis-tests/testData/resolve/inference/definitelyNotNullIntersectionType.kt"); } + @Test + @TestMetadata("dnnThrowJavaCall.kt") + public void testDnnThrowJavaCall() throws Exception { + runTest("compiler/fir/analysis-tests/testData/resolve/inference/dnnThrowJavaCall.kt"); + } + @Test @TestMetadata("elvisAndUnit.kt") public void testElvisAndUnit() throws Exception { diff --git a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirDiagnosticsWithLightTreeTestGenerated.java b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirDiagnosticsWithLightTreeTestGenerated.java index 5eb57949be2..7007b00a1eb 100644 --- a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirDiagnosticsWithLightTreeTestGenerated.java +++ b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirDiagnosticsWithLightTreeTestGenerated.java @@ -2799,6 +2799,12 @@ public class FirDiagnosticsWithLightTreeTestGenerated extends AbstractFirDiagnos runTest("compiler/fir/analysis-tests/testData/resolve/inference/definitelyNotNullIntersectionType.kt"); } + @Test + @TestMetadata("dnnThrowJavaCall.kt") + public void testDnnThrowJavaCall() throws Exception { + runTest("compiler/fir/analysis-tests/testData/resolve/inference/dnnThrowJavaCall.kt"); + } + @Test @TestMetadata("elvisAndUnit.kt") public void testElvisAndUnit() throws Exception { diff --git a/compiler/fir/providers/src/org/jetbrains/kotlin/fir/types/TypeUtils.kt b/compiler/fir/providers/src/org/jetbrains/kotlin/fir/types/TypeUtils.kt index f72e8a46ed1..2798497a9c2 100644 --- a/compiler/fir/providers/src/org/jetbrains/kotlin/fir/types/TypeUtils.kt +++ b/compiler/fir/providers/src/org/jetbrains/kotlin/fir/types/TypeUtils.kt @@ -53,17 +53,19 @@ fun ConeInferenceContext.intersectTypesOrNull(types: List): Cone fun TypeCheckerProviderContext.equalTypes(a: ConeKotlinType, b: ConeKotlinType): Boolean = AbstractTypeChecker.equalTypes(this, a, b) -private fun ConeTypeContext.makesSenseToBeDefinitelyNotNull(type: ConeKotlinType): Boolean = when (type) { - is ConeTypeParameterType -> type.isNullableType() - // Actually, this branch should work for type parameters as well, but it breaks some cases. See KT-40114. - // Basically, if we have `T : X..X?`, then `T <: Any` but we still have `T` != `T & Any`. - is ConeTypeVariableType, is ConeCapturedType, is ConeFlexibleType -> - !AbstractNullabilityChecker.isSubtypeOfAny( - newTypeCheckerState(errorTypesEqualToAnything = false, stubTypesEqualToAnything = false), type - ) - // For all other types `T & Any` is the same as `T` without a question mark. - // TODO: not true for flexible types. - else -> false +private fun ConeTypeContext.makesSenseToBeDefinitelyNotNull(originalType: ConeKotlinType): Boolean { + return when (val type = originalType.lowerBoundIfFlexible()) { + is ConeTypeParameterType -> type.isNullableType() + // Actually, this branch should work for type parameters as well, but it breaks some cases. See KT-40114. + // Basically, if we have `T : X..X?`, then `T <: Any` but we still have `T` != `T & Any`. + is ConeTypeVariableType, is ConeCapturedType -> { + !AbstractNullabilityChecker.isSubtypeOfAny( + newTypeCheckerState(errorTypesEqualToAnything = false, stubTypesEqualToAnything = false), type + ) + } + // For all other types `T & Any` is the same as `T` without a question mark. + else -> false + } } // TODO: leave only one of `create` and `makeConeTypeDefinitelyNotNullOrNotNull` diff --git a/compiler/testData/diagnostics/tests/inference/builderInference/stubTypes/commonSuperTypeContravariant.fir.kt b/compiler/testData/diagnostics/tests/inference/builderInference/stubTypes/commonSuperTypeContravariant.fir.kt index 59b4dc1f4a6..e1980d1a743 100644 --- a/compiler/testData/diagnostics/tests/inference/builderInference/stubTypes/commonSuperTypeContravariant.fir.kt +++ b/compiler/testData/diagnostics/tests/inference/builderInference/stubTypes/commonSuperTypeContravariant.fir.kt @@ -77,8 +77,8 @@ fun test() { select2(Test.foo(get()), getIn()) select3(get(), getIn()) select3(get(), Test.foo(getIn())) - select3(Test.foo(get()), Test.foo(getIn())) - select3(Test.foo(get()), getIn()) + select3(Test.foo(get()), Test.foo(getIn())) + select3(Test.foo(get()), getIn()) "" } }