diff --git a/compiler/frontend.java/src/org/jetbrains/kotlin/resolve/jvm/checkers/JavaNullabilityChecker.kt b/compiler/frontend.java/src/org/jetbrains/kotlin/resolve/jvm/checkers/JavaNullabilityChecker.kt index 2f4913cc14d..0047400f59a 100644 --- a/compiler/frontend.java/src/org/jetbrains/kotlin/resolve/jvm/checkers/JavaNullabilityChecker.kt +++ b/compiler/frontend.java/src/org/jetbrains/kotlin/resolve/jvm/checkers/JavaNullabilityChecker.kt @@ -285,7 +285,7 @@ class JavaNullabilityChecker(val upperBoundChecker: UpperBoundChecker) : Additio subType: KotlinType, superType: KotlinType ): Boolean { - if (superType !is NotNullTypeVariable || subType is NotNullTypeVariable) return false + if (superType !is NotNullTypeParameter || subType is NotNullTypeParameter) return false return !AbstractNullabilityChecker.isSubtypeOfAny( createClassicTypeCheckerState(isErrorTypeEqualsToAnything = true), subType diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/inference/ConstraintSystemBuilderImpl.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/inference/ConstraintSystemBuilderImpl.kt index 4c6f9fe98ff..d55b47b1844 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/inference/ConstraintSystemBuilderImpl.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/inference/ConstraintSystemBuilderImpl.kt @@ -44,8 +44,6 @@ import org.jetbrains.kotlin.types.typeUtil.builtIns import org.jetbrains.kotlin.types.typeUtil.defaultProjections import org.jetbrains.kotlin.types.typeUtil.isDefaultBound import org.jetbrains.kotlin.utils.addToStdlib.cast -import java.lang.IllegalArgumentException -import java.lang.IllegalStateException import java.util.* open class ConstraintSystemBuilderImpl(private val mode: Mode = ConstraintSystemBuilderImpl.Mode.INFERENCE) : ConstraintSystem.Builder { @@ -310,9 +308,9 @@ open class ConstraintSystemBuilderImpl(private val mode: Mode = ConstraintSystem // Foo >: T! // both Foo and Foo? transform to Foo! here if (parameterType.isFlexible()) { - val customTypeVariable = parameterType.getCustomTypeVariable() - if (customTypeVariable != null) { - newConstrainingType = customTypeVariable.substitutionResult(constrainingType) + val customTypeParameter = parameterType.getCustomTypeParameter() + if (customTypeParameter != null) { + newConstrainingType = customTypeParameter.substitutionResult(constrainingType) } } diff --git a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/components/SimpleArgumentsChecks.kt b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/components/SimpleArgumentsChecks.kt index cf1ffb638d5..2a57a842589 100644 --- a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/components/SimpleArgumentsChecks.kt +++ b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/components/SimpleArgumentsChecks.kt @@ -96,7 +96,7 @@ private fun checkExpressionArgument( val position = if (isReceiver) ReceiverConstraintPositionImpl(expressionArgument) else ArgumentConstraintPositionImpl(expressionArgument) // Used only for arguments with @NotNull annotation - if (expectedType is NotNullTypeVariable && argumentType.isMarkedNullable) { + if (expectedType is NotNullTypeParameter && argumentType.isMarkedNullable) { diagnosticsHolder.addDiagnostic(ArgumentNullabilityMismatchDiagnostic(expectedType, argumentType, expressionArgument)) } diff --git a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/components/NewTypeSubstitutor.kt b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/components/NewTypeSubstitutor.kt index 1bf30bcb8db..3bab1500451 100644 --- a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/components/NewTypeSubstitutor.kt +++ b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/components/NewTypeSubstitutor.kt @@ -156,7 +156,7 @@ interface NewTypeSubstitutor : TypeSubstitutorMarker { if (type.isDefinitelyNotNullType) { replacement = replacement.makeDefinitelyNotNullOrNotNull() } - if (type is CustomTypeVariable) { + if (type is CustomTypeParameter) { replacement = type.substitutionResult(replacement).unwrap() } diff --git a/core/compiler.common/src/org/jetbrains/kotlin/types/AbstractTypeChecker.kt b/core/compiler.common/src/org/jetbrains/kotlin/types/AbstractTypeChecker.kt index db6566b5d51..cffe1241284 100644 --- a/core/compiler.common/src/org/jetbrains/kotlin/types/AbstractTypeChecker.kt +++ b/core/compiler.common/src/org/jetbrains/kotlin/types/AbstractTypeChecker.kt @@ -448,7 +448,7 @@ object AbstractTypeChecker { @OptIn(ObsoleteTypeKind::class) private fun TypeSystemContext.isCommonDenotableType(type: KotlinTypeMarker): Boolean = type.typeConstructor().isDenotable() && - !type.isDynamic() && !type.isDefinitelyNotNullType() && !type.isNotNullTypeVariable() && + !type.isDynamic() && !type.isDefinitelyNotNullType() && !type.isNotNullTypeParameter() && type.lowerBoundIfFlexible().typeConstructor() == type.upperBoundIfFlexible().typeConstructor() fun effectiveVariance(declared: TypeVariance, useSite: TypeVariance): TypeVariance? { @@ -704,7 +704,7 @@ object AbstractNullabilityChecker { // i.e. subType is definitely not null @OptIn(ObsoleteTypeKind::class) - if (subType.isDefinitelyNotNullType() || subType.isNotNullTypeVariable()) return true + if (subType.isDefinitelyNotNullType() || subType.isNotNullTypeParameter()) return true // i.e. subType is captured type, projection of which is marked not-null if (subType is CapturedTypeMarker && subType.isProjectionNotNull()) return true diff --git a/core/compiler.common/src/org/jetbrains/kotlin/types/model/TypeSystemContext.kt b/core/compiler.common/src/org/jetbrains/kotlin/types/model/TypeSystemContext.kt index 3e0314da714..aab9bace0ba 100644 --- a/core/compiler.common/src/org/jetbrains/kotlin/types/model/TypeSystemContext.kt +++ b/core/compiler.common/src/org/jetbrains/kotlin/types/model/TypeSystemContext.kt @@ -380,7 +380,7 @@ interface TypeSystemContext : TypeSystemOptimizationContext { // This kind of types is obsolete (expected to be removed at 1.7) and shouldn't be used further in a new code // Now, such types are being replaced with definitely non-nullable types @ObsoleteTypeKind - fun KotlinTypeMarker.isNotNullTypeVariable(): Boolean = false + fun KotlinTypeMarker.isNotNullTypeParameter(): Boolean = false fun KotlinTypeMarker.hasFlexibleNullability() = lowerBoundIfFlexible().isMarkedNullable() != upperBoundIfFlexible().isMarkedNullable() 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 28638b928ad..783ae48d6ea 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 @@ -254,7 +254,7 @@ private class SignatureParts( get() = TypeUtils.getClassDescriptor(this as KotlinType)?.let { DescriptorUtils.getFqName(it) } override val KotlinTypeMarker.isNotNullTypeParameterCompat: Boolean - get() = (this as KotlinType).unwrap() is NotNullTypeParameter + get() = (this as KotlinType).unwrap() is NotNullTypeParameterImpl override fun KotlinTypeMarker.isEqual(other: KotlinTypeMarker): Boolean = containerContext.components.kotlinTypeChecker.equalTypes(this as KotlinType, other as KotlinType) diff --git a/core/descriptors.jvm/src/org/jetbrains/kotlin/load/java/typeEnhancement/typeEnhancement.kt b/core/descriptors.jvm/src/org/jetbrains/kotlin/load/java/typeEnhancement/typeEnhancement.kt index b964eee52ea..bfd705d36ea 100644 --- a/core/descriptors.jvm/src/org/jetbrains/kotlin/load/java/typeEnhancement/typeEnhancement.kt +++ b/core/descriptors.jvm/src/org/jetbrains/kotlin/load/java/typeEnhancement/typeEnhancement.kt @@ -34,7 +34,6 @@ import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.resolve.constants.ConstantValue import org.jetbrains.kotlin.types.* import org.jetbrains.kotlin.types.checker.SimpleClassicTypeSystemContext -import org.jetbrains.kotlin.types.TypeRefinement import org.jetbrains.kotlin.types.typeUtil.createProjection import org.jetbrains.kotlin.types.typeUtil.isTypeParameter @@ -166,7 +165,7 @@ class JavaTypeEnhancement(private val javaResolverSettings: JavaResolverSettings if (javaResolverSettings.correctNullabilityForNotNullTypeParameter) enhancedType.makeSimpleTypeDefinitelyNotNullOrNotNull(useCorrectedNullabilityForTypeParameters = true) else - NotNullTypeParameter(enhancedType) + NotNullTypeParameterImpl(enhancedType) } private fun List.compositeAnnotationsOrSingle() = when (size) { @@ -224,9 +223,9 @@ private object EnhancedTypeAnnotationDescriptor : AnnotationDescriptor { override fun toString() = "[EnhancedType]" } -internal class NotNullTypeParameter(override val delegate: SimpleType) : NotNullTypeVariable, DelegatingSimpleType() { +internal class NotNullTypeParameterImpl(override val delegate: SimpleType) : NotNullTypeParameter, DelegatingSimpleType() { - override val isTypeVariable: Boolean + override val isTypeParameter: Boolean get() = true override fun substitutionResult(replacement: KotlinType): KotlinType { @@ -250,13 +249,13 @@ internal class NotNullTypeParameter(override val delegate: SimpleType) : NotNull val result = makeNullableAsSpecified(false) if (!this.isTypeParameter()) return result - return NotNullTypeParameter(result) + return NotNullTypeParameterImpl(result) } - override fun replaceAnnotations(newAnnotations: Annotations) = NotNullTypeParameter(delegate.replaceAnnotations(newAnnotations)) + override fun replaceAnnotations(newAnnotations: Annotations) = NotNullTypeParameterImpl(delegate.replaceAnnotations(newAnnotations)) override fun makeNullableAsSpecified(newNullability: Boolean) = if (newNullability) delegate.makeNullableAsSpecified(true) else this @TypeRefinement - override fun replaceDelegate(delegate: SimpleType) = NotNullTypeParameter(delegate) + override fun replaceDelegate(delegate: SimpleType) = NotNullTypeParameterImpl(delegate) } diff --git a/core/descriptors/src/org/jetbrains/kotlin/types/SpecialTypes.kt b/core/descriptors/src/org/jetbrains/kotlin/types/SpecialTypes.kt index 00de767e3e9..aa0ebe3d023 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/types/SpecialTypes.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/types/SpecialTypes.kt @@ -93,7 +93,7 @@ class LazyWrappedType( class DefinitelyNotNullType private constructor( val original: SimpleType, private val useCorrectedNullabilityForTypeParameters: Boolean -) : DelegatingSimpleType(), CustomTypeVariable, +) : DelegatingSimpleType(), CustomTypeParameter, DefinitelyNotNullTypeMarker { companion object { @@ -161,7 +161,7 @@ class DefinitelyNotNullType private constructor( override val isMarkedNullable: Boolean get() = false - override val isTypeVariable: Boolean + override val isTypeParameter: Boolean get() = delegate.constructor is NewTypeVariableConstructor || delegate.constructor.declarationDescriptor is TypeParameterDescriptor diff --git a/core/descriptors/src/org/jetbrains/kotlin/types/TypeCapabilities.kt b/core/descriptors/src/org/jetbrains/kotlin/types/TypeCapabilities.kt index 428df8a0d3f..6e4eb1d137e 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/types/TypeCapabilities.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/types/TypeCapabilities.kt @@ -18,24 +18,24 @@ package org.jetbrains.kotlin.types // To facilitate laziness, any KotlinType implementation may inherit from this trait, -// even if it turns out that the type an instance represents is not actually a type variable -// (i.e. it is not derived from a type parameter), see isTypeVariable -interface CustomTypeVariable { - val isTypeVariable: Boolean +// even if it turns out that the type an instance represents is not actually a type parameter +// (i.e. it is not derived from a type parameter), see isTypeParameter +interface CustomTypeParameter { + val isTypeParameter: Boolean - // Throws an exception when isTypeVariable == false + // Throws an exception when isTypeParameter == false fun substitutionResult(replacement: KotlinType): KotlinType } // That interface is needed to provide information about definitely not null // type parameters (e.g. from @NotNull annotation) to type system -interface NotNullTypeVariable : CustomTypeVariable +interface NotNullTypeParameter : CustomTypeParameter -fun KotlinType.isCustomTypeVariable(): Boolean = (unwrap() as? CustomTypeVariable)?.isTypeVariable ?: false -fun KotlinType.getCustomTypeVariable(): CustomTypeVariable? = - (unwrap() as? CustomTypeVariable)?.let { - if (it.isTypeVariable) it else null - } +fun KotlinType.isCustomTypeParameter(): Boolean = (unwrap() as? CustomTypeParameter)?.isTypeParameter ?: false +fun KotlinType.getCustomTypeParameter(): CustomTypeParameter? = + (unwrap() as? CustomTypeParameter)?.let { + if (it.isTypeParameter) it else null + } interface SubtypingRepresentatives { val subTypeRepresentative: KotlinType @@ -45,13 +45,13 @@ interface SubtypingRepresentatives { } fun KotlinType.getSubtypeRepresentative(): KotlinType = - (unwrap() as? SubtypingRepresentatives)?.subTypeRepresentative ?: this + (unwrap() as? SubtypingRepresentatives)?.subTypeRepresentative ?: this fun KotlinType.getSupertypeRepresentative(): KotlinType = - (unwrap() as? SubtypingRepresentatives)?.superTypeRepresentative ?: this + (unwrap() as? SubtypingRepresentatives)?.superTypeRepresentative ?: this fun sameTypeConstructors(first: KotlinType, second: KotlinType): Boolean { return (first.unwrap() as? SubtypingRepresentatives)?.sameTypeConstructor(second) ?: false - || (second.unwrap() as? SubtypingRepresentatives)?.sameTypeConstructor(first) ?: false + || (second.unwrap() as? SubtypingRepresentatives)?.sameTypeConstructor(first) ?: false } diff --git a/core/descriptors/src/org/jetbrains/kotlin/types/TypeSubstitutor.java b/core/descriptors/src/org/jetbrains/kotlin/types/TypeSubstitutor.java index d90993e69f9..2247e8af913 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/types/TypeSubstitutor.java +++ b/core/descriptors/src/org/jetbrains/kotlin/types/TypeSubstitutor.java @@ -208,7 +208,7 @@ public class TypeSubstitutor implements TypeSubstitutorMarker { null; Variance originalProjectionKind = originalProjection.getProjectionKind(); - if (replacement == null && FlexibleTypesKt.isFlexible(type) && !TypeCapabilitiesKt.isCustomTypeVariable(type)) { + if (replacement == null && FlexibleTypesKt.isFlexible(type) && !TypeCapabilitiesKt.isCustomTypeParameter(type)) { FlexibleType flexibleType = FlexibleTypesKt.asFlexibleType(type); TypeProjection substitutedLower = unsafeSubstitute( @@ -255,12 +255,12 @@ public class TypeSubstitutor implements TypeSubstitutorMarker { } } KotlinType substitutedType; - CustomTypeVariable typeVariable = TypeCapabilitiesKt.getCustomTypeVariable(type); + CustomTypeParameter customTypeParameter = TypeCapabilitiesKt.getCustomTypeParameter(type); if (replacement.isStarProjection()) { return replacement; } - else if (typeVariable != null) { - substitutedType = typeVariable.substitutionResult(replacement.getType()); + else if (customTypeParameter != null) { + substitutedType = customTypeParameter.substitutionResult(replacement.getType()); } else { // this is a simple type T or T?: if it's T, we should just take replacement, if T? - we make replacement nullable diff --git a/core/descriptors/src/org/jetbrains/kotlin/types/checker/ClassicTypeSystemContext.kt b/core/descriptors/src/org/jetbrains/kotlin/types/checker/ClassicTypeSystemContext.kt index 13dc77d3f32..27ce646e012 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/types/checker/ClassicTypeSystemContext.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/types/checker/ClassicTypeSystemContext.kt @@ -151,7 +151,7 @@ interface ClassicTypeSystemContext : TypeSystemInferenceExtensionContext, TypeSy } @OptIn(ObsoleteTypeKind::class) - override fun KotlinTypeMarker.isNotNullTypeVariable(): Boolean = this is NotNullTypeVariable + override fun KotlinTypeMarker.isNotNullTypeParameter(): Boolean = this is NotNullTypeParameter override fun SimpleTypeMarker.isMarkedNullable(): Boolean { require(this is SimpleType, this::errorMessage) diff --git a/core/descriptors/src/org/jetbrains/kotlin/types/flexibleTypes.kt b/core/descriptors/src/org/jetbrains/kotlin/types/flexibleTypes.kt index 02239334c3f..802ad7ed1ef 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/types/flexibleTypes.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/types/flexibleTypes.kt @@ -26,7 +26,6 @@ import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameSafe import org.jetbrains.kotlin.types.checker.ErrorTypesAreEqualToAnything import org.jetbrains.kotlin.types.checker.KotlinTypeChecker import org.jetbrains.kotlin.types.checker.KotlinTypeRefiner -import org.jetbrains.kotlin.types.TypeRefinement import org.jetbrains.kotlin.types.typeUtil.builtIns fun KotlinType.isFlexible(): Boolean = unwrap() is FlexibleType @@ -82,7 +81,7 @@ fun KotlinType.upperIfFlexible(): SimpleType = with(unwrap()) { } } -class FlexibleTypeImpl(lowerBound: SimpleType, upperBound: SimpleType) : FlexibleType(lowerBound, upperBound), CustomTypeVariable { +class FlexibleTypeImpl(lowerBound: SimpleType, upperBound: SimpleType) : FlexibleType(lowerBound, upperBound), CustomTypeParameter { companion object { @JvmField var RUN_SLOW_ASSERTIONS = false @@ -114,7 +113,7 @@ class FlexibleTypeImpl(lowerBound: SimpleType, upperBound: SimpleType) : Flexibl return lowerBound } - override val isTypeVariable: Boolean + override val isTypeParameter: Boolean get() = lowerBound.constructor.declarationDescriptor is TypeParameterDescriptor && lowerBound.constructor == upperBound.constructor