Check type parameter bounds by java nullability annotations not only in basic resolution context

^KT-47920 Fixed
This commit is contained in:
Victor Petukhov
2021-07-27 13:24:54 +03:00
committed by teamcityserver
parent 20d50cfee7
commit c5d783596d
7 changed files with 37 additions and 5 deletions
@@ -128,13 +128,16 @@ class JavaNullabilityChecker(val upperBoundChecker: UpperBoundChecker) : Additio
upperBoundChecker.checkBoundsOfExpandedTypeAlias(expressionType.expandedType, expression, c.trace) upperBoundChecker.checkBoundsOfExpandedTypeAlias(expressionType.expandedType, expression, c.trace)
} }
if (c !is BasicCallResolutionContext || upperBoundChecker !is WarningAwareUpperBoundChecker) return if (upperBoundChecker !is WarningAwareUpperBoundChecker) return
val resolvedCall = c.trace.bindingContext[BindingContext.RESOLVED_CALL, c.call] ?: return val call = (c as? BasicCallResolutionContext)?.call
?: c.trace.bindingContext[BindingContext.CALL, (expression as? KtCallExpression)?.calleeExpression]
?: return
val resolvedCall = c.trace.bindingContext[BindingContext.RESOLVED_CALL, call] ?: return
for ((typeParameter, typeArgument) in resolvedCall.typeArguments) { for ((typeParameter, typeArgument) in resolvedCall.typeArguments) {
// continue if we don't have explicit type arguments // continue if we don't have explicit type arguments
val typeReference = c.call.typeArguments.getOrNull(typeParameter.index)?.typeReference ?: continue val typeReference = call.typeArguments.getOrNull(typeParameter.index)?.typeReference ?: continue
upperBoundChecker.checkBounds( upperBoundChecker.checkBounds(
typeReference, typeArgument, typeParameter, TypeSubstitutor.create(typeArgument), c.trace, withOnlyCheckForWarning = true typeReference, typeArgument, typeParameter, TypeSubstitutor.create(typeArgument), c.trace, withOnlyCheckForWarning = true
@@ -14,7 +14,7 @@ public class ClassTypeParameterBoundWithWarnings <T extends @NotNull String> {
// FILE: main.kt // FILE: main.kt
fun main(x: ClassTypeParameterBoundWithWarnings<<!UPPER_BOUND_VIOLATED_BASED_ON_JAVA_ANNOTATIONS!>String?<!>>, y: ClassTypeParameterBoundWithWarnings<String>, a: String?, b: String) { fun main(x: ClassTypeParameterBoundWithWarnings<<!UPPER_BOUND_VIOLATED_BASED_ON_JAVA_ANNOTATIONS!>String?<!>>, y: ClassTypeParameterBoundWithWarnings<String>, a: String?, b: String) {
val x2 = ClassTypeParameterBoundWithWarnings<String?>() val x2 = ClassTypeParameterBoundWithWarnings<<!UPPER_BOUND_VIOLATED_BASED_ON_JAVA_ANNOTATIONS!>String?<!>>()
val y2 = ClassTypeParameterBoundWithWarnings<String>() val y2 = ClassTypeParameterBoundWithWarnings<String>()
val x3 = ClassTypeParameterBoundWithWarnings(<!NULLABILITY_MISMATCH_BASED_ON_JAVA_ANNOTATIONS!>a<!>) val x3 = ClassTypeParameterBoundWithWarnings(<!NULLABILITY_MISMATCH_BASED_ON_JAVA_ANNOTATIONS!>a<!>)
@@ -14,7 +14,7 @@ public class ClassTypeParameterBoundWithWarnings <T extends @NotNull String> {
// FILE: main.kt // FILE: main.kt
fun main(x: ClassTypeParameterBoundWithWarnings<<!UPPER_BOUND_VIOLATED_BASED_ON_JAVA_ANNOTATIONS!>String?<!>>, y: ClassTypeParameterBoundWithWarnings<String>, a: String?, b: String) { fun main(x: ClassTypeParameterBoundWithWarnings<<!UPPER_BOUND_VIOLATED_BASED_ON_JAVA_ANNOTATIONS!>String?<!>>, y: ClassTypeParameterBoundWithWarnings<String>, a: String?, b: String) {
val x2 = ClassTypeParameterBoundWithWarnings<String?>() val x2 = ClassTypeParameterBoundWithWarnings<<!UPPER_BOUND_VIOLATED_BASED_ON_JAVA_ANNOTATIONS!>String?<!>>()
val y2 = ClassTypeParameterBoundWithWarnings<String>() val y2 = ClassTypeParameterBoundWithWarnings<String>()
val x3 = ClassTypeParameterBoundWithWarnings(<!NULLABILITY_MISMATCH_BASED_ON_JAVA_ANNOTATIONS!>a<!>) val x3 = ClassTypeParameterBoundWithWarnings(<!NULLABILITY_MISMATCH_BASED_ON_JAVA_ANNOTATIONS!>a<!>)
@@ -0,0 +1,11 @@
// MUTE_FOR_PSI_CLASS_FILES_READING
// FILE: J1.java
import io.reactivex.rxjava3.annotations.*;
public class J1<@NonNull T> {}
// FILE: main.kt
fun main() {
J1<<!UPPER_BOUND_VIOLATED_BASED_ON_JAVA_ANNOTATIONS!>Any?<!>>() // violated nullability, no warnings; but there is an error with -Xtype-enhancement-improvements-strict-mode
}
@@ -80,6 +80,12 @@ public class ForeignAnnotationsCompiledJavaTestGenerated extends AbstractForeign
runTest("compiler/testData/diagnostics/foreignAnnotationsTests/tests/irrelevantQualifierNicknames.kt"); runTest("compiler/testData/diagnostics/foreignAnnotationsTests/tests/irrelevantQualifierNicknames.kt");
} }
@Test
@TestMetadata("kt47920.kt")
public void testKt47920() throws Exception {
runTest("compiler/testData/diagnostics/foreignAnnotationsTests/tests/kt47920.kt");
}
@Test @Test
@TestMetadata("lombokSimple.kt") @TestMetadata("lombokSimple.kt")
public void testLombokSimple() throws Exception { public void testLombokSimple() throws Exception {
@@ -80,6 +80,12 @@ public class ForeignAnnotationsCompiledJavaWithPsiClassReadingTestGenerated exte
runTest("compiler/testData/diagnostics/foreignAnnotationsTests/tests/irrelevantQualifierNicknames.kt"); runTest("compiler/testData/diagnostics/foreignAnnotationsTests/tests/irrelevantQualifierNicknames.kt");
} }
@Test
@TestMetadata("kt47920.kt")
public void testKt47920() throws Exception {
runTest("compiler/testData/diagnostics/foreignAnnotationsTests/tests/kt47920.kt");
}
@Test @Test
@TestMetadata("lombokSimple.kt") @TestMetadata("lombokSimple.kt")
public void testLombokSimple() throws Exception { public void testLombokSimple() throws Exception {
@@ -80,6 +80,12 @@ public class ForeignAnnotationsSourceJavaTestGenerated extends AbstractForeignAn
runTest("compiler/testData/diagnostics/foreignAnnotationsTests/tests/irrelevantQualifierNicknames.kt"); runTest("compiler/testData/diagnostics/foreignAnnotationsTests/tests/irrelevantQualifierNicknames.kt");
} }
@Test
@TestMetadata("kt47920.kt")
public void testKt47920() throws Exception {
runTest("compiler/testData/diagnostics/foreignAnnotationsTests/tests/kt47920.kt");
}
@Test @Test
@TestMetadata("lombokSimple.kt") @TestMetadata("lombokSimple.kt")
public void testLombokSimple() throws Exception { public void testLombokSimple() throws Exception {