K2: don't count use-sites of Java type parameter with nullable bounds as not null
This commit is contained in:
committed by
Space Team
parent
d353fd400d
commit
c4bcdc42c1
+4
-8
@@ -17,19 +17,15 @@ public class Test {}
|
||||
fun <T : Test> main(a1: NonPlatformTypeParameter<Any?>, a2: NonPlatformTypeParameter<Test>, x: T): Unit {
|
||||
a1.foo(null)
|
||||
a1.bar<Test?>(null)
|
||||
// jspecify_nullness_mismatch
|
||||
a1.bar<T>(<!NULL_FOR_NONNULL_TYPE!>null<!>)
|
||||
a1.bar<T>(null)
|
||||
a1.bar<T>(x)
|
||||
|
||||
// jspecify_nullness_mismatch
|
||||
a2.foo(<!NULL_FOR_NONNULL_TYPE!>null<!>)
|
||||
a2.foo(null)
|
||||
a2.bar<Test?>(null)
|
||||
// jspecify_nullness_mismatch
|
||||
a2.bar<T>(<!NULL_FOR_NONNULL_TYPE!>null<!>)
|
||||
a2.bar<T>(null)
|
||||
a2.bar<T>(x)
|
||||
}
|
||||
|
||||
fun testNullable(a1: NonPlatformTypeParameter<Test>, x: Test?) {
|
||||
// jspecify_nullness_mismatch
|
||||
a1.foo(<!ARGUMENT_TYPE_MISMATCH!>x<!>)
|
||||
a1.foo(x)
|
||||
}
|
||||
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
// !LANGUAGE: +ProhibitUsingNullableTypeParameterAgainstNotNullAnnotated +TypeEnhancementImprovementsInStrictMode
|
||||
// JSPECIFY_STATE: strict
|
||||
// MUTE_FOR_PSI_CLASS_FILES_READING
|
||||
|
||||
// FILE: NonPlatformTypeParameter.java
|
||||
import org.jspecify.nullness.*;
|
||||
|
||||
public class NonPlatformTypeParameter<T extends @Nullable Object> {
|
||||
public void foo(T t) {}
|
||||
public <E extends @Nullable Object> void bar(E e) {}
|
||||
}
|
||||
|
||||
// FILE: Test.java
|
||||
public class Test {}
|
||||
|
||||
// FILE: main.kt
|
||||
fun <T : Test> main(a1: NonPlatformTypeParameter<Any?>, a2: NonPlatformTypeParameter<Test>, x: T): Unit {
|
||||
a1.foo(null)
|
||||
a1.bar<Test?>(null)
|
||||
// jspecify_nullness_mismatch
|
||||
a1.bar<T>(null)
|
||||
a1.bar<T>(x)
|
||||
|
||||
// jspecify_nullness_mismatch
|
||||
a2.foo(null)
|
||||
a2.bar<Test?>(null)
|
||||
// jspecify_nullness_mismatch
|
||||
a2.bar<T>(null)
|
||||
a2.bar<T>(x)
|
||||
}
|
||||
+1
-2
@@ -1,4 +1,3 @@
|
||||
// FIR_IDENTICAL
|
||||
// !LANGUAGE: +ProhibitUsingNullableTypeParameterAgainstNotNullAnnotated +TypeEnhancementImprovementsInStrictMode
|
||||
// JSPECIFY_STATE: strict
|
||||
// MUTE_FOR_PSI_CLASS_FILES_READING
|
||||
@@ -28,4 +27,4 @@ fun <T : Test> main(a1: NonPlatformTypeParameter<Any?>, a2: NonPlatformTypeParam
|
||||
// jspecify_nullness_mismatch
|
||||
a2.bar<T>(<!NULL_FOR_NONNULL_TYPE!>null<!>)
|
||||
a2.bar<T>(x)
|
||||
}
|
||||
}
|
||||
|
||||
Vendored
-39
@@ -1,39 +0,0 @@
|
||||
// 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
@@ -1,3 +1,4 @@
|
||||
// FIR_IDENTICAL
|
||||
// FULL_JDK
|
||||
// !DIAGNOSTICS: -UNUSED_VARIABLE -UNUSED_PARAMETER
|
||||
// LANGUAGE: -SupportJavaErrorEnhancementOfArgumentsOfWarningLevelEnhanced
|
||||
|
||||
Reference in New Issue
Block a user