From 35b475f9f7209b173ecc37e82c23c57346f25c4e Mon Sep 17 00:00:00 2001 From: Mikhail Glukhikh Date: Mon, 12 Jun 2023 15:01:30 +0200 Subject: [PATCH] K2: Fix processing inference lower bound NullableType <: T & Any This commit is a follow-up to 0d070f8ba9afd7f3d8506f9016cb51929724dc72. Here we remove accidentally returning "Not a sub-type" for a constraint like TypeVariable <: DNN type #KT-59241 Fixed --- .../TypeCheckerStateForConstraintSystem.kt | 8 ++++--- .../notNullTypeParameter/supplier.fir.kt | 23 ------------------- .../notNullTypeParameter/supplier.kt | 1 + 3 files changed, 6 insertions(+), 26 deletions(-) delete mode 100644 compiler/testData/diagnostics/tests/platformTypes/notNullTypeParameter/supplier.fir.kt diff --git a/compiler/resolution.common/src/org/jetbrains/kotlin/resolve/calls/inference/components/TypeCheckerStateForConstraintSystem.kt b/compiler/resolution.common/src/org/jetbrains/kotlin/resolve/calls/inference/components/TypeCheckerStateForConstraintSystem.kt index 3052ed48e67..11ef367b848 100644 --- a/compiler/resolution.common/src/org/jetbrains/kotlin/resolve/calls/inference/components/TypeCheckerStateForConstraintSystem.kt +++ b/compiler/resolution.common/src/org/jetbrains/kotlin/resolve/calls/inference/components/TypeCheckerStateForConstraintSystem.kt @@ -218,12 +218,15 @@ abstract class TypeCheckerStateForConstraintSystem( subType: KotlinTypeMarker, isFromNullabilityConstraint: Boolean = false ): Boolean = with(extensionTypeContext) { + val subTypeConstructor = subType.typeConstructor() val lowerConstraint = when (typeVariable) { is SimpleTypeMarker -> when { // Foo? (any type which cannot be used as dispatch receiver because of nullability) <: T & Any => ERROR (for K2 only) - isK2 && typeVariable.isDefinitelyNotNullType() - && !AbstractNullabilityChecker.isSubtypeOfAny(extensionTypeContext, subType) -> return false + isK2 && typeVariable.isDefinitelyNotNullType() && !subTypeConstructor.isTypeVariable() && + !AbstractNullabilityChecker.isSubtypeOfAny(extensionTypeContext, subType) -> { + return false + } /* * Foo <: T? (T is contained in invariant or contravariant positions of a return type) -- Foo <: T * Example: @@ -236,7 +239,6 @@ abstract class TypeCheckerStateForConstraintSystem( */ typeVariable.isMarkedNullable() -> { val typeVariableTypeConstructor = typeVariable.typeConstructor() - val subTypeConstructor = subType.typeConstructor() val needToMakeDefNotNull = subTypeConstructor.isTypeVariable() || typeVariableTypeConstructor !is TypeVariableTypeConstructorMarker || !typeVariableTypeConstructor.isContainedInInvariantOrContravariantPositions() diff --git a/compiler/testData/diagnostics/tests/platformTypes/notNullTypeParameter/supplier.fir.kt b/compiler/testData/diagnostics/tests/platformTypes/notNullTypeParameter/supplier.fir.kt deleted file mode 100644 index 6dc9246aa0e..00000000000 --- a/compiler/testData/diagnostics/tests/platformTypes/notNullTypeParameter/supplier.fir.kt +++ /dev/null @@ -1,23 +0,0 @@ -// FULL_JDK - -// FILE: JavaClass.java -import java.util.function.Supplier; -import org.jetbrains.annotations.NotNull; - -public class JavaClass { - public static void consume(@NotNull E value) {} - public static void compute(Supplier<@NotNull E> computable) {} -} - -// FILE: KotlinMain.kt -fun test1() { - JavaClass.compute { 42.apply {} } -} - -fun test2() { - JavaClass.compute { if (true) 42 else 42 } -} - -fun test3() { - 42.apply(JavaClass::consume) -} diff --git a/compiler/testData/diagnostics/tests/platformTypes/notNullTypeParameter/supplier.kt b/compiler/testData/diagnostics/tests/platformTypes/notNullTypeParameter/supplier.kt index a58bb08e214..0b106f875e4 100644 --- a/compiler/testData/diagnostics/tests/platformTypes/notNullTypeParameter/supplier.kt +++ b/compiler/testData/diagnostics/tests/platformTypes/notNullTypeParameter/supplier.kt @@ -1,3 +1,4 @@ +// FIR_IDENTICAL // FULL_JDK // FILE: JavaClass.java