From bf009a49496b5c951bd2dcd694093dedb4a38a32 Mon Sep 17 00:00:00 2001 From: Mikhail Glukhikh Date: Wed, 24 Jun 2020 11:02:18 +0300 Subject: [PATCH] [FIR2IR] Handle delegating constructor call type arguments properly --- .../generators/ClassMemberGenerator.kt | 21 +++++++++++-------- ...structorCallToTypeAliasConstructor.fir.txt | 1 + .../thisOfGenericOuterClass.fir.txt | 1 - 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/generators/ClassMemberGenerator.kt b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/generators/ClassMemberGenerator.kt index db619a4b4f2..daf76abf455 100644 --- a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/generators/ClassMemberGenerator.kt +++ b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/generators/ClassMemberGenerator.kt @@ -14,6 +14,7 @@ import org.jetbrains.kotlin.fir.expressions.FirDelegatedConstructorCall import org.jetbrains.kotlin.fir.expressions.FirExpression import org.jetbrains.kotlin.fir.expressions.impl.FirNoReceiverExpression import org.jetbrains.kotlin.fir.references.FirResolvedNamedReference +import org.jetbrains.kotlin.fir.resolve.fullyExpandedType import org.jetbrains.kotlin.fir.symbols.impl.FirConstructorSymbol import org.jetbrains.kotlin.fir.types.* import org.jetbrains.kotlin.ir.declarations.* @@ -256,30 +257,32 @@ internal class ClassMemberGenerator( val firDispatchReceiver = dispatchReceiver return convertWithOffsets { startOffset, endOffset -> val irConstructorSymbol = declarationStorage.getIrFunctionSymbol(constructorSymbol) as IrConstructorSymbol - val typeArguments = (constructedTypeRef as? FirResolvedTypeRef)?.type?.typeArguments + val typeArguments = (constructedTypeRef as? FirResolvedTypeRef)?.type?.fullyExpandedType(session)?.typeArguments val constructor = constructorSymbol.fir if (constructor.isFromEnumClass || constructor.returnTypeRef.isEnum) { IrEnumConstructorCallImpl( startOffset, endOffset, constructedIrType, irConstructorSymbol, - typeArgumentsCount = typeArguments?.size ?: 0, + typeArgumentsCount = constructor.typeParameters.size, valueArgumentsCount = constructor.valueParameters.size - ).apply { - } + ) } else { IrDelegatingConstructorCallImpl( startOffset, endOffset, constructedIrType, irConstructorSymbol, - typeArgumentsCount = typeArguments?.size ?: 0, + typeArgumentsCount = constructor.typeParameters.size, valueArgumentsCount = irConstructorSymbol.owner.valueParameters.size ) }.let { - if (typeArguments != null) { - for ((index, typeArgument) in typeArguments.withIndex()) { - val irType = (typeArgument as ConeKotlinTypeProjection).type.toIrType() - it.putTypeArgument(index, irType) + if (constructor.typeParameters.isNotEmpty()) { + if (typeArguments?.isNotEmpty() == true) { + for ((index, typeArgument) in typeArguments.withIndex()) { + if (index >= constructor.typeParameters.size) break + val irType = (typeArgument as ConeKotlinTypeProjection).type.toIrType() + it.putTypeArgument(index, irType) + } } } if (firDispatchReceiver !is FirNoReceiverExpression) { diff --git a/compiler/testData/ir/irText/classes/delegatingConstructorCallToTypeAliasConstructor.fir.txt b/compiler/testData/ir/irText/classes/delegatingConstructorCallToTypeAliasConstructor.fir.txt index 4fc7f46080d..02f7367f981 100644 --- a/compiler/testData/ir/irText/classes/delegatingConstructorCallToTypeAliasConstructor.fir.txt +++ b/compiler/testData/ir/irText/classes/delegatingConstructorCallToTypeAliasConstructor.fir.txt @@ -66,6 +66,7 @@ FILE fqName: fileName:/delegatingConstructorCallToTypeAliasConstructor.kt CONSTRUCTOR visibility:public <> () returnType:.C2 [primary] BLOCK_BODY DELEGATING_CONSTRUCTOR_CALL 'public constructor (value: T of .Cell) [primary] declared in .Cell' + : kotlin.String value: CONST String type=kotlin.String value="K" INSTANCE_INITIALIZER_CALL classDescriptor='CLASS CLASS name:C2 modality:FINAL visibility:public superTypes:[.Cell]' PROPERTY FAKE_OVERRIDE name:value visibility:public modality:FINAL [fake_override,val] diff --git a/compiler/testData/ir/irText/expressions/thisOfGenericOuterClass.fir.txt b/compiler/testData/ir/irText/expressions/thisOfGenericOuterClass.fir.txt index 7a0c8561375..95cf7d7ba8e 100644 --- a/compiler/testData/ir/irText/expressions/thisOfGenericOuterClass.fir.txt +++ b/compiler/testData/ir/irText/expressions/thisOfGenericOuterClass.fir.txt @@ -73,7 +73,6 @@ FILE fqName: fileName:/thisOfGenericOuterClass.kt CONSTRUCTOR visibility:private <> () returnType:.test. [primary] BLOCK_BODY DELEGATING_CONSTRUCTOR_CALL 'public constructor (y: kotlin.Int) [primary] declared in .Outer.Inner' - <1>: kotlin.Int $this: GET_VAR ': .Outer declared in .test' type=.Outer origin=null y: CONST Int type=kotlin.Int value=42 INSTANCE_INITIALIZER_CALL classDescriptor='CLASS CLASS name: modality:FINAL visibility:local superTypes:[.Outer.Inner]'