K2: Replicate K1 enhancement behavior for warning-mode top-level types

^KT-56657 Fixed
^KT-57307 Related
This commit is contained in:
Denis.Zharkov
2023-03-14 16:20:35 +01:00
committed by Space Team
parent f39d5d57e1
commit 3e2f8b834c
8 changed files with 83 additions and 1 deletions
@@ -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")
@@ -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")
@@ -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")
@@ -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) {
@@ -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 <K extends @Nullable Object, V extends @Nullable Object> java.util.HashMap<K,V> newHashMap() { return null; }
}
// FILE: main.kt
fun foo() {
val x = Maps.newHashMap<String, Int>()
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<String, Int>!`, i.e. with non-flexible type arguments, thus not allowing nulls.
x.put("", null)
}
@@ -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")
@@ -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")
@@ -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")