[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
@@ -34,7 +34,6 @@ import org.jetbrains.kotlin.types.checker.prepareArgumentTypeRegardingCaptureTyp
import org.jetbrains.kotlin.types.typeUtil.isNullableNothing
import org.jetbrains.kotlin.types.typeUtil.makeNullable
import org.jetbrains.kotlin.utils.DFS
import org.jetbrains.kotlin.utils.addToStdlib.safeAs
internal fun unexpectedArgument(argument: KotlinCallArgument): Nothing =
error("Unexpected argument type: $argument, ${argument.javaClass.canonicalName}.")
@@ -90,11 +89,11 @@ internal fun KotlinCallArgument.getExpectedType(parameter: ParameterDescriptor,
) {
parameter.type.unwrap()
} else {
parameter.safeAs<ValueParameterDescriptor>()?.varargElementType?.unwrap() ?: parameter.type.unwrap()
(parameter as? ValueParameterDescriptor)?.varargElementType?.unwrap() ?: parameter.type.unwrap()
}
val ValueParameterDescriptor.isVararg: Boolean get() = varargElementType != null
val ParameterDescriptor.isVararg: Boolean get() = this.safeAs<ValueParameterDescriptor>()?.isVararg ?: false
val ParameterDescriptor.isVararg: Boolean get() = (this as? ValueParameterDescriptor)?.isVararg ?: false
/**
* @return `true` iff the parameter has a default value, i.e. declares it, inherits it by overriding a parameter which has a default value,
@@ -17,7 +17,6 @@ import org.jetbrains.kotlin.resolve.calls.inference.model.TypeVariableTypeConstr
import org.jetbrains.kotlin.types.*
import org.jetbrains.kotlin.types.checker.*
import org.jetbrains.kotlin.types.model.*
import org.jetbrains.kotlin.utils.addToStdlib.cast
class ClassicTypeSystemContextForCS(
override val builtIns: KotlinBuiltIns,
@@ -59,7 +58,8 @@ class ClassicTypeSystemContextForCS(
override fun typeSubstitutorByTypeConstructor(map: Map<TypeConstructorMarker, KotlinTypeMarker>): TypeSubstitutorMarker {
if (map.isEmpty()) return createEmptySubstitutor()
return NewTypeSubstitutorByConstructorMap(map.cast())
@Suppress("UNCHECKED_CAST")
return NewTypeSubstitutorByConstructorMap(map as Map<TypeConstructor, UnwrappedType>)
}
override fun createEmptySubstitutor(): TypeSubstitutorMarker {
@@ -26,7 +26,6 @@ import org.jetbrains.kotlin.types.*
import org.jetbrains.kotlin.types.error.ErrorUtils
import org.jetbrains.kotlin.types.error.ErrorTypeKind
import org.jetbrains.kotlin.types.typeUtil.builtIns
import org.jetbrains.kotlin.utils.addToStdlib.safeAs
fun resolveKtPrimitive(
csBuilder: ConstraintSystemBuilder,
@@ -102,7 +101,7 @@ private fun extraLambdaInfo(
val isSuspend = expectedType?.isSuspendFunctionType ?: false
val isFunctionSupertype = expectedType != null && KotlinBuiltIns.isNotNullOrNullableFunctionSupertype(expectedType)
val argumentAsFunctionExpression = argument.safeAs<FunctionExpression>()
val argumentAsFunctionExpression = argument as? FunctionExpression
val typeVariable = TypeVariableForLambdaReturnType(builtIns, "_L")
@@ -153,7 +152,7 @@ private fun extractLambdaInfoFromFunctionalType(
val expectedParameters = expectedType.getValueParameterTypesFromFunctionType()
val expectedReceiver = expectedType.getReceiverTypeFromFunctionType()?.unwrap()
val expectedContextReceivers = expectedType.getContextReceiverTypesFromFunctionType().map { it.unwrap() }.toTypedArray()
val argumentAsFunctionExpression = argument.safeAs<FunctionExpression>()
val argumentAsFunctionExpression = argument as? FunctionExpression
val receiverFromExpected = argumentAsFunctionExpression?.receiverType == null && expectedReceiver != null
@@ -14,12 +14,13 @@ import org.jetbrains.kotlin.resolve.calls.inference.addSubsystemFromArgument
import org.jetbrains.kotlin.resolve.calls.inference.components.ConstraintSystemCompletionMode
import org.jetbrains.kotlin.resolve.calls.inference.model.BuilderInferencePosition
import org.jetbrains.kotlin.resolve.calls.inference.model.LambdaArgumentConstraintPositionImpl
import org.jetbrains.kotlin.resolve.calls.inference.model.NewTypeVariable
import org.jetbrains.kotlin.resolve.calls.model.*
import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.types.StubTypeForBuilderInference
import org.jetbrains.kotlin.types.UnwrappedType
import org.jetbrains.kotlin.types.model.*
import org.jetbrains.kotlin.types.typeUtil.builtIns
import org.jetbrains.kotlin.utils.addToStdlib.cast
class PostponedArgumentsAnalyzer(
private val callableReferenceArgumentResolver: CallableReferenceArgumentResolver,
@@ -128,6 +129,7 @@ class PostponedArgumentsAnalyzer(
else FilteredAnnotations(annotations, true) { it != StandardNames.FqNames.extensionFunctionType }
}
@Suppress("UNCHECKED_CAST")
val returnArgumentsAnalysisResult = resolutionCallbacks.analyzeAndGetLambdaReturnArguments(
lambda.atom,
lambda.isSuspend,
@@ -136,7 +138,7 @@ class PostponedArgumentsAnalyzer(
parameters,
expectedTypeForReturnArguments,
convertedAnnotations ?: Annotations.EMPTY,
substitutorAndStubsForLambdaAnalysis.stubsForPostponedVariables.cast(),
substitutorAndStubsForLambdaAnalysis.stubsForPostponedVariables as Map<NewTypeVariable, StubTypeForBuilderInference>,
)
applyResultsOfAnalyzedLambdaToCandidateSystem(c, lambda, returnArgumentsAnalysisResult, completionMode, diagnosticHolder, substitute)
return returnArgumentsAnalysisResult
@@ -29,9 +29,7 @@ import org.jetbrains.kotlin.types.error.ErrorUtils
import org.jetbrains.kotlin.types.model.*
import org.jetbrains.kotlin.types.typeUtil.*
import org.jetbrains.kotlin.utils.SmartList
import org.jetbrains.kotlin.utils.addToStdlib.cast
import org.jetbrains.kotlin.utils.addToStdlib.compactIfPossible
import org.jetbrains.kotlin.utils.addToStdlib.safeAs
internal object CheckVisibility : ResolutionPart() {
override fun ResolutionCandidate.process(workIndex: Int) {
@@ -851,7 +849,7 @@ internal object ErrorDescriptorResolutionPart : ResolutionPart() {
resolvedCall.knownParametersSubstitutor = EmptySubstitutor
resolvedCall.argumentToCandidateParameter = emptyMap()
kotlinCall.explicitReceiver?.safeAs<SimpleKotlinCallArgument>()?.let {
(kotlinCall.explicitReceiver as? SimpleKotlinCallArgument)?.let {
resolveKotlinArgument(it, null, ReceiverInfo.notReceiver)
}
for (argument in kotlinCall.argumentsInParenthesis) {
@@ -918,8 +916,10 @@ internal object CheckIncompatibleTypeVariableUpperBounds : ResolutionPart() {
* Check if the candidate was already discriminated by `CompatibilityOfTypeVariableAsIntersectionTypePart` resolution part
* If it's true we shouldn't mark the candidate with warning, but should mark with error, to repeat the existing proper behaviour
*/
private fun ResolutionCandidate.wasPreviouslyDiscriminated(upperTypes: List<KotlinTypeMarker>) =
callComponents.statelessCallbacks.isOldIntersectionIsEmpty(upperTypes.cast())
private fun ResolutionCandidate.wasPreviouslyDiscriminated(upperTypes: List<KotlinTypeMarker>): Boolean {
@Suppress("UNCHECKED_CAST")
return callComponents.statelessCallbacks.isOldIntersectionIsEmpty(upperTypes as List<KotlinType>)
}
override fun ResolutionCandidate.process(workIndex: Int) = with(getSystem().asConstraintSystemCompleterContext()) {
val constraintSystem = getSystem()
@@ -16,7 +16,6 @@ import org.jetbrains.kotlin.resolve.calls.model.SubKotlinCallArgument
import org.jetbrains.kotlin.resolve.descriptorUtil.builtIns
import org.jetbrains.kotlin.types.*
import org.jetbrains.kotlin.types.model.TypeSystemInferenceExtensionContext
import org.jetbrains.kotlin.utils.addToStdlib.safeAs
fun ConstraintStorage.buildResultingSubstitutor(
context: TypeSystemInferenceExtensionContext,
@@ -75,7 +74,7 @@ fun PostponedArgumentsAnalyzerContext.addSubsystemFromArgument(argument: KotlinC
}
is CallableReferenceKotlinCallArgument -> {
addSubsystemFromArgument(argument.lhsResult.safeAs<LHSResult.Expression>()?.lshCallArgument)
addSubsystemFromArgument((argument.lhsResult as? LHSResult.Expression)?.lshCallArgument)
}
else -> false
@@ -20,8 +20,6 @@ import org.jetbrains.kotlin.types.model.TypeVariableTypeConstructorMarker
import org.jetbrains.kotlin.types.model.safeSubstitute
import org.jetbrains.kotlin.types.typeUtil.asTypeProjection
import org.jetbrains.kotlin.utils.addIfNotNull
import org.jetbrains.kotlin.utils.addToStdlib.cast
import org.jetbrains.kotlin.utils.addToStdlib.safeAs
class KotlinConstraintSystemCompleter(
private val resultTypeResolver: ResultTypeResolver,
@@ -291,8 +289,8 @@ class KotlinConstraintSystemCompleter(
argument: PostponedAtomWithRevisableExpectedType,
diagnosticsHolder: KotlinDiagnosticsHolder
): Boolean = with(c) {
val revisedExpectedType: UnwrappedType =
argument.revisedExpectedType?.takeIf { it.isFunctionOrKFunctionWithAnySuspendability() }?.cast() ?: return false
val revisedExpectedType: UnwrappedType = argument.revisedExpectedType
?.takeIf { it.isFunctionOrKFunctionWithAnySuspendability() } as UnwrappedType? ?: return false
when (argument) {
is PostponedCallableReferenceAtom ->
@@ -488,7 +486,7 @@ class KotlinConstraintSystemCompleter(
companion object {
fun getOrderedNotAnalyzedPostponedArguments(topLevelAtoms: List<ResolvedAtom>): List<PostponedResolvedAtom> {
fun ResolvedAtom.process(to: MutableList<PostponedResolvedAtom>) {
to.addIfNotNull(this.safeAs<PostponedResolvedAtom>()?.takeUnless { it.analyzed })
to.addIfNotNull((this as? PostponedResolvedAtom)?.takeUnless { it.analyzed })
if (analyzed) {
subResolvedAtoms?.forEach { it.process(to) }
@@ -6,7 +6,6 @@
package org.jetbrains.kotlin.resolve.calls.model
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.utils.addToStdlib.safeAs
interface KotlinCall : ResolutionAtom {
@@ -42,8 +41,8 @@ fun KotlinCall.checkCallInvariants() {
"Lambda argument or callable reference is not allowed as explicit receiver: $explicitReceiver"
}
explicitReceiver.safeAs<SimpleKotlinCallArgument>()?.checkReceiverInvariants()
dispatchReceiverForInvokeExtension.safeAs<SimpleKotlinCallArgument>()?.checkReceiverInvariants()
(explicitReceiver as? SimpleKotlinCallArgument)?.checkReceiverInvariants()
(dispatchReceiverForInvokeExtension as? SimpleKotlinCallArgument)?.checkReceiverInvariants()
when (callKind) {
KotlinCallKind.FUNCTION, KotlinCallKind.INVOKE -> {
@@ -28,7 +28,6 @@ import org.jetbrains.kotlin.types.UnwrappedType
import org.jetbrains.kotlin.types.model.KotlinTypeMarker
import org.jetbrains.kotlin.types.typeUtil.unCapture
import org.jetbrains.kotlin.utils.addIfNotNull
import org.jetbrains.kotlin.utils.addToStdlib.safeAs
/**
* Call, Callable reference, lambda & function expression, collection literal.
@@ -281,7 +280,7 @@ sealed class CallResolutionResult(
return diagnostics.map {
val error = it.constraintSystemError ?: return@map it
if (error !is NewConstraintMismatch) return@map it
val lowerType = error.lowerType.safeAs<KotlinType>()?.unwrap() ?: return@map it
val lowerType = (error.lowerType as? KotlinType)?.unwrap() ?: return@map it
val newLowerType = substitutor.safeSubstitute(lowerType.unCapture())
when (error) {
is NewConstraintError -> NewConstraintError(newLowerType, error.upperType, error.position).asDiagnostic()
@@ -20,7 +20,6 @@ import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.incremental.components.LookupLocation
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.utils.Printer
import org.jetbrains.kotlin.utils.addToStdlib.safeAs
class LexicalWritableScope(
parent: LexicalScope,
@@ -84,8 +83,7 @@ class LexicalWritableScope(
name: Name,
location: LookupLocation
): DescriptorWithDeprecation<ClassifierDescriptor>? {
return variableOrClassDescriptorByName(name, descriptorLimit)
?.safeAs<ClassifierDescriptor>()
return (variableOrClassDescriptorByName(name, descriptorLimit) as? ClassifierDescriptor)
?.let { DescriptorWithDeprecation.createNonDeprecated(it) }
}