JVM_IR: fold inline lambdas when computing OUTERCLASS

so that the enclosing method of objects defined inside lambdas is the
one they are declared in.

Note that this does not fix *all* enclosingInfo tests because JVM_IR
currently follows the KT-28064 proposal, i.e. does not regenerate
objects defined inside lambdas under any circumstances. For example,
this causes test boxInline/enclosingInfo/inlineChain2.kt to fail because
the enclosing method of objects is _2Kt.box instead of (non-existent in
source code) `_2Kt$box$inlined$call$1.invoke` or whatever. What's more
important is that OUTERCLASS no longer points to a non-existent
`box$lambda-N` and therefore `.enclosingMethod` no longer throws.
This commit is contained in:
pyos
2019-11-11 11:57:38 +01:00
committed by max-kammerer
parent f906524d76
commit ce0fb662c0
7 changed files with 11 additions and 20 deletions
@@ -52,6 +52,7 @@ open class ClassCodegen protected constructor(
internal val irClass: IrClass, internal val irClass: IrClass,
val context: JvmBackendContext, val context: JvmBackendContext,
private val parentClassCodegen: ClassCodegen? = null, private val parentClassCodegen: ClassCodegen? = null,
private val parentFunction: IrFunction? = null,
private val withinInline: Boolean = false private val withinInline: Boolean = false
) : InnerClassConsumer { ) : InnerClassConsumer {
private val innerClasses = mutableListOf<IrClass>() private val innerClasses = mutableListOf<IrClass>()
@@ -303,8 +304,8 @@ open class ClassCodegen protected constructor(
} }
} }
fun generateLocalClass(klass: IrClass, withinInline: Boolean): ReifiedTypeParametersUsages { fun generateLocalClass(klass: IrClass, parentFunction: IrFunction): ReifiedTypeParametersUsages {
return ClassCodegen(klass, context, this, withinInline = withinInline || this.withinInline).generate() return ClassCodegen(klass, context, this, parentFunction, withinInline = withinInline || parentFunction.isInline).generate()
} }
private fun generateField(field: IrField) { private fun generateField(field: IrField) {
@@ -404,12 +405,9 @@ open class ClassCodegen protected constructor(
// or constructor, the name and type of the function is recorded as well. // or constructor, the name and type of the function is recorded as well.
if (parentClassCodegen != null) { if (parentClassCodegen != null) {
val outerClassName = parentClassCodegen.type.internalName val outerClassName = parentClassCodegen.type.internalName
// TODO: Since the class could have been reparented in lowerings, this could // TODO: LocalDeclarationsLowering could have moved this class out of its enclosing method.
// be a class instead of the actual function that the class is nested inside if (parentFunction != null) {
// in the source. val method = methodSignatureMapper.mapAsmMethod(parentFunction)
val containingDeclaration = irClass.symbol.owner.parent
if (containingDeclaration is IrFunction) {
val method = methodSignatureMapper.mapAsmMethod(containingDeclaration)
visitor.visitOuterClass(outerClassName, method.name, method.descriptor) visitor.visitOuterClass(outerClassName, method.name, method.descriptor)
} else if (irClass.isAnonymousObject) { } else if (irClass.isAnonymousObject) {
visitor.visitOuterClass(outerClassName, null, null) visitor.visitOuterClass(outerClassName, null, null)
@@ -556,7 +556,7 @@ class ExpressionCodegen(
) )
override fun visitClass(declaration: IrClass, data: BlockInfo): PromisedValue { override fun visitClass(declaration: IrClass, data: BlockInfo): PromisedValue {
classCodegen.generateLocalClass(declaration, generateSequence(this) { it.inlinedInto }.any { it.irFunction.isInline }).also { classCodegen.generateLocalClass(declaration, generateSequence(this) { it.inlinedInto }.last().irFunction).also {
closureReifiedMarkers[declaration] = it closureReifiedMarkers[declaration] = it
} }
return immaterialUnitValue return immaterialUnitValue
@@ -58,11 +58,11 @@ class IrSourceCompilerForInline(
override val inlineCallSiteInfo: InlineCallSiteInfo override val inlineCallSiteInfo: InlineCallSiteInfo
get() { get() {
//TODO: support nested inline calls val root = generateSequence(codegen) { it.inlinedInto }.last()
return InlineCallSiteInfo( return InlineCallSiteInfo(
codegen.classCodegen.type.internalName, root.classCodegen.type.internalName,
codegen.signature.asmMethod.name, root.signature.asmMethod.name,
codegen.signature.asmMethod.descriptor, root.signature.asmMethod.descriptor,
//compilationContextFunctionDescriptor.isInlineOrInsideInline() //compilationContextFunctionDescriptor.isInlineOrInsideInline()
false, false,
compilationContextFunctionDescriptor.isSuspend compilationContextFunctionDescriptor.isSuspend
@@ -1,4 +1,3 @@
// IGNORE_BACKEND: JVM_IR
// TARGET_BACKEND: JVM // TARGET_BACKEND: JVM
// WITH_REFLECT // WITH_REFLECT
@@ -1,5 +1,3 @@
// IGNORE_BACKEND: JVM_IR
// IGNORE_BACKEND_MULTI_MODULE: JVM_IR
// TARGET_BACKEND: JVM // TARGET_BACKEND: JVM
// NO_CHECK_LAMBDA_INLINING // NO_CHECK_LAMBDA_INLINING
// WITH_REFLECT // WITH_REFLECT
@@ -1,5 +1,3 @@
// IGNORE_BACKEND: JVM_IR
// IGNORE_BACKEND_MULTI_MODULE: JVM_IR
// TARGET_BACKEND: JVM // TARGET_BACKEND: JVM
// FILE: 1.kt // FILE: 1.kt
// WITH_REFLECT // WITH_REFLECT
@@ -1,5 +1,3 @@
// IGNORE_BACKEND: JVM_IR
// IGNORE_BACKEND_MULTI_MODULE: JVM_IR
// TARGET_BACKEND: JVM // TARGET_BACKEND: JVM
// FILE: 1.kt // FILE: 1.kt
// WITH_REFLECT // WITH_REFLECT