diff --git a/compiler/resolution.common/src/org/jetbrains/kotlin/resolve/calls/NewCommonSuperTypeCalculator.kt b/compiler/resolution.common/src/org/jetbrains/kotlin/resolve/calls/NewCommonSuperTypeCalculator.kt index 94b056296d0..65a04ef082d 100644 --- a/compiler/resolution.common/src/org/jetbrains/kotlin/resolve/calls/NewCommonSuperTypeCalculator.kt +++ b/compiler/resolution.common/src/org/jetbrains/kotlin/resolve/calls/NewCommonSuperTypeCalculator.kt @@ -14,6 +14,15 @@ import org.jetbrains.kotlin.types.model.* object NewCommonSuperTypeCalculator { fun TypeSystemCommonSuperTypesContext.commonSuperType(types: List): KotlinTypeMarker { + // Skip computation if the list has only one element. + // It's not only an optimization, but it's required to not mess up attributes. + // See compiler/testData/diagnostics/foreignAnnotationsTests/tests/jsr305/nullabilityWarnings/kt65193.kt + // When the type is flexible, calling replaceCustomAttributes(unionTypeAttributes(types)) will take the attributes of the + // lower bound which will mess up `EnhancedTypeForWarningAttribute`s. + // It's not a problem if the list has multiple entries, because `EnhancedTypeForWarningAttribute.union` returns null, + // meaning that whenever we union multiple types, we remove any `EnhancedTypeForWarningAttribute`. + types.singleOrNull()?.let { return it } + val maxDepth = types.maxOfOrNull { it.typeDepth() } ?: 0 return commonSuperType(types, -maxDepth, true).replaceCustomAttributes(unionTypeAttributes(types)) } diff --git a/compiler/testData/diagnostics/foreignAnnotationsTests/tests/jsr305/nullabilityWarnings/kt65193.fir.kt b/compiler/testData/diagnostics/foreignAnnotationsTests/tests/jsr305/nullabilityWarnings/kt65193.fir.kt deleted file mode 100644 index c0252f1afca..00000000000 --- a/compiler/testData/diagnostics/foreignAnnotationsTests/tests/jsr305/nullabilityWarnings/kt65193.fir.kt +++ /dev/null @@ -1,49 +0,0 @@ -// FILE: test/NonNullApi.java -package test; - -import java.lang.annotation.*; -import javax.annotation.Nonnull; -import javax.annotation.meta.TypeQualifierDefault; - -@Target(ElementType.PACKAGE) -@Retention(RetentionPolicy.RUNTIME) -@Documented -@Nonnull -@TypeQualifierDefault({ElementType.METHOD, ElementType.PARAMETER, ElementType.PACKAGE}) -public @interface NonNullApi { -} - -// FILE: test/package-info.java -@NonNullApi -package test; - -// FILE: test/JpaSpecificationExecutor.java -package test; - -import java.util.List; - -public interface JpaSpecificationExecutor { - List findAll(); -} - -// FILE: mockito/OngoingStubbing.java -package mockito; - -public interface OngoingStubbing { - OngoingStubbing thenReturn(T var1); - - public static OngoingStubbing when(T... methodCall) { - return null; - } -} - - -// FILE: test.kt -package test - -import mockito.OngoingStubbing - -fun test(wrapper: JpaSpecificationExecutor, l: List, l2: List) { - OngoingStubbing.`when`(wrapper.findAll()).thenReturn(l) - OngoingStubbing.`when`(wrapper.findAll(), l2).thenReturn(l) -} \ No newline at end of file diff --git a/compiler/testData/diagnostics/foreignAnnotationsTests/tests/jsr305/nullabilityWarnings/kt65193.kt b/compiler/testData/diagnostics/foreignAnnotationsTests/tests/jsr305/nullabilityWarnings/kt65193.kt index df41bfcf76d..17f1ba9ee1b 100644 --- a/compiler/testData/diagnostics/foreignAnnotationsTests/tests/jsr305/nullabilityWarnings/kt65193.kt +++ b/compiler/testData/diagnostics/foreignAnnotationsTests/tests/jsr305/nullabilityWarnings/kt65193.kt @@ -1,3 +1,4 @@ +// FIR_IDENTICAL // FILE: test/NonNullApi.java package test;