From 3e2f8b834cf8e47e6ca0f9d644dda993d78715af Mon Sep 17 00:00:00 2001 From: "Denis.Zharkov" Date: Tue, 14 Mar 2023 16:20:35 +0100 Subject: [PATCH] K2: Replicate K1 enhancement behavior for warning-mode top-level types ^KT-56657 Fixed ^KT-57307 Related --- ...nAnnotationsCompiledJavaTestGenerated.java | 6 +++ ...dJavaWithPsiClassReadingTestGenerated.java | 6 +++ ...ignAnnotationsSourceJavaTestGenerated.java | 6 +++ .../fir/java/enhancement/javaTypeUtils.kt | 10 ++++- .../java8Tests/warningModeForHeadType.kt | 38 +++++++++++++++++++ ...nAnnotationsCompiledJavaTestGenerated.java | 6 +++ ...dJavaWithPsiClassReadingTestGenerated.java | 6 +++ ...ignAnnotationsSourceJavaTestGenerated.java | 6 +++ 8 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 compiler/testData/diagnostics/foreignAnnotationsTests/java8Tests/warningModeForHeadType.kt diff --git a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirPsiOldFrontendForeignAnnotationsCompiledJavaTestGenerated.java b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirPsiOldFrontendForeignAnnotationsCompiledJavaTestGenerated.java index c099161ccd0..9494800cc0a 100644 --- a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirPsiOldFrontendForeignAnnotationsCompiledJavaTestGenerated.java +++ b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirPsiOldFrontendForeignAnnotationsCompiledJavaTestGenerated.java @@ -630,6 +630,12 @@ public class FirPsiOldFrontendForeignAnnotationsCompiledJavaTestGenerated extend runTest("compiler/testData/diagnostics/foreignAnnotationsTests/java8Tests/typeUseOnObject.kt"); } + @Test + @TestMetadata("warningModeForHeadType.kt") + public void testWarningModeForHeadType() throws Exception { + runTest("compiler/testData/diagnostics/foreignAnnotationsTests/java8Tests/warningModeForHeadType.kt"); + } + @Nested @TestMetadata("compiler/testData/diagnostics/foreignAnnotationsTests/java8Tests/jspecify") @TestDataPath("$PROJECT_ROOT") diff --git a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirPsiOldFrontendForeignAnnotationsCompiledJavaWithPsiClassReadingTestGenerated.java b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirPsiOldFrontendForeignAnnotationsCompiledJavaWithPsiClassReadingTestGenerated.java index 2bd4cbc1f20..fa7c1eb721d 100644 --- a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirPsiOldFrontendForeignAnnotationsCompiledJavaWithPsiClassReadingTestGenerated.java +++ b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirPsiOldFrontendForeignAnnotationsCompiledJavaWithPsiClassReadingTestGenerated.java @@ -630,6 +630,12 @@ public class FirPsiOldFrontendForeignAnnotationsCompiledJavaWithPsiClassReadingT runTest("compiler/testData/diagnostics/foreignAnnotationsTests/java8Tests/typeUseOnObject.kt"); } + @Test + @TestMetadata("warningModeForHeadType.kt") + public void testWarningModeForHeadType() throws Exception { + runTest("compiler/testData/diagnostics/foreignAnnotationsTests/java8Tests/warningModeForHeadType.kt"); + } + @Nested @TestMetadata("compiler/testData/diagnostics/foreignAnnotationsTests/java8Tests/jspecify") @TestDataPath("$PROJECT_ROOT") diff --git a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirPsiOldFrontendForeignAnnotationsSourceJavaTestGenerated.java b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirPsiOldFrontendForeignAnnotationsSourceJavaTestGenerated.java index cfd05b33717..833c4ce1bc8 100644 --- a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirPsiOldFrontendForeignAnnotationsSourceJavaTestGenerated.java +++ b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirPsiOldFrontendForeignAnnotationsSourceJavaTestGenerated.java @@ -630,6 +630,12 @@ public class FirPsiOldFrontendForeignAnnotationsSourceJavaTestGenerated extends runTest("compiler/testData/diagnostics/foreignAnnotationsTests/java8Tests/typeUseOnObject.kt"); } + @Test + @TestMetadata("warningModeForHeadType.kt") + public void testWarningModeForHeadType() throws Exception { + runTest("compiler/testData/diagnostics/foreignAnnotationsTests/java8Tests/warningModeForHeadType.kt"); + } + @Nested @TestMetadata("compiler/testData/diagnostics/foreignAnnotationsTests/java8Tests/jspecify") @TestDataPath("$PROJECT_ROOT") diff --git a/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/enhancement/javaTypeUtils.kt b/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/enhancement/javaTypeUtils.kt index 2d182c1db5e..131b9722839 100644 --- a/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/enhancement/javaTypeUtils.kt +++ b/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/enhancement/javaTypeUtils.kt @@ -40,6 +40,14 @@ private fun ConeKotlinType.enhanceConeKotlinType( ): ConeKotlinType? { return when (this) { is ConeFlexibleType -> { + // Currently, the warnings are left unsupported in K2 (see KT-57307) + // But modulo information for warnings, we reproduce the K1 behavior: if head type qualifier is for warnings, we totally ignore + // enhancement on its arguments, too (see JavaTypeEnhancement.enhancePossiblyFlexible). + // It's not totally correct, but tolerable since we would like to avoid excessive breaking changes and the warnings should be + // anyway reported. + // TODO: support not loosing information for warnings here, too + if (qualifiers(index).isNullabilityQualifierForWarning) return null + val lowerResult = lowerBound.enhanceInflexibleType( session, TypeComponentPosition.FLEXIBLE_LOWER, qualifiers, index, subtreeSizes ) @@ -88,7 +96,7 @@ private fun ConeSimpleKotlinType.enhanceInflexibleType( val effectiveQualifiers = qualifiers(index) val enhancedTag = lookupTag.enhanceMutability(effectiveQualifiers, position) - // TODO: implement warnings + // TODO: implement warnings (see KT-57307) val nullabilityFromQualifiers = effectiveQualifiers.nullability .takeIf { shouldEnhance && !effectiveQualifiers.isNullabilityQualifierForWarning } val enhancedIsNullable = when (nullabilityFromQualifiers) { diff --git a/compiler/testData/diagnostics/foreignAnnotationsTests/java8Tests/warningModeForHeadType.kt b/compiler/testData/diagnostics/foreignAnnotationsTests/java8Tests/warningModeForHeadType.kt new file mode 100644 index 00000000000..f124cb567db --- /dev/null +++ b/compiler/testData/diagnostics/foreignAnnotationsTests/java8Tests/warningModeForHeadType.kt @@ -0,0 +1,38 @@ +// FIR_IDENTICAL +// FULL_JDK +// !DIAGNOSTICS: -UNUSED_VARIABLE -UNUSED_PARAMETER + +// FILE: ElementTypesAreNonnullByDefault.java +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; + +@Retention(RetentionPolicy.RUNTIME) +@Target({ElementType.TYPE}) +@TypeQualifierDefault({ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER}) +@Nonnull +@interface ElementTypesAreNonnullByDefault { +} + +// FILE: Maps.java +import org.checkerframework.checker.nullness.qual.Nullable; +// Here it's important that @ElementTypesAreNonnullByDefault is a JSR-305 default qualifier and disabled by default (resulting in warnings-only) +// Thus return type (head type) is considered as warningly-annotated as not-nullable and that makes annotations on bounds for K and V +// be effectively ignored on non-warnings level. +@ElementTypesAreNonnullByDefault +public final class Maps { + public static java.util.HashMap newHashMap() { return null; } +} + +// FILE: main.kt + +fun foo() { + val x = Maps.newHashMap() + x.put("", 1) + // If there were no @ElementTypesAreNonnullByDefault on the Maps class, there would be an error on `null` argument because the type of `x` + // would be `HashMap!`, i.e. with non-flexible type arguments, thus not allowing nulls. + x.put("", null) +} 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 a7d79ed1106..59decd35496 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 @@ -630,6 +630,12 @@ public class ForeignAnnotationsCompiledJavaTestGenerated extends AbstractForeign runTest("compiler/testData/diagnostics/foreignAnnotationsTests/java8Tests/typeUseOnObject.kt"); } + @Test + @TestMetadata("warningModeForHeadType.kt") + public void testWarningModeForHeadType() throws Exception { + runTest("compiler/testData/diagnostics/foreignAnnotationsTests/java8Tests/warningModeForHeadType.kt"); + } + @Nested @TestMetadata("compiler/testData/diagnostics/foreignAnnotationsTests/java8Tests/jspecify") @TestDataPath("$PROJECT_ROOT") 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 5ca9c1a1f43..d37fbd2d0d4 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 @@ -630,6 +630,12 @@ public class ForeignAnnotationsCompiledJavaWithPsiClassReadingTestGenerated exte runTest("compiler/testData/diagnostics/foreignAnnotationsTests/java8Tests/typeUseOnObject.kt"); } + @Test + @TestMetadata("warningModeForHeadType.kt") + public void testWarningModeForHeadType() throws Exception { + runTest("compiler/testData/diagnostics/foreignAnnotationsTests/java8Tests/warningModeForHeadType.kt"); + } + @Nested @TestMetadata("compiler/testData/diagnostics/foreignAnnotationsTests/java8Tests/jspecify") @TestDataPath("$PROJECT_ROOT") 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 58bcd57e83c..523344af9cd 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 @@ -630,6 +630,12 @@ public class ForeignAnnotationsSourceJavaTestGenerated extends AbstractForeignAn runTest("compiler/testData/diagnostics/foreignAnnotationsTests/java8Tests/typeUseOnObject.kt"); } + @Test + @TestMetadata("warningModeForHeadType.kt") + public void testWarningModeForHeadType() throws Exception { + runTest("compiler/testData/diagnostics/foreignAnnotationsTests/java8Tests/warningModeForHeadType.kt"); + } + @Nested @TestMetadata("compiler/testData/diagnostics/foreignAnnotationsTests/java8Tests/jspecify") @TestDataPath("$PROJECT_ROOT")