From a3f0e429e38e0ad5708b526a24eb0fae87939c43 Mon Sep 17 00:00:00 2001 From: Mikhail Glukhikh Date: Thu, 2 Mar 2023 10:25:21 +0100 Subject: [PATCH] K2: report empty intersection warnings from completion properly --- ...LightTreeBlackBoxCodegenTestGenerated.java | 6 ++ .../FirPsiBlackBoxCodegenTestGenerated.java | 6 ++ ...rCallCompletionResultsWriterTransformer.kt | 3 +- .../box/fir/emptyIntersectionWarning.kt | 13 +++ .../coercionToUnit/afterBareReturn.fir.kt | 41 --------- .../coercionToUnit/afterBareReturn.kt | 1 + .../exclExclAsCall.fir.kt | 2 +- ...IntersectUpperBoundWithExpectedType.fir.kt | 2 +- .../tests/typeParameters/kt42472.fir.kt | 2 +- .../kt46186withEmptyIntersections.fir.kt | 84 ------------------- .../kt46186withEmptyIntersections.kt | 1 + .../IrBlackBoxCodegenTestGenerated.java | 6 ++ 12 files changed, 38 insertions(+), 129 deletions(-) create mode 100644 compiler/testData/codegen/box/fir/emptyIntersectionWarning.kt delete mode 100644 compiler/testData/diagnostics/tests/inference/coercionToUnit/afterBareReturn.fir.kt delete mode 100644 compiler/testData/diagnostics/tests/typeParameters/kt46186withEmptyIntersections.fir.kt diff --git a/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirLightTreeBlackBoxCodegenTestGenerated.java b/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirLightTreeBlackBoxCodegenTestGenerated.java index 3f7b4cf68ce..6924a833573 100644 --- a/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirLightTreeBlackBoxCodegenTestGenerated.java +++ b/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirLightTreeBlackBoxCodegenTestGenerated.java @@ -18325,6 +18325,12 @@ public class FirLightTreeBlackBoxCodegenTestGenerated extends AbstractFirLightTr runTest("compiler/testData/codegen/box/fir/differentSinceKotlin.kt"); } + @Test + @TestMetadata("emptyIntersectionWarning.kt") + public void testEmptyIntersectionWarning() throws Exception { + runTest("compiler/testData/codegen/box/fir/emptyIntersectionWarning.kt"); + } + @Test @TestMetadata("ExtensionAlias.kt") public void testExtensionAlias() throws Exception { diff --git a/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirPsiBlackBoxCodegenTestGenerated.java b/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirPsiBlackBoxCodegenTestGenerated.java index 427d7693a45..fa9ed5c524c 100644 --- a/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirPsiBlackBoxCodegenTestGenerated.java +++ b/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirPsiBlackBoxCodegenTestGenerated.java @@ -18325,6 +18325,12 @@ public class FirPsiBlackBoxCodegenTestGenerated extends AbstractFirPsiBlackBoxCo runTest("compiler/testData/codegen/box/fir/differentSinceKotlin.kt"); } + @Test + @TestMetadata("emptyIntersectionWarning.kt") + public void testEmptyIntersectionWarning() throws Exception { + runTest("compiler/testData/codegen/box/fir/emptyIntersectionWarning.kt"); + } + @Test @TestMetadata("ExtensionAlias.kt") public void testExtensionAlias() throws Exception { diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/FirCallCompletionResultsWriterTransformer.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/FirCallCompletionResultsWriterTransformer.kt index 5ff0da98482..f3fa271ca34 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/FirCallCompletionResultsWriterTransformer.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/FirCallCompletionResultsWriterTransformer.kt @@ -802,8 +802,9 @@ class FirCallCompletionResultsWriterTransformer( return varargArgumentsExpression } + // TODO: report warning with a checker and return true here only in case of errors private fun FirNamedReferenceWithCandidate.hasAdditionalResolutionErrors(): Boolean = - candidate.system.errors.any { it is InferredEmptyIntersection && it.kind.isDefinitelyEmpty } + candidate.system.errors.any { it is InferredEmptyIntersection } private fun FirNamedReferenceWithCandidate.toResolvedReference(): FirNamedReference { val errorDiagnostic = when { diff --git a/compiler/testData/codegen/box/fir/emptyIntersectionWarning.kt b/compiler/testData/codegen/box/fir/emptyIntersectionWarning.kt new file mode 100644 index 00000000000..e79215c9e3d --- /dev/null +++ b/compiler/testData/codegen/box/fir/emptyIntersectionWarning.kt @@ -0,0 +1,13 @@ +// TARGET_BACKEND: JVM_IR +// IGNORE_BACKEND_K1: JVM_IR + +interface I +class View1 + +fun findViewById1(): T? = null +fun test1(): I? = findViewById1() + +fun box(): String { + test1() + return "OK" +} diff --git a/compiler/testData/diagnostics/tests/inference/coercionToUnit/afterBareReturn.fir.kt b/compiler/testData/diagnostics/tests/inference/coercionToUnit/afterBareReturn.fir.kt deleted file mode 100644 index 1ceca4d4ca9..00000000000 --- a/compiler/testData/diagnostics/tests/inference/coercionToUnit/afterBareReturn.fir.kt +++ /dev/null @@ -1,41 +0,0 @@ -interface I -open class C - -fun completed(): String = "..." - -fun incomplete(): T = null!! - -fun incompatibleI(): T = null!! -fun incompatibleC(): T = null!! - -val p = false - -fun expectUnit(x: Unit) = x - -fun test1() = run { - if (p) return@run - completed() // ok, not returned -} - -fun test2() = run { - if (p) return@run - incomplete() // ? either uninferred T or Unit -} - -fun test3() = run { - if (p) return@run - incompatibleI() // ? either uninferred T or error (Unit run { - if (p) return@run - incompatibleC() // ? either uninferred T or error (Unit emptyNullableListOfA(): List? = null fun testExclExcl() { doList(emptyNullableListOfA()!!) //should be an error here - val l: List = ")!>id(emptyNullableListOfA()!!) + val l: List = ")!>id(emptyNullableListOfA()!!) doList(strangeNullableList { doInt(it) }!!) //lambda should be analyzed (at completion phase) } diff --git a/compiler/testData/diagnostics/tests/typeParameters/dontIntersectUpperBoundWithExpectedType.fir.kt b/compiler/testData/diagnostics/tests/typeParameters/dontIntersectUpperBoundWithExpectedType.fir.kt index dee9485f70f..d28b0ce367d 100644 --- a/compiler/testData/diagnostics/tests/typeParameters/dontIntersectUpperBoundWithExpectedType.fir.kt +++ b/compiler/testData/diagnostics/tests/typeParameters/dontIntersectUpperBoundWithExpectedType.fir.kt @@ -14,4 +14,4 @@ fun main() { fun wtf(): T = TODO() -val bar: Int = wtf() // happily compiles +val bar: Int = wtf() // happily compiles diff --git a/compiler/testData/diagnostics/tests/typeParameters/kt42472.fir.kt b/compiler/testData/diagnostics/tests/typeParameters/kt42472.fir.kt index d57e1595b87..048a1e7f0af 100644 --- a/compiler/testData/diagnostics/tests/typeParameters/kt42472.fir.kt +++ b/compiler/testData/diagnostics/tests/typeParameters/kt42472.fir.kt @@ -7,6 +7,6 @@ fun interface ReadOnlyProperty { } class Problem { - val variable: Int by delegate() // delegate returns `ReadOnlyProperty` + val variable: Int by delegate() // delegate returns `ReadOnlyProperty` fun delegate() = null as ReadOnlyProperty } diff --git a/compiler/testData/diagnostics/tests/typeParameters/kt46186withEmptyIntersections.fir.kt b/compiler/testData/diagnostics/tests/typeParameters/kt46186withEmptyIntersections.fir.kt deleted file mode 100644 index d2268e78b1c..00000000000 --- a/compiler/testData/diagnostics/tests/typeParameters/kt46186withEmptyIntersections.fir.kt +++ /dev/null @@ -1,84 +0,0 @@ -// !DIAGNOSTICS: -FINAL_UPPER_BOUND -CAST_NEVER_SUCCEEDS -// !LANGUAGE: +AllowEmptyIntersectionsInResultTypeResolver - -interface I - -class View1 -open class View2 -interface View3 -abstract class View4 -interface View5 - -fun findViewById1(): T = null as T -fun test1(): I = findViewById1() - -fun findViewById2(): T = null as T -fun test2(): I = findViewById2() - -inline fun findViewById3(): T = null as T -fun test3(): I = findViewById3() - -inline fun findViewById4(): T = null as T -fun test4(): I = findViewById4() - -fun findViewById5(): T = null as T -fun test5(): I = findViewById5() - -inline fun findViewById6(): T = null as T -fun test6(): I = findViewById6() - -fun findViewById7(): T = null as T -fun test7(): I = findViewById7() - -inline fun findViewById8(): T = null as T -fun test8(): I = findViewById8() - -fun findViewById9(): T where T: View3, T: View5 = null as T -fun test9(): I = findViewById9() - -inline fun findViewById10(): T where T: View3, T: View5 = null as T -fun test10(): I = findViewById10() - -fun findViewById11(): T = null as T -fun test11(): View4 = findViewById11() - -object Obj { - fun findViewById1(): T = null as T - fun test1(): View1 = findViewById1() - - fun findViewById2(): T = null as T - fun test2(): View2 = findViewById2() - - inline fun findViewById3(): T = null as T - fun test3(): View1 = findViewById3() - - inline fun findViewById4(): T = null as T - fun test4(): View2 = findViewById4() - - fun findViewById5(): T = null as T - fun test5(): View3 = findViewById5() - - inline fun findViewById6(): T = null as T - fun test6(): View3 = findViewById6() - - fun findViewById7(): T = null as T - fun test7(): View4 = findViewById7() - - inline fun findViewById8(): T = null as T - fun test8(): View4 = findViewById8() - - fun findViewById9(): T where T: View3, T: View5 = null as T - fun test9(): View1 = findViewById9() - - inline fun findViewById10(): T where T: View3, T: View5 = null as T - fun test10(): View1 = findViewById10() - - fun findViewById11(): T = null as T - fun test11(): View4 = findViewById11() -} - -interface A -open class B { - fun f(): T where T : A, T : B = null as T - fun g(): A = f() -} diff --git a/compiler/testData/diagnostics/tests/typeParameters/kt46186withEmptyIntersections.kt b/compiler/testData/diagnostics/tests/typeParameters/kt46186withEmptyIntersections.kt index 837a6af8d37..9e18f1fd557 100644 --- a/compiler/testData/diagnostics/tests/typeParameters/kt46186withEmptyIntersections.kt +++ b/compiler/testData/diagnostics/tests/typeParameters/kt46186withEmptyIntersections.kt @@ -1,3 +1,4 @@ +// FIR_IDENTICAL // !DIAGNOSTICS: -FINAL_UPPER_BOUND -CAST_NEVER_SUCCEEDS // !LANGUAGE: +AllowEmptyIntersectionsInResultTypeResolver diff --git a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenTestGenerated.java b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenTestGenerated.java index 85f9e5f14bb..a9cb5d3ee07 100644 --- a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenTestGenerated.java +++ b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenTestGenerated.java @@ -18325,6 +18325,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes runTest("compiler/testData/codegen/box/fir/differentSinceKotlin.kt"); } + @Test + @TestMetadata("emptyIntersectionWarning.kt") + public void testEmptyIntersectionWarning() throws Exception { + runTest("compiler/testData/codegen/box/fir/emptyIntersectionWarning.kt"); + } + @Test @TestMetadata("ExtensionAlias.kt") public void testExtensionAlias() throws Exception {