[FIR] Don't lose error level enhancements in warning-level-enhanced arguments

The error-level enhancement is kept as warning-level and a new
LanguageFeature is introduced to turn the warning into an error.

#KT-63208 Fixed
#KT-63209
This commit is contained in:
Kirill Rakhman
2024-01-05 15:14:29 +01:00
committed by Space Team
parent 9189154cae
commit 371b1eb3d5
22 changed files with 470 additions and 33 deletions
@@ -0,0 +1,39 @@
// FULL_JDK
// !DIAGNOSTICS: -UNUSED_VARIABLE -UNUSED_PARAMETER
// LANGUAGE: -SupportJavaErrorEnhancementOfArgumentsOfWarningLevelEnhanced
// 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.
// Behavior was changed in K2, see KT-63209.
@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("", <!NULLABILITY_MISMATCH_BASED_ON_JAVA_ANNOTATIONS!>null<!>)
}
@@ -1,6 +1,6 @@
// FIR_IDENTICAL
// FULL_JDK
// !DIAGNOSTICS: -UNUSED_VARIABLE -UNUSED_PARAMETER
// LANGUAGE: -SupportJavaErrorEnhancementOfArgumentsOfWarningLevelEnhanced
// FILE: ElementTypesAreNonnullByDefault.java
import java.lang.annotation.ElementType;
@@ -22,6 +22,7 @@ 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.
// Behavior was changed in K2, see KT-63209.
@ElementTypesAreNonnullByDefault
public final class Maps {
public static <K extends @Nullable Object, V extends @Nullable Object> java.util.HashMap<K,V> newHashMap() { return null; }