From 47d0211370b132dd1b1c556e0781dec150f6938b Mon Sep 17 00:00:00 2001 From: pyos Date: Mon, 16 Aug 2021 18:41:45 +0200 Subject: [PATCH] FE: do not enhance `?` in jspecify `NullMarked` scope No clue whether this should be done for `@TypeQualifierDefault` since it appears to have no specification whatsoever. #KT-48262 Fixed --- .../strictMode/WildcardsWithDefault.fir.kt | 35 ------------------- .../strictMode/WildcardsWithDefault.kt | 9 +++-- .../strictMode/WildcardsWithDefault.txt | 2 +- .../warnMode/WildcardsWithDefault.fir.kt | 35 ------------------- .../jspecify/warnMode/WildcardsWithDefault.kt | 9 +++-- .../load/java/AnnotationQualifiersFqNames.kt | 6 ++-- .../typeEnhancement/signatureEnhancement.kt | 16 +++++---- 7 files changed, 23 insertions(+), 89 deletions(-) delete mode 100644 compiler/testData/diagnostics/foreignAnnotationsTests/java8Tests/jspecify/strictMode/WildcardsWithDefault.fir.kt delete mode 100644 compiler/testData/diagnostics/foreignAnnotationsTests/java8Tests/jspecify/warnMode/WildcardsWithDefault.fir.kt diff --git a/compiler/testData/diagnostics/foreignAnnotationsTests/java8Tests/jspecify/strictMode/WildcardsWithDefault.fir.kt b/compiler/testData/diagnostics/foreignAnnotationsTests/java8Tests/jspecify/strictMode/WildcardsWithDefault.fir.kt deleted file mode 100644 index 8dd33227973..00000000000 --- a/compiler/testData/diagnostics/foreignAnnotationsTests/java8Tests/jspecify/strictMode/WildcardsWithDefault.fir.kt +++ /dev/null @@ -1,35 +0,0 @@ -// JSPECIFY_STATE: strict -// MUTE_FOR_PSI_CLASS_FILES_READING - -// FILE: WildcardsWithDefault.java -import org.jspecify.nullness.*; - -@NullMarked -public class WildcardsWithDefault { - public void noBoundsNotNull(A a) {} - public void noBoundsNullable(A a) {} -} - -// FILE: A.java -import org.jspecify.nullness.*; - -public class A {} - -// FILE: main.kt -fun main( - aNotNullNotNullNotNull: A, - aNotNullNotNullNull: A, - aNotNullNullNotNull: A, - aNotNullNullNull: A, - b: WildcardsWithDefault -): Unit { - b.noBoundsNotNull(aNotNullNotNullNotNull) - b.noBoundsNotNull(aNotNullNotNullNull) - b.noBoundsNotNull(aNotNullNullNotNull) - b.noBoundsNotNull(aNotNullNullNull) - - b.noBoundsNullable(aNotNullNotNullNotNull) - b.noBoundsNullable(aNotNullNotNullNull) - b.noBoundsNullable(aNotNullNullNotNull) - b.noBoundsNullable(aNotNullNullNull) -} diff --git a/compiler/testData/diagnostics/foreignAnnotationsTests/java8Tests/jspecify/strictMode/WildcardsWithDefault.kt b/compiler/testData/diagnostics/foreignAnnotationsTests/java8Tests/jspecify/strictMode/WildcardsWithDefault.kt index 9de21a10463..b49a858209c 100644 --- a/compiler/testData/diagnostics/foreignAnnotationsTests/java8Tests/jspecify/strictMode/WildcardsWithDefault.kt +++ b/compiler/testData/diagnostics/foreignAnnotationsTests/java8Tests/jspecify/strictMode/WildcardsWithDefault.kt @@ -1,3 +1,4 @@ +// FIR_IDENTICAL // JSPECIFY_STATE: strict // MUTE_FOR_PSI_CLASS_FILES_READING @@ -24,14 +25,12 @@ fun main( b: WildcardsWithDefault ): Unit { b.noBoundsNotNull(aNotNullNotNullNotNull) - // jspecify_nullness_mismatch - b.noBoundsNotNull(aNotNullNotNullNull) + b.noBoundsNotNull(aNotNullNotNullNull) b.noBoundsNotNull(aNotNullNullNotNull) - // jspecify_nullness_mismatch - b.noBoundsNotNull(aNotNullNullNull) + b.noBoundsNotNull(aNotNullNullNull) b.noBoundsNullable(aNotNullNotNullNotNull) b.noBoundsNullable(aNotNullNotNullNull) b.noBoundsNullable(aNotNullNullNotNull) b.noBoundsNullable(aNotNullNullNull) -} \ No newline at end of file +} diff --git a/compiler/testData/diagnostics/foreignAnnotationsTests/java8Tests/jspecify/strictMode/WildcardsWithDefault.txt b/compiler/testData/diagnostics/foreignAnnotationsTests/java8Tests/jspecify/strictMode/WildcardsWithDefault.txt index 6d5a373a940..63de62cdd61 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/warnMode/WildcardsWithDefault.fir.kt b/compiler/testData/diagnostics/foreignAnnotationsTests/java8Tests/jspecify/warnMode/WildcardsWithDefault.fir.kt deleted file mode 100644 index 0af8c19f3c8..00000000000 --- a/compiler/testData/diagnostics/foreignAnnotationsTests/java8Tests/jspecify/warnMode/WildcardsWithDefault.fir.kt +++ /dev/null @@ -1,35 +0,0 @@ -// JSPECIFY_STATE: warn -// MUTE_FOR_PSI_CLASS_FILES_READING - -// FILE: WildcardsWithDefault.java -import org.jspecify.nullness.*; - -@NullMarked -public class WildcardsWithDefault { - public void noBoundsNotNull(A a) {} - public void noBoundsNullable(A a) {} -} - -// FILE: A.java -import org.jspecify.nullness.*; - -public class A {} - -// FILE: main.kt -fun main( - aNotNullNotNullNotNull: A, - aNotNullNotNullNull: A, - aNotNullNullNotNull: A, - aNotNullNullNull: A, - b: WildcardsWithDefault -): Unit { - b.noBoundsNotNull(aNotNullNotNullNotNull) - b.noBoundsNotNull(aNotNullNotNullNull) - b.noBoundsNotNull(aNotNullNullNotNull) - b.noBoundsNotNull(aNotNullNullNull) - - b.noBoundsNullable(aNotNullNotNullNotNull) - b.noBoundsNullable(aNotNullNotNullNull) - b.noBoundsNullable(aNotNullNullNotNull) - b.noBoundsNullable(aNotNullNullNull) -} diff --git a/compiler/testData/diagnostics/foreignAnnotationsTests/java8Tests/jspecify/warnMode/WildcardsWithDefault.kt b/compiler/testData/diagnostics/foreignAnnotationsTests/java8Tests/jspecify/warnMode/WildcardsWithDefault.kt index ab04f5996ba..689eccab52b 100644 --- a/compiler/testData/diagnostics/foreignAnnotationsTests/java8Tests/jspecify/warnMode/WildcardsWithDefault.kt +++ b/compiler/testData/diagnostics/foreignAnnotationsTests/java8Tests/jspecify/warnMode/WildcardsWithDefault.kt @@ -1,3 +1,4 @@ +// FIR_IDENTICAL // JSPECIFY_STATE: warn // MUTE_FOR_PSI_CLASS_FILES_READING @@ -24,14 +25,12 @@ fun main( b: WildcardsWithDefault ): Unit { b.noBoundsNotNull(aNotNullNotNullNotNull) - // jspecify_nullness_mismatch - b.noBoundsNotNull(aNotNullNotNullNull) + b.noBoundsNotNull(aNotNullNotNullNull) b.noBoundsNotNull(aNotNullNullNotNull) - // jspecify_nullness_mismatch - b.noBoundsNotNull(aNotNullNullNull) + b.noBoundsNotNull(aNotNullNullNull) b.noBoundsNullable(aNotNullNotNullNotNull) b.noBoundsNullable(aNotNullNotNullNull) b.noBoundsNullable(aNotNullNullNotNull) b.noBoundsNullable(aNotNullNullNull) -} \ No newline at end of file +} diff --git a/core/compiler.common.jvm/src/org/jetbrains/kotlin/load/java/AnnotationQualifiersFqNames.kt b/core/compiler.common.jvm/src/org/jetbrains/kotlin/load/java/AnnotationQualifiersFqNames.kt index 35b5242f25e..3adf3d89f96 100644 --- a/core/compiler.common.jvm/src/org/jetbrains/kotlin/load/java/AnnotationQualifiersFqNames.kt +++ b/core/compiler.common.jvm/src/org/jetbrains/kotlin/load/java/AnnotationQualifiersFqNames.kt @@ -12,7 +12,8 @@ import org.jetbrains.kotlin.name.FqName data class JavaDefaultQualifiers( val nullabilityQualifier: NullabilityQualifierWithMigrationStatus, val qualifierApplicabilityTypes: Collection, - val affectsTypeParameterBasedTypes: Boolean = nullabilityQualifier.qualifier == NullabilityQualifier.NOT_NULL + val affectsTypeParameterBasedTypes: Boolean = nullabilityQualifier.qualifier == NullabilityQualifier.NOT_NULL, + val affectsStarProjection: Boolean = affectsTypeParameterBasedTypes ) { val makesTypeParameterNotNull get() = nullabilityQualifier.qualifier == NullabilityQualifier.NOT_NULL && affectsTypeParameterBasedTypes } @@ -35,7 +36,8 @@ val JSPECIFY_DEFAULT_ANNOTATIONS = mapOf( JSPECIFY_NULL_MARKED to JavaDefaultQualifiers( NullabilityQualifierWithMigrationStatus(NullabilityQualifier.NOT_NULL), DEFAULT_JSPECIFY_APPLICABILITY, - affectsTypeParameterBasedTypes = false + affectsTypeParameterBasedTypes = false, + affectsStarProjection = false ) ) 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 ec06a047f50..63aaa549c70 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 @@ -317,6 +317,12 @@ class SignatureEnhancement( typeParameterForArgument: TypeParameterDescriptor?, isFromStarProjection: Boolean ): JavaTypeQualifiers { + if (isFromStarProjection && typeParameterForArgument?.variance == Variance.IN_VARIANCE) { + // Star projections can only be enhanced in one way: `?` -> `? extends `. Given a Kotlin type `C + // (declaration-site variance), this is not a valid enhancement due to conflicting variances. + return JavaTypeQualifiers.NONE + } + val areImprovementsInStrictMode = containerContext.components.settings.typeEnhancementImprovementsInStrictMode val composedAnnotation = @@ -349,7 +355,7 @@ class SignatureEnhancement( containerContext.defaultTypeQualifiers?.get(containerApplicabilityType) else defaultQualifiersForType)?.takeIf { - it.affectsTypeParameterBasedTypes || !isTypeParameter() + (it.affectsTypeParameterBasedTypes || !isTypeParameter()) && (it.affectsStarProjection || !isFromStarProjection) } val (nullabilityFromBoundsForTypeBasedOnTypeParameter, isTypeParameterWithNotNullableBounds) = @@ -362,8 +368,7 @@ class SignatureEnhancement( ?: computeNullabilityInfoInTheAbsenceOfExplicitAnnotation( nullabilityFromBoundsForTypeBasedOnTypeParameter, defaultTypeQualifier, - typeParameterForArgument, - isFromStarProjection + typeParameterForArgument ) val isNotNullTypeParameter = @@ -390,8 +395,7 @@ class SignatureEnhancement( private fun computeNullabilityInfoInTheAbsenceOfExplicitAnnotation( nullabilityFromBoundsForTypeBasedOnTypeParameter: NullabilityQualifierWithMigrationStatus?, defaultTypeQualifier: JavaDefaultQualifiers?, - typeParameterForArgument: TypeParameterDescriptor?, - isFromStarProjection: Boolean + typeParameterForArgument: TypeParameterDescriptor? ): NullabilityQualifierWithMigrationStatus? { val result = @@ -412,7 +416,7 @@ class SignatureEnhancement( ) } - if (result == null || isFromStarProjection) return boundsFromTypeParameterForArgument + if (result == null) return boundsFromTypeParameterForArgument return mostSpecific(boundsFromTypeParameterForArgument, result) }