diff --git a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrConversionScope.kt b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrConversionScope.kt index 51d69e1c472..c6dab0333ea 100644 --- a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrConversionScope.kt +++ b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrConversionScope.kt @@ -8,6 +8,7 @@ package org.jetbrains.kotlin.fir.backend import org.jetbrains.kotlin.fir.declarations.FirSimpleFunction import org.jetbrains.kotlin.fir.expressions.FirReturnExpression import org.jetbrains.kotlin.ir.declarations.* +import org.jetbrains.kotlin.ir.util.parentClassOrNull class Fir2IrConversionScope { private val parentStack = mutableListOf() @@ -76,6 +77,15 @@ class Fir2IrConversionScope { fun parent(): IrDeclarationParent? = parentStack.lastOrNull() + fun dispatchReceiverParameter(irClass: IrClass): IrValueParameter? { + for (function in functionStack.asReversed()) { + if (function.parentClassOrNull == irClass) { + function.dispatchReceiverParameter?.let { return it } + } + } + return irClass.thisReceiver + } + fun lastDispatchReceiverParameter(): IrValueParameter? { // Use the dispatch receiver of the containing/enclosing functions (from the last to the first) for (function in functionStack.asReversed()) { 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 82195ed7067..37b45c76a4f 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 @@ -285,21 +285,20 @@ class Fir2IrVisitor( override fun visitThisReceiverExpression(thisReceiverExpression: FirThisReceiverExpression, data: Any?): IrElement { val calleeReference = thisReceiverExpression.calleeReference - if (calleeReference.labelName == null && calleeReference.boundSymbol is FirClassSymbol) { + val boundSymbol = calleeReference.boundSymbol + if (calleeReference.labelName == null && boundSymbol is FirClassSymbol) { // Object case - val firObject = (calleeReference.boundSymbol?.fir as? FirClass)?.takeIf { - it is FirAnonymousObject || it is FirRegularClass && it.classKind == ClassKind.OBJECT - } - if (firObject != null) { - val irObject = classifierStorage.getCachedIrClass(firObject)!! - if (irObject != conversionScope.lastClass()) { + val firClass = boundSymbol.fir as FirClass + val irClass = classifierStorage.getCachedIrClass(firClass)!! + if (firClass is FirAnonymousObject || firClass is FirRegularClass && firClass.classKind == ClassKind.OBJECT) { + if (irClass != conversionScope.lastClass()) { return thisReceiverExpression.convertWithOffsets { startOffset, endOffset -> - IrGetObjectValueImpl(startOffset, endOffset, irObject.defaultType, irObject.symbol) + IrGetObjectValueImpl(startOffset, endOffset, irClass.defaultType, irClass.symbol) } } } - val dispatchReceiver = conversionScope.lastDispatchReceiverParameter() + val dispatchReceiver = conversionScope.dispatchReceiverParameter(irClass) if (dispatchReceiver != null) { return thisReceiverExpression.convertWithOffsets { startOffset, endOffset -> IrGetValueImpl(startOffset, endOffset, dispatchReceiver.type, dispatchReceiver.symbol) diff --git a/compiler/testData/codegen/box/classes/selfcreate.kt b/compiler/testData/codegen/box/classes/selfcreate.kt index 95d67471511..2c6f1571e17 100644 --- a/compiler/testData/codegen/box/classes/selfcreate.kt +++ b/compiler/testData/codegen/box/classes/selfcreate.kt @@ -1,3 +1,4 @@ +// IGNORE_BACKEND_FIR: JVM_IR class B () {} open class A(val b : B) { diff --git a/compiler/testData/codegen/box/extensionProperties/accessorForPrivateSetter.kt b/compiler/testData/codegen/box/extensionProperties/accessorForPrivateSetter.kt index 0c7060640f0..6dae405db6d 100644 --- a/compiler/testData/codegen/box/extensionProperties/accessorForPrivateSetter.kt +++ b/compiler/testData/codegen/box/extensionProperties/accessorForPrivateSetter.kt @@ -1,4 +1,3 @@ -// IGNORE_BACKEND_FIR: JVM_IR class A { var result = "Fail" diff --git a/compiler/testData/codegen/box/functions/kt1413.kt b/compiler/testData/codegen/box/functions/kt1413.kt index 0e1040ae35d..15b496fb9b9 100644 --- a/compiler/testData/codegen/box/functions/kt1413.kt +++ b/compiler/testData/codegen/box/functions/kt1413.kt @@ -1,4 +1,3 @@ -// IGNORE_BACKEND_FIR: JVM_IR package t interface I{ diff --git a/compiler/testData/codegen/box/innerNested/superConstructorCall/objectExtendsLocalCaptureInSuperCall.kt b/compiler/testData/codegen/box/innerNested/superConstructorCall/objectExtendsLocalCaptureInSuperCall.kt index a36192913f5..35bb545eb82 100644 --- a/compiler/testData/codegen/box/innerNested/superConstructorCall/objectExtendsLocalCaptureInSuperCall.kt +++ b/compiler/testData/codegen/box/innerNested/superConstructorCall/objectExtendsLocalCaptureInSuperCall.kt @@ -1,4 +1,3 @@ -// IGNORE_BACKEND_FIR: JVM_IR open class A(val s: String) fun box(): String { diff --git a/compiler/testData/codegen/box/objects/kt1136.kt b/compiler/testData/codegen/box/objects/kt1136.kt index 9caf5855971..93b56bdb5a2 100644 --- a/compiler/testData/codegen/box/objects/kt1136.kt +++ b/compiler/testData/codegen/box/objects/kt1136.kt @@ -1,3 +1,4 @@ +// IGNORE_BACKEND_FIR: JVM_IR // TARGET_BACKEND: JVM public object SomeObject { diff --git a/compiler/testData/codegen/box/objects/objectLiteral.kt b/compiler/testData/codegen/box/objects/objectLiteral.kt index b96cbda0499..e6030a1ccd9 100644 --- a/compiler/testData/codegen/box/objects/objectLiteral.kt +++ b/compiler/testData/codegen/box/objects/objectLiteral.kt @@ -1,4 +1,3 @@ -// IGNORE_BACKEND_FIR: JVM_IR class C(x: Int, val y: Int) { fun initChild(x0: Int): Any { var x = x0 diff --git a/compiler/testData/codegen/box/operatorConventions/kt14201.kt b/compiler/testData/codegen/box/operatorConventions/kt14201.kt index 678983b51b4..3ee91af468f 100644 --- a/compiler/testData/codegen/box/operatorConventions/kt14201.kt +++ b/compiler/testData/codegen/box/operatorConventions/kt14201.kt @@ -1,4 +1,3 @@ -// IGNORE_BACKEND_FIR: JVM_IR interface Intf { val aValue: String } diff --git a/compiler/testData/codegen/box/properties/classFieldInsideNested.kt b/compiler/testData/codegen/box/properties/classFieldInsideNested.kt index 0ac00ddb630..858bbbe4832 100644 --- a/compiler/testData/codegen/box/properties/classFieldInsideNested.kt +++ b/compiler/testData/codegen/box/properties/classFieldInsideNested.kt @@ -1,3 +1,4 @@ +// IGNORE_BACKEND_FIR: JVM_IR abstract class Your { abstract val your: String diff --git a/compiler/testData/ir/irText/classes/outerClassAccess.fir.txt b/compiler/testData/ir/irText/classes/outerClassAccess.fir.txt index 95faa8a6a06..d0bb1fe572f 100644 --- a/compiler/testData/ir/irText/classes/outerClassAccess.fir.txt +++ b/compiler/testData/ir/irText/classes/outerClassAccess.fir.txt @@ -19,7 +19,7 @@ FILE fqName: fileName:/outerClassAccess.kt $this: VALUE_PARAMETER name: type:.Outer.Inner BLOCK_BODY CALL 'public final fun foo (): kotlin.Unit declared in .Outer' type=kotlin.Unit origin=null - $this: GET_VAR ': .Outer.Inner declared in .Outer.Inner.test' type=.Outer.Inner origin=null + $this: GET_VAR ': .Outer declared in .Outer' type=.Outer origin=null CLASS CLASS name:Inner2 modality:FINAL visibility:public [inner] superTypes:[kotlin.Any] $this: VALUE_PARAMETER INSTANCE_RECEIVER name: type:.Outer.Inner.Inner2 CONSTRUCTOR visibility:public <> ($this:.Outer.Inner.Inner2) returnType:.Outer.Inner.Inner2 [primary] @@ -31,9 +31,9 @@ FILE fqName: fileName:/outerClassAccess.kt $this: VALUE_PARAMETER name: type:.Outer.Inner.Inner2 BLOCK_BODY CALL 'public final fun test (): kotlin.Unit declared in .Outer.Inner' type=kotlin.Unit origin=null - $this: GET_VAR ': .Outer.Inner.Inner2 declared in .Outer.Inner.Inner2.test2' type=.Outer.Inner.Inner2 origin=null + $this: GET_VAR ': .Outer.Inner declared in .Outer.Inner' type=.Outer.Inner origin=null CALL 'public final fun foo (): kotlin.Unit declared in .Outer' type=kotlin.Unit origin=null - $this: GET_VAR ': .Outer.Inner.Inner2 declared in .Outer.Inner.Inner2.test2' type=.Outer.Inner.Inner2 origin=null + $this: GET_VAR ': .Outer declared in .Outer' type=.Outer origin=null FUN name:test3 visibility:public modality:FINAL <> ($this:.Outer.Inner.Inner2, $receiver:.Outer) returnType:kotlin.Unit $this: VALUE_PARAMETER name: type:.Outer.Inner.Inner2 $receiver: VALUE_PARAMETER name: type:.Outer diff --git a/compiler/testData/ir/irText/expressions/multipleThisReferences.fir.txt b/compiler/testData/ir/irText/expressions/multipleThisReferences.fir.txt index f7eb6099fdb..de0355b4fa0 100644 --- a/compiler/testData/ir/irText/expressions/multipleThisReferences.fir.txt +++ b/compiler/testData/ir/irText/expressions/multipleThisReferences.fir.txt @@ -86,7 +86,7 @@ FILE fqName: fileName:/multipleThisReferences.kt EXPRESSION_BODY CALL 'public final fun plus (other: kotlin.Int): kotlin.Int [operator] declared in kotlin.Int' type=kotlin.Int origin=PLUS $this: CALL 'public final fun (): kotlin.Int declared in .Outer.Inner' type=kotlin.Int origin=GET_PROPERTY - $this: GET_VAR ': .Host declared in .Host.test' type=.Host origin=null + $this: GET_VAR ': .Host.test. declared in .Host.test.' type=.Host.test. origin=null other: CALL 'public final fun (): kotlin.Int declared in .Host' type=kotlin.Int origin=GET_PROPERTY $this: GET_VAR ': .Host declared in .Host.test' type=.Host origin=null FUN DEFAULT_PROPERTY_ACCESSOR name: visibility:public modality:FINAL <> ($this:.Host.test.) returnType:kotlin.Int diff --git a/compiler/testData/ir/irText/expressions/outerClassInstanceReference.fir.txt b/compiler/testData/ir/irText/expressions/outerClassInstanceReference.fir.txt index dd6f338c720..63832a8569d 100644 --- a/compiler/testData/ir/irText/expressions/outerClassInstanceReference.fir.txt +++ b/compiler/testData/ir/irText/expressions/outerClassInstanceReference.fir.txt @@ -20,7 +20,7 @@ FILE fqName: fileName:/outerClassInstanceReference.kt BLOCK_BODY RETURN type=kotlin.Nothing from='public final fun inner (): kotlin.Unit declared in .Outer.Inner' CALL 'public final fun outer (): kotlin.Unit declared in .Outer' type=kotlin.Unit origin=null - $this: GET_VAR ': .Outer.Inner declared in .Outer.Inner.inner' type=.Outer.Inner origin=null + $this: GET_VAR ': .Outer declared in .Outer' type=.Outer 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 [operator] declared in kotlin.Any