JVM_IR KT-46578 resolve fake overrides for fields

This commit is contained in:
Dmitry Petrov
2021-05-11 16:23:54 +03:00
committed by teamcityserver
parent a255f44d6e
commit d1322280dd
15 changed files with 413 additions and 8 deletions
@@ -40279,6 +40279,52 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT
public void testSyntheticAccessorNames() throws Exception {
runTest("compiler/testData/codegen/box/syntheticAccessors/syntheticAccessorNames.kt");
}
@Nested
@TestMetadata("compiler/testData/codegen/box/syntheticAccessors/kt46578")
@TestDataPath("$PROJECT_ROOT")
public class Kt46578 {
@Test
public void testAllFilesPresentInKt46578() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/syntheticAccessors/kt46578"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM_IR, true);
}
@Test
@TestMetadata("kt46578_anonObject.kt")
public void testKt46578_anonObject() throws Exception {
runTest("compiler/testData/codegen/box/syntheticAccessors/kt46578/kt46578_anonObject.kt");
}
@Test
@TestMetadata("kt46578_delegated.kt")
public void testKt46578_delegated() throws Exception {
runTest("compiler/testData/codegen/box/syntheticAccessors/kt46578/kt46578_delegated.kt");
}
@Test
@TestMetadata("kt46578_kotlin_delegated.kt")
public void testKt46578_kotlin_delegated() throws Exception {
runTest("compiler/testData/codegen/box/syntheticAccessors/kt46578/kt46578_kotlin_delegated.kt");
}
@Test
@TestMetadata("kt46578_kotlin_propertyRef.kt")
public void testKt46578_kotlin_propertyRef() throws Exception {
runTest("compiler/testData/codegen/box/syntheticAccessors/kt46578/kt46578_kotlin_propertyRef.kt");
}
@Test
@TestMetadata("kt46578_lambda.kt")
public void testKt46578_lambda() throws Exception {
runTest("compiler/testData/codegen/box/syntheticAccessors/kt46578/kt46578_lambda.kt");
}
@Test
@TestMetadata("kt46578_propertyRef.kt")
public void testKt46578_propertyRef() throws Exception {
runTest("compiler/testData/codegen/box/syntheticAccessors/kt46578/kt46578_propertyRef.kt");
}
}
}
@Nested
@@ -232,11 +232,11 @@ internal class SyntheticAccessorLowering(val context: JvmBackendContext) : IrEle
override fun visitGetField(expression: IrGetField): IrExpression {
val dispatchReceiverType = expression.receiver?.type
val dispatchReceiverClassSymbol = dispatchReceiverType?.classifierOrNull as? IrClassSymbol
return super.visitExpression(
if (!expression.symbol.isAccessible(false, dispatchReceiverType?.classifierOrNull as? IrClassSymbol)) {
if (!expression.symbol.isAccessible(false, dispatchReceiverClassSymbol)) {
val symbol = expression.symbol
val parent =
symbol.owner.accessorParent(dispatchReceiverType?.classOrNull?.owner ?: symbol.owner.parent) as IrClass
val parent = symbol.owner.accessorParent(dispatchReceiverClassSymbol?.owner ?: symbol.owner.parent) as IrClass
modifyGetterExpression(
expression,
getterMap.getOrPut(FieldKey(symbol, parent, expression.superQualifierSymbol)) {
@@ -251,11 +251,11 @@ internal class SyntheticAccessorLowering(val context: JvmBackendContext) : IrEle
override fun visitSetField(expression: IrSetField): IrExpression {
val dispatchReceiverType = expression.receiver?.type
val dispatchReceiverClassSymbol = dispatchReceiverType?.classifierOrNull as? IrClassSymbol
return super.visitExpression(
if (!expression.symbol.isAccessible(false, dispatchReceiverType?.classifierOrNull as? IrClassSymbol)) {
if (!expression.symbol.isAccessible(false, dispatchReceiverClassSymbol)) {
val symbol = expression.symbol
val parent =
symbol.owner.accessorParent(dispatchReceiverType?.classOrNull?.owner ?: symbol.owner.parent) as IrClass
val parent = symbol.owner.accessorParent(dispatchReceiverClassSymbol?.owner ?: symbol.owner.parent) as IrClass
modifySetterExpression(
expression,
setterMap.getOrPut(FieldKey(symbol, parent, expression.superQualifierSymbol)) {
@@ -725,8 +725,26 @@ internal class SyntheticAccessorLowering(val context: JvmBackendContext) : IrEle
if (symbolOwner is IrConstructor && symbolOwner.parentClassOrNull?.isEnumEntry == true)
return true
val declaration =
(declarationRaw as? IrSimpleFunction)?.resolveFakeOverride(allowAbstract = true) ?: declarationRaw
val declaration = when (declarationRaw) {
is IrSimpleFunction ->
declarationRaw.resolveFakeOverride(allowAbstract = true)
?: declarationRaw
is IrField -> {
val correspondingProperty = declarationRaw.correspondingPropertySymbol?.owner
if (correspondingProperty != null && correspondingProperty.isFakeOverride) {
val realProperty = correspondingProperty.resolveFakeOverrideForProperty()
realProperty.backingField
?: throw AssertionError(
"Fake override property ${correspondingProperty.render()} with backing field " +
"overrides a real property with no backing field: ${realProperty.render()}"
)
} else {
declarationRaw
}
}
else ->
declarationRaw
}
// If local variables are accessible by Kotlin rules, they also are by Java rules.
val ownerClass = declaration.parent as? IrClass ?: return true
@@ -747,6 +765,16 @@ internal class SyntheticAccessorLowering(val context: JvmBackendContext) : IrEle
}
}
private fun IrProperty.resolveFakeOverrideForProperty(): IrProperty {
if (!isFakeOverride) return this
return this.overriddenSymbols
.map { it.owner }
.collectAndFilterRealOverrides()
.firstOrNull() as? IrProperty
?: throw AssertionError("No real override for ${this.render()}")
}
private class OuterClassInfo(val outerClass: IrClass, val throughCrossinlineLambda: Boolean)
private fun getOuterClassInfo(): OuterClassInfo? {
@@ -0,0 +1,26 @@
// TARGET_BACKEND: JVM
// WITH_RUNTIME
// FILE: kt46578_anonObject.kt
import p.*
class Derived : Base() {
init {
javaProtectedField = "OK"
}
val anonObject = object {
override fun toString(): String =
javaProtectedField
}
}
fun box(): String {
return Derived().anonObject.toString()
}
// FILE: p/Base.java
package p;
public class Base {
protected String javaProtectedField;
}
@@ -0,0 +1,24 @@
// TARGET_BACKEND: JVM
// WITH_RUNTIME
// IGNORE_BACKEND_FIR: JVM_IR
// ^ ::jpf is incorrectly represented as a reference to Base::jpf (should be: reference to fake override in Derived)
// FILE: kt46578_delegated.kt
import p.*
class Derived : Base() {
var delegated by ::jpf
}
fun box(): String {
val d = Derived()
d.delegated = "OK"
return d.delegated
}
// FILE: p/Base.java
package p;
public class Base {
protected String jpf;
}
@@ -0,0 +1,23 @@
// TARGET_BACKEND: JVM
// WITH_RUNTIME
// FILE: kt46578_kotlin_delegated.kt
import p.*
class Derived : Base() {
init {
pf = "OK"
}
val delegated by ::pf
}
fun box(): String {
return Derived().delegated
}
// FILE: p/Base.kt
package p
open class Base {
protected var pf = ""
}
@@ -0,0 +1,23 @@
// TARGET_BACKEND: JVM
// WITH_RUNTIME
// FILE: kt46578_kotlin_propertyRef.kt
import p.*
class Derived : Base() {
init {
pf = "OK"
}
val ref = ::pf
}
fun box(): String {
return Derived().ref.get()
}
// FILE: p/Base.kt
package p
open class Base {
protected var pf = ""
}
@@ -0,0 +1,23 @@
// TARGET_BACKEND: JVM
// WITH_RUNTIME
// FILE: kt46578_lambda.kt
import p.*
class Derived : Base() {
init {
jpf = "OK"
}
val lambda = { jpf }
}
fun box(): String {
return Derived().lambda()
}
// FILE: p/Base.java
package p;
public class Base {
protected String jpf;
}
@@ -0,0 +1,25 @@
// TARGET_BACKEND: JVM
// WITH_RUNTIME
// IGNORE_BACKEND_FIR: JVM_IR
// ^ ::jpf is incorrectly represented as a reference to Base::jpf (should be: reference to fake override in Derived)
// FILE: kt46578_propertyRef.kt
import p.*
class Derived : Base() {
init {
jpf = "OK"
}
val ref = ::jpf
}
fun box(): String {
return Derived().ref.get()
}
// FILE: p/Base.java
package p;
public class Base {
protected String jpf;
}
@@ -40255,6 +40255,52 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
public void testSyntheticAccessorNames() throws Exception {
runTest("compiler/testData/codegen/box/syntheticAccessors/syntheticAccessorNames.kt");
}
@Nested
@TestMetadata("compiler/testData/codegen/box/syntheticAccessors/kt46578")
@TestDataPath("$PROJECT_ROOT")
public class Kt46578 {
@Test
public void testAllFilesPresentInKt46578() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/syntheticAccessors/kt46578"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM, true);
}
@Test
@TestMetadata("kt46578_anonObject.kt")
public void testKt46578_anonObject() throws Exception {
runTest("compiler/testData/codegen/box/syntheticAccessors/kt46578/kt46578_anonObject.kt");
}
@Test
@TestMetadata("kt46578_delegated.kt")
public void testKt46578_delegated() throws Exception {
runTest("compiler/testData/codegen/box/syntheticAccessors/kt46578/kt46578_delegated.kt");
}
@Test
@TestMetadata("kt46578_kotlin_delegated.kt")
public void testKt46578_kotlin_delegated() throws Exception {
runTest("compiler/testData/codegen/box/syntheticAccessors/kt46578/kt46578_kotlin_delegated.kt");
}
@Test
@TestMetadata("kt46578_kotlin_propertyRef.kt")
public void testKt46578_kotlin_propertyRef() throws Exception {
runTest("compiler/testData/codegen/box/syntheticAccessors/kt46578/kt46578_kotlin_propertyRef.kt");
}
@Test
@TestMetadata("kt46578_lambda.kt")
public void testKt46578_lambda() throws Exception {
runTest("compiler/testData/codegen/box/syntheticAccessors/kt46578/kt46578_lambda.kt");
}
@Test
@TestMetadata("kt46578_propertyRef.kt")
public void testKt46578_propertyRef() throws Exception {
runTest("compiler/testData/codegen/box/syntheticAccessors/kt46578/kt46578_propertyRef.kt");
}
}
}
@Nested
@@ -40279,6 +40279,52 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
public void testSyntheticAccessorNames() throws Exception {
runTest("compiler/testData/codegen/box/syntheticAccessors/syntheticAccessorNames.kt");
}
@Nested
@TestMetadata("compiler/testData/codegen/box/syntheticAccessors/kt46578")
@TestDataPath("$PROJECT_ROOT")
public class Kt46578 {
@Test
public void testAllFilesPresentInKt46578() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/syntheticAccessors/kt46578"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM_IR, true);
}
@Test
@TestMetadata("kt46578_anonObject.kt")
public void testKt46578_anonObject() throws Exception {
runTest("compiler/testData/codegen/box/syntheticAccessors/kt46578/kt46578_anonObject.kt");
}
@Test
@TestMetadata("kt46578_delegated.kt")
public void testKt46578_delegated() throws Exception {
runTest("compiler/testData/codegen/box/syntheticAccessors/kt46578/kt46578_delegated.kt");
}
@Test
@TestMetadata("kt46578_kotlin_delegated.kt")
public void testKt46578_kotlin_delegated() throws Exception {
runTest("compiler/testData/codegen/box/syntheticAccessors/kt46578/kt46578_kotlin_delegated.kt");
}
@Test
@TestMetadata("kt46578_kotlin_propertyRef.kt")
public void testKt46578_kotlin_propertyRef() throws Exception {
runTest("compiler/testData/codegen/box/syntheticAccessors/kt46578/kt46578_kotlin_propertyRef.kt");
}
@Test
@TestMetadata("kt46578_lambda.kt")
public void testKt46578_lambda() throws Exception {
runTest("compiler/testData/codegen/box/syntheticAccessors/kt46578/kt46578_lambda.kt");
}
@Test
@TestMetadata("kt46578_propertyRef.kt")
public void testKt46578_propertyRef() throws Exception {
runTest("compiler/testData/codegen/box/syntheticAccessors/kt46578/kt46578_propertyRef.kt");
}
}
}
@Nested
@@ -32275,6 +32275,49 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes
public void testSyntheticAccessorNames() throws Exception {
runTest("compiler/testData/codegen/box/syntheticAccessors/syntheticAccessorNames.kt");
}
@TestMetadata("compiler/testData/codegen/box/syntheticAccessors/kt46578")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class Kt46578 extends AbstractLightAnalysisModeTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doTest, TargetBackend.JVM, testDataFilePath);
}
public void testAllFilesPresentInKt46578() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/syntheticAccessors/kt46578"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM, true);
}
@TestMetadata("kt46578_anonObject.kt")
public void testKt46578_anonObject() throws Exception {
runTest("compiler/testData/codegen/box/syntheticAccessors/kt46578/kt46578_anonObject.kt");
}
@TestMetadata("kt46578_delegated.kt")
public void testKt46578_delegated() throws Exception {
runTest("compiler/testData/codegen/box/syntheticAccessors/kt46578/kt46578_delegated.kt");
}
@TestMetadata("kt46578_kotlin_delegated.kt")
public void testKt46578_kotlin_delegated() throws Exception {
runTest("compiler/testData/codegen/box/syntheticAccessors/kt46578/kt46578_kotlin_delegated.kt");
}
@TestMetadata("kt46578_kotlin_propertyRef.kt")
public void testKt46578_kotlin_propertyRef() throws Exception {
runTest("compiler/testData/codegen/box/syntheticAccessors/kt46578/kt46578_kotlin_propertyRef.kt");
}
@TestMetadata("kt46578_lambda.kt")
public void testKt46578_lambda() throws Exception {
runTest("compiler/testData/codegen/box/syntheticAccessors/kt46578/kt46578_lambda.kt");
}
@TestMetadata("kt46578_propertyRef.kt")
public void testKt46578_propertyRef() throws Exception {
runTest("compiler/testData/codegen/box/syntheticAccessors/kt46578/kt46578_propertyRef.kt");
}
}
}
@TestMetadata("compiler/testData/codegen/box/syntheticExtensions")
@@ -27115,6 +27115,19 @@ public class IrJsCodegenBoxES6TestGenerated extends AbstractIrJsCodegenBoxES6Tes
public void testSuperCallFromMultipleSubclasses() throws Exception {
runTest("compiler/testData/codegen/box/syntheticAccessors/superCallFromMultipleSubclasses.kt");
}
@TestMetadata("compiler/testData/codegen/box/syntheticAccessors/kt46578")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class Kt46578 extends AbstractIrJsCodegenBoxES6Test {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest0(this::doTest, TargetBackend.JS_IR_ES6, testDataFilePath);
}
public void testAllFilesPresentInKt46578() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/syntheticAccessors/kt46578"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JS_IR_ES6, true);
}
}
}
@TestMetadata("compiler/testData/codegen/box/syntheticExtensions")
@@ -26521,6 +26521,19 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest {
public void testSuperCallFromMultipleSubclasses() throws Exception {
runTest("compiler/testData/codegen/box/syntheticAccessors/superCallFromMultipleSubclasses.kt");
}
@TestMetadata("compiler/testData/codegen/box/syntheticAccessors/kt46578")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class Kt46578 extends AbstractIrJsCodegenBoxTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest0(this::doTest, TargetBackend.JS_IR, testDataFilePath);
}
public void testAllFilesPresentInKt46578() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/syntheticAccessors/kt46578"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JS_IR, true);
}
}
}
@TestMetadata("compiler/testData/codegen/box/syntheticExtensions")
@@ -26481,6 +26481,19 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest {
public void testSuperCallFromMultipleSubclasses() throws Exception {
runTest("compiler/testData/codegen/box/syntheticAccessors/superCallFromMultipleSubclasses.kt");
}
@TestMetadata("compiler/testData/codegen/box/syntheticAccessors/kt46578")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class Kt46578 extends AbstractJsCodegenBoxTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest0(this::doTest, TargetBackend.JS, testDataFilePath);
}
public void testAllFilesPresentInKt46578() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/syntheticAccessors/kt46578"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JS, true);
}
}
}
@TestMetadata("compiler/testData/codegen/box/syntheticExtensions")
@@ -14777,6 +14777,19 @@ public class IrCodegenBoxWasmTestGenerated extends AbstractIrCodegenBoxWasmTest
public void testSuperCallFromMultipleSubclasses() throws Exception {
runTest("compiler/testData/codegen/box/syntheticAccessors/superCallFromMultipleSubclasses.kt");
}
@TestMetadata("compiler/testData/codegen/box/syntheticAccessors/kt46578")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class Kt46578 extends AbstractIrCodegenBoxWasmTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest0(this::doTest, TargetBackend.WASM, testDataFilePath);
}
public void testAllFilesPresentInKt46578() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/syntheticAccessors/kt46578"), Pattern.compile("^([^_](.+))\\.kt$"), null, TargetBackend.WASM, true);
}
}
}
@TestMetadata("compiler/testData/codegen/box/syntheticExtensions")