[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.declarations.FirSimpleFunction
|
||||||
import org.jetbrains.kotlin.fir.expressions.FirReturnExpression
|
import org.jetbrains.kotlin.fir.expressions.FirReturnExpression
|
||||||
import org.jetbrains.kotlin.ir.declarations.*
|
import org.jetbrains.kotlin.ir.declarations.*
|
||||||
|
import org.jetbrains.kotlin.ir.util.parentClassOrNull
|
||||||
|
|
||||||
class Fir2IrConversionScope {
|
class Fir2IrConversionScope {
|
||||||
private val parentStack = mutableListOf<IrDeclarationParent>()
|
private val parentStack = mutableListOf<IrDeclarationParent>()
|
||||||
@@ -76,6 +77,15 @@ class Fir2IrConversionScope {
|
|||||||
|
|
||||||
fun parent(): IrDeclarationParent? = parentStack.lastOrNull()
|
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? {
|
fun lastDispatchReceiverParameter(): IrValueParameter? {
|
||||||
// Use the dispatch receiver of the containing/enclosing functions (from the last to the first)
|
// Use the dispatch receiver of the containing/enclosing functions (from the last to the first)
|
||||||
for (function in functionStack.asReversed()) {
|
for (function in functionStack.asReversed()) {
|
||||||
|
|||||||
@@ -285,21 +285,20 @@ class Fir2IrVisitor(
|
|||||||
|
|
||||||
override fun visitThisReceiverExpression(thisReceiverExpression: FirThisReceiverExpression, data: Any?): IrElement {
|
override fun visitThisReceiverExpression(thisReceiverExpression: FirThisReceiverExpression, data: Any?): IrElement {
|
||||||
val calleeReference = thisReceiverExpression.calleeReference
|
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
|
// Object case
|
||||||
val firObject = (calleeReference.boundSymbol?.fir as? FirClass)?.takeIf {
|
val firClass = boundSymbol.fir as FirClass
|
||||||
it is FirAnonymousObject || it is FirRegularClass && it.classKind == ClassKind.OBJECT
|
val irClass = classifierStorage.getCachedIrClass(firClass)!!
|
||||||
}
|
if (firClass is FirAnonymousObject || firClass is FirRegularClass && firClass.classKind == ClassKind.OBJECT) {
|
||||||
if (firObject != null) {
|
if (irClass != conversionScope.lastClass()) {
|
||||||
val irObject = classifierStorage.getCachedIrClass(firObject)!!
|
|
||||||
if (irObject != conversionScope.lastClass()) {
|
|
||||||
return thisReceiverExpression.convertWithOffsets { startOffset, endOffset ->
|
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) {
|
if (dispatchReceiver != null) {
|
||||||
return thisReceiverExpression.convertWithOffsets { startOffset, endOffset ->
|
return thisReceiverExpression.convertWithOffsets { startOffset, endOffset ->
|
||||||
IrGetValueImpl(startOffset, endOffset, dispatchReceiver.type, dispatchReceiver.symbol)
|
IrGetValueImpl(startOffset, endOffset, dispatchReceiver.type, dispatchReceiver.symbol)
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
// IGNORE_BACKEND_FIR: JVM_IR
|
||||||
class B () {}
|
class B () {}
|
||||||
|
|
||||||
open class A(val b : B) {
|
open class A(val b : B) {
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
// IGNORE_BACKEND_FIR: JVM_IR
|
|
||||||
class A {
|
class A {
|
||||||
var result = "Fail"
|
var result = "Fail"
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
// IGNORE_BACKEND_FIR: JVM_IR
|
|
||||||
package t
|
package t
|
||||||
|
|
||||||
interface I{
|
interface I{
|
||||||
|
|||||||
-1
@@ -1,4 +1,3 @@
|
|||||||
// IGNORE_BACKEND_FIR: JVM_IR
|
|
||||||
open class A(val s: String)
|
open class A(val s: String)
|
||||||
|
|
||||||
fun box(): String {
|
fun box(): String {
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
// IGNORE_BACKEND_FIR: JVM_IR
|
||||||
// TARGET_BACKEND: JVM
|
// TARGET_BACKEND: JVM
|
||||||
|
|
||||||
public object SomeObject {
|
public object SomeObject {
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
// IGNORE_BACKEND_FIR: JVM_IR
|
|
||||||
class C(x: Int, val y: Int) {
|
class C(x: Int, val y: Int) {
|
||||||
fun initChild(x0: Int): Any {
|
fun initChild(x0: Int): Any {
|
||||||
var x = x0
|
var x = x0
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
// IGNORE_BACKEND_FIR: JVM_IR
|
|
||||||
interface Intf {
|
interface Intf {
|
||||||
val aValue: String
|
val aValue: String
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
// IGNORE_BACKEND_FIR: JVM_IR
|
||||||
abstract class Your {
|
abstract class Your {
|
||||||
abstract val your: String
|
abstract val your: String
|
||||||
|
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ FILE fqName:<root> fileName:/outerClassAccess.kt
|
|||||||
$this: VALUE_PARAMETER name:<this> type:<root>.Outer.Inner
|
$this: VALUE_PARAMETER name:<this> type:<root>.Outer.Inner
|
||||||
BLOCK_BODY
|
BLOCK_BODY
|
||||||
CALL 'public final fun foo (): kotlin.Unit declared in <root>.Outer' type=kotlin.Unit 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 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]
|
CLASS CLASS name:Inner2 modality:FINAL visibility:public [inner] superTypes:[kotlin.Any]
|
||||||
$this: VALUE_PARAMETER INSTANCE_RECEIVER name:<this> type:<root>.Outer.Inner.Inner2
|
$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]
|
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
|
$this: VALUE_PARAMETER name:<this> type:<root>.Outer.Inner.Inner2
|
||||||
BLOCK_BODY
|
BLOCK_BODY
|
||||||
CALL 'public final fun test (): kotlin.Unit declared in <root>.Outer.Inner' type=kotlin.Unit origin=null
|
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
|
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
|
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
|
$this: VALUE_PARAMETER name:<this> type:<root>.Outer.Inner.Inner2
|
||||||
$receiver: VALUE_PARAMETER name:<this> type:<root>.Outer
|
$receiver: VALUE_PARAMETER name:<this> type:<root>.Outer
|
||||||
|
|||||||
@@ -86,7 +86,7 @@ FILE fqName:<root> fileName:/multipleThisReferences.kt
|
|||||||
EXPRESSION_BODY
|
EXPRESSION_BODY
|
||||||
CALL 'public final fun plus (other: kotlin.Int): kotlin.Int [operator] declared in kotlin.Int' type=kotlin.Int origin=PLUS
|
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: 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
|
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
|
$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
|
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
|
BLOCK_BODY
|
||||||
RETURN type=kotlin.Nothing from='public final fun inner (): kotlin.Unit declared in <root>.Outer.Inner'
|
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
|
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]
|
FUN FAKE_OVERRIDE name:equals visibility:public modality:OPEN <> ($this:kotlin.Any, other:kotlin.Any?) returnType:kotlin.Boolean [fake_override,operator]
|
||||||
overridden:
|
overridden:
|
||||||
public open fun equals (other: kotlin.Any?): kotlin.Boolean [operator] declared in kotlin.Any
|
public open fun equals (other: kotlin.Any?): kotlin.Boolean [operator] declared in kotlin.Any
|
||||||
|
|||||||
Reference in New Issue
Block a user