diff --git a/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/enhancement/SignatureEnhancement.kt b/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/enhancement/SignatureEnhancement.kt index a123a752506..adf29dc2648 100644 --- a/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/enhancement/SignatureEnhancement.kt +++ b/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/enhancement/SignatureEnhancement.kt @@ -47,6 +47,7 @@ import org.jetbrains.kotlin.fir.utils.exceptions.withFirEntry import org.jetbrains.kotlin.load.java.AnnotationQualifierApplicabilityType import org.jetbrains.kotlin.load.java.AnnotationQualifierApplicabilityType.VALUE_PARAMETER import org.jetbrains.kotlin.load.java.FakePureImplementationsProvider +import org.jetbrains.kotlin.load.java.JavaDefaultQualifiers import org.jetbrains.kotlin.load.java.JavaTypeQualifiersByElementType import org.jetbrains.kotlin.load.java.JvmAnnotationNames import org.jetbrains.kotlin.load.java.typeEnhancement.* @@ -793,6 +794,14 @@ private class EnhancementSignatureParts( override val TypeParameterMarker.isFromJava: Boolean get() = (this as ConeTypeParameterLookupTag).symbol.fir.origin is FirDeclarationOrigin.Java + + override fun getDefaultNullability( + referencedParameterBoundsNullability: NullabilityQualifierWithMigrationStatus?, + defaultTypeQualifiers: JavaDefaultQualifiers? + ): NullabilityQualifierWithMigrationStatus? { + return referencedParameterBoundsNullability?.takeIf { it.qualifier == NullabilityQualifier.NOT_NULL } + ?: defaultTypeQualifiers?.nullabilityQualifier + } } class FirEnhancedSymbolsStorage(private val cachesFactory: FirCachesFactory) : FirSessionComponent { diff --git a/compiler/testData/diagnostics/foreignAnnotationsTests/java8Tests/jspecify/strictMode/NonPlatformTypeParameter.fir.kt b/compiler/testData/diagnostics/foreignAnnotationsTests/java8Tests/jspecify/strictMode/NonPlatformTypeParameter.fir.kt index 2c0109dc50a..e63b0bf2660 100644 --- a/compiler/testData/diagnostics/foreignAnnotationsTests/java8Tests/jspecify/strictMode/NonPlatformTypeParameter.fir.kt +++ b/compiler/testData/diagnostics/foreignAnnotationsTests/java8Tests/jspecify/strictMode/NonPlatformTypeParameter.fir.kt @@ -17,19 +17,15 @@ public class Test {} fun main(a1: NonPlatformTypeParameter, a2: NonPlatformTypeParameter, x: T): Unit { a1.foo(null) a1.bar(null) - // jspecify_nullness_mismatch - a1.bar(null) + a1.bar(null) a1.bar(x) - // jspecify_nullness_mismatch - a2.foo(null) + a2.foo(null) a2.bar(null) - // jspecify_nullness_mismatch - a2.bar(null) + a2.bar(null) a2.bar(x) } fun testNullable(a1: NonPlatformTypeParameter, x: Test?) { - // jspecify_nullness_mismatch - a1.foo(x) + a1.foo(x) } diff --git a/compiler/testData/diagnostics/foreignAnnotationsTests/java8Tests/jspecifyOld/strictMode/NonPlatformTypeParameter.fir.kt b/compiler/testData/diagnostics/foreignAnnotationsTests/java8Tests/jspecifyOld/strictMode/NonPlatformTypeParameter.fir.kt new file mode 100644 index 00000000000..0f27cdf02d1 --- /dev/null +++ b/compiler/testData/diagnostics/foreignAnnotationsTests/java8Tests/jspecifyOld/strictMode/NonPlatformTypeParameter.fir.kt @@ -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 { + public void foo(T t) {} + public void bar(E e) {} +} + +// FILE: Test.java +public class Test {} + +// FILE: main.kt +fun main(a1: NonPlatformTypeParameter, a2: NonPlatformTypeParameter, x: T): Unit { + a1.foo(null) + a1.bar(null) + // jspecify_nullness_mismatch + a1.bar(null) + a1.bar(x) + + // jspecify_nullness_mismatch + a2.foo(null) + a2.bar(null) + // jspecify_nullness_mismatch + a2.bar(null) + a2.bar(x) +} \ No newline at end of file diff --git a/compiler/testData/diagnostics/foreignAnnotationsTests/java8Tests/jspecifyOld/strictMode/NonPlatformTypeParameter.kt b/compiler/testData/diagnostics/foreignAnnotationsTests/java8Tests/jspecifyOld/strictMode/NonPlatformTypeParameter.kt index f79a6a4f077..1c7346e5f64 100644 --- a/compiler/testData/diagnostics/foreignAnnotationsTests/java8Tests/jspecifyOld/strictMode/NonPlatformTypeParameter.kt +++ b/compiler/testData/diagnostics/foreignAnnotationsTests/java8Tests/jspecifyOld/strictMode/NonPlatformTypeParameter.kt @@ -1,4 +1,3 @@ -// FIR_IDENTICAL // !LANGUAGE: +ProhibitUsingNullableTypeParameterAgainstNotNullAnnotated +TypeEnhancementImprovementsInStrictMode // JSPECIFY_STATE: strict // MUTE_FOR_PSI_CLASS_FILES_READING @@ -28,4 +27,4 @@ fun main(a1: NonPlatformTypeParameter, a2: NonPlatformTypeParam // jspecify_nullness_mismatch a2.bar(null) a2.bar(x) -} \ No newline at end of file +} diff --git a/compiler/testData/diagnostics/foreignAnnotationsTests/java8Tests/warningModeForHeadType.fir.kt b/compiler/testData/diagnostics/foreignAnnotationsTests/java8Tests/warningModeForHeadType.fir.kt deleted file mode 100644 index 87a0295cfa8..00000000000 --- a/compiler/testData/diagnostics/foreignAnnotationsTests/java8Tests/warningModeForHeadType.fir.kt +++ /dev/null @@ -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 java.util.HashMap newHashMap() { return null; } -} - -// FILE: main.kt - -fun foo() { - val x = Maps.newHashMap() - 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!`, i.e. with non-flexible type arguments, thus not allowing nulls. - x.put("", null) -} diff --git a/compiler/testData/diagnostics/foreignAnnotationsTests/java8Tests/warningModeForHeadType.kt b/compiler/testData/diagnostics/foreignAnnotationsTests/java8Tests/warningModeForHeadType.kt index 7980ce00f37..e454967325d 100644 --- a/compiler/testData/diagnostics/foreignAnnotationsTests/java8Tests/warningModeForHeadType.kt +++ b/compiler/testData/diagnostics/foreignAnnotationsTests/java8Tests/warningModeForHeadType.kt @@ -1,3 +1,4 @@ +// FIR_IDENTICAL // FULL_JDK // !DIAGNOSTICS: -UNUSED_VARIABLE -UNUSED_PARAMETER // LANGUAGE: -SupportJavaErrorEnhancementOfArgumentsOfWarningLevelEnhanced diff --git a/compiler/testData/ir/irText/fakeOverrides/annotations/typeParameterAnnotationOverride.fir.ir.txt b/compiler/testData/ir/irText/fakeOverrides/annotations/typeParameterAnnotationOverride.fir.ir.txt index 4e2d15d83ad..58afc356c77 100644 --- a/compiler/testData/ir/irText/fakeOverrides/annotations/typeParameterAnnotationOverride.fir.ir.txt +++ b/compiler/testData/ir/irText/fakeOverrides/annotations/typeParameterAnnotationOverride.fir.ir.txt @@ -93,20 +93,20 @@ FILE fqName: fileName:/1.kt DELEGATING_CONSTRUCTOR_CALL 'public constructor () declared in .Java3' : kotlin.Int? INSTANCE_INITIALIZER_CALL classDescriptor='CLASS CLASS name:D modality:FINAL visibility:public superTypes:[.Java3]' - FUN FAKE_OVERRIDE name:bar visibility:public modality:OPEN <> ($this:.Java3) returnType:@[EnhancedNullability] kotlin.Int? [fake_override] + FUN FAKE_OVERRIDE name:bar visibility:public modality:OPEN <> ($this:.Java3) returnType:@[FlexibleNullability] kotlin.Int? [fake_override] overridden: - public open fun bar (): @[EnhancedNullability] T of .Java3 declared in .Java3 + public open fun bar (): @[FlexibleNullability] T of .Java3? declared in .Java3 $this: VALUE_PARAMETER name: type:.Java3 FUN FAKE_OVERRIDE name:equals visibility:public modality:OPEN <> ($this:kotlin.Any, other:kotlin.Any?) returnType:kotlin.Boolean [fake_override,operator] overridden: public open fun equals (other: kotlin.Any?): kotlin.Boolean declared in .Java3 $this: VALUE_PARAMETER name: type:kotlin.Any VALUE_PARAMETER name:other index:0 type:kotlin.Any? - FUN FAKE_OVERRIDE name:foo visibility:public modality:OPEN <> ($this:.Java3, s:@[EnhancedNullability] kotlin.Int?) returnType:kotlin.Unit [fake_override] + FUN FAKE_OVERRIDE name:foo visibility:public modality:OPEN <> ($this:.Java3, s:@[FlexibleNullability] kotlin.Int?) returnType:kotlin.Unit [fake_override] overridden: - public open fun foo (s: @[EnhancedNullability] T of .Java3): kotlin.Unit declared in .Java3 + public open fun foo (s: @[FlexibleNullability] T of .Java3?): kotlin.Unit declared in .Java3 $this: VALUE_PARAMETER name: type:.Java3 - VALUE_PARAMETER name:s index:0 type:@[EnhancedNullability] kotlin.Int? + VALUE_PARAMETER name:s index:0 type:@[FlexibleNullability] kotlin.Int? FUN FAKE_OVERRIDE name:hashCode visibility:public modality:OPEN <> ($this:kotlin.Any) returnType:kotlin.Int [fake_override] overridden: public open fun hashCode (): kotlin.Int declared in .Java3 @@ -122,20 +122,20 @@ FILE fqName: fileName:/1.kt DELEGATING_CONSTRUCTOR_CALL 'public constructor () declared in .Java3' : kotlin.Int INSTANCE_INITIALIZER_CALL classDescriptor='CLASS CLASS name:E modality:FINAL visibility:public superTypes:[.Java3]' - FUN FAKE_OVERRIDE name:bar visibility:public modality:OPEN <> ($this:.Java3) returnType:@[EnhancedNullability] kotlin.Int [fake_override] + FUN FAKE_OVERRIDE name:bar visibility:public modality:OPEN <> ($this:.Java3) returnType:@[FlexibleNullability] kotlin.Int? [fake_override] overridden: - public open fun bar (): @[EnhancedNullability] T of .Java3 declared in .Java3 + public open fun bar (): @[FlexibleNullability] T of .Java3? declared in .Java3 $this: VALUE_PARAMETER name: type:.Java3 FUN FAKE_OVERRIDE name:equals visibility:public modality:OPEN <> ($this:kotlin.Any, other:kotlin.Any?) returnType:kotlin.Boolean [fake_override,operator] overridden: public open fun equals (other: kotlin.Any?): kotlin.Boolean declared in .Java3 $this: VALUE_PARAMETER name: type:kotlin.Any VALUE_PARAMETER name:other index:0 type:kotlin.Any? - FUN FAKE_OVERRIDE name:foo visibility:public modality:OPEN <> ($this:.Java3, s:@[EnhancedNullability] kotlin.Int) returnType:kotlin.Unit [fake_override] + FUN FAKE_OVERRIDE name:foo visibility:public modality:OPEN <> ($this:.Java3, s:@[FlexibleNullability] kotlin.Int?) returnType:kotlin.Unit [fake_override] overridden: - public open fun foo (s: @[EnhancedNullability] T of .Java3): kotlin.Unit declared in .Java3 + public open fun foo (s: @[FlexibleNullability] T of .Java3?): kotlin.Unit declared in .Java3 $this: VALUE_PARAMETER name: type:.Java3 - VALUE_PARAMETER name:s index:0 type:@[EnhancedNullability] kotlin.Int + VALUE_PARAMETER name:s index:0 type:@[FlexibleNullability] kotlin.Int? FUN FAKE_OVERRIDE name:hashCode visibility:public modality:OPEN <> ($this:kotlin.Any) returnType:kotlin.Int [fake_override] overridden: public open fun hashCode (): kotlin.Int declared in .Java3 @@ -246,23 +246,23 @@ FILE fqName: fileName:/1.kt $this: GET_VAR 'c: .C declared in .test' type=.C origin=null s: CONST Int type=kotlin.Int value=1 VAR name:k7 type:kotlin.Int? [val] - GET_FIELD 'FIELD IR_EXTERNAL_JAVA_DECLARATION_STUB name:a type:@[EnhancedNullability] T of .Java3 visibility:public' type=@[EnhancedNullability] kotlin.Int? origin=null + GET_FIELD 'FIELD IR_EXTERNAL_JAVA_DECLARATION_STUB name:a type:@[FlexibleNullability] T of .Java3? visibility:public' type=@[FlexibleNullability] kotlin.Int? origin=null receiver: GET_VAR 'd: .D declared in .test' type=.D origin=null VAR name:k8 type:kotlin.Int? [val] - CALL 'public open fun bar (): @[EnhancedNullability] kotlin.Int? declared in .D' type=@[EnhancedNullability] kotlin.Int? origin=null + CALL 'public open fun bar (): @[FlexibleNullability] kotlin.Int? declared in .D' type=@[FlexibleNullability] kotlin.Int? origin=null $this: GET_VAR 'd: .D declared in .test' type=.D origin=null - CALL 'public open fun foo (s: @[EnhancedNullability] kotlin.Int?): kotlin.Unit declared in .D' type=kotlin.Unit origin=null + CALL 'public open fun foo (s: @[FlexibleNullability] kotlin.Int?): kotlin.Unit declared in .D' type=kotlin.Unit origin=null $this: GET_VAR 'd: .D declared in .test' type=.D origin=null s: CONST Null type=kotlin.Nothing? value=null VAR name:k9 type:kotlin.Int [val] TYPE_OP type=kotlin.Int origin=IMPLICIT_NOTNULL typeOperand=kotlin.Int - GET_FIELD 'FIELD IR_EXTERNAL_JAVA_DECLARATION_STUB name:a type:@[EnhancedNullability] T of .Java3 visibility:public' type=@[EnhancedNullability] kotlin.Int origin=null + GET_FIELD 'FIELD IR_EXTERNAL_JAVA_DECLARATION_STUB name:a type:@[FlexibleNullability] T of .Java3? visibility:public' type=@[FlexibleNullability] kotlin.Int? origin=null receiver: GET_VAR 'e: .E declared in .test' type=.E origin=null VAR name:k10 type:kotlin.Int [val] TYPE_OP type=kotlin.Int origin=IMPLICIT_NOTNULL typeOperand=kotlin.Int - CALL 'public open fun bar (): @[EnhancedNullability] kotlin.Int declared in .E' type=@[EnhancedNullability] kotlin.Int origin=null + CALL 'public open fun bar (): @[FlexibleNullability] kotlin.Int? declared in .E' type=@[FlexibleNullability] kotlin.Int? origin=null $this: GET_VAR 'e: .E declared in .test' type=.E origin=null - CALL 'public open fun foo (s: @[EnhancedNullability] kotlin.Int): kotlin.Unit declared in .E' type=kotlin.Unit origin=null + CALL 'public open fun foo (s: @[FlexibleNullability] kotlin.Int?): kotlin.Unit declared in .E' type=kotlin.Unit origin=null $this: GET_VAR 'e: .E declared in .test' type=.E origin=null s: CONST Int type=kotlin.Int value=1 VAR name:k11 type:kotlin.Int [val] diff --git a/compiler/testData/ir/irText/fakeOverrides/annotations/typeParameterAnnotationOverride.fir.sig.kt.txt b/compiler/testData/ir/irText/fakeOverrides/annotations/typeParameterAnnotationOverride.fir.sig.kt.txt new file mode 100644 index 00000000000..6f200467ef4 --- /dev/null +++ b/compiler/testData/ir/irText/fakeOverrides/annotations/typeParameterAnnotationOverride.fir.sig.kt.txt @@ -0,0 +1,173 @@ +// CHECK: +// Mangled name: A +// Public signature: /A|null[0] +class A : Java1 { + // CHECK: + // Mangled name: A#(){} + // Public signature: /A.|-5645683436151566731[0] + // Public signature debug description: (){} + constructor() /* primary */ + + // CHECK JVM_IR: + // Mangled name: A#bar(){}kotlin.Int{EnhancedNullability} + // Public signature: /A.bar|-8608075298013083950[0] + // Public signature debug description: bar(){}kotlin.Int{EnhancedNullability} + /* fake */ override fun bar(): @EnhancedNullability Int + + // CHECK: + // Mangled name: A#foo(kotlin.Int{EnhancedNullability}){} + // Public signature: /A.foo|6054053236451526699[0] + // Public signature debug description: foo(kotlin.Int{EnhancedNullability}){} + /* fake */ override fun foo(s: @EnhancedNullability Int): Unit + +} + +// CHECK: +// Mangled name: B +// Public signature: /B|null[0] +class B : Java1 { + // CHECK: + // Mangled name: B#(){} + // Public signature: /B.|-5645683436151566731[0] + // Public signature debug description: (){} + constructor() /* primary */ + + // CHECK JVM_IR: + // Mangled name: B#bar(){}1:0{EnhancedNullability} + // Public signature: /B.bar|582597716914558172[0] + // Public signature debug description: bar(){}1:0{EnhancedNullability} + /* fake */ override fun bar(): @EnhancedNullability T + + // CHECK: + // Mangled name: B#foo(1:0{EnhancedNullability}){} + // Public signature: /B.foo|-5059967198211209740[0] + // Public signature debug description: foo(1:0{EnhancedNullability}){} + /* fake */ override fun foo(s: @EnhancedNullability T): Unit + +} + +// CHECK: +// Mangled name: C +// Public signature: /C|null[0] +class C : Java2 { + // CHECK: + // Mangled name: C#(){} + // Public signature: /C.|-5645683436151566731[0] + // Public signature debug description: (){} + constructor() /* primary */ + + // CHECK JVM_IR: + // Mangled name: C#bar(){}kotlin.Int{EnhancedNullability} + // Public signature: /C.bar|-8608075298013083950[0] + // Public signature debug description: bar(){}kotlin.Int{EnhancedNullability} + /* fake */ override fun bar(): @EnhancedNullability Int + + // CHECK: + // Mangled name: C#foo(kotlin.Int{EnhancedNullability}){} + // Public signature: /C.foo|6054053236451526699[0] + // Public signature debug description: foo(kotlin.Int{EnhancedNullability}){} + /* fake */ override fun foo(s: @EnhancedNullability Int): Unit + +} + +// CHECK: +// Mangled name: D +// Public signature: /D|null[0] +class D : Java3 { + // CHECK: + // Mangled name: D#(){} + // Public signature: /D.|-5645683436151566731[0] + // Public signature debug description: (){} + constructor() /* primary */ + + // CHECK JVM_IR: + // Mangled name: D#bar(){}kotlin.Int? + // Public signature: /D.bar|793910862689138186[0] + // Public signature debug description: bar(){}kotlin.Int? + /* fake */ override fun bar(): Int? + + // CHECK: + // Mangled name: D#foo(kotlin.Int?){} + // Public signature: /D.foo|-4109638554625433529[0] + // Public signature debug description: foo(kotlin.Int?){} + /* fake */ override fun foo(s: Int?): Unit + +} + +// CHECK: +// Mangled name: E +// Public signature: /E|null[0] +class E : Java3 { + // CHECK: + // Mangled name: E#(){} + // Public signature: /E.|-5645683436151566731[0] + // Public signature debug description: (){} + constructor() /* primary */ + + // CHECK JVM_IR: + // Mangled name: E#bar(){}kotlin.Int? + // Public signature: /E.bar|793910862689138186[0] + // Public signature debug description: bar(){}kotlin.Int? + /* fake */ override fun bar(): Int? + + // CHECK: + // Mangled name: E#foo(kotlin.Int?){} + // Public signature: /E.foo|-4109638554625433529[0] + // Public signature debug description: foo(kotlin.Int?){} + /* fake */ override fun foo(s: Int?): Unit + +} + +// CHECK: +// Mangled name: F +// Public signature: /F|null[0] +class F : Java4 { + // CHECK: + // Mangled name: F#(){} + // Public signature: /F.|-5645683436151566731[0] + // Public signature debug description: (){} + constructor() /* primary */ + + // CHECK JVM_IR: + // Mangled name: F#bar(){}kotlin.Int{EnhancedNullability} + // Public signature: /F.bar|-8608075298013083950[0] + // Public signature debug description: bar(){}kotlin.Int{EnhancedNullability} + /* fake */ override fun bar(): @EnhancedNullability Int + + // CHECK: + // Mangled name: F#foo(kotlin.Int{EnhancedNullability}){} + // Public signature: /F.foo|6054053236451526699[0] + // Public signature debug description: foo(kotlin.Int{EnhancedNullability}){} + /* fake */ override fun foo(s: @EnhancedNullability Int): Unit + +} + +// CHECK: +// Mangled name: G +// Public signature: /G|null[0] +class G : Java4 { + // CHECK: + // Mangled name: G#(){} + // Public signature: /G.|-5645683436151566731[0] + // Public signature debug description: (){} + constructor() /* primary */ + + // CHECK JVM_IR: + // Mangled name: G#bar(){}1:0{EnhancedNullability} + // Public signature: /G.bar|582597716914558172[0] + // Public signature debug description: bar(){}1:0{EnhancedNullability} + /* fake */ override fun bar(): @EnhancedNullability T + + // CHECK: + // Mangled name: G#foo(1:0{EnhancedNullability}){} + // Public signature: /G.foo|-5059967198211209740[0] + // Public signature debug description: foo(1:0{EnhancedNullability}){} + /* fake */ override fun foo(s: @EnhancedNullability T): Unit + +} + +// CHECK: +// Mangled name: #test(A;B;C;D;E;F;G){} +// Public signature: /test|-5249852593213170382[0] +// Public signature debug description: test(A;B;C;D;E;F;G){} +fun test(a: A, b: B, c: C, d: D, e: E, f: F, g: G): Unit diff --git a/compiler/testData/ir/irText/fakeOverrides/annotations/typeParameterAnnotationOverride.kt b/compiler/testData/ir/irText/fakeOverrides/annotations/typeParameterAnnotationOverride.kt index e934eb23188..1a920e9edfa 100644 --- a/compiler/testData/ir/irText/fakeOverrides/annotations/typeParameterAnnotationOverride.kt +++ b/compiler/testData/ir/irText/fakeOverrides/annotations/typeParameterAnnotationOverride.kt @@ -1,4 +1,5 @@ // TARGET_BACKEND: JVM +// SEPARATE_SIGNATURE_DUMP_FOR_K2 // FILE: Java1.java import org.jetbrains.annotations.NotNull; diff --git a/compiler/testData/ir/irText/firProblems/SignatureComputationComplexJavaGeneric.fir.ir.txt b/compiler/testData/ir/irText/firProblems/SignatureComputationComplexJavaGeneric.fir.ir.txt new file mode 100644 index 00000000000..e99b987c6db --- /dev/null +++ b/compiler/testData/ir/irText/firProblems/SignatureComputationComplexJavaGeneric.fir.ir.txt @@ -0,0 +1,114 @@ +FILE fqName: fileName:/test.kt + CLASS CLASS name:Inv modality:FINAL visibility:public superTypes:[kotlin.Any] + $this: VALUE_PARAMETER INSTANCE_RECEIVER name: type:.Inv.Inv> + TYPE_PARAMETER name:T index:0 variance: superTypes:[kotlin.Any?] reified:false + PROPERTY name:x visibility:public modality:FINAL [val] + FIELD PROPERTY_BACKING_FIELD name:x type:T of .Inv visibility:private [final] + EXPRESSION_BODY + GET_VAR 'x: T of .Inv declared in .Inv.' type=T of .Inv origin=INITIALIZE_PROPERTY_FROM_PARAMETER + FUN DEFAULT_PROPERTY_ACCESSOR name: visibility:public modality:FINAL <> ($this:.Inv.Inv>) returnType:T of .Inv + correspondingProperty: PROPERTY name:x visibility:public modality:FINAL [val] + $this: VALUE_PARAMETER name: type:.Inv.Inv> + BLOCK_BODY + RETURN type=kotlin.Nothing from='public final fun (): T of .Inv declared in .Inv' + GET_FIELD 'FIELD PROPERTY_BACKING_FIELD name:x type:T of .Inv visibility:private [final]' type=T of .Inv origin=null + receiver: GET_VAR ': .Inv.Inv> declared in .Inv.' type=.Inv.Inv> origin=null + CONSTRUCTOR visibility:public <> (x:T of .Inv) returnType:.Inv.Inv> [primary] + VALUE_PARAMETER name:x index:0 type:T of .Inv + BLOCK_BODY + DELEGATING_CONSTRUCTOR_CALL 'public constructor () declared in kotlin.Any' + INSTANCE_INITIALIZER_CALL classDescriptor='CLASS CLASS name:Inv modality:FINAL visibility:public superTypes:[kotlin.Any]' + FUN FAKE_OVERRIDE name:equals visibility:public modality:OPEN <> ($this:kotlin.Any, other:kotlin.Any?) returnType:kotlin.Boolean [fake_override,operator] + overridden: + public open fun equals (other: kotlin.Any?): kotlin.Boolean declared in kotlin.Any + $this: VALUE_PARAMETER name: type:kotlin.Any + VALUE_PARAMETER name:other index:0 type:kotlin.Any? + FUN FAKE_OVERRIDE name:hashCode visibility:public modality:OPEN <> ($this:kotlin.Any) returnType:kotlin.Int [fake_override] + overridden: + public open fun hashCode (): kotlin.Int declared in kotlin.Any + $this: VALUE_PARAMETER name: type:kotlin.Any + FUN FAKE_OVERRIDE name:toString visibility:public modality:OPEN <> ($this:kotlin.Any) returnType:kotlin.String [fake_override] + overridden: + public open fun toString (): kotlin.String declared in kotlin.Any + $this: VALUE_PARAMETER name: type:kotlin.Any + CLASS CLASS name:Test_1 modality:FINAL visibility:public superTypes:[.JavaClass1.Test_1>] + $this: VALUE_PARAMETER INSTANCE_RECEIVER name: type:.Test_1.Test_1> + TYPE_PARAMETER name:TT index:0 variance: superTypes:[kotlin.Any?] reified:false + PROPERTY name:x visibility:public modality:FINAL [val] + FIELD PROPERTY_BACKING_FIELD name:x type:TT of .Test_1 visibility:private [final] + EXPRESSION_BODY + GET_VAR 'x: TT of .Test_1 declared in .Test_1.' type=TT of .Test_1 origin=INITIALIZE_PROPERTY_FROM_PARAMETER + FUN DEFAULT_PROPERTY_ACCESSOR name: visibility:public modality:FINAL <> ($this:.Test_1.Test_1>) returnType:TT of .Test_1 + correspondingProperty: PROPERTY name:x visibility:public modality:FINAL [val] + $this: VALUE_PARAMETER name: type:.Test_1.Test_1> + BLOCK_BODY + RETURN type=kotlin.Nothing from='public final fun (): TT of .Test_1 declared in .Test_1' + GET_FIELD 'FIELD PROPERTY_BACKING_FIELD name:x type:TT of .Test_1 visibility:private [final]' type=TT of .Test_1 origin=null + receiver: GET_VAR ': .Test_1.Test_1> declared in .Test_1.' type=.Test_1.Test_1> origin=null + CONSTRUCTOR visibility:public <> (x:TT of .Test_1) returnType:.Test_1.Test_1> [primary] + VALUE_PARAMETER name:x index:0 type:TT of .Test_1 + BLOCK_BODY + DELEGATING_CONSTRUCTOR_CALL 'public constructor () declared in .JavaClass1' + : TT of .Test_1 + INSTANCE_INITIALIZER_CALL classDescriptor='CLASS CLASS name:Test_1 modality:FINAL visibility:public superTypes:[.JavaClass1.Test_1>]' + FUN FAKE_OVERRIDE name:equals visibility:public modality:OPEN <> ($this:kotlin.Any, other:kotlin.Any?) returnType:kotlin.Boolean [fake_override,operator] + overridden: + public open fun equals (other: kotlin.Any?): kotlin.Boolean declared in .JavaClass1 + $this: VALUE_PARAMETER name: type:kotlin.Any + VALUE_PARAMETER name:other index:0 type:kotlin.Any? + FUN FAKE_OVERRIDE name:hashCode visibility:public modality:OPEN <> ($this:kotlin.Any) returnType:kotlin.Int [fake_override] + overridden: + public open fun hashCode (): kotlin.Int declared in .JavaClass1 + $this: VALUE_PARAMETER name: type:kotlin.Any + FUN FAKE_OVERRIDE name:toString visibility:public modality:OPEN <> ($this:kotlin.Any) returnType:kotlin.String [fake_override] + overridden: + public open fun toString (): kotlin.String declared in .JavaClass1 + $this: VALUE_PARAMETER name: type:kotlin.Any + FUN name:test visibility:public modality:FINAL <> ($this:.Test_1.Test_1>, b:.JavaClass1.B.Test_1>) returnType:kotlin.Unit + $this: VALUE_PARAMETER name: type:.Test_1.Test_1> + VALUE_PARAMETER name:b index:0 type:.JavaClass1.B.Test_1> + BLOCK_BODY + CALL 'public open fun output (x: @[FlexibleNullability] T of .JavaClass1?): kotlin.Unit declared in .JavaClass1.B' type=kotlin.Unit origin=null + $this: GET_VAR 'b: .JavaClass1.B.Test_1> declared in .Test_1.test' type=.JavaClass1.B.Test_1> origin=null + x: CALL 'public final fun (): TT of .Test_1 declared in .Test_1' type=TT of .Test_1 origin=GET_PROPERTY + $this: GET_VAR ': .Test_1.Test_1> declared in .Test_1.test' type=.Test_1.Test_1> origin=null + CLASS CLASS name:Test_2 modality:FINAL visibility:public superTypes:[.JavaClass2.Test_2, .Inv.Test_2>>] + $this: VALUE_PARAMETER INSTANCE_RECEIVER name: type:.Test_2.Test_2> + TYPE_PARAMETER name:TT index:0 variance: superTypes:[kotlin.Any?] reified:false + CONSTRUCTOR visibility:public <> () returnType:.Test_2.Test_2> [primary] + BLOCK_BODY + DELEGATING_CONSTRUCTOR_CALL 'public constructor () declared in .JavaClass2' + : TT of .Test_2 + : .Inv.Test_2> + INSTANCE_INITIALIZER_CALL classDescriptor='CLASS CLASS name:Test_2 modality:FINAL visibility:public superTypes:[.JavaClass2.Test_2, .Inv.Test_2>>]' + FUN FAKE_OVERRIDE name:equals visibility:public modality:OPEN <> ($this:kotlin.Any, other:kotlin.Any?) returnType:kotlin.Boolean [fake_override,operator] + overridden: + public open fun equals (other: kotlin.Any?): kotlin.Boolean declared in .JavaClass2 + $this: VALUE_PARAMETER name: type:kotlin.Any + VALUE_PARAMETER name:other index:0 type:kotlin.Any? + FUN FAKE_OVERRIDE name:hashCode visibility:public modality:OPEN <> ($this:kotlin.Any) returnType:kotlin.Int [fake_override] + overridden: + public open fun hashCode (): kotlin.Int declared in .JavaClass2 + $this: VALUE_PARAMETER name: type:kotlin.Any + FUN FAKE_OVERRIDE name:toString visibility:public modality:OPEN <> ($this:kotlin.Any) returnType:kotlin.String [fake_override] + overridden: + public open fun toString (): kotlin.String declared in .JavaClass2 + $this: VALUE_PARAMETER name: type:kotlin.Any + FUN name:process visibility:public modality:FINAL <> ($this:.Test_2.Test_2>, b:.JavaClass2.B.Test_2, .Inv.Test_2>>) returnType:kotlin.Unit + $this: VALUE_PARAMETER name: type:.Test_2.Test_2> + VALUE_PARAMETER name:b index:0 type:.JavaClass2.B.Test_2, .Inv.Test_2>> + BLOCK_BODY + CALL 'public abstract fun output (output: @[FlexibleNullability] R of .JavaClass2?): kotlin.Unit declared in .JavaClass2.B' type=kotlin.Unit origin=null + $this: GET_VAR 'b: .JavaClass2.B.Test_2, .Inv.Test_2>> declared in .Test_2.process' type=.JavaClass2.B.Test_2, .Inv.Test_2>> origin=null + output: CONSTRUCTOR_CALL 'public constructor (x: T of .Inv) declared in .Inv' type=.Inv.Test_2> origin=null + : TT of .Test_2 + x: CALL 'public abstract fun element (): @[FlexibleNullability] T of .JavaClass2? declared in .JavaClass2.B' type=@[FlexibleNullability] TT of .Test_2? origin=null + $this: GET_VAR 'b: .JavaClass2.B.Test_2, .Inv.Test_2>> declared in .Test_2.process' type=.JavaClass2.B.Test_2, .Inv.Test_2>> origin=null + FUN name:test_3 visibility:public modality:FINAL (jb:.JavaClass1.B.test_3>, r:R of .test_3) returnType:kotlin.Unit + TYPE_PARAMETER name:R index:0 variance: superTypes:[kotlin.Any?] reified:false + VALUE_PARAMETER name:jb index:0 type:.JavaClass1.B.test_3> + VALUE_PARAMETER name:r index:1 type:R of .test_3 + BLOCK_BODY + CALL 'public open fun output (x: @[FlexibleNullability] T of .JavaClass1?): kotlin.Unit declared in .JavaClass1.B' type=kotlin.Unit origin=null + $this: GET_VAR 'jb: .JavaClass1.B.test_3> declared in .test_3' type=.JavaClass1.B.test_3> origin=null + x: GET_VAR 'r: R of .test_3 declared in .test_3' type=R of .test_3 origin=null diff --git a/compiler/testData/ir/irText/firProblems/SignatureComputationComplexJavaGeneric.fir.kt.txt b/compiler/testData/ir/irText/firProblems/SignatureComputationComplexJavaGeneric.fir.kt.txt new file mode 100644 index 00000000000..2287ae9ac6e --- /dev/null +++ b/compiler/testData/ir/irText/firProblems/SignatureComputationComplexJavaGeneric.fir.kt.txt @@ -0,0 +1,46 @@ +class Inv { + val x: T + field = x + get + + constructor(x: T) /* primary */ { + super/*Any*/() + /* () */ + + } + +} + +class Test_1 : JavaClass1 { + val x: TT + field = x + get + + constructor(x: TT) /* primary */ { + super/*JavaClass1*/() + /* () */ + + } + + fun test(b: B) { + b.output(x = .()) + } + +} + +class Test_2 : JavaClass2> { + constructor() /* primary */ { + super/*JavaClass2*/>() + /* () */ + + } + + fun process(b: B>) { + b.output(output = Inv(x = b.element())) + } + +} + +fun test_3(jb: B, r: R) { + jb.output(x = r) +} diff --git a/compiler/testData/ir/irText/firProblems/SignatureComputationComplexJavaGeneric.kt b/compiler/testData/ir/irText/firProblems/SignatureComputationComplexJavaGeneric.kt index 5f5980251dd..3726655a0e2 100644 --- a/compiler/testData/ir/irText/firProblems/SignatureComputationComplexJavaGeneric.kt +++ b/compiler/testData/ir/irText/firProblems/SignatureComputationComplexJavaGeneric.kt @@ -1,4 +1,3 @@ -// FIR_IDENTICAL // TARGET_BACKEND: JVM_IR // ISSUE: KT-57022 diff --git a/core/compiler.common.jvm/src/org/jetbrains/kotlin/load/java/typeEnhancement/AbstractSignatureParts.kt b/core/compiler.common.jvm/src/org/jetbrains/kotlin/load/java/typeEnhancement/AbstractSignatureParts.kt index fd1213921c1..e0ae0fec31a 100644 --- a/core/compiler.common.jvm/src/org/jetbrains/kotlin/load/java/typeEnhancement/AbstractSignatureParts.kt +++ b/core/compiler.common.jvm/src/org/jetbrains/kotlin/load/java/typeEnhancement/AbstractSignatureParts.kt @@ -8,6 +8,7 @@ package org.jetbrains.kotlin.load.java.typeEnhancement import org.jetbrains.kotlin.builtins.jvm.JavaToKotlinClassMap import org.jetbrains.kotlin.load.java.AbstractAnnotationTypeQualifierResolver import org.jetbrains.kotlin.load.java.AnnotationQualifierApplicabilityType +import org.jetbrains.kotlin.load.java.JavaDefaultQualifiers import org.jetbrains.kotlin.load.java.JavaTypeQualifiersByElementType import org.jetbrains.kotlin.name.FqNameUnsafe import org.jetbrains.kotlin.types.model.KotlinTypeMarker @@ -112,14 +113,14 @@ abstract class AbstractSignatureParts { // happens if T is bounded by @NotNull (technically !! is redundant) or context says unannotated // type parameters are non-null; // T - NOT_NULL, isNotNullTypeParameter = false - // happens if T is bounded by @Nullable (should it?) or context says unannotated types in general are non-null; + // happens if T is bounded by @Nullable (should it? see *) or context says unannotated types in general are non-null; // T? - NULLABLE, isNotNullTypeParameter = false // happens if context says unannotated types in general are nullable. + // (*) in this questionable case K1 counts parameter use-sites as not null, and K2 as flexible if has no other context + // see getDefaultNullability implementations // For other types, this is more straightforward (just take nullability from the context). // TODO: clean up the representation of those cases in JavaTypeQualifiers - val defaultNullability = - referencedParameterBoundsNullability?.copy(qualifier = NullabilityQualifier.NOT_NULL) - ?: defaultTypeQualifier?.nullabilityQualifier + val defaultNullability = getDefaultNullability(referencedParameterBoundsNullability, defaultTypeQualifier) val definitelyNotNull = referencedParameterBoundsNullability?.qualifier == NullabilityQualifier.NOT_NULL || (typeParameterUse != null && defaultTypeQualifier?.definitelyNotNull == true) @@ -132,6 +133,11 @@ abstract class AbstractSignatureParts { return JavaTypeQualifiers(result?.qualifier, annotationsMutability, definitelyNotNull, result?.isForWarningOnly == true) } + protected abstract fun getDefaultNullability( + referencedParameterBoundsNullability: NullabilityQualifierWithMigrationStatus?, + defaultTypeQualifiers: JavaDefaultQualifiers? + ): NullabilityQualifierWithMigrationStatus? + private fun mostSpecific( a: NullabilityQualifierWithMigrationStatus?, b: NullabilityQualifierWithMigrationStatus? diff --git a/core/descriptors.jvm/src/org/jetbrains/kotlin/load/java/typeEnhancement/signatureEnhancement.kt b/core/descriptors.jvm/src/org/jetbrains/kotlin/load/java/typeEnhancement/signatureEnhancement.kt index 0fbc365cfca..164c2b22c3d 100644 --- a/core/descriptors.jvm/src/org/jetbrains/kotlin/load/java/typeEnhancement/signatureEnhancement.kt +++ b/core/descriptors.jvm/src/org/jetbrains/kotlin/load/java/typeEnhancement/signatureEnhancement.kt @@ -278,4 +278,12 @@ private class SignatureParts( override val TypeParameterMarker.isFromJava: Boolean get() = this is LazyJavaTypeParameterDescriptor + + override fun getDefaultNullability( + referencedParameterBoundsNullability: NullabilityQualifierWithMigrationStatus?, + defaultTypeQualifiers: JavaDefaultQualifiers? + ): NullabilityQualifierWithMigrationStatus? { + return referencedParameterBoundsNullability?.copy(qualifier = NullabilityQualifier.NOT_NULL) + ?: defaultTypeQualifiers?.nullabilityQualifier + } }