From c5d783596d2dcfbf43adf310d592135ed866bbf4 Mon Sep 17 00:00:00 2001 From: Victor Petukhov Date: Tue, 27 Jul 2021 13:24:54 +0300 Subject: [PATCH] Check type parameter bounds by java nullability annotations not only in basic resolution context ^KT-47920 Fixed --- .../resolve/jvm/checkers/JavaNullabilityChecker.kt | 9 ++++++--- .../misc/classTypeParameterBoundWithWarnings.fir.kt | 2 +- .../misc/classTypeParameterBoundWithWarnings.kt | 2 +- .../foreignAnnotationsTests/tests/kt47920.kt | 11 +++++++++++ .../ForeignAnnotationsCompiledJavaTestGenerated.java | 6 ++++++ ...sCompiledJavaWithPsiClassReadingTestGenerated.java | 6 ++++++ .../ForeignAnnotationsSourceJavaTestGenerated.java | 6 ++++++ 7 files changed, 37 insertions(+), 5 deletions(-) create mode 100644 compiler/testData/diagnostics/foreignAnnotationsTests/tests/kt47920.kt diff --git a/compiler/frontend.java/src/org/jetbrains/kotlin/resolve/jvm/checkers/JavaNullabilityChecker.kt b/compiler/frontend.java/src/org/jetbrains/kotlin/resolve/jvm/checkers/JavaNullabilityChecker.kt index 3159c9c5551..c85a86a2512 100644 --- a/compiler/frontend.java/src/org/jetbrains/kotlin/resolve/jvm/checkers/JavaNullabilityChecker.kt +++ b/compiler/frontend.java/src/org/jetbrains/kotlin/resolve/jvm/checkers/JavaNullabilityChecker.kt @@ -128,13 +128,16 @@ class JavaNullabilityChecker(val upperBoundChecker: UpperBoundChecker) : Additio upperBoundChecker.checkBoundsOfExpandedTypeAlias(expressionType.expandedType, expression, c.trace) } - if (c !is BasicCallResolutionContext || upperBoundChecker !is WarningAwareUpperBoundChecker) return + if (upperBoundChecker !is WarningAwareUpperBoundChecker) return - val resolvedCall = c.trace.bindingContext[BindingContext.RESOLVED_CALL, c.call] ?: return + val call = (c as? BasicCallResolutionContext)?.call + ?: c.trace.bindingContext[BindingContext.CALL, (expression as? KtCallExpression)?.calleeExpression] + ?: return + val resolvedCall = c.trace.bindingContext[BindingContext.RESOLVED_CALL, call] ?: return for ((typeParameter, typeArgument) in resolvedCall.typeArguments) { // continue if we don't have explicit type arguments - val typeReference = c.call.typeArguments.getOrNull(typeParameter.index)?.typeReference ?: continue + val typeReference = call.typeArguments.getOrNull(typeParameter.index)?.typeReference ?: continue upperBoundChecker.checkBounds( typeReference, typeArgument, typeParameter, TypeSubstitutor.create(typeArgument), c.trace, withOnlyCheckForWarning = true diff --git a/compiler/testData/diagnostics/foreignAnnotationsTests/java8Tests/misc/classTypeParameterBoundWithWarnings.fir.kt b/compiler/testData/diagnostics/foreignAnnotationsTests/java8Tests/misc/classTypeParameterBoundWithWarnings.fir.kt index 42e8d492fce..15821df9fb0 100644 --- a/compiler/testData/diagnostics/foreignAnnotationsTests/java8Tests/misc/classTypeParameterBoundWithWarnings.fir.kt +++ b/compiler/testData/diagnostics/foreignAnnotationsTests/java8Tests/misc/classTypeParameterBoundWithWarnings.fir.kt @@ -14,7 +14,7 @@ public class ClassTypeParameterBoundWithWarnings { // FILE: main.kt fun main(x: ClassTypeParameterBoundWithWarnings<String?>, y: ClassTypeParameterBoundWithWarnings, a: String?, b: String) { - val x2 = ClassTypeParameterBoundWithWarnings() + val x2 = ClassTypeParameterBoundWithWarnings<String?>() val y2 = ClassTypeParameterBoundWithWarnings() val x3 = ClassTypeParameterBoundWithWarnings(a) diff --git a/compiler/testData/diagnostics/foreignAnnotationsTests/java8Tests/misc/classTypeParameterBoundWithWarnings.kt b/compiler/testData/diagnostics/foreignAnnotationsTests/java8Tests/misc/classTypeParameterBoundWithWarnings.kt index 42e8d492fce..15821df9fb0 100644 --- a/compiler/testData/diagnostics/foreignAnnotationsTests/java8Tests/misc/classTypeParameterBoundWithWarnings.kt +++ b/compiler/testData/diagnostics/foreignAnnotationsTests/java8Tests/misc/classTypeParameterBoundWithWarnings.kt @@ -14,7 +14,7 @@ public class ClassTypeParameterBoundWithWarnings { // FILE: main.kt fun main(x: ClassTypeParameterBoundWithWarnings<String?>, y: ClassTypeParameterBoundWithWarnings, a: String?, b: String) { - val x2 = ClassTypeParameterBoundWithWarnings() + val x2 = ClassTypeParameterBoundWithWarnings<String?>() val y2 = ClassTypeParameterBoundWithWarnings() val x3 = ClassTypeParameterBoundWithWarnings(a) diff --git a/compiler/testData/diagnostics/foreignAnnotationsTests/tests/kt47920.kt b/compiler/testData/diagnostics/foreignAnnotationsTests/tests/kt47920.kt new file mode 100644 index 00000000000..613eb2259c5 --- /dev/null +++ b/compiler/testData/diagnostics/foreignAnnotationsTests/tests/kt47920.kt @@ -0,0 +1,11 @@ +// MUTE_FOR_PSI_CLASS_FILES_READING + +// FILE: J1.java +import io.reactivex.rxjava3.annotations.*; + +public class J1<@NonNull T> {} + +// FILE: main.kt +fun main() { + J1<Any?>() // violated nullability, no warnings; but there is an error with -Xtype-enhancement-improvements-strict-mode +} diff --git a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/ForeignAnnotationsCompiledJavaTestGenerated.java b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/ForeignAnnotationsCompiledJavaTestGenerated.java index fbf607f513c..8922bd48281 100644 --- a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/ForeignAnnotationsCompiledJavaTestGenerated.java +++ b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/ForeignAnnotationsCompiledJavaTestGenerated.java @@ -80,6 +80,12 @@ public class ForeignAnnotationsCompiledJavaTestGenerated extends AbstractForeign runTest("compiler/testData/diagnostics/foreignAnnotationsTests/tests/irrelevantQualifierNicknames.kt"); } + @Test + @TestMetadata("kt47920.kt") + public void testKt47920() throws Exception { + runTest("compiler/testData/diagnostics/foreignAnnotationsTests/tests/kt47920.kt"); + } + @Test @TestMetadata("lombokSimple.kt") public void testLombokSimple() throws Exception { diff --git a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/ForeignAnnotationsCompiledJavaWithPsiClassReadingTestGenerated.java b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/ForeignAnnotationsCompiledJavaWithPsiClassReadingTestGenerated.java index a6146450779..154c1b4c138 100644 --- a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/ForeignAnnotationsCompiledJavaWithPsiClassReadingTestGenerated.java +++ b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/ForeignAnnotationsCompiledJavaWithPsiClassReadingTestGenerated.java @@ -80,6 +80,12 @@ public class ForeignAnnotationsCompiledJavaWithPsiClassReadingTestGenerated exte runTest("compiler/testData/diagnostics/foreignAnnotationsTests/tests/irrelevantQualifierNicknames.kt"); } + @Test + @TestMetadata("kt47920.kt") + public void testKt47920() throws Exception { + runTest("compiler/testData/diagnostics/foreignAnnotationsTests/tests/kt47920.kt"); + } + @Test @TestMetadata("lombokSimple.kt") public void testLombokSimple() throws Exception { diff --git a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/ForeignAnnotationsSourceJavaTestGenerated.java b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/ForeignAnnotationsSourceJavaTestGenerated.java index 4f500004efd..ab31aabf51a 100644 --- a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/ForeignAnnotationsSourceJavaTestGenerated.java +++ b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/ForeignAnnotationsSourceJavaTestGenerated.java @@ -80,6 +80,12 @@ public class ForeignAnnotationsSourceJavaTestGenerated extends AbstractForeignAn runTest("compiler/testData/diagnostics/foreignAnnotationsTests/tests/irrelevantQualifierNicknames.kt"); } + @Test + @TestMetadata("kt47920.kt") + public void testKt47920() throws Exception { + runTest("compiler/testData/diagnostics/foreignAnnotationsTests/tests/kt47920.kt"); + } + @Test @TestMetadata("lombokSimple.kt") public void testLombokSimple() throws Exception {