[FE 1.0] Remove usages of safeAs and cast from most of FE 1.0 modules:

- :core:descriptors
- :core:descriptors.jvm
- :core:deserialization
- :compiler:cli
- :compiler:frontend
- :compiler:frontend:cfg
- :compiler:frontend.java
- :compiler:frontend.common.jvm
- :compiler:psi
- :compiler:resolution
- :compiler:resolution.common
- :compiler:resolution.common.jvm
- :kotlin-reflect-api
This commit is contained in:
Dmitriy Novozhilov
2022-10-11 15:26:45 +03:00
committed by Space Team
parent 6afceb1e84
commit d423782fac
65 changed files with 156 additions and 241 deletions
@@ -13,7 +13,6 @@ import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.types.TypeSubstitutor
import org.jetbrains.kotlin.types.TypeUtils
import org.jetbrains.kotlin.types.Variance
import org.jetbrains.kotlin.utils.addToStdlib.safeAs
val JVM_INLINE_ANNOTATION_FQ_NAME = FqName("kotlin.jvm.JvmInline")
val JVM_INLINE_ANNOTATION_CLASS_ID = ClassId.topLevel(JVM_INLINE_ANNOTATION_FQ_NAME)
@@ -28,10 +27,10 @@ fun DeclarationDescriptor.isMultiFieldValueClass(): Boolean =
fun DeclarationDescriptor.isValueClass(): Boolean = isInlineClass() || isMultiFieldValueClass()
fun KotlinType.unsubstitutedUnderlyingType(): KotlinType? =
constructor.declarationDescriptor.safeAs<ClassDescriptor>()?.inlineClassRepresentation?.underlyingType
(constructor.declarationDescriptor as? ClassDescriptor)?.inlineClassRepresentation?.underlyingType
fun KotlinType.unsubstitutedUnderlyingTypes(): List<KotlinType> {
val declarationDescriptor = constructor.declarationDescriptor.safeAs<ClassDescriptor>() ?: return emptyList()
val declarationDescriptor = constructor.declarationDescriptor as? ClassDescriptor ?: return emptyList()
return when {
declarationDescriptor.isInlineClass() -> listOfNotNull(unsubstitutedUnderlyingType())
declarationDescriptor.isMultiFieldValueClass() ->
@@ -19,7 +19,6 @@ import org.jetbrains.kotlin.types.checker.REFINER_CAPABILITY
import org.jetbrains.kotlin.types.checker.TypeRefinementSupport
import org.jetbrains.kotlin.types.model.KotlinTypeMarker
import org.jetbrains.kotlin.utils.DFS
import org.jetbrains.kotlin.utils.addToStdlib.safeAs
@OptIn(TypeRefinement::class)
class KotlinTypeRefinerImpl(
@@ -203,8 +202,8 @@ private val TypeConstructor.allDependentTypeConstructors: Collection<TypeConstru
else -> supertypes.map { it.constructor }
}
private fun TypeConstructor.isExpectClass() =
declarationDescriptor?.safeAs<ClassDescriptor>()?.isExpect == true
private fun TypeConstructor.isExpectClass(): Boolean =
(declarationDescriptor as? ClassDescriptor)?.isExpect == true
private fun KotlinType.restoreAdditionalTypeInformation(prototype: KotlinType): KotlinType {
return TypeUtils.makeNullableAsSpecified(this, prototype.isMarkedNullable).replace(prototype.arguments)
@@ -28,7 +28,6 @@ import org.jetbrains.kotlin.types.checker.*
import org.jetbrains.kotlin.types.error.ErrorType
import org.jetbrains.kotlin.types.model.TypeArgumentMarker
import org.jetbrains.kotlin.types.model.TypeVariableTypeConstructorMarker
import org.jetbrains.kotlin.utils.addToStdlib.safeAs
import org.jetbrains.kotlin.types.error.ErrorUtils
import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.contract
@@ -337,7 +336,7 @@ fun SimpleType.unCapture(): UnwrappedType {
}
fun unCaptureProjection(projection: TypeProjection): TypeProjection {
val unCapturedProjection = projection.type.constructor.safeAs<NewCapturedTypeConstructor>()?.projection ?: projection
val unCapturedProjection = (projection.type.constructor as? NewCapturedTypeConstructor)?.projection ?: projection
if (unCapturedProjection.isStarProjection || unCapturedProjection.type is ErrorType) return unCapturedProjection
val newArguments = unCapturedProjection.type.arguments.map(::unCaptureProjection)
@@ -30,7 +30,6 @@ import org.jetbrains.kotlin.types.model.*
import org.jetbrains.kotlin.types.typeUtil.*
import org.jetbrains.kotlin.utils.addIfNotNull
import org.jetbrains.kotlin.utils.addToStdlib.firstIsInstanceOrNull
import org.jetbrains.kotlin.utils.addToStdlib.safeAs
import org.jetbrains.kotlin.types.typeUtil.isSignedOrUnsignedNumberType as classicIsSignedOrUnsignedNumberType
import org.jetbrains.kotlin.types.typeUtil.isStubType as isSimpleTypeStubType
import org.jetbrains.kotlin.types.typeUtil.isStubTypeForBuilderInference as isSimpleTypeStubTypeForBuilderInference
@@ -837,7 +836,7 @@ interface ClassicTypeSystemContext : TypeSystemInferenceExtensionContext, TypeSy
require(firstCandidate is KotlinType, this::errorMessage)
require(secondCandidate is KotlinType, this::errorMessage)
firstCandidate.constructor.safeAs<IntersectionTypeConstructor>()?.let { intersectionConstructor ->
(firstCandidate.constructor as? IntersectionTypeConstructor)?.let { intersectionConstructor ->
val intersectionTypeWithAlternative = intersectionConstructor.setAlternative(secondCandidate).createType()
return if (firstCandidate.isMarkedNullable) intersectionTypeWithAlternative.makeNullableAsSpecified(true)
else intersectionTypeWithAlternative
@@ -16,7 +16,6 @@ import org.jetbrains.kotlin.types.TypeProjection
import org.jetbrains.kotlin.types.isError
import org.jetbrains.kotlin.types.typeUtil.contains
import org.jetbrains.kotlin.types.typeUtil.isUnresolvedType
import org.jetbrains.kotlin.utils.addToStdlib.cast
object ErrorUtils {
val errorModule: ModuleDescriptor = ErrorModuleDescriptor
@@ -110,6 +109,6 @@ object ErrorUtils {
fun unresolvedTypeAsItIs(type: KotlinType): String {
assert(isUnresolvedType(type))
return type.constructor.cast<ErrorTypeConstructor>().getParam(0)
return (type.constructor as ErrorTypeConstructor).getParam(0)
}
}
@@ -54,7 +54,6 @@ import org.jetbrains.kotlin.util.OperatorNameConventions.SIMPLE_UNARY_OPERATION_
import org.jetbrains.kotlin.util.ReturnsCheck.*
import org.jetbrains.kotlin.util.ValueParameterCountCheck.NoValueParameters
import org.jetbrains.kotlin.util.ValueParameterCountCheck.SingleValueParameter
import org.jetbrains.kotlin.utils.addToStdlib.safeAs
sealed class CheckResult(val isSuccess: Boolean) {
class IllegalSignature(val error: String) : CheckResult(false)
@@ -241,8 +240,7 @@ object OperatorChecks : AbstractModifierChecks() {
val potentialActualAliasId = classDescriptor.classId ?: return false
val actualReceiverTypeAlias =
classDescriptor.module.findClassifierAcrossModuleDependencies(potentialActualAliasId).safeAs<TypeAliasDescriptor>()
?: return false
classDescriptor.module.findClassifierAcrossModuleDependencies(potentialActualAliasId) as? TypeAliasDescriptor ?: return false
returnType?.let { returnType ->
return returnType.isSubtypeOf(actualReceiverTypeAlias.expandedType)