Fix outer class accesses inside suspendImpl functions

We need to generate this$0 fields to get to the outer class and generate
accesses to these fields inside suspendImpl function.
 #KT-46214 Fixed
This commit is contained in:
Ilmir Usmanov
2021-04-27 03:22:58 +02:00
parent 71365d2452
commit 0c0710bb79
9 changed files with 123 additions and 0 deletions
@@ -9795,6 +9795,12 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT
runTest("compiler/testData/codegen/box/coroutines/featureIntersection/overrideInInlineClass.kt");
}
@Test
@TestMetadata("overrideInInnerClass.kt")
public void testOverrideInInnerClass() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/featureIntersection/overrideInInnerClass.kt");
}
@Test
@TestMetadata("safeCallOnTwoReceivers.kt")
public void testSafeCallOnTwoReceivers() throws Exception {
@@ -226,6 +226,44 @@ private class AddContinuationLowering(context: JvmBackendContext) : SuspendLower
copyMetadata = false
)
static.body = irFunction.moveBodyTo(static)
// Fixup dispatch parameter to outer class
if (irFunction.parentAsClass.isInner) {
val movedDispatchParameter = static.valueParameters[0]
assert(movedDispatchParameter.origin == IrDeclarationOrigin.MOVED_DISPATCH_RECEIVER) {
"MOVED_DISPATCH_RECEIVER should be the first parameter in ${static.render()}"
}
static.body!!.transformChildrenVoid(object : IrElementTransformerVoid() {
override fun visitGetValue(expression: IrGetValue): IrExpression {
val owner = expression.symbol.owner
if (owner is IrValueParameter && isInstanceReceiverOfOuterClass(owner)) {
// If inner class has inner classes, we need to traverse this$0 chain to get to captured dispatch receivers
var cursor = irFunction.parentAsClass
var value: IrExpression = IrGetValueImpl(expression.startOffset, expression.endOffset, movedDispatchParameter.symbol)
while (cursor != owner.parent) {
val outerThisField = context.innerClassesSupport.getOuterThisField(cursor)
value = IrGetFieldImpl(
expression.startOffset, expression.endOffset, outerThisField.symbol, outerThisField.type, value
)
cursor = cursor.parentAsClass
}
return value
}
return super.visitGetValue(expression)
}
private fun isInstanceReceiverOfOuterClass(param: IrValueParameter): Boolean {
if (param.origin != IrDeclarationOrigin.INSTANCE_RECEIVER) return false
if (param.parent !is IrClass) return false
var cursor = irFunction.parentAsClass.parent
while (cursor is IrClass) {
if (cursor == param.parent) return true
cursor = (cursor as IrClass).parent
}
return false
}
})
}
static.copyAttributes(irFunction)
// Rewrite the body of the original suspend method to forward to the new static method.
irFunction.body = context.createIrBuilder(irFunction.symbol).irBlockBody {
@@ -0,0 +1,47 @@
// WITH_RUNTIME
import kotlin.coroutines.*
class B {
val value: Long = 10L
open inner class C : A<Unit> {
override suspend fun getTotalFrames(): Long? = this@B.value
open inner class D : A<Unit> {
override suspend fun getTotalFrames(): Long? = this@B.value
}
suspend fun getInnerTotalFrames(): Long? = D().getTotalFrames()
}
suspend fun get1(): Long? {
return C().getTotalFrames()
}
suspend fun get2(): Long? = C().getInnerTotalFrames()
}
interface A<T> {
suspend fun getTotalFrames(): Long? = null
}
fun builder(c: suspend () -> Unit) {
c.startCoroutine(Continuation(EmptyCoroutineContext) {
it.getOrThrow()
})
}
fun box(): String {
var res = "FAIL"
builder {
res = if (B().get1() == 10L) "OK" else "FAIL 1 ${B().get1()}"
}
if (res != "OK") return res
res = "FAIL 2"
builder {
res = if (B().get2() == 10L) "OK" else "FAIL 2 ${B().get2()}"
}
return res
}
@@ -9795,6 +9795,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
runTest("compiler/testData/codegen/box/coroutines/featureIntersection/overrideInInlineClass.kt");
}
@Test
@TestMetadata("overrideInInnerClass.kt")
public void testOverrideInInnerClass() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/featureIntersection/overrideInInnerClass.kt");
}
@Test
@TestMetadata("safeCallOnTwoReceivers.kt")
public void testSafeCallOnTwoReceivers() throws Exception {
@@ -9795,6 +9795,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
runTest("compiler/testData/codegen/box/coroutines/featureIntersection/overrideInInlineClass.kt");
}
@Test
@TestMetadata("overrideInInnerClass.kt")
public void testOverrideInInnerClass() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/featureIntersection/overrideInInnerClass.kt");
}
@Test
@TestMetadata("safeCallOnTwoReceivers.kt")
public void testSafeCallOnTwoReceivers() throws Exception {
@@ -7695,6 +7695,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes
runTest("compiler/testData/codegen/box/coroutines/featureIntersection/overrideInInlineClass.kt");
}
@TestMetadata("overrideInInnerClass.kt")
public void testOverrideInInnerClass() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/featureIntersection/overrideInInnerClass.kt");
}
@TestMetadata("safeCallOnTwoReceivers.kt")
public void testSafeCallOnTwoReceivers() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/featureIntersection/safeCallOnTwoReceivers.kt");
@@ -6879,6 +6879,11 @@ public class IrJsCodegenBoxES6TestGenerated extends AbstractIrJsCodegenBoxES6Tes
runTest("compiler/testData/codegen/box/coroutines/featureIntersection/overrideInInlineClass.kt");
}
@TestMetadata("overrideInInnerClass.kt")
public void testOverrideInInnerClass() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/featureIntersection/overrideInInnerClass.kt");
}
@TestMetadata("safeCallOnTwoReceivers.kt")
public void testSafeCallOnTwoReceivers() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/featureIntersection/safeCallOnTwoReceivers.kt");
@@ -6290,6 +6290,11 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest {
runTest("compiler/testData/codegen/box/coroutines/featureIntersection/overrideInInlineClass.kt");
}
@TestMetadata("overrideInInnerClass.kt")
public void testOverrideInInnerClass() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/featureIntersection/overrideInInnerClass.kt");
}
@TestMetadata("safeCallOnTwoReceivers.kt")
public void testSafeCallOnTwoReceivers() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/featureIntersection/safeCallOnTwoReceivers.kt");
@@ -6290,6 +6290,11 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest {
runTest("compiler/testData/codegen/box/coroutines/featureIntersection/overrideInInlineClass.kt");
}
@TestMetadata("overrideInInnerClass.kt")
public void testOverrideInInnerClass() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/featureIntersection/overrideInInnerClass.kt");
}
@TestMetadata("safeCallOnTwoReceivers.kt")
public void testSafeCallOnTwoReceivers() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/featureIntersection/safeCallOnTwoReceivers.kt");