From 85f4cec948929c971b43cb669eb85fb79bb1a91f Mon Sep 17 00:00:00 2001 From: Victor Petukhov Date: Tue, 15 Jun 2021 14:31:34 +0300 Subject: [PATCH] Don't ignore Java nullability annotations without target and unresolved when nullability qualifiers are extracted, with enabled type enhancement improvements ^KT-47276 Fixed --- .../dontIgnoreAnnotationsWithoutTarget.kt | 48 +++++++++++++++++++ .../dontIgnoreAnnotationsWithoutTarget.txt | 29 +++++++++++ ...nAnnotationsCompiledJavaTestGenerated.java | 6 +++ ...dJavaWithPsiClassReadingTestGenerated.java | 6 +++ ...ignAnnotationsSourceJavaTestGenerated.java | 6 +++ .../typeEnhancement/signatureEnhancement.kt | 3 +- 6 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 compiler/testData/diagnostics/foreignAnnotationsTests/tests/jsr305/dontIgnoreAnnotationsWithoutTarget.kt create mode 100644 compiler/testData/diagnostics/foreignAnnotationsTests/tests/jsr305/dontIgnoreAnnotationsWithoutTarget.txt diff --git a/compiler/testData/diagnostics/foreignAnnotationsTests/tests/jsr305/dontIgnoreAnnotationsWithoutTarget.kt b/compiler/testData/diagnostics/foreignAnnotationsTests/tests/jsr305/dontIgnoreAnnotationsWithoutTarget.kt new file mode 100644 index 00000000000..068b85ca222 --- /dev/null +++ b/compiler/testData/diagnostics/foreignAnnotationsTests/tests/jsr305/dontIgnoreAnnotationsWithoutTarget.kt @@ -0,0 +1,48 @@ +// !LANGUAGE: +TypeEnhancementImprovementsInStrictMode +// !DIAGNOSTICS: -UNUSED_PARAMETER +// JSR305_GLOBAL_REPORT: strict + +// FILE: test/NonNullApi.java +package test; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; +import javax.annotation.Nonnull; +import javax.annotation.meta.TypeQualifierDefault; + +@Target(ElementType.PACKAGE) +@Retention(RetentionPolicy.RUNTIME) +@Documented +@Nonnull +@TypeQualifierDefault({ElementType.METHOD, ElementType.PARAMETER, ElementType.PACKAGE}) +public @interface NonNullApi { +} + +// FILE: test/package-info.java +@NonNullApi +package test; + +// FILE: test/A.java +package test; + +import javax.annotation.Nullable; + +public class A { + @Nullable + public String bar1() { return ""; } +} + +// FILE: main.kt +import test.A + +class Inv(x: T) + +fun foo(x: Inv) { } + +fun main(a: A) { + val x = ")!>Inv(a.bar1()) + foo(x) +} diff --git a/compiler/testData/diagnostics/foreignAnnotationsTests/tests/jsr305/dontIgnoreAnnotationsWithoutTarget.txt b/compiler/testData/diagnostics/foreignAnnotationsTests/tests/jsr305/dontIgnoreAnnotationsWithoutTarget.txt new file mode 100644 index 00000000000..79611dc7544 --- /dev/null +++ b/compiler/testData/diagnostics/foreignAnnotationsTests/tests/jsr305/dontIgnoreAnnotationsWithoutTarget.txt @@ -0,0 +1,29 @@ +package + +public fun foo(/*0*/ x: Inv): kotlin.Unit +public fun main(/*0*/ a: test.A): kotlin.Unit + +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 +} + +package test { + + public open class A { + public constructor A() + @javax.annotation.Nullable public open fun bar1(): kotlin.String? + 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 + } + + @kotlin.annotation.Target(allowedTargets = {}) @kotlin.annotation.Retention(value = AnnotationRetention.RUNTIME) @kotlin.annotation.MustBeDocumented @javax.annotation.Nonnull @javax.annotation.meta.TypeQualifierDefault(value = {ElementType.METHOD, ElementType.PARAMETER, ElementType.PACKAGE}) public final annotation class NonNullApi : kotlin.Annotation { + public constructor NonNullApi() + 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/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 ef57f8b1239..0b233b28706 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 @@ -101,6 +101,12 @@ public class ForeignAnnotationsCompiledJavaTestGenerated extends AbstractForeign KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/diagnostics/foreignAnnotationsTests/tests/jsr305"), Pattern.compile("^(.+)\\.kt$"), null, true); } + @Test + @TestMetadata("dontIgnoreAnnotationsWithoutTarget.kt") + public void testDontIgnoreAnnotationsWithoutTarget() throws Exception { + runTest("compiler/testData/diagnostics/foreignAnnotationsTests/tests/jsr305/dontIgnoreAnnotationsWithoutTarget.kt"); + } + @Test @TestMetadata("nonNullNever.kt") public void testNonNullNever() 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 3d4a9985fb0..f02e06c5866 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 @@ -101,6 +101,12 @@ public class ForeignAnnotationsCompiledJavaWithPsiClassReadingTestGenerated exte KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/diagnostics/foreignAnnotationsTests/tests/jsr305"), Pattern.compile("^(.+)\\.kt$"), null, true); } + @Test + @TestMetadata("dontIgnoreAnnotationsWithoutTarget.kt") + public void testDontIgnoreAnnotationsWithoutTarget() throws Exception { + runTest("compiler/testData/diagnostics/foreignAnnotationsTests/tests/jsr305/dontIgnoreAnnotationsWithoutTarget.kt"); + } + @Test @TestMetadata("nonNullNever.kt") public void testNonNullNever() 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 b9b0403ebe0..b791423a262 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 @@ -101,6 +101,12 @@ public class ForeignAnnotationsSourceJavaTestGenerated extends AbstractForeignAn KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/diagnostics/foreignAnnotationsTests/tests/jsr305"), Pattern.compile("^(.+)\\.kt$"), null, true); } + @Test + @TestMetadata("dontIgnoreAnnotationsWithoutTarget.kt") + public void testDontIgnoreAnnotationsWithoutTarget() throws Exception { + runTest("compiler/testData/diagnostics/foreignAnnotationsTests/tests/jsr305/dontIgnoreAnnotationsWithoutTarget.kt"); + } + @Test @TestMetadata("nonNullNever.kt") public void testNonNullNever() throws Exception { diff --git a/core/descriptors.jvm/src/org/jetbrains/kotlin/load/java/typeEnhancement/signatureEnhancement.kt b/core/descriptors.jvm/src/org/jetbrains/kotlin/load/java/typeEnhancement/signatureEnhancement.kt index 27447a64da0..2d2798566a5 100644 --- a/core/descriptors.jvm/src/org/jetbrains/kotlin/load/java/typeEnhancement/signatureEnhancement.kt +++ b/core/descriptors.jvm/src/org/jetbrains/kotlin/load/java/typeEnhancement/signatureEnhancement.kt @@ -332,7 +332,8 @@ class SignatureEnhancement( val composedAnnotation = if (isHeadTypeConstructor && typeContainer != null && typeContainer !is TypeParameterDescriptor && areImprovementsInStrictMode) { val filteredContainerAnnotations = typeContainer.annotations.filter { - val (_, targets) = annotationTypeQualifierResolver.resolveAnnotation(it) ?: return@filter false + val (_, targets) = annotationTypeQualifierResolver.resolveAnnotation(it) + ?: return@filter true // don't exclude annotations without specified target or unresolved /* * We don't apply container type use annotations to avoid double applying them like with arrays: * @NotNull Integer [] f15();