[FIR2IR] Correct 'this' conversion when it points to non-closest class
This commit is contained in:
@@ -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<IrDeclarationParent>()
|
||||
@@ -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()) {
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
// IGNORE_BACKEND_FIR: JVM_IR
|
||||
class B () {}
|
||||
|
||||
open class A(val b : B) {
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
// IGNORE_BACKEND_FIR: JVM_IR
|
||||
class A {
|
||||
var result = "Fail"
|
||||
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
// IGNORE_BACKEND_FIR: JVM_IR
|
||||
package t
|
||||
|
||||
interface I{
|
||||
|
||||
-1
@@ -1,4 +1,3 @@
|
||||
// IGNORE_BACKEND_FIR: JVM_IR
|
||||
open class A(val s: String)
|
||||
|
||||
fun box(): String {
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
// IGNORE_BACKEND_FIR: JVM_IR
|
||||
// TARGET_BACKEND: JVM
|
||||
|
||||
public object SomeObject {
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
// IGNORE_BACKEND_FIR: JVM_IR
|
||||
class C(x: Int, val y: Int) {
|
||||
fun initChild(x0: Int): Any {
|
||||
var x = x0
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
// IGNORE_BACKEND_FIR: JVM_IR
|
||||
interface Intf {
|
||||
val aValue: String
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
// IGNORE_BACKEND_FIR: JVM_IR
|
||||
abstract class Your {
|
||||
abstract val your: String
|
||||
|
||||
|
||||
@@ -19,7 +19,7 @@ FILE fqName:<root> fileName:/outerClassAccess.kt
|
||||
$this: VALUE_PARAMETER name:<this> type:<root>.Outer.Inner
|
||||
BLOCK_BODY
|
||||
CALL 'public final fun foo (): kotlin.Unit declared in <root>.Outer' type=kotlin.Unit origin=null
|
||||
$this: GET_VAR '<this>: <root>.Outer.Inner declared in <root>.Outer.Inner.test' type=<root>.Outer.Inner origin=null
|
||||
$this: GET_VAR '<this>: <root>.Outer declared in <root>.Outer' type=<root>.Outer origin=null
|
||||
CLASS CLASS name:Inner2 modality:FINAL visibility:public [inner] superTypes:[kotlin.Any]
|
||||
$this: VALUE_PARAMETER INSTANCE_RECEIVER name:<this> type:<root>.Outer.Inner.Inner2
|
||||
CONSTRUCTOR visibility:public <> ($this:<root>.Outer.Inner.Inner2) returnType:<root>.Outer.Inner.Inner2 [primary]
|
||||
@@ -31,9 +31,9 @@ FILE fqName:<root> fileName:/outerClassAccess.kt
|
||||
$this: VALUE_PARAMETER name:<this> type:<root>.Outer.Inner.Inner2
|
||||
BLOCK_BODY
|
||||
CALL 'public final fun test (): kotlin.Unit declared in <root>.Outer.Inner' type=kotlin.Unit origin=null
|
||||
$this: GET_VAR '<this>: <root>.Outer.Inner.Inner2 declared in <root>.Outer.Inner.Inner2.test2' type=<root>.Outer.Inner.Inner2 origin=null
|
||||
$this: GET_VAR '<this>: <root>.Outer.Inner declared in <root>.Outer.Inner' type=<root>.Outer.Inner origin=null
|
||||
CALL 'public final fun foo (): kotlin.Unit declared in <root>.Outer' type=kotlin.Unit origin=null
|
||||
$this: GET_VAR '<this>: <root>.Outer.Inner.Inner2 declared in <root>.Outer.Inner.Inner2.test2' type=<root>.Outer.Inner.Inner2 origin=null
|
||||
$this: GET_VAR '<this>: <root>.Outer declared in <root>.Outer' type=<root>.Outer origin=null
|
||||
FUN name:test3 visibility:public modality:FINAL <> ($this:<root>.Outer.Inner.Inner2, $receiver:<root>.Outer) returnType:kotlin.Unit
|
||||
$this: VALUE_PARAMETER name:<this> type:<root>.Outer.Inner.Inner2
|
||||
$receiver: VALUE_PARAMETER name:<this> type:<root>.Outer
|
||||
|
||||
@@ -86,7 +86,7 @@ FILE fqName:<root> 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 <get-x> (): kotlin.Int declared in <root>.Outer.Inner' type=kotlin.Int origin=GET_PROPERTY
|
||||
$this: GET_VAR '<this>: <root>.Host declared in <root>.Host.test' type=<root>.Host origin=null
|
||||
$this: GET_VAR '<this>: <root>.Host.test.<no name provided> declared in <root>.Host.test.<no name provided>' type=<root>.Host.test.<no name provided> origin=null
|
||||
other: CALL 'public final fun <get-y> (): kotlin.Int declared in <root>.Host' type=kotlin.Int origin=GET_PROPERTY
|
||||
$this: GET_VAR '<this>: <root>.Host declared in <root>.Host.test' type=<root>.Host origin=null
|
||||
FUN DEFAULT_PROPERTY_ACCESSOR name:<get-xx> visibility:public modality:FINAL <> ($this:<root>.Host.test.<no name provided>) returnType:kotlin.Int
|
||||
|
||||
+1
-1
@@ -20,7 +20,7 @@ FILE fqName:<root> fileName:/outerClassInstanceReference.kt
|
||||
BLOCK_BODY
|
||||
RETURN type=kotlin.Nothing from='public final fun inner (): kotlin.Unit declared in <root>.Outer.Inner'
|
||||
CALL 'public final fun outer (): kotlin.Unit declared in <root>.Outer' type=kotlin.Unit origin=null
|
||||
$this: GET_VAR '<this>: <root>.Outer.Inner declared in <root>.Outer.Inner.inner' type=<root>.Outer.Inner origin=null
|
||||
$this: GET_VAR '<this>: <root>.Outer declared in <root>.Outer' type=<root>.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
|
||||
|
||||
Reference in New Issue
Block a user