From 954ef58d3599c729bbf755baecd674391fbd0bfb Mon Sep 17 00:00:00 2001 From: Dmitriy Novozhilov Date: Thu, 31 Aug 2023 13:29:04 +0300 Subject: [PATCH] [FIR2IR] Don't create calls to members of Nothing for data/value generated members Without this change, the following tests will fail: - `FirLightTreeBlackBoxCodegenTestGenerated.InlineClasses#testJavaPrimitiveTypeIC` - `FirLightTreeBlackBoxCodegenTestGenerated.InlineClasses#testJavaPrimitiveTypeICGeneric` - `FirPsiJvmIrTextTestGenerated.Classes.DataClasses#testKt31649` ^KT-61637 --- .../generators/DataClassMembersGenerator.kt | 25 +++++++++++-------- 1 file changed, 15 insertions(+), 10 deletions(-) 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 f137dc0d8f2..c688423abd9 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 @@ -9,10 +9,7 @@ import org.jetbrains.kotlin.builtins.StandardNames.HASHCODE_NAME import org.jetbrains.kotlin.descriptors.ClassKind import org.jetbrains.kotlin.descriptors.DescriptorVisibilities import org.jetbrains.kotlin.descriptors.Modality -import org.jetbrains.kotlin.fir.analysis.checkers.toRegularClassSymbol import org.jetbrains.kotlin.fir.backend.* -import org.jetbrains.kotlin.fir.backend.declareThisReceiverParameter -import org.jetbrains.kotlin.fir.backend.unsubstitutedScope import org.jetbrains.kotlin.fir.containingClassLookupTag import org.jetbrains.kotlin.fir.declarations.* import org.jetbrains.kotlin.fir.declarations.utils.classId @@ -23,10 +20,7 @@ import org.jetbrains.kotlin.fir.scopes.getFunctions import org.jetbrains.kotlin.fir.scopes.getProperties import org.jetbrains.kotlin.fir.symbols.impl.FirNamedFunctionSymbol import org.jetbrains.kotlin.fir.symbols.impl.FirPropertySymbol -import org.jetbrains.kotlin.fir.types.coneType -import org.jetbrains.kotlin.fir.types.isArrayOrPrimitiveArray -import org.jetbrains.kotlin.fir.types.toRegularClassSymbol -import org.jetbrains.kotlin.fir.types.toSymbol +import org.jetbrains.kotlin.fir.types.* import org.jetbrains.kotlin.ir.UNDEFINED_OFFSET import org.jetbrains.kotlin.ir.builders.IrGeneratorContextBase import org.jetbrains.kotlin.ir.declarations.* @@ -135,7 +129,7 @@ class DataClassMembersGenerator(val components: Fir2IrComponents) : Fir2IrCompon get() { // Pick the (necessarily unique) non-interface upper bound if it exists for (type in bounds) { - val klass = type.toRegularClassSymbol(session)?.fir ?: continue + val klass = type.coneType.nothingToAny().toRegularClassSymbol(session)?.fir ?: continue val kind = klass.classKind if (kind != ClassKind.INTERFACE && kind != ClassKind.ANNOTATION_CLASS) return klass } @@ -143,7 +137,8 @@ class DataClassMembersGenerator(val components: Fir2IrComponents) : Fir2IrCompon // Otherwise, choose either the first IrClass supertype or recurse. // In the first case, all supertypes are interface types and the choice was arbitrary. // In the second case, there is only a single supertype. - return when (val firstSuper = bounds.first().coneType.fullyExpandedType(session).toSymbol(session)?.fir) { + val firstBoundType = bounds.first().coneType.fullyExpandedType(session).nothingToAny() + return when (val firstSuper = firstBoundType.toSymbol(session)?.fir) { is FirRegularClass -> firstSuper is FirTypeParameter -> firstSuper.erasedUpperBound else -> error("unknown supertype kind $firstSuper") @@ -163,7 +158,7 @@ class DataClassMembersGenerator(val components: Fir2IrComponents) : Fir2IrCompon val symbol = when { type.isArrayOrPrimitiveArray(checkUnsignedArrays = false) -> context.irBuiltIns.dataClassArrayMemberHashCodeSymbol else -> { - val classForType = when (val classifier = type.toSymbol(session)?.fir) { + val classForType = when (val classifier = type.nothingToAny().toSymbol(session)?.fir) { is FirRegularClass -> classifier is FirTypeParameter -> classifier.erasedUpperBound else -> error("Unknown classifier kind $classifier") @@ -177,6 +172,16 @@ class DataClassMembersGenerator(val components: Fir2IrComponents) : Fir2IrCompon } } + /** + * kotlin.Nothing has no members, so any function on property of type Nothing should be actually taken from kotlin.Any + */ + private fun ConeKotlinType.nothingToAny(): ConeKotlinType { + return when { + this.isNothingOrNullableNothing -> session.builtinTypes.anyType.type + else -> this + } + } + fun generateDispatchReceiverParameter(irFunction: IrFunction) = irFunction.declareThisReceiverParameter( irClass.defaultType,