From 157eedca430e9f4c6b55488c450aea2b8f156443 Mon Sep 17 00:00:00 2001 From: Ivan Kochurkin Date: Wed, 8 Jun 2022 00:59:46 +0300 Subject: [PATCH] [FIR2IR] Create IrConst instead of IrGetField for default value param in annotation class, ^KT-52676 Fixed --- .../kotlin/fir/backend/Fir2IrVisitor.kt | 24 +++++++++---- .../generators/CallAndReferenceGenerator.kt | 18 +++++++--- .../runners/ir/Fir2IrTextTestGenerated.java | 6 ++++ .../ir/LightTreeFir2IrTextTestGenerated.java | 6 ++++ ...rgWithDefaultValueInAnnotationClass.ir.txt | 34 +++++++++++++++++++ .../argWithDefaultValueInAnnotationClass.kt | 13 +++++++ ...rgWithDefaultValueInAnnotationClass.kt.txt | 12 +++++++ .../test/runners/ir/IrTextTestGenerated.java | 6 ++++ 8 files changed, 107 insertions(+), 12 deletions(-) create mode 100644 compiler/testData/ir/irText/declarations/annotations/argWithDefaultValueInAnnotationClass.ir.txt create mode 100644 compiler/testData/ir/irText/declarations/annotations/argWithDefaultValueInAnnotationClass.kt create mode 100644 compiler/testData/ir/irText/declarations/annotations/argWithDefaultValueInAnnotationClass.kt.txt 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 37e789615e6..b00a8c07f32 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 @@ -448,14 +448,23 @@ class Fir2IrVisitor( } override fun visitQualifiedAccessExpression(qualifiedAccessExpression: FirQualifiedAccessExpression, data: Any?): IrElement { - val explicitReceiverExpression = convertToIrReceiverExpression( - qualifiedAccessExpression.explicitReceiver, qualifiedAccessExpression.calleeReference - ) - return callGenerator.convertToIrCall(qualifiedAccessExpression, qualifiedAccessExpression.typeRef, explicitReceiverExpression) + return convertQualifiedAccessExpression(qualifiedAccessExpression) } override fun visitPropertyAccessExpression(propertyAccessExpression: FirPropertyAccessExpression, data: Any?): IrElement { - return visitQualifiedAccessExpression(propertyAccessExpression, data) + return convertQualifiedAccessExpression(propertyAccessExpression) + } + + private fun convertQualifiedAccessExpression( + qualifiedAccessExpression: FirQualifiedAccessExpression, + annotationMode: Boolean = false + ) : IrExpression { + val explicitReceiverExpression = convertToIrReceiverExpression( + qualifiedAccessExpression.explicitReceiver, qualifiedAccessExpression.calleeReference + ) + return callGenerator.convertToIrCall( + qualifiedAccessExpression, qualifiedAccessExpression.typeRef, explicitReceiverExpression, annotationMode = annotationMode + ) } // Note that this mimics psi2ir [StatementGenerator#isThisForClassPhysicallyAvailable]. @@ -621,9 +630,10 @@ class Fir2IrVisitor( val unwrappedExpression = expression.unwrapArgument() if (annotationMode) { when (unwrappedExpression) { - is FirFunctionCall -> convertToIrCall(unwrappedExpression, annotationMode) - is FirArrayOfCall -> convertToArrayOfCall(unwrappedExpression, annotationMode) + is FirFunctionCall -> convertToIrCall(unwrappedExpression, true) + is FirArrayOfCall -> convertToArrayOfCall(unwrappedExpression, true) is FirCallableReferenceAccess -> convertCallableReferenceAccess(unwrappedExpression, isDelegate) + is FirQualifiedAccessExpression -> convertQualifiedAccessExpression(unwrappedExpression, true) else -> expression.accept(this, null) as IrExpression } } else { 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 a0d944ba5d3..243f0ec40fc 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 @@ -480,11 +480,19 @@ class CallAndReferenceGenerator( ) } } - is IrFieldSymbol -> IrGetFieldImpl( - startOffset, endOffset, symbol, type, - origin = IrStatementOrigin.GET_PROPERTY.takeIf { calleeReference !is FirDelegateFieldReference }, - superQualifierSymbol = dispatchReceiver.superQualifierSymbolForField(symbol) - ) + is IrFieldSymbol -> if (annotationMode) { + val resolvedSymbol = calleeReference.resolvedSymbol ?: error("should have resolvedSymbol") + val returnType = (resolvedSymbol as FirCallableSymbol<*>).resolvedReturnTypeRef.toIrType() + val firConstExpression = (resolvedSymbol.fir as FirVariable).initializer as? FirConstExpression<*> + ?: error("should be FirConstExpression") + firConstExpression.toIrConst(returnType) + } else { + IrGetFieldImpl( + startOffset, endOffset, symbol, type, + origin = IrStatementOrigin.GET_PROPERTY.takeIf { calleeReference !is FirDelegateFieldReference }, + superQualifierSymbol = dispatchReceiver.superQualifierSymbolForField(symbol) + ) + } is IrValueSymbol -> { IrGetValueImpl( startOffset, endOffset, type, symbol, diff --git a/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/ir/Fir2IrTextTestGenerated.java b/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/ir/Fir2IrTextTestGenerated.java index dceb0208ed4..ceb812ca31f 100644 --- a/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/ir/Fir2IrTextTestGenerated.java +++ b/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/ir/Fir2IrTextTestGenerated.java @@ -561,6 +561,12 @@ public class Fir2IrTextTestGenerated extends AbstractFir2IrTextTest { runTest("compiler/testData/ir/irText/declarations/annotations/annotationsWithVarargParameters.kt"); } + @Test + @TestMetadata("argWithDefaultValueInAnnotationClass.kt") + public void testArgWithDefaultValueInAnnotationClass() throws Exception { + runTest("compiler/testData/ir/irText/declarations/annotations/argWithDefaultValueInAnnotationClass.kt"); + } + @Test @TestMetadata("arrayInAnnotationArguments.kt") public void testArrayInAnnotationArguments() throws Exception { diff --git a/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/ir/LightTreeFir2IrTextTestGenerated.java b/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/ir/LightTreeFir2IrTextTestGenerated.java index 814135fe792..d89bc042ee6 100644 --- a/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/ir/LightTreeFir2IrTextTestGenerated.java +++ b/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/ir/LightTreeFir2IrTextTestGenerated.java @@ -561,6 +561,12 @@ public class LightTreeFir2IrTextTestGenerated extends AbstractLightTreeFir2IrTex runTest("compiler/testData/ir/irText/declarations/annotations/annotationsWithVarargParameters.kt"); } + @Test + @TestMetadata("argWithDefaultValueInAnnotationClass.kt") + public void testArgWithDefaultValueInAnnotationClass() throws Exception { + runTest("compiler/testData/ir/irText/declarations/annotations/argWithDefaultValueInAnnotationClass.kt"); + } + @Test @TestMetadata("arrayInAnnotationArguments.kt") public void testArrayInAnnotationArguments() throws Exception { diff --git a/compiler/testData/ir/irText/declarations/annotations/argWithDefaultValueInAnnotationClass.ir.txt b/compiler/testData/ir/irText/declarations/annotations/argWithDefaultValueInAnnotationClass.ir.txt new file mode 100644 index 00000000000..32ee86f7db8 --- /dev/null +++ b/compiler/testData/ir/irText/declarations/annotations/argWithDefaultValueInAnnotationClass.ir.txt @@ -0,0 +1,34 @@ +FILE fqName: fileName:/main.kt + CLASS ANNOTATION_CLASS name:ModuleInfo modality:OPEN visibility:public superTypes:[kotlin.Annotation] + $this: VALUE_PARAMETER INSTANCE_RECEIVER name: type:.ModuleInfo + CONSTRUCTOR visibility:public <> (keyBind:kotlin.Int) returnType:.ModuleInfo [primary] + VALUE_PARAMETER name:keyBind index:0 type:kotlin.Int + EXPRESSION_BODY + CONST Int type=kotlin.Int value=1234 + BLOCK_BODY + DELEGATING_CONSTRUCTOR_CALL 'public constructor () [primary] declared in kotlin.Any' + INSTANCE_INITIALIZER_CALL classDescriptor='CLASS ANNOTATION_CLASS name:ModuleInfo modality:OPEN visibility:public superTypes:[kotlin.Annotation]' + PROPERTY name:keyBind visibility:public modality:FINAL [val] + FIELD PROPERTY_BACKING_FIELD name:keyBind type:kotlin.Int visibility:private [final] + EXPRESSION_BODY + GET_VAR 'keyBind: kotlin.Int declared in .ModuleInfo.' type=kotlin.Int origin=INITIALIZE_PROPERTY_FROM_PARAMETER + FUN DEFAULT_PROPERTY_ACCESSOR name: visibility:public modality:FINAL <> ($this:.ModuleInfo) returnType:kotlin.Int + correspondingProperty: PROPERTY name:keyBind visibility:public modality:FINAL [val] + $this: VALUE_PARAMETER name: type:.ModuleInfo + BLOCK_BODY + RETURN type=kotlin.Nothing from='public final fun (): kotlin.Int declared in .ModuleInfo' + GET_FIELD 'FIELD PROPERTY_BACKING_FIELD name:keyBind type:kotlin.Int visibility:private [final]' type=kotlin.Int origin=null + receiver: GET_VAR ': .ModuleInfo declared in .ModuleInfo.' type=.ModuleInfo origin=null + FUN FAKE_OVERRIDE name:equals visibility:public modality:OPEN <> ($this:kotlin.Any, other:kotlin.Any?) returnType:kotlin.Boolean [fake_override,operator] + overridden: + public open fun equals (other: kotlin.Any?): kotlin.Boolean [fake_override,operator] declared in kotlin.Annotation + $this: VALUE_PARAMETER name: type:kotlin.Any + VALUE_PARAMETER name:other index:0 type:kotlin.Any? + FUN FAKE_OVERRIDE name:hashCode visibility:public modality:OPEN <> ($this:kotlin.Any) returnType:kotlin.Int [fake_override] + overridden: + public open fun hashCode (): kotlin.Int [fake_override] declared in kotlin.Annotation + $this: VALUE_PARAMETER name: type:kotlin.Any + FUN FAKE_OVERRIDE name:toString visibility:public modality:OPEN <> ($this:kotlin.Any) returnType:kotlin.String [fake_override] + overridden: + public open fun toString (): kotlin.String [fake_override] declared in kotlin.Annotation + $this: VALUE_PARAMETER name: type:kotlin.Any diff --git a/compiler/testData/ir/irText/declarations/annotations/argWithDefaultValueInAnnotationClass.kt b/compiler/testData/ir/irText/declarations/annotations/argWithDefaultValueInAnnotationClass.kt new file mode 100644 index 00000000000..0273fb2e140 --- /dev/null +++ b/compiler/testData/ir/irText/declarations/annotations/argWithDefaultValueInAnnotationClass.kt @@ -0,0 +1,13 @@ +// TARGET_BACKEND: JVM +// FIR_IDENTICAL +// ISSUE: KT-52676 + +// FILE: Keyboard.java + +public class Keyboard { + public static final int CHAR_NONE = 1234; +} + +// FILE: main.kt + +annotation class ModuleInfo(val keyBind: Int = Keyboard.CHAR_NONE) \ No newline at end of file diff --git a/compiler/testData/ir/irText/declarations/annotations/argWithDefaultValueInAnnotationClass.kt.txt b/compiler/testData/ir/irText/declarations/annotations/argWithDefaultValueInAnnotationClass.kt.txt new file mode 100644 index 00000000000..ca59a5f3d1f --- /dev/null +++ b/compiler/testData/ir/irText/declarations/annotations/argWithDefaultValueInAnnotationClass.kt.txt @@ -0,0 +1,12 @@ +open annotation class ModuleInfo : Annotation { + constructor(keyBind: Int = 1234) /* primary */ { + super/*Any*/() + /* () */ + + } + + val keyBind: Int + field = keyBind + get + +} diff --git a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/ir/IrTextTestGenerated.java b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/ir/IrTextTestGenerated.java index bdda88e567f..3b5f1c9a72e 100644 --- a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/ir/IrTextTestGenerated.java +++ b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/ir/IrTextTestGenerated.java @@ -561,6 +561,12 @@ public class IrTextTestGenerated extends AbstractIrTextTest { runTest("compiler/testData/ir/irText/declarations/annotations/annotationsWithVarargParameters.kt"); } + @Test + @TestMetadata("argWithDefaultValueInAnnotationClass.kt") + public void testArgWithDefaultValueInAnnotationClass() throws Exception { + runTest("compiler/testData/ir/irText/declarations/annotations/argWithDefaultValueInAnnotationClass.kt"); + } + @Test @TestMetadata("arrayInAnnotationArguments.kt") public void testArrayInAnnotationArguments() throws Exception {