From a8a6f51a0e1ece11965db438bd046a4c34d32b8d Mon Sep 17 00:00:00 2001 From: Dmitry Gridin Date: Fri, 5 Mar 2021 21:24:26 +0700 Subject: [PATCH] [common] fix flexible type approximation in renders ^KTIJ-3030 Fixed (cherry picked from commit 4855d88a9f6fbce9aeeea6fe1f29dd099a833aed) KT-CR-2373 --- .../jetbrains/kotlin/idea/util/TypeUtils.kt | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/plugins/scripting/scripting-ide-common/src/org/jetbrains/kotlin/idea/util/TypeUtils.kt b/plugins/scripting/scripting-ide-common/src/org/jetbrains/kotlin/idea/util/TypeUtils.kt index 7f9ab6c6248..0b6859784e6 100644 --- a/plugins/scripting/scripting-ide-common/src/org/jetbrains/kotlin/idea/util/TypeUtils.kt +++ b/plugins/scripting/scripting-ide-common/src/org/jetbrains/kotlin/idea/util/TypeUtils.kt @@ -32,7 +32,9 @@ private fun KotlinType.approximateNonDynamicFlexibleTypes( if (isFlexible()) { val flexible = asFlexibleType() - val lowerClass = flexible.lowerBound.constructor.declarationDescriptor as? ClassDescriptor? + val lowerBound = flexible.lowerBound + val upperBound = flexible.upperBound + val lowerClass = lowerBound.constructor.declarationDescriptor as? ClassDescriptor? val isCollection = lowerClass != null && JavaToKotlinClassMapper.isMutable(lowerClass) // (Mutable)Collection! -> MutableCollection? // Foo<(Mutable)Collection!>! -> Foo>? @@ -40,17 +42,21 @@ private fun KotlinType.approximateNonDynamicFlexibleTypes( // Foo! -> Foo? var approximation = if (isCollection) - flexible.lowerBound.makeNullableAsSpecified(!preferNotNull) - else - if (this is RawType && preferStarForRaw) flexible.upperBound.makeNullableAsSpecified(!preferNotNull) + // (Mutable)Collection! + if (lowerBound.isMarkedNullable != upperBound.isMarkedNullable) + lowerBound.makeNullableAsSpecified(!preferNotNull) else - if (preferNotNull) flexible.lowerBound else flexible.upperBound + lowerBound + else + if (this is RawType && preferStarForRaw) upperBound.makeNullableAsSpecified(!preferNotNull) + else + if (preferNotNull) lowerBound else upperBound approximation = approximation.approximateNonDynamicFlexibleTypes() approximation = if (nullability() == TypeNullability.NOT_NULL) approximation.makeNullableAsSpecified(false) else approximation - if (approximation.isMarkedNullable && !flexible.lowerBound + if (approximation.isMarkedNullable && !lowerBound .isMarkedNullable && TypeUtils.isTypeParameter(approximation) && TypeUtils.hasNullableSuperType(approximation) ) { approximation = approximation.makeNullableAsSpecified(false)