NI: discard def not null types if they appear in return positions, in inv or in variance
^KT-37343 Fixed
This commit is contained in:
@@ -21,6 +21,7 @@ import org.jetbrains.kotlin.descriptors.annotations.Annotations
|
||||
import org.jetbrains.kotlin.resolve.scopes.MemberScope
|
||||
import org.jetbrains.kotlin.storage.StorageManager
|
||||
import org.jetbrains.kotlin.types.checker.KotlinTypeRefiner
|
||||
import org.jetbrains.kotlin.types.checker.NewCapturedType
|
||||
import org.jetbrains.kotlin.types.checker.NewTypeVariableConstructor
|
||||
import org.jetbrains.kotlin.types.checker.NullabilityChecker
|
||||
import org.jetbrains.kotlin.types.model.DefinitelyNotNullTypeMarker
|
||||
@@ -150,6 +151,9 @@ fun SimpleType.makeSimpleTypeDefinitelyNotNullOrNotNull(): SimpleType =
|
||||
?: makeIntersectionTypeDefinitelyNotNullOrNotNull()
|
||||
?: makeNullableAsSpecified(false)
|
||||
|
||||
fun NewCapturedType.withNotNullProjection() =
|
||||
NewCapturedType(captureStatus, constructor, lowerType, annotations, isMarkedNullable, isProjectionNotNull = true)
|
||||
|
||||
fun UnwrappedType.makeDefinitelyNotNullOrNotNull(): UnwrappedType =
|
||||
DefinitelyNotNullType.makeDefinitelyNotNull(this)
|
||||
?: makeIntersectionTypeDefinitelyNotNullOrNotNull()
|
||||
|
||||
@@ -405,6 +405,17 @@ interface ClassicTypeSystemContext : TypeSystemInferenceExtensionContext, TypeSy
|
||||
return this.constructor.projection
|
||||
}
|
||||
|
||||
override fun CapturedTypeMarker.withNotNullProjection(): KotlinTypeMarker {
|
||||
require(this is NewCapturedType, this::errorMessage)
|
||||
|
||||
return NewCapturedType(captureStatus, constructor, lowerType, annotations, isMarkedNullable, isProjectionNotNull = true)
|
||||
}
|
||||
|
||||
override fun CapturedTypeMarker.isProjectionNotNull(): Boolean {
|
||||
require(this is NewCapturedType, this::errorMessage)
|
||||
return this.isProjectionNotNull
|
||||
}
|
||||
|
||||
override fun CapturedTypeMarker.typeParameter(): TypeParameterMarker? {
|
||||
require(this is NewCapturedType, this::errorMessage)
|
||||
return this.constructor.typeParameter
|
||||
|
||||
@@ -94,7 +94,9 @@ object TypeIntersector {
|
||||
* and other types is captured types or type parameters without not-null upper bound. Example: `String? & T` such types we should leave as is.
|
||||
*/
|
||||
val correctNullability = inputTypes.mapTo(LinkedHashSet()) {
|
||||
if (resultNullability == ResultNullability.NOT_NULL) it.makeSimpleTypeDefinitelyNotNullOrNotNull() else it
|
||||
if (resultNullability == ResultNullability.NOT_NULL) {
|
||||
(if (it is NewCapturedType) it.withNotNullProjection() else it).makeSimpleTypeDefinitelyNotNullOrNotNull()
|
||||
} else it
|
||||
}
|
||||
|
||||
return intersectTypesWithoutIntersectionType(correctNullability)
|
||||
|
||||
@@ -110,7 +110,8 @@ class NewCapturedType(
|
||||
override val constructor: NewCapturedTypeConstructor,
|
||||
val lowerType: UnwrappedType?, // todo check lower type for nullable captured types
|
||||
override val annotations: Annotations = Annotations.EMPTY,
|
||||
override val isMarkedNullable: Boolean = false
|
||||
override val isMarkedNullable: Boolean = false,
|
||||
val isProjectionNotNull: Boolean = false
|
||||
) : SimpleType(), CapturedTypeMarker {
|
||||
internal constructor(
|
||||
captureStatus: CaptureStatus, lowerType: UnwrappedType?, projection: TypeProjection, typeParameter: TypeParameterDescriptor
|
||||
|
||||
@@ -508,6 +508,9 @@ object AbstractNullabilityChecker {
|
||||
// i.e. subType is definitely not null
|
||||
if (subType.isDefinitelyNotNullType()) return true
|
||||
|
||||
// i.e. subType is captured type, projection of which is marked not-null
|
||||
if (subType is CapturedTypeMarker && subType.isProjectionNotNull()) return true
|
||||
|
||||
// i.e. subType is not-nullable
|
||||
if (hasNotNullSupertype(subType, SupertypesPolicy.LowerIfFlexible)) return true
|
||||
|
||||
|
||||
@@ -154,6 +154,7 @@ interface TypeSystemInferenceExtensionContext : TypeSystemContext, TypeSystemBui
|
||||
|
||||
fun CapturedTypeMarker.typeConstructorProjection(): TypeArgumentMarker
|
||||
fun CapturedTypeMarker.typeParameter(): TypeParameterMarker?
|
||||
fun CapturedTypeMarker.withNotNullProjection(): KotlinTypeMarker
|
||||
|
||||
fun DefinitelyNotNullTypeMarker.original(): SimpleTypeMarker
|
||||
|
||||
@@ -200,6 +201,7 @@ interface TypeSystemContext : TypeSystemOptimizationContext {
|
||||
|
||||
fun CapturedTypeMarker.typeConstructor(): CapturedTypeConstructorMarker
|
||||
fun CapturedTypeMarker.captureStatus(): CaptureStatus
|
||||
fun CapturedTypeMarker.isProjectionNotNull(): Boolean
|
||||
fun CapturedTypeConstructorMarker.projection(): TypeArgumentMarker
|
||||
|
||||
fun KotlinTypeMarker.argumentsCount(): Int
|
||||
|
||||
Reference in New Issue
Block a user