From f9cb0d61a8fe350c6cebc583a07909624ac2ea4d Mon Sep 17 00:00:00 2001 From: Victor Petukhov Date: Mon, 19 Jul 2021 16:26:25 +0300 Subject: [PATCH] Don't apply Java defaulting nullability annotations rules to unbounded wildcards ^KT-47396 Fixed --- .../strictMode/WildcardsWithDefault.fir.kt | 3 +-- .../strictMode/WildcardsWithDefault.fir.txt | 2 +- .../strictMode/WildcardsWithDefault.kt | 3 +-- .../strictMode/WildcardsWithDefault.txt | 2 +- .../java8Tests/jspecify/strictMode/kt47396.kt | 22 +++++++++++++++++++ .../jspecify/strictMode/kt47396.txt | 16 ++++++++++++++ .../java8Tests/jspecify/strictMode/kt47422.kt | 1 + .../java8Tests/jspecify/strictMode/kt47437.kt | 1 + .../warnMode/WildcardsWithDefault.fir.kt | 3 +-- .../jspecify/warnMode/WildcardsWithDefault.kt | 3 +-- ...nAnnotationsCompiledJavaTestGenerated.java | 6 +++++ ...dJavaWithPsiClassReadingTestGenerated.java | 6 +++++ ...ignAnnotationsSourceJavaTestGenerated.java | 6 +++++ .../typeEnhancement/signatureEnhancement.kt | 8 ++++--- 14 files changed, 69 insertions(+), 13 deletions(-) create mode 100644 compiler/testData/diagnostics/foreignAnnotationsTests/java8Tests/jspecify/strictMode/kt47396.kt create mode 100644 compiler/testData/diagnostics/foreignAnnotationsTests/java8Tests/jspecify/strictMode/kt47396.txt diff --git a/compiler/testData/diagnostics/foreignAnnotationsTests/java8Tests/jspecify/strictMode/WildcardsWithDefault.fir.kt b/compiler/testData/diagnostics/foreignAnnotationsTests/java8Tests/jspecify/strictMode/WildcardsWithDefault.fir.kt index 796d01a8bcb..9de21a10463 100644 --- a/compiler/testData/diagnostics/foreignAnnotationsTests/java8Tests/jspecify/strictMode/WildcardsWithDefault.fir.kt +++ b/compiler/testData/diagnostics/foreignAnnotationsTests/java8Tests/jspecify/strictMode/WildcardsWithDefault.fir.kt @@ -26,8 +26,7 @@ fun main( b.noBoundsNotNull(aNotNullNotNullNotNull) // jspecify_nullness_mismatch b.noBoundsNotNull(aNotNullNotNullNull) - // jspecify_nullness_mismatch - b.noBoundsNotNull(aNotNullNullNotNull) + b.noBoundsNotNull(aNotNullNullNotNull) // jspecify_nullness_mismatch b.noBoundsNotNull(aNotNullNullNull) diff --git a/compiler/testData/diagnostics/foreignAnnotationsTests/java8Tests/jspecify/strictMode/WildcardsWithDefault.fir.txt b/compiler/testData/diagnostics/foreignAnnotationsTests/java8Tests/jspecify/strictMode/WildcardsWithDefault.fir.txt index f23f649a186..b4eca666fff 100644 --- a/compiler/testData/diagnostics/foreignAnnotationsTests/java8Tests/jspecify/strictMode/WildcardsWithDefault.fir.txt +++ b/compiler/testData/diagnostics/foreignAnnotationsTests/java8Tests/jspecify/strictMode/WildcardsWithDefault.fir.txt @@ -13,7 +13,7 @@ public open class A): kotlin.Unit + public open fun noBoundsNotNull(/*0*/ a: A): kotlin.Unit public open fun noBoundsNullable(/*0*/ a: A): kotlin.Unit public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String } diff --git a/compiler/testData/diagnostics/foreignAnnotationsTests/java8Tests/jspecify/strictMode/WildcardsWithDefault.kt b/compiler/testData/diagnostics/foreignAnnotationsTests/java8Tests/jspecify/strictMode/WildcardsWithDefault.kt index 796d01a8bcb..9de21a10463 100644 --- a/compiler/testData/diagnostics/foreignAnnotationsTests/java8Tests/jspecify/strictMode/WildcardsWithDefault.kt +++ b/compiler/testData/diagnostics/foreignAnnotationsTests/java8Tests/jspecify/strictMode/WildcardsWithDefault.kt @@ -26,8 +26,7 @@ fun main( b.noBoundsNotNull(aNotNullNotNullNotNull) // jspecify_nullness_mismatch b.noBoundsNotNull(aNotNullNotNullNull) - // jspecify_nullness_mismatch - b.noBoundsNotNull(aNotNullNullNotNull) + b.noBoundsNotNull(aNotNullNullNotNull) // jspecify_nullness_mismatch b.noBoundsNotNull(aNotNullNullNull) diff --git a/compiler/testData/diagnostics/foreignAnnotationsTests/java8Tests/jspecify/strictMode/WildcardsWithDefault.txt b/compiler/testData/diagnostics/foreignAnnotationsTests/java8Tests/jspecify/strictMode/WildcardsWithDefault.txt index 37d62cc9b97..bc95e1d8837 100644 --- a/compiler/testData/diagnostics/foreignAnnotationsTests/java8Tests/jspecify/strictMode/WildcardsWithDefault.txt +++ b/compiler/testData/diagnostics/foreignAnnotationsTests/java8Tests/jspecify/strictMode/WildcardsWithDefault.txt @@ -13,7 +13,7 @@ public open class A): kotlin.Unit + public open fun noBoundsNotNull(/*0*/ a: A): kotlin.Unit public open fun noBoundsNullable(/*0*/ a: A): kotlin.Unit public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String } diff --git a/compiler/testData/diagnostics/foreignAnnotationsTests/java8Tests/jspecify/strictMode/kt47396.kt b/compiler/testData/diagnostics/foreignAnnotationsTests/java8Tests/jspecify/strictMode/kt47396.kt new file mode 100644 index 00000000000..24ca4c007f9 --- /dev/null +++ b/compiler/testData/diagnostics/foreignAnnotationsTests/java8Tests/jspecify/strictMode/kt47396.kt @@ -0,0 +1,22 @@ +// JSPECIFY_STATE: strict +// !LANGUAGE: +TypeEnhancementImprovementsInStrictMode +// MUTE_FOR_PSI_CLASS_FILES_READING + +// FILE: Foo.java +import org.jspecify.nullness.*; + +@NullMarked +public interface Foo { + void test(Bar list); +} + +// FILE: Bar.java +import org.jspecify.nullness.*; + +@NullMarked +public interface Bar {} + +// FILE: main.kt +fun test(foo: Foo, bar: Bar) { + foo.test(")!>bar) +} diff --git a/compiler/testData/diagnostics/foreignAnnotationsTests/java8Tests/jspecify/strictMode/kt47396.txt b/compiler/testData/diagnostics/foreignAnnotationsTests/java8Tests/jspecify/strictMode/kt47396.txt new file mode 100644 index 00000000000..70bcc842f87 --- /dev/null +++ b/compiler/testData/diagnostics/foreignAnnotationsTests/java8Tests/jspecify/strictMode/kt47396.txt @@ -0,0 +1,16 @@ +package + +public fun test(/*0*/ foo: Foo, /*1*/ bar: Bar): kotlin.Unit + +@org.jspecify.nullness.NullMarked public interface Bar { + 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 +} + +@org.jspecify.nullness.NullMarked public interface Foo { + 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 abstract fun test(/*0*/ list: Bar<*>): kotlin.Unit + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String +} diff --git a/compiler/testData/diagnostics/foreignAnnotationsTests/java8Tests/jspecify/strictMode/kt47422.kt b/compiler/testData/diagnostics/foreignAnnotationsTests/java8Tests/jspecify/strictMode/kt47422.kt index 26a1aeb8395..ff0f52fa648 100644 --- a/compiler/testData/diagnostics/foreignAnnotationsTests/java8Tests/jspecify/strictMode/kt47422.kt +++ b/compiler/testData/diagnostics/foreignAnnotationsTests/java8Tests/jspecify/strictMode/kt47422.kt @@ -1,6 +1,7 @@ // JSPECIFY_STATE: strict // !DIAGNOSTICS: -UNUSED_PARAMETER // !LANGUAGE: +TypeEnhancementImprovementsInStrictMode +// MUTE_FOR_PSI_CLASS_FILES_READING // FILE: Foo.java import org.jspecify.nullness.*; diff --git a/compiler/testData/diagnostics/foreignAnnotationsTests/java8Tests/jspecify/strictMode/kt47437.kt b/compiler/testData/diagnostics/foreignAnnotationsTests/java8Tests/jspecify/strictMode/kt47437.kt index 3e2dca9d0ff..4cc3afe614e 100644 --- a/compiler/testData/diagnostics/foreignAnnotationsTests/java8Tests/jspecify/strictMode/kt47437.kt +++ b/compiler/testData/diagnostics/foreignAnnotationsTests/java8Tests/jspecify/strictMode/kt47437.kt @@ -1,5 +1,6 @@ // JSPECIFY_STATE: strict // !LANGUAGE: +TypeEnhancementImprovementsInStrictMode +// MUTE_FOR_PSI_CLASS_FILES_READING // FILE: Foo.java import org.jspecify.nullness.*; diff --git a/compiler/testData/diagnostics/foreignAnnotationsTests/java8Tests/jspecify/warnMode/WildcardsWithDefault.fir.kt b/compiler/testData/diagnostics/foreignAnnotationsTests/java8Tests/jspecify/warnMode/WildcardsWithDefault.fir.kt index 4c81f634f00..ab04f5996ba 100644 --- a/compiler/testData/diagnostics/foreignAnnotationsTests/java8Tests/jspecify/warnMode/WildcardsWithDefault.fir.kt +++ b/compiler/testData/diagnostics/foreignAnnotationsTests/java8Tests/jspecify/warnMode/WildcardsWithDefault.fir.kt @@ -26,8 +26,7 @@ fun main( b.noBoundsNotNull(aNotNullNotNullNotNull) // jspecify_nullness_mismatch b.noBoundsNotNull(aNotNullNotNullNull) - // jspecify_nullness_mismatch - b.noBoundsNotNull(aNotNullNullNotNull) + b.noBoundsNotNull(aNotNullNullNotNull) // jspecify_nullness_mismatch b.noBoundsNotNull(aNotNullNullNull) diff --git a/compiler/testData/diagnostics/foreignAnnotationsTests/java8Tests/jspecify/warnMode/WildcardsWithDefault.kt b/compiler/testData/diagnostics/foreignAnnotationsTests/java8Tests/jspecify/warnMode/WildcardsWithDefault.kt index 4c81f634f00..ab04f5996ba 100644 --- a/compiler/testData/diagnostics/foreignAnnotationsTests/java8Tests/jspecify/warnMode/WildcardsWithDefault.kt +++ b/compiler/testData/diagnostics/foreignAnnotationsTests/java8Tests/jspecify/warnMode/WildcardsWithDefault.kt @@ -26,8 +26,7 @@ fun main( b.noBoundsNotNull(aNotNullNotNullNotNull) // jspecify_nullness_mismatch b.noBoundsNotNull(aNotNullNotNullNull) - // jspecify_nullness_mismatch - b.noBoundsNotNull(aNotNullNullNotNull) + b.noBoundsNotNull(aNotNullNullNotNull) // jspecify_nullness_mismatch b.noBoundsNotNull(aNotNullNullNull) 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 95aac6c2068..fbf607f513c 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 @@ -654,6 +654,12 @@ public class ForeignAnnotationsCompiledJavaTestGenerated extends AbstractForeign runTest("compiler/testData/diagnostics/foreignAnnotationsTests/java8Tests/jspecify/strictMode/IgnoreAnnotations.fir.kt"); } + @Test + @TestMetadata("kt47396.kt") + public void testKt47396() throws Exception { + runTest("compiler/testData/diagnostics/foreignAnnotationsTests/java8Tests/jspecify/strictMode/kt47396.kt"); + } + @Test @TestMetadata("kt47422.kt") public void testKt47422() 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 61bf79e9ab4..a6146450779 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 @@ -654,6 +654,12 @@ public class ForeignAnnotationsCompiledJavaWithPsiClassReadingTestGenerated exte runTest("compiler/testData/diagnostics/foreignAnnotationsTests/java8Tests/jspecify/strictMode/IgnoreAnnotations.fir.kt"); } + @Test + @TestMetadata("kt47396.kt") + public void testKt47396() throws Exception { + runTest("compiler/testData/diagnostics/foreignAnnotationsTests/java8Tests/jspecify/strictMode/kt47396.kt"); + } + @Test @TestMetadata("kt47422.kt") public void testKt47422() 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 b05f38ed5a8..4f500004efd 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 @@ -654,6 +654,12 @@ public class ForeignAnnotationsSourceJavaTestGenerated extends AbstractForeignAn runTest("compiler/testData/diagnostics/foreignAnnotationsTests/java8Tests/jspecify/strictMode/IgnoreAnnotations.fir.kt"); } + @Test + @TestMetadata("kt47396.kt") + public void testKt47396() throws Exception { + runTest("compiler/testData/diagnostics/foreignAnnotationsTests/java8Tests/jspecify/strictMode/kt47396.kt"); + } + @Test @TestMetadata("kt47422.kt") public void testKt47422() 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 9c33645dc50..4edffe4de6e 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 @@ -358,7 +358,8 @@ class SignatureEnhancement( ?: computeNullabilityInfoInTheAbsenceOfExplicitAnnotation( nullabilityFromBoundsForTypeBasedOnTypeParameter, defaultTypeQualifier, - typeParameterForArgument + typeParameterForArgument, + isFromStarProjection ) val isNotNullTypeParameter = @@ -385,7 +386,8 @@ class SignatureEnhancement( private fun computeNullabilityInfoInTheAbsenceOfExplicitAnnotation( nullabilityFromBoundsForTypeBasedOnTypeParameter: NullabilityQualifierWithMigrationStatus?, defaultTypeQualifier: JavaDefaultQualifiers?, - typeParameterForArgument: TypeParameterDescriptor? + typeParameterForArgument: TypeParameterDescriptor?, + isFromStarProjection: Boolean ): NullabilityQualifierWithMigrationStatus? { val result = @@ -406,7 +408,7 @@ class SignatureEnhancement( ) } - if (result == null) return boundsFromTypeParameterForArgument + if (result == null || isFromStarProjection) return boundsFromTypeParameterForArgument return mostSpecific(boundsFromTypeParameterForArgument, result) }