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 f2acb6f3146..8e90f162eaa 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 @@ -22,6 +22,7 @@ import org.jetbrains.kotlin.fir.resolve.toSymbol import org.jetbrains.kotlin.fir.symbols.impl.FirCallableSymbol import org.jetbrains.kotlin.fir.symbols.impl.FirClassSymbol import org.jetbrains.kotlin.fir.symbols.impl.FirFunctionSymbol +import org.jetbrains.kotlin.fir.symbols.impl.FirRegularClassSymbol import org.jetbrains.kotlin.fir.types.* import org.jetbrains.kotlin.ir.declarations.IrFunction import org.jetbrains.kotlin.ir.declarations.IrProperty @@ -78,12 +79,7 @@ internal class CallAndReferenceGenerator( ) } } - }.apply { - applyTypeArguments(callableReferenceAccess) - if (callableReferenceAccess.explicitReceiver !is FirResolvedQualifier) { - applyReceivers(callableReferenceAccess) - } - } + }.applyTypeArguments(callableReferenceAccess).applyReceivers(callableReferenceAccess) } fun convertToIrCall(qualifiedAccess: FirQualifiedAccess, typeRef: FirTypeRef): IrExpression { @@ -237,9 +233,18 @@ internal class CallAndReferenceGenerator( } fun convertToGetObject(qualifier: FirResolvedQualifier): IrExpression { - val irType = qualifier.typeRef.toIrType() + return convertToGetObject(qualifier, callableReferenceMode = false)!! + } + + private fun convertToGetObject(qualifier: FirResolvedQualifier, callableReferenceMode: Boolean): IrExpression? { val typeRef = qualifier.typeRef as? FirResolvedTypeRef val classSymbol = (typeRef?.type as? ConeClassLikeType)?.lookupTag?.toSymbol(session) + if (callableReferenceMode && classSymbol is FirRegularClassSymbol) { + if (classSymbol.classId != qualifier.classId) { + return null + } + } + val irType = qualifier.typeRef.toIrType() return qualifier.convertWithOffsets { startOffset, endOffset -> if (classSymbol != null) { IrGetObjectValueImpl( @@ -331,9 +336,16 @@ internal class CallAndReferenceGenerator( private fun FirQualifiedAccess.findIrReceiver(isDispatch: Boolean): IrExpression? { val firReceiver = if (isDispatch) dispatchReceiver else extensionReceiver - if (firReceiver is FirResolvedQualifier) return convertToGetObject(firReceiver) + if (firReceiver is FirResolvedQualifier) { + return convertToGetObject(firReceiver, callableReferenceMode = this is FirCallableReferenceAccess) + } return firReceiver.takeIf { it !is FirNoReceiverExpression }?.let { visitor.convertToIrExpression(it) } - ?: explicitReceiver?.let { visitor.convertToIrExpression(it) } // NB: this applies to the situation when call is unresolved + ?: explicitReceiver?.let { + if (it is FirResolvedQualifier) { + return convertToGetObject(it, callableReferenceMode = this is FirCallableReferenceAccess) + } + visitor.convertToIrExpression(it) + } ?: run { // Object case val callableReference = calleeReference as? FirResolvedNamedReference diff --git a/compiler/testData/codegen/box/callableReference/bound/boundJvmFieldInInterfaceCompanion.kt b/compiler/testData/codegen/box/callableReference/bound/boundJvmFieldInInterfaceCompanion.kt index c4bd1b75240..cb901b2c384 100644 --- a/compiler/testData/codegen/box/callableReference/bound/boundJvmFieldInInterfaceCompanion.kt +++ b/compiler/testData/codegen/box/callableReference/bound/boundJvmFieldInInterfaceCompanion.kt @@ -1,5 +1,4 @@ // !LANGUAGE: +JvmFieldInInterface -// IGNORE_BACKEND_FIR: JVM_IR // TARGET_BACKEND: JVM // WITH_RUNTIME diff --git a/compiler/testData/codegen/box/callableReference/bound/companionObjectReceiver.kt b/compiler/testData/codegen/box/callableReference/bound/companionObjectReceiver.kt index 9e8fd0277d0..ddf856c54da 100644 --- a/compiler/testData/codegen/box/callableReference/bound/companionObjectReceiver.kt +++ b/compiler/testData/codegen/box/callableReference/bound/companionObjectReceiver.kt @@ -1,4 +1,3 @@ -// IGNORE_BACKEND_FIR: JVM_IR class A { companion object { fun ok() = "OK" diff --git a/compiler/testData/codegen/box/callableReference/bound/objectReceiver.kt b/compiler/testData/codegen/box/callableReference/bound/objectReceiver.kt index 11fdd91596a..a03203792d5 100644 --- a/compiler/testData/codegen/box/callableReference/bound/objectReceiver.kt +++ b/compiler/testData/codegen/box/callableReference/bound/objectReceiver.kt @@ -1,4 +1,3 @@ -// IGNORE_BACKEND_FIR: JVM_IR object Singleton { fun ok() = "OK" } diff --git a/compiler/testData/codegen/box/jvmField/classFieldReference.kt b/compiler/testData/codegen/box/jvmField/classFieldReference.kt index ddaeaaf5218..8391329f534 100644 --- a/compiler/testData/codegen/box/jvmField/classFieldReference.kt +++ b/compiler/testData/codegen/box/jvmField/classFieldReference.kt @@ -1,4 +1,3 @@ -// IGNORE_BACKEND_FIR: JVM_IR // TARGET_BACKEND: JVM // WITH_RUNTIME diff --git a/compiler/testData/ir/irText/expressions/callableReferences/boundInnerGenericConstructor.fir.txt b/compiler/testData/ir/irText/expressions/callableReferences/boundInnerGenericConstructor.fir.txt index e9f2e54e1cc..64dfab4b0a4 100644 --- a/compiler/testData/ir/irText/expressions/callableReferences/boundInnerGenericConstructor.fir.txt +++ b/compiler/testData/ir/irText/expressions/callableReferences/boundInnerGenericConstructor.fir.txt @@ -85,8 +85,7 @@ FILE fqName:test fileName:/boundInnerGenericConstructor.kt ERROR_CALL 'Unresolved reference: #' type=IrErrorType CONST String type=kotlin.String value="O" CONST String type=kotlin.String value="K" - FUNCTION_REFERENCE 'public constructor (a: T of test.Foo, b: P of test.Foo.Inner) [primary] declared in test.Foo.Inner' type=kotlin.reflect.KFunction2> origin=null reflectionTarget= - $this: GET_VAR 'val z: test.Foo [val] declared in test.box' type=test.Foo origin=null + ERROR_EXPR 'Cannot bind 1 type arguments to ??? call with 0 type parameters' type=kotlin.reflect.KFunction2> RETURN type=kotlin.Nothing from='public final fun box (): kotlin.String declared in test' CALL 'public final fun plus (other: kotlin.Any?): kotlin.String [operator] declared in kotlin' type=kotlin.String origin=PLUS $receiver: ERROR_CALL 'Unresolved reference: #' type=IrErrorType diff --git a/compiler/testData/ir/irText/expressions/callableReferences/importedFromObject.fir.txt b/compiler/testData/ir/irText/expressions/callableReferences/importedFromObject.fir.txt deleted file mode 100644 index 3cc85cf7c8c..00000000000 --- a/compiler/testData/ir/irText/expressions/callableReferences/importedFromObject.fir.txt +++ /dev/null @@ -1,74 +0,0 @@ -FILE fqName:test fileName:/importedFromObject.kt - CLASS OBJECT name:Foo modality:FINAL visibility:public superTypes:[kotlin.Any] - $this: VALUE_PARAMETER INSTANCE_RECEIVER name: type:test.Foo - CONSTRUCTOR visibility:private <> () returnType:test.Foo [primary] - BLOCK_BODY - DELEGATING_CONSTRUCTOR_CALL 'public constructor () [primary] declared in kotlin.Any' - INSTANCE_INITIALIZER_CALL classDescriptor='CLASS OBJECT name:Foo modality:FINAL visibility:public superTypes:[kotlin.Any]' - PROPERTY name:a visibility:public modality:FINAL [val] - FIELD PROPERTY_BACKING_FIELD name:a type:kotlin.String visibility:private [final] - EXPRESSION_BODY - CONST String type=kotlin.String value="" - FUN DEFAULT_PROPERTY_ACCESSOR name: visibility:public modality:FINAL <> ($this:test.Foo) returnType:kotlin.String - correspondingProperty: PROPERTY name:a visibility:public modality:FINAL [val] - $this: VALUE_PARAMETER name: type:test.Foo - BLOCK_BODY - RETURN type=kotlin.Nothing from='public final fun (): kotlin.String declared in test.Foo' - GET_FIELD 'FIELD PROPERTY_BACKING_FIELD name:a type:kotlin.String visibility:private [final]' type=kotlin.String origin=null - receiver: GET_VAR ': test.Foo declared in test.Foo.' type=test.Foo origin=null - FUN name:foo visibility:public modality:FINAL <> ($this:test.Foo) returnType:kotlin.String - $this: VALUE_PARAMETER name: type:test.Foo - BLOCK_BODY - RETURN type=kotlin.Nothing from='public final fun foo (): kotlin.String declared in test.Foo' - CONST String type=kotlin.String value="" - 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 [operator] declared in kotlin.Any - $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 declared in kotlin.Any - $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 declared in kotlin.Any - $this: VALUE_PARAMETER name: type:kotlin.Any - PROPERTY name:test1 visibility:public modality:FINAL [val] - FIELD PROPERTY_BACKING_FIELD name:test1 type:kotlin.reflect.KProperty0 visibility:private [final,static] - EXPRESSION_BODY - PROPERTY_REFERENCE 'public final a: kotlin.String [val]' field=null getter='public final fun (): kotlin.String declared in test.Foo' setter=null type=kotlin.reflect.KProperty0 origin=null - $this: GET_OBJECT 'CLASS OBJECT name:Foo modality:FINAL visibility:public superTypes:[kotlin.Any]' type=test.Foo - FUN DEFAULT_PROPERTY_ACCESSOR name: visibility:public modality:FINAL <> () returnType:kotlin.reflect.KProperty0 - correspondingProperty: PROPERTY name:test1 visibility:public modality:FINAL [val] - BLOCK_BODY - RETURN type=kotlin.Nothing from='public final fun (): kotlin.reflect.KProperty0 declared in test' - GET_FIELD 'FIELD PROPERTY_BACKING_FIELD name:test1 type:kotlin.reflect.KProperty0 visibility:private [final,static]' type=kotlin.reflect.KProperty0 origin=null - PROPERTY name:test1a visibility:public modality:FINAL [val] - FIELD PROPERTY_BACKING_FIELD name:test1a type:kotlin.reflect.KProperty0 visibility:private [final,static] - EXPRESSION_BODY - PROPERTY_REFERENCE 'public final a: kotlin.String [val]' field=null getter='public final fun (): kotlin.String declared in test.Foo' setter=null type=kotlin.reflect.KProperty0 origin=null - FUN DEFAULT_PROPERTY_ACCESSOR name: visibility:public modality:FINAL <> () returnType:kotlin.reflect.KProperty0 - correspondingProperty: PROPERTY name:test1a visibility:public modality:FINAL [val] - BLOCK_BODY - RETURN type=kotlin.Nothing from='public final fun (): kotlin.reflect.KProperty0 declared in test' - GET_FIELD 'FIELD PROPERTY_BACKING_FIELD name:test1a type:kotlin.reflect.KProperty0 visibility:private [final,static]' type=kotlin.reflect.KProperty0 origin=null - PROPERTY name:test2 visibility:public modality:FINAL [val] - FIELD PROPERTY_BACKING_FIELD name:test2 type:kotlin.reflect.KFunction0 visibility:private [final,static] - EXPRESSION_BODY - FUNCTION_REFERENCE 'public final fun foo (): kotlin.String declared in test.Foo' type=kotlin.reflect.KFunction0 origin=null reflectionTarget= - $this: GET_OBJECT 'CLASS OBJECT name:Foo modality:FINAL visibility:public superTypes:[kotlin.Any]' type=test.Foo - FUN DEFAULT_PROPERTY_ACCESSOR name: visibility:public modality:FINAL <> () returnType:kotlin.reflect.KFunction0 - correspondingProperty: PROPERTY name:test2 visibility:public modality:FINAL [val] - BLOCK_BODY - RETURN type=kotlin.Nothing from='public final fun (): kotlin.reflect.KFunction0 declared in test' - GET_FIELD 'FIELD PROPERTY_BACKING_FIELD name:test2 type:kotlin.reflect.KFunction0 visibility:private [final,static]' type=kotlin.reflect.KFunction0 origin=null - PROPERTY name:test2a visibility:public modality:FINAL [val] - FIELD PROPERTY_BACKING_FIELD name:test2a type:kotlin.reflect.KFunction0 visibility:private [final,static] - EXPRESSION_BODY - FUNCTION_REFERENCE 'public final fun foo (): kotlin.String declared in test.Foo' type=kotlin.reflect.KFunction0 origin=null reflectionTarget= - FUN DEFAULT_PROPERTY_ACCESSOR name: visibility:public modality:FINAL <> () returnType:kotlin.reflect.KFunction0 - correspondingProperty: PROPERTY name:test2a visibility:public modality:FINAL [val] - BLOCK_BODY - RETURN type=kotlin.Nothing from='public final fun (): kotlin.reflect.KFunction0 declared in test' - GET_FIELD 'FIELD PROPERTY_BACKING_FIELD name:test2a type:kotlin.reflect.KFunction0 visibility:private [final,static]' type=kotlin.reflect.KFunction0 origin=null diff --git a/compiler/testData/ir/irText/expressions/callableReferences/importedFromObject.kt b/compiler/testData/ir/irText/expressions/callableReferences/importedFromObject.kt index 996561aaa3e..ff3f18b84d7 100644 --- a/compiler/testData/ir/irText/expressions/callableReferences/importedFromObject.kt +++ b/compiler/testData/ir/irText/expressions/callableReferences/importedFromObject.kt @@ -1,3 +1,4 @@ +// FIR_IDENTICAL package test import test.Foo.a diff --git a/compiler/testData/ir/irText/expressions/callableReferences/unboundMemberReferenceWithAdaptedArguments.fir.txt b/compiler/testData/ir/irText/expressions/callableReferences/unboundMemberReferenceWithAdaptedArguments.fir.txt index 52f759c461d..075c48f6624 100644 --- a/compiler/testData/ir/irText/expressions/callableReferences/unboundMemberReferenceWithAdaptedArguments.fir.txt +++ b/compiler/testData/ir/irText/expressions/callableReferences/unboundMemberReferenceWithAdaptedArguments.fir.txt @@ -69,3 +69,4 @@ FILE fqName: fileName:/unboundMemberReferenceWithAdaptedArguments.kt BLOCK_BODY ERROR_CALL 'Unresolved reference: #' type=IrErrorType FUNCTION_REFERENCE 'public final fun foo (vararg xs: kotlin.Int): kotlin.Int declared in .Obj' type=kotlin.reflect.KFunction1 origin=null reflectionTarget= + $this: GET_OBJECT 'CLASS OBJECT name:Obj modality:FINAL visibility:public superTypes:[.A]' type=.Obj