From 6cb3687d5deb199117f0f7b05e2e4d0d77cafc3a Mon Sep 17 00:00:00 2001 From: Mikhail Glukhikh Date: Mon, 29 Jun 2020 13:42:04 +0300 Subject: [PATCH] [FIR2IR] Replace 'throw AssertionError()' with error() / assert() {...} --- .../fir/backend/jvm/FirJvmMangleComputer.kt | 4 ++-- .../kotlin/fir/backend/ConversionUtils.kt | 4 ++-- .../fir/backend/Fir2IrClassifierStorage.kt | 2 +- .../kotlin/fir/backend/Fir2IrConverter.kt | 2 +- .../fir/backend/Fir2IrDeclarationStorage.kt | 19 +++++++++---------- .../kotlin/fir/backend/Fir2IrTypeConverter.kt | 3 +-- .../kotlin/fir/backend/Fir2IrVisitor.kt | 2 +- .../kotlin/fir/backend/PrimitiveComparison.kt | 4 ++-- .../generators/CallAndReferenceGenerator.kt | 4 +--- .../generators/DataClassMembersGenerator.kt | 2 +- .../generators/OperatorExpressionGenerator.kt | 4 ++-- .../kotlin/fir/lazy/Fir2IrLazyClass.kt | 9 +++++---- .../kotlin/fir/lazy/Fir2IrLazyConstructor.kt | 4 ++-- .../kotlin/fir/lazy/Fir2IrLazyProperty.kt | 2 +- .../fir/lazy/Fir2IrLazySimpleFunction.kt | 3 +-- .../signaturer/FirBasedSignatureComposer.kt | 2 +- 16 files changed, 33 insertions(+), 37 deletions(-) diff --git a/compiler/fir/fir2ir/jvm-backend/src/org/jetbrains/kotlin/fir/backend/jvm/FirJvmMangleComputer.kt b/compiler/fir/fir2ir/jvm-backend/src/org/jetbrains/kotlin/fir/backend/jvm/FirJvmMangleComputer.kt index 0f0ec73ce54..17d2ed5fbd1 100644 --- a/compiler/fir/fir2ir/jvm-backend/src/org/jetbrains/kotlin/fir/backend/jvm/FirJvmMangleComputer.kt +++ b/compiler/fir/fir2ir/jvm-backend/src/org/jetbrains/kotlin/fir/backend/jvm/FirJvmMangleComputer.kt @@ -80,11 +80,11 @@ open class FirJvmMangleComputer( } if (parentClassId != null && !parentClassId.isLocal) { val parentClassLike = session.firSymbolProvider.getClassLikeSymbolByFqName(parentClassId)?.fir - ?: throw AssertionError("Attempt to find parent ($parentClassId) for probably-local declaration!") + ?: error("Attempt to find parent ($parentClassId) for probably-local declaration!") if (parentClassLike is FirRegularClass || parentClassLike is FirTypeAlias) { parentClassLike.accept(this@FirJvmMangleComputer, false) } else { - throw AssertionError("Strange class-like declaration: ${parentClassLike.render()}") + error("Strange class-like declaration: ${parentClassLike.render()}") } } else if (parentClassId == null && !parentPackageFqName.isRoot) { builder.appendName(parentPackageFqName.asString()) diff --git a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/ConversionUtils.kt b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/ConversionUtils.kt index afd5640d682..dcdb02fa4dd 100644 --- a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/ConversionUtils.kt +++ b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/ConversionUtils.kt @@ -106,7 +106,7 @@ fun FirClassifierSymbol<*>.toSymbol( is FirClassSymbol -> { classifierStorage.getIrClassSymbol(this) } - else -> throw AssertionError("Should not be here: $this") + else -> error("Unknown symbol: $this") } } @@ -127,7 +127,7 @@ fun FirReference.toSymbol( resolvedSymbol.toSymbol(session, classifierStorage) } else -> { - throw AssertionError("Unknown symbol: $resolvedSymbol") + error("Unknown symbol: $resolvedSymbol") } } } diff --git a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrClassifierStorage.kt b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrClassifierStorage.kt index b6fde228fd3..23955fb7a53 100644 --- a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrClassifierStorage.kt +++ b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrClassifierStorage.kt @@ -451,6 +451,6 @@ class Fir2IrClassifierStorage( typeContext: ConversionTypeContext ): IrTypeParameterSymbol { return getCachedIrTypeParameter(firTypeParameterSymbol.fir, typeContext = typeContext)?.symbol - ?: throw AssertionError("Cannot find cached type parameter by FIR symbol: ${firTypeParameterSymbol.name}") + ?: error("Cannot find cached type parameter by FIR symbol: ${firTypeParameterSymbol.name}") } } \ No newline at end of file diff --git a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrConverter.kt b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrConverter.kt index 91dc0b3834d..e3e80bacd5b 100644 --- a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrConverter.kt +++ b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrConverter.kt @@ -158,7 +158,7 @@ class Fir2IrConverter( null } else -> { - throw AssertionError("Unexpected member: ${declaration::class}") + error("Unexpected member: ${declaration::class}") } } } diff --git a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrDeclarationStorage.kt b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrDeclarationStorage.kt index d48797197e4..cab193bf936 100644 --- a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrDeclarationStorage.kt +++ b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrDeclarationStorage.kt @@ -201,7 +201,7 @@ class Fir2IrDeclarationStorage( val containerFile = when (firBasedSymbol) { is FirCallableSymbol -> firProvider.getFirCallableContainerFile(firBasedSymbol) is FirClassLikeSymbol -> firProvider.getFirClassifierContainerFileIfAny(firBasedSymbol) - else -> throw AssertionError("Unexpected: $firBasedSymbol") + else -> error("Unknown symbol: $firBasedSymbol") } when { @@ -302,8 +302,7 @@ class Fir2IrDeclarationStorage( if (function !is FirAnonymousFunction && containingClass != null && !isStatic) { dispatchReceiverParameter = declareThisReceiverParameter( symbolTable, - thisType = containingClass.thisReceiver?.type - ?: throw AssertionError(), + thisType = containingClass.thisReceiver?.type ?: error("No this receiver"), thisOrigin = thisOrigin ) } @@ -831,8 +830,8 @@ class Fir2IrDeclarationStorage( constructorCache[firConstructor] = irFunction return irConstructorSymbol } - if (parentOrigin == IrDeclarationOrigin.DEFINED) { - throw AssertionError() + assert(parentOrigin != IrDeclarationOrigin.DEFINED) { + "Should not have reference to public API uncached constructor from source code" } val symbol = Fir2IrConstructorSymbol(signature) val irConstructor = firConstructor.convertWithOffsets { startOffset, endOffset -> @@ -870,8 +869,8 @@ class Fir2IrDeclarationStorage( } // TODO: package fragment members (?) if (firDeclaration is FirSimpleFunction && irParent is Fir2IrLazyClass) { - if (parentOrigin == IrDeclarationOrigin.DEFINED) { - throw AssertionError() + assert(parentOrigin != IrDeclarationOrigin.DEFINED) { + "Should not have reference to public API uncached simple function from source code" } val symbol = Fir2IrSimpleFunctionSymbol(signature, firDeclaration.containerSource) val irFunction = firDeclaration.convertWithOffsets { startOffset, endOffset -> @@ -899,7 +898,7 @@ class Fir2IrDeclarationStorage( is FirConstructor -> { getIrConstructorSymbol(firDeclaration.symbol) } - else -> throw AssertionError("Should not be here: ${firDeclaration::class.java}: ${firDeclaration.render()}") + else -> error("Unknown kind of function: ${firDeclaration::class.java}: ${firDeclaration.render()}") } } @@ -918,8 +917,8 @@ class Fir2IrDeclarationStorage( } // TODO: package fragment members (?) if (irParent is Fir2IrLazyClass) { - if (parentOrigin == IrDeclarationOrigin.DEFINED) { - throw AssertionError() + assert(parentOrigin != IrDeclarationOrigin.DEFINED) { + "Should not have reference to public API uncached property from source code" } val symbol = Fir2IrPropertySymbol(signature, fir.containerSource) val irProperty = fir.convertWithOffsets { startOffset, endOffset -> diff --git a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrTypeConverter.kt b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrTypeConverter.kt index 23e84eb20eb..4573c64a089 100644 --- a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrTypeConverter.kt +++ b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrTypeConverter.kt @@ -124,8 +124,7 @@ class Fir2IrTypeConverter( private fun getArrayClassSymbol(classId: ClassId?): IrClassSymbol? { val primitiveId = StandardClassIds.elementTypeByPrimitiveArrayType[classId] ?: return null val irType = classIdToTypeMap[primitiveId] - return irBuiltIns.primitiveArrayForType[irType] - ?: throw AssertionError("Strange primitiveId $primitiveId from array: $classId") + return irBuiltIns.primitiveArrayForType[irType] ?: error("Strange primitiveId $primitiveId from array: $classId") } private fun getBuiltInClassSymbol(classId: ClassId?): IrClassSymbol? { diff --git a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrVisitor.kt b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrVisitor.kt index c7177fa2226..eee10a6e408 100644 --- a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrVisitor.kt +++ b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrVisitor.kt @@ -241,7 +241,7 @@ class Fir2IrVisitor( when (irTarget) { is IrConstructor -> irTarget.symbol is IrSimpleFunction -> irTarget.symbol - else -> throw AssertionError("Should not be here: $irTarget") + else -> error("Unknown return target: $irTarget") }, convertToIrExpression(result) ) diff --git a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/PrimitiveComparison.kt b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/PrimitiveComparison.kt index a880716c225..2e5e43bfd16 100644 --- a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/PrimitiveComparison.kt +++ b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/PrimitiveComparison.kt @@ -47,7 +47,7 @@ private fun leastCommonPrimitiveNumericType(t1: ConeClassLikeType, t2: ConeClass pt1.isFloat() || pt2.isFloat() -> PrimitiveTypes.Float pt1.isLong() || pt2.isLong() -> PrimitiveTypes.Long pt1.isInt() || pt2.isInt() -> PrimitiveTypes.Int - else -> throw AssertionError("Unexpected types: t1=$t1, t2=$t2") + else -> error("Unexpected types: t1=$t1, t2=$t2") } } @@ -55,7 +55,7 @@ private fun ConeClassLikeType.promoteIntegerTypeToIntIfRequired(): ConeClassLike when (lookupTag.classId) { StandardClassIds.Byte, StandardClassIds.Short -> PrimitiveTypes.Int StandardClassIds.Long, StandardClassIds.Int, StandardClassIds.Float, StandardClassIds.Double, StandardClassIds.Char -> this - else -> throw AssertionError("Primitive number type expected: $this") + else -> error("Primitive number type expected: $this") } private fun ConeKotlinType.getPrimitiveTypeOrSupertype(): ConeClassLikeType? = diff --git a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/generators/CallAndReferenceGenerator.kt b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/generators/CallAndReferenceGenerator.kt index 727ecd89541..e69108a83ff 100644 --- a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/generators/CallAndReferenceGenerator.kt +++ b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/generators/CallAndReferenceGenerator.kt @@ -485,9 +485,7 @@ class CallAndReferenceGenerator( ?: run { if (this is FirCallableReferenceAccess) return null val name = if (isDispatch) "Dispatch" else "Extension" - throw AssertionError( - "$name receiver expected: ${render()} to ${calleeReference.render()}" - ) + error("$name receiver expected: ${render()} to ${calleeReference.render()}") } } diff --git a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/generators/DataClassMembersGenerator.kt b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/generators/DataClassMembersGenerator.kt index 25c4f9d6d3f..bbc57d69829 100644 --- a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/generators/DataClassMembersGenerator.kt +++ b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/generators/DataClassMembersGenerator.kt @@ -250,7 +250,7 @@ class DataClassMembersGenerator(val components: Fir2IrComponents) { components.irBuiltIns.booleanType -> FirImplicitBooleanTypeRef(null) components.irBuiltIns.intType -> FirImplicitIntTypeRef(null) components.irBuiltIns.stringType -> FirImplicitStringTypeRef(null) - else -> throw AssertionError("Should not be here") + else -> error("Unexpected synthetic data class function return type: $returnType") } if (irValueParameter != null) { this.valueParameters.add( diff --git a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/generators/OperatorExpressionGenerator.kt b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/generators/OperatorExpressionGenerator.kt index 5ca02a8e15f..92ed1481687 100644 --- a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/generators/OperatorExpressionGenerator.kt +++ b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/generators/OperatorExpressionGenerator.kt @@ -145,14 +145,14 @@ internal class OperatorExpressionGenerator( targetType: ConeClassLikeType? ): IrExpression { if (targetType == null) return this - if (operandType == null) throw AssertionError("operandType should be non-null") + if (operandType == null) error("operandType should be non-null if targetType is non-null") val operandClassId = operandType.lookupTag.classId val targetClassId = targetType.lookupTag.classId if (operandClassId == targetClassId) return this val conversionFunction = typeConverter.classIdToSymbolMap[operandClassId]?.getSimpleFunction("to${targetType.lookupTag.classId.shortClassName.asString()}") - ?: throw AssertionError("No conversion function for $operandType ~> $targetType") + ?: error("No conversion function for $operandType ~> $targetType") val dispatchReceiver = this@asComparisonOperand val unsafeIrCall = IrCallImpl( diff --git a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/lazy/Fir2IrLazyClass.kt b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/lazy/Fir2IrLazyClass.kt index 9fc2a344eb9..81312d40a38 100644 --- a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/lazy/Fir2IrLazyClass.kt +++ b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/lazy/Fir2IrLazyClass.kt @@ -56,19 +56,19 @@ class Fir2IrLazyClass( override var visibility: Visibility get() = fir.visibility set(_) { - throw AssertionError("Mutating Fir2Ir lazy elements is not possible") + error("Mutating Fir2Ir lazy elements is not possible") } override var modality: Modality get() = fir.modality!! set(_) { - throw AssertionError("Mutating Fir2Ir lazy elements is not possible") + error("Mutating Fir2Ir lazy elements is not possible") } override var attributeOwnerId: IrAttributeContainer get() = this set(_) { - throw AssertionError("Mutating Fir2Ir lazy elements is not possible") + error("Mutating Fir2Ir lazy elements is not possible") } override val kind: ClassKind @@ -160,9 +160,10 @@ class Fir2IrLazyClass( with(fakeOverrideGenerator) { result += getFakeOverrides(fir, processedNames) } + // TODO: remove this check to save time for (declaration in result) { if (declaration.parent != this) { - throw AssertionError() + throw AssertionError("Unmatched parent for lazy class member") } } result diff --git a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/lazy/Fir2IrLazyConstructor.kt b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/lazy/Fir2IrLazyConstructor.kt index e63ddaf77f8..77242fe8de8 100644 --- a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/lazy/Fir2IrLazyConstructor.kt +++ b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/lazy/Fir2IrLazyConstructor.kt @@ -68,7 +68,7 @@ class Fir2IrLazyConstructor( override var visibility: Visibility get() = fir.visibility set(_) { - throw AssertionError("Mutating Fir2Ir lazy elements is not possible") + error("Mutating Fir2Ir lazy elements is not possible") } override var returnType: IrType by lazyVar { @@ -93,7 +93,7 @@ class Fir2IrLazyConstructor( override var extensionReceiverParameter: IrValueParameter? get() = null set(_) { - throw AssertionError("Mutating Fir2Ir lazy elements is not possible") + error("Mutating Fir2Ir lazy elements is not possible") } override var valueParameters: List by lazyVar { diff --git a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/lazy/Fir2IrLazyProperty.kt b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/lazy/Fir2IrLazyProperty.kt index 65446b651a3..1a7c1fb6572 100644 --- a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/lazy/Fir2IrLazyProperty.kt +++ b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/lazy/Fir2IrLazyProperty.kt @@ -74,7 +74,7 @@ class Fir2IrLazyProperty( override var visibility: Visibility get() = fir.visibility set(_) { - throw AssertionError("Mutating Fir2Ir lazy elements is not possible") + error("Mutating Fir2Ir lazy elements is not possible") } override val modality: Modality diff --git a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/lazy/Fir2IrLazySimpleFunction.kt b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/lazy/Fir2IrLazySimpleFunction.kt index 6963d009527..567d1f71f00 100644 --- a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/lazy/Fir2IrLazySimpleFunction.kt +++ b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/lazy/Fir2IrLazySimpleFunction.kt @@ -92,8 +92,7 @@ class Fir2IrLazySimpleFunction( declarationStorage.enterScope(this) declareThisReceiverParameter( symbolTable, - thisType = containingClass.thisReceiver?.type - ?: throw AssertionError(), + thisType = containingClass.thisReceiver?.type ?: error("No this receiver for containing class"), thisOrigin = origin ).apply { declarationStorage.leaveScope(this@Fir2IrLazySimpleFunction) diff --git a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/signaturer/FirBasedSignatureComposer.kt b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/signaturer/FirBasedSignatureComposer.kt index 04d9b15f3b6..775da7ef736 100644 --- a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/signaturer/FirBasedSignatureComposer.kt +++ b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/signaturer/FirBasedSignatureComposer.kt @@ -91,7 +91,7 @@ class FirBasedSignatureComposer(private val mangler: FirMangler) : Fir2IrSignatu callableId.packageName.asString(), callableId.relativeCallableName.asString(), builder.hashId, builder.mask ) } - else -> throw AssertionError("Unsupported FIR declaration in signature composer: ${declaration.render()}") + else -> error("Unsupported FIR declaration in signature composer: ${declaration.render()}") } }