Remove unsafe cast function usages from IR modules

This commit is contained in:
Alexander Udalov
2022-11-01 14:04:39 +01:00
committed by Space Team
parent 1418423423
commit fd9b19ee49
11 changed files with 15 additions and 31 deletions
-3
View File
@@ -371,9 +371,6 @@ val projectsWithOptInToUnsafeCastFunctionsFromAddToStdLib by extra {
":analysis:symbol-light-classes", ":analysis:symbol-light-classes",
":compiler", ":compiler",
":compiler:backend.js", ":compiler:backend.js",
":compiler:ir.backend.common",
":compiler:ir.psi2ir",
":compiler:ir.tree",
":compiler:light-classes", ":compiler:light-classes",
":core:reflection.jvm", ":core:reflection.jvm",
":jps:jps-common", ":jps:jps-common",
@@ -22,7 +22,6 @@ import org.jetbrains.kotlin.ir.util.*
import org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid import org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid
import org.jetbrains.kotlin.resolve.descriptorUtil.module import org.jetbrains.kotlin.resolve.descriptorUtil.module
import org.jetbrains.kotlin.resolve.multiplatform.* import org.jetbrains.kotlin.resolve.multiplatform.*
import org.jetbrains.kotlin.utils.addToStdlib.safeAs
// `doRemove` means should expect-declaration be removed from IR // `doRemove` means should expect-declaration be removed from IR
@OptIn(ObsoleteDescriptorBasedAPI::class) @OptIn(ObsoleteDescriptorBasedAPI::class)
@@ -229,7 +228,7 @@ class ExpectDeclarationRemover(val symbolTable: ReferenceSymbolTable, private va
// Nothing we can do with those. // Nothing we can do with those.
// TODO they may not actually have the defaults though -- may be a frontend bug. // TODO they may not actually have the defaults though -- may be a frontend bug.
val expectFunction = val expectFunction =
function.descriptor.findExpectForActual().safeAs<FunctionDescriptor>()?.let { symbolTable.referenceFunction(it).owner } (function.descriptor.findExpectForActual() as? FunctionDescriptor)?.let { symbolTable.referenceFunction(it).owner }
?: return ?: return
val defaultValue = expectFunction.valueParameters[index].defaultValue ?: return val defaultValue = expectFunction.valueParameters[index].defaultValue ?: return
@@ -34,7 +34,6 @@ import org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid
import org.jetbrains.kotlin.ir.visitors.IrElementVisitor import org.jetbrains.kotlin.ir.visitors.IrElementVisitor
import org.jetbrains.kotlin.ir.visitors.transformChildrenVoid import org.jetbrains.kotlin.ir.visitors.transformChildrenVoid
import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.utils.addToStdlib.safeAs
interface VisibilityPolicy { interface VisibilityPolicy {
fun forClass(declaration: IrClass, inInlineFunctionScope: Boolean): DescriptorVisibility = fun forClass(declaration: IrClass, inInlineFunctionScope: Boolean): DescriptorVisibility =
@@ -909,7 +908,7 @@ class LocalDeclarationsLowering(
get() { get() {
val parentFun = parent as? IrSimpleFunction val parentFun = parent as? IrSimpleFunction
?: throw AssertionError("Extension receiver parent is not a simple function: ${parent.render()}") ?: throw AssertionError("Extension receiver parent is not a simple function: ${parent.render()}")
val correspondingProperty = parentFun.safeAs<IrSimpleFunction>()?.correspondingPropertySymbol?.owner val correspondingProperty = parentFun.correspondingPropertySymbol?.owner
return when { return when {
correspondingProperty != null -> correspondingProperty != null ->
correspondingProperty.name.asStringStripSpecialMarkers() correspondingProperty.name.asStringStripSpecialMarkers()
@@ -51,7 +51,6 @@ import org.jetbrains.kotlin.resolve.scopes.receivers.*
import org.jetbrains.kotlin.types.* import org.jetbrains.kotlin.types.*
import org.jetbrains.kotlin.types.typeUtil.isUnit import org.jetbrains.kotlin.types.typeUtil.isUnit
import org.jetbrains.kotlin.util.OperatorNameConventions import org.jetbrains.kotlin.util.OperatorNameConventions
import org.jetbrains.kotlin.utils.addToStdlib.safeAs
import kotlin.math.max import kotlin.math.max
import kotlin.math.min import kotlin.math.min
@@ -629,7 +628,7 @@ fun StatementGenerator.generateSamConversionForValueArgumentsIfRequired(call: Ca
"$originalDescriptor has ${originalDescriptor.typeParameters}" "$originalDescriptor has ${originalDescriptor.typeParameters}"
} }
val resolvedCallArguments = resolvedCall.safeAs<NewResolvedCallImpl<*>>()?.argumentMappingByOriginal?.values val resolvedCallArguments = (resolvedCall as? NewResolvedCallImpl<*>)?.argumentMappingByOriginal?.values
assert(resolvedCallArguments == null || resolvedCallArguments.size == underlyingValueParameters.size) { assert(resolvedCallArguments == null || resolvedCallArguments.size == underlyingValueParameters.size) {
"Mismatching resolved call arguments:\n" + "Mismatching resolved call arguments:\n" +
"${resolvedCallArguments?.size} != ${underlyingValueParameters.size}" "${resolvedCallArguments?.size} != ${underlyingValueParameters.size}"
@@ -30,7 +30,6 @@ import org.jetbrains.kotlin.resolve.constants.evaluate.ConstantExpressionEvaluat
import org.jetbrains.kotlin.resolve.descriptorUtil.isAnnotationConstructor import org.jetbrains.kotlin.resolve.descriptorUtil.isAnnotationConstructor
import org.jetbrains.kotlin.resolve.descriptorUtil.isEffectivelyExternal import org.jetbrains.kotlin.resolve.descriptorUtil.isEffectivelyExternal
import org.jetbrains.kotlin.resolve.descriptorUtil.propertyIfAccessor import org.jetbrains.kotlin.resolve.descriptorUtil.propertyIfAccessor
import org.jetbrains.kotlin.utils.addToStdlib.safeAs
@ObsoleteDescriptorBasedAPI @ObsoleteDescriptorBasedAPI
class FunctionGenerator(declarationGenerator: DeclarationGenerator) : DeclarationGeneratorExtension(declarationGenerator) { class FunctionGenerator(declarationGenerator: DeclarationGenerator) : DeclarationGeneratorExtension(declarationGenerator) {
@@ -352,7 +351,7 @@ class FunctionGenerator(declarationGenerator: DeclarationGenerator) : Declaratio
val ktParameter = DescriptorToSourceUtils.getSourceFromDescriptor(valueParameterDescriptor) as? KtParameter val ktParameter = DescriptorToSourceUtils.getSourceFromDescriptor(valueParameterDescriptor) as? KtParameter
irValueParameter.defaultValue = ktParameter?.defaultValue?.let { defaultValue -> irValueParameter.defaultValue = ktParameter?.defaultValue?.let { defaultValue ->
val inAnnotation = val inAnnotation =
valueParameterDescriptor.containingDeclaration.safeAs<ConstructorDescriptor>()?.isAnnotationConstructor() ?: false (valueParameterDescriptor.containingDeclaration as? ConstructorDescriptor)?.isAnnotationConstructor() == true
if (inAnnotation) { if (inAnnotation) {
generateDefaultAnnotationParameterValue(defaultValue, valueParameterDescriptor) generateDefaultAnnotationParameterValue(defaultValue, valueParameterDescriptor)
} else if (context.configuration.generateBodies) { } else if (context.configuration.generateBodies) {
@@ -16,7 +16,6 @@ import org.jetbrains.kotlin.ir.util.isImmutable
import org.jetbrains.kotlin.ir.util.parentAsClass import org.jetbrains.kotlin.ir.util.parentAsClass
import org.jetbrains.kotlin.ir.util.primaryConstructor import org.jetbrains.kotlin.ir.util.primaryConstructor
import org.jetbrains.kotlin.ir.util.render import org.jetbrains.kotlin.ir.util.render
import org.jetbrains.kotlin.utils.addToStdlib.assertedCast
val IrBuilderWithScope.parent get() = scope.getLocalDeclarationParent() val IrBuilderWithScope.parent get() = scope.getLocalDeclarationParent()
@@ -76,9 +75,8 @@ fun IrBuilderWithScope.irReturn(value: IrExpression) =
IrReturnImpl( IrReturnImpl(
startOffset, endOffset, startOffset, endOffset,
context.irBuiltIns.nothingType, context.irBuiltIns.nothingType,
scope.scopeOwnerSymbol.assertedCast<IrReturnTargetSymbol> { scope.scopeOwnerSymbol as? IrReturnTargetSymbol
"Function scope expected: ${scope.scopeOwnerSymbol.owner.render()}" ?: throw AssertionError("Function scope expected: ${scope.scopeOwnerSymbol.owner.render()}"),
},
value value
) )
@@ -28,8 +28,6 @@ import org.jetbrains.kotlin.ir.symbols.IrClassSymbol
import org.jetbrains.kotlin.ir.types.IrType import org.jetbrains.kotlin.ir.types.IrType
import org.jetbrains.kotlin.ir.types.impl.IrSimpleTypeImpl import org.jetbrains.kotlin.ir.types.impl.IrSimpleTypeImpl
import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.utils.addToStdlib.safeAs
import java.util.*
abstract class IrBuilder( abstract class IrBuilder(
override val context: IrGeneratorContext, override val context: IrGeneratorContext,
@@ -107,9 +105,11 @@ class IrBlockBuilder(
override fun doBuild(): IrContainerExpression { override fun doBuild(): IrContainerExpression {
val resultType = this.resultType val resultType = this.resultType
?: statements.lastOrNull().safeAs<IrExpression>()?.type ?: (statements.lastOrNull() as? IrExpression)?.type
?: context.irBuiltIns.unitType ?: context.irBuiltIns.unitType
val irBlock = if (isTransparent) IrCompositeImpl(startOffset, endOffset, resultType, origin) else IrBlockImpl(startOffset, endOffset, resultType, origin) val irBlock =
if (isTransparent) IrCompositeImpl(startOffset, endOffset, resultType, origin)
else IrBlockImpl(startOffset, endOffset, resultType, origin)
irBlock.statements.addAll(statements) irBlock.statements.addAll(statements)
return irBlock return irBlock
} }
@@ -15,7 +15,6 @@ import org.jetbrains.kotlin.types.Variance
import org.jetbrains.kotlin.types.model.CaptureStatus import org.jetbrains.kotlin.types.model.CaptureStatus
import org.jetbrains.kotlin.types.model.CapturedTypeConstructorMarker import org.jetbrains.kotlin.types.model.CapturedTypeConstructorMarker
import org.jetbrains.kotlin.types.model.CapturedTypeMarker import org.jetbrains.kotlin.types.model.CapturedTypeMarker
import org.jetbrains.kotlin.utils.addToStdlib.safeAs
abstract class IrTypeBase(val kotlinType: KotlinType?) : IrType(), IrTypeProjection { abstract class IrTypeBase(val kotlinType: KotlinType?) : IrType(), IrTypeProjection {
override val type: IrType get() = this override val type: IrType get() = this
@@ -43,8 +42,7 @@ class IrDynamicTypeImpl(
} }
val IrType.originalKotlinType: KotlinType? val IrType.originalKotlinType: KotlinType?
get() = safeAs<IrTypeBase>()?.kotlinType get() = (this as? IrTypeBase)?.kotlinType
object IrStarProjectionImpl : IrStarProjection { object IrStarProjectionImpl : IrStarProjection {
override fun equals(other: Any?): Boolean = this === other override fun equals(other: Any?): Boolean = this === other
@@ -33,7 +33,6 @@ import org.jetbrains.kotlin.resolve.descriptorUtil.isEffectivelyExternal
import org.jetbrains.kotlin.resolve.isValueClass import org.jetbrains.kotlin.resolve.isValueClass
import org.jetbrains.kotlin.serialization.deserialization.descriptors.DeserializedContainerSource import org.jetbrains.kotlin.serialization.deserialization.descriptors.DeserializedContainerSource
import org.jetbrains.kotlin.types.KotlinType import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.utils.addToStdlib.safeAs
@OptIn(ObsoleteDescriptorBasedAPI::class) @OptIn(ObsoleteDescriptorBasedAPI::class)
abstract class DeclarationStubGenerator( abstract class DeclarationStubGenerator(
@@ -89,7 +88,7 @@ abstract class DeclarationStubGenerator(
} }
fun generateOrGetFacadeClass(descriptor: DeclarationDescriptor): IrClass? { fun generateOrGetFacadeClass(descriptor: DeclarationDescriptor): IrClass? {
val directMember = descriptor.safeAs<PropertyAccessorDescriptor>()?.correspondingProperty ?: descriptor val directMember = (descriptor as? PropertyAccessorDescriptor)?.correspondingProperty ?: descriptor
val packageFragment = directMember.containingDeclaration as? PackageFragmentDescriptor ?: return null val packageFragment = directMember.containingDeclaration as? PackageFragmentDescriptor ?: return null
val containerSource = extensions.getContainerSource(directMember) ?: return null val containerSource = extensions.getContainerSource(directMember) ?: return null
return facadeClassMap.getOrPut(containerSource) { return facadeClassMap.getOrPut(containerSource) {
@@ -8,7 +8,6 @@ package org.jetbrains.kotlin.ir.util
import org.jetbrains.kotlin.descriptors.Modality import org.jetbrains.kotlin.descriptors.Modality
import org.jetbrains.kotlin.ir.declarations.* import org.jetbrains.kotlin.ir.declarations.*
import org.jetbrains.kotlin.ir.symbols.IrSymbol import org.jetbrains.kotlin.ir.symbols.IrSymbol
import org.jetbrains.kotlin.utils.addToStdlib.safeAs
val IrDeclaration.isReal: Boolean get() = !isFakeOverride val IrDeclaration.isReal: Boolean get() = !isFakeOverride
@@ -115,7 +114,7 @@ fun <S : IrSymbol, T : IrOverridableDeclaration<S>> T.resolveFakeOverrideOrNull(
collectRealOverrides(toSkip, { it.modality == Modality.ABSTRACT }) collectRealOverrides(toSkip, { it.modality == Modality.ABSTRACT })
.let { realOverrides -> .let { realOverrides ->
// Kotlin forbids conflicts between overrides, but they may trickle down from Java. // Kotlin forbids conflicts between overrides, but they may trickle down from Java.
realOverrides.singleOrNull { it.parent.safeAs<IrClass>()?.isInterface != true } realOverrides.singleOrNull { (it.parent as? IrClass)?.isInterface != true }
// TODO: We take firstOrNull instead of singleOrNull here because of KT-36188. // TODO: We take firstOrNull instead of singleOrNull here because of KT-36188.
?: realOverrides.firstOrNull() ?: realOverrides.firstOrNull()
} }
@@ -31,7 +31,6 @@ import org.jetbrains.kotlin.name.SpecialNames
import org.jetbrains.kotlin.util.OperatorNameConventions import org.jetbrains.kotlin.util.OperatorNameConventions
import org.jetbrains.kotlin.utils.DFS import org.jetbrains.kotlin.utils.DFS
import org.jetbrains.kotlin.utils.addIfNotNull import org.jetbrains.kotlin.utils.addIfNotNull
import org.jetbrains.kotlin.utils.addToStdlib.safeAs
import java.io.StringWriter import java.io.StringWriter
/** /**
@@ -342,10 +341,8 @@ fun IrFunction.isFakeOverriddenFromAny(): Boolean {
fun IrCall.isSuperToAny() = superQualifierSymbol?.let { this.symbol.owner.isFakeOverriddenFromAny() } ?: false fun IrCall.isSuperToAny() = superQualifierSymbol?.let { this.symbol.owner.isFakeOverriddenFromAny() } ?: false
fun IrDeclaration.hasInterfaceParent() = fun IrDeclaration.hasInterfaceParent() =
parent.safeAs<IrClass>()?.isInterface == true (parent as? IrClass)?.isInterface == true
fun IrPossiblyExternalDeclaration.isEffectivelyExternal(): Boolean = fun IrPossiblyExternalDeclaration.isEffectivelyExternal(): Boolean =
this.isExternal this.isExternal