diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/context/CodegenContext.java b/compiler/backend/src/org/jetbrains/kotlin/codegen/context/CodegenContext.java index 97eca2a117f..e07b4a79778 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/context/CodegenContext.java +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/context/CodegenContext.java @@ -511,20 +511,21 @@ public abstract class CodegenContext { @Nullable ClassDescriptor superCallTarget ) { DeclarationDescriptor enclosing = descriptor.getContainingDeclaration(); - if (!isInlineMethodContext() && ( + boolean isInliningContext = isInlineMethodContext(); + if (!isInliningContext && ( !hasThisDescriptor() || enclosing == getThisDescriptor() || enclosing == getClassOrPackageParentContext().getContextDescriptor())) { return descriptor; } - return accessibleDescriptorIfNeeded(descriptor, superCallTarget); + return accessibleDescriptorIfNeeded(descriptor, superCallTarget, isInliningContext); } public void recordSyntheticAccessorIfNeeded(@NotNull CallableMemberDescriptor descriptor, @NotNull BindingContext bindingContext) { if (hasThisDescriptor() && Boolean.TRUE.equals(bindingContext.get(NEED_SYNTHETIC_ACCESSOR, descriptor))) { // Not a super call because neither constructors nor private members can be targets of super calls - accessibleDescriptorIfNeeded(descriptor, /* superCallTarget = */ null); + accessibleDescriptorIfNeeded(descriptor, /* superCallTarget = */ null, false); } } @@ -532,7 +533,8 @@ public abstract class CodegenContext { @NotNull private D accessibleDescriptorIfNeeded( @NotNull D descriptor, - @Nullable ClassDescriptor superCallTarget + @Nullable ClassDescriptor superCallTarget, + boolean withinInliningContext ) { CallableMemberDescriptor unwrappedDescriptor = DescriptorUtils.unwrapFakeOverride(descriptor); @@ -553,10 +555,15 @@ public abstract class CodegenContext { superCallTarget = (ClassDescriptor) enclosed; } + if (descriptorContext == null && withinInliningContext && superCallTarget != null) { + //generate super callы within inline function through synthetic accessors + descriptorContext = ExpressionCodegen.getParentContextSubclassOf((ClassDescriptor) enclosed, this); + } + if (descriptorContext == null) { return descriptor; } - + boolean isSuperCallInsideInline = withinInliningContext && superCallTarget != null; if (descriptor instanceof PropertyDescriptor) { PropertyDescriptor propertyDescriptor = (PropertyDescriptor) descriptor; int propertyAccessFlag = getVisibilityAccessFlag(descriptor); @@ -564,12 +571,14 @@ public abstract class CodegenContext { PropertyGetterDescriptor getter = propertyDescriptor.getGetter(); int getterAccessFlag = getter == null ? propertyAccessFlag : propertyAccessFlag | getVisibilityAccessFlag(getter); - boolean getterAccessorRequired = isAccessorRequired(getterAccessFlag, unwrappedDescriptor, descriptorContext); + boolean getterAccessorRequired = isAccessorRequired(getterAccessFlag, unwrappedDescriptor, descriptorContext, + isSuperCallInsideInline); PropertySetterDescriptor setter = propertyDescriptor.getSetter(); int setterAccessFlag = setter == null ? propertyAccessFlag : propertyAccessFlag | getVisibilityAccessFlag(setter); - boolean setterAccessorRequired = isAccessorRequired(setterAccessFlag, unwrappedDescriptor, descriptorContext); + boolean setterAccessorRequired = isAccessorRequired(setterAccessFlag, unwrappedDescriptor, descriptorContext, + isSuperCallInsideInline); if (!getterAccessorRequired && !setterAccessorRequired) { return descriptor; @@ -578,7 +587,7 @@ public abstract class CodegenContext { } else { int flag = getVisibilityAccessFlag(unwrappedDescriptor); - if (!isAccessorRequired(flag, unwrappedDescriptor, descriptorContext)) { + if (!isAccessorRequired(flag, unwrappedDescriptor, descriptorContext, isSuperCallInsideInline)) { return descriptor; } return (D) descriptorContext.getAccessor(descriptor, superCallTarget); @@ -588,9 +597,10 @@ public abstract class CodegenContext { private static boolean isAccessorRequired( int accessFlag, @NotNull CallableMemberDescriptor unwrappedDescriptor, - @NotNull CodegenContext descriptorContext + @NotNull CodegenContext descriptorContext, + boolean isSuperCallInsideInline ) { - return (accessFlag & ACC_PRIVATE) != 0 || + return isSuperCallInsideInline || (accessFlag & ACC_PRIVATE) != 0 || ((accessFlag & ACC_PROTECTED) != 0 && !isInSamePackage(unwrappedDescriptor, descriptorContext.getContextDescriptor())); } diff --git a/compiler/testData/codegen/boxInline/modifiers/packagePrivateMembers.1.kt b/compiler/testData/codegen/boxInline/syntheticAccessors/packagePrivateMembers.1.kt similarity index 100% rename from compiler/testData/codegen/boxInline/modifiers/packagePrivateMembers.1.kt rename to compiler/testData/codegen/boxInline/syntheticAccessors/packagePrivateMembers.1.kt diff --git a/compiler/testData/codegen/boxInline/modifiers/packagePrivateMembers.2.kt b/compiler/testData/codegen/boxInline/syntheticAccessors/packagePrivateMembers.2.kt similarity index 68% rename from compiler/testData/codegen/boxInline/modifiers/packagePrivateMembers.2.kt rename to compiler/testData/codegen/boxInline/syntheticAccessors/packagePrivateMembers.2.kt index c8c03c723ac..fb63bc65db0 100644 --- a/compiler/testData/codegen/boxInline/modifiers/packagePrivateMembers.2.kt +++ b/compiler/testData/codegen/boxInline/syntheticAccessors/packagePrivateMembers.2.kt @@ -1,9 +1,8 @@ package test -/*TODO rollback when supported*/ -/*private*/ val packageProp = "O" +private val packageProp = "O" -/*private*/ fun packageFun() = "K" +private fun packageFun() = "K" internal inline fun packageInline(p: (String, String) -> String): String { return p(packageProp, packageFun()) diff --git a/compiler/testData/codegen/boxInline/modifiers/propertyModifiers.1.kt b/compiler/testData/codegen/boxInline/syntheticAccessors/propertyModifiers.1.kt similarity index 100% rename from compiler/testData/codegen/boxInline/modifiers/propertyModifiers.1.kt rename to compiler/testData/codegen/boxInline/syntheticAccessors/propertyModifiers.1.kt diff --git a/compiler/testData/codegen/boxInline/modifiers/propertyModifiers.2.kt b/compiler/testData/codegen/boxInline/syntheticAccessors/propertyModifiers.2.kt similarity index 100% rename from compiler/testData/codegen/boxInline/modifiers/propertyModifiers.2.kt rename to compiler/testData/codegen/boxInline/syntheticAccessors/propertyModifiers.2.kt diff --git a/compiler/testData/codegen/boxInline/syntheticAccessors/superCall.1.kt b/compiler/testData/codegen/boxInline/syntheticAccessors/superCall.1.kt new file mode 100644 index 00000000000..44796bf6352 --- /dev/null +++ b/compiler/testData/codegen/boxInline/syntheticAccessors/superCall.1.kt @@ -0,0 +1,5 @@ +import test.* + +fun box(): String { + return X.doTest() +} \ No newline at end of file diff --git a/compiler/testData/codegen/boxInline/syntheticAccessors/superCall.2.kt b/compiler/testData/codegen/boxInline/syntheticAccessors/superCall.2.kt new file mode 100644 index 00000000000..2328e976ce4 --- /dev/null +++ b/compiler/testData/codegen/boxInline/syntheticAccessors/superCall.2.kt @@ -0,0 +1,15 @@ +package test + +open class A { + open fun test() = "OK" +} + +object X : A() { + override fun test(): String { + return "fail" + } + + inline fun doTest(): String { + return super.test() + } +} diff --git a/compiler/testData/codegen/boxInline/syntheticAccessors/superProperty.1.kt b/compiler/testData/codegen/boxInline/syntheticAccessors/superProperty.1.kt new file mode 100644 index 00000000000..44796bf6352 --- /dev/null +++ b/compiler/testData/codegen/boxInline/syntheticAccessors/superProperty.1.kt @@ -0,0 +1,5 @@ +import test.* + +fun box(): String { + return X.doTest() +} \ No newline at end of file diff --git a/compiler/testData/codegen/boxInline/syntheticAccessors/superProperty.2.kt b/compiler/testData/codegen/boxInline/syntheticAccessors/superProperty.2.kt new file mode 100644 index 00000000000..8a3a94c67cf --- /dev/null +++ b/compiler/testData/codegen/boxInline/syntheticAccessors/superProperty.2.kt @@ -0,0 +1,14 @@ +package test + +open class A { + open val test = "OK" +} + +object X : A() { + override val test: String + get() = "fail" + + inline fun doTest(): String { + return super.test + } +} diff --git a/compiler/testData/codegen/boxWithJava/statics/protectedStaticAndInline/First.java b/compiler/testData/codegen/boxWithJava/statics/protectedStaticAndInline/First.java new file mode 100644 index 00000000000..1c54e9f5ae6 --- /dev/null +++ b/compiler/testData/codegen/boxWithJava/statics/protectedStaticAndInline/First.java @@ -0,0 +1,7 @@ +public abstract class First { + protected static String TEST = "O"; + + protected static String test() { + return "K"; + } +} \ No newline at end of file diff --git a/compiler/testData/codegen/boxWithJava/statics/protectedStaticAndInline/Kotlin.kt b/compiler/testData/codegen/boxWithJava/statics/protectedStaticAndInline/Kotlin.kt new file mode 100644 index 00000000000..30ef19c065b --- /dev/null +++ b/compiler/testData/codegen/boxWithJava/statics/protectedStaticAndInline/Kotlin.kt @@ -0,0 +1,14 @@ +package anotherPackage + +import First + +class Test : First() { + + inline fun doTest(): String { + return TEST + test() + } +} + +fun box(): String { + return Test().doTest() +} \ No newline at end of file diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/generated/BlackBoxInlineCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/generated/BlackBoxInlineCodegenTestGenerated.java index 653e497eb95..4be71e10e98 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/generated/BlackBoxInlineCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/generated/BlackBoxInlineCodegenTestGenerated.java @@ -500,27 +500,6 @@ public class BlackBoxInlineCodegenTestGenerated extends AbstractBlackBoxInlineCo } } - @TestMetadata("compiler/testData/codegen/boxInline/modifiers") - @TestDataPath("$PROJECT_ROOT") - @RunWith(JUnit3RunnerWithInners.class) - public static class Modifiers extends AbstractBlackBoxInlineCodegenTest { - public void testAllFilesPresentInModifiers() throws Exception { - KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/boxInline/modifiers"), Pattern.compile("^(.+)\\.1.kt$"), true); - } - - @TestMetadata("packagePrivateMembers.1.kt") - public void testPackagePrivateMembers() throws Exception { - String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/modifiers/packagePrivateMembers.1.kt"); - doTestMultiFileWithInlineCheck(fileName); - } - - @TestMetadata("propertyModifiers.1.kt") - public void testPropertyModifiers() throws Exception { - String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/modifiers/propertyModifiers.1.kt"); - doTestMultiFileWithInlineCheck(fileName); - } - } - @TestMetadata("compiler/testData/codegen/boxInline/multifileClasses") @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class) @@ -1382,6 +1361,39 @@ public class BlackBoxInlineCodegenTestGenerated extends AbstractBlackBoxInlineCo } } + @TestMetadata("compiler/testData/codegen/boxInline/syntheticAccessors") + @TestDataPath("$PROJECT_ROOT") + @RunWith(JUnit3RunnerWithInners.class) + public static class SyntheticAccessors extends AbstractBlackBoxInlineCodegenTest { + public void testAllFilesPresentInSyntheticAccessors() throws Exception { + KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/boxInline/syntheticAccessors"), Pattern.compile("^(.+)\\.1.kt$"), true); + } + + @TestMetadata("packagePrivateMembers.1.kt") + public void testPackagePrivateMembers() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/syntheticAccessors/packagePrivateMembers.1.kt"); + doTestMultiFileWithInlineCheck(fileName); + } + + @TestMetadata("propertyModifiers.1.kt") + public void testPropertyModifiers() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/syntheticAccessors/propertyModifiers.1.kt"); + doTestMultiFileWithInlineCheck(fileName); + } + + @TestMetadata("superCall.1.kt") + public void testSuperCall() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/syntheticAccessors/superCall.1.kt"); + doTestMultiFileWithInlineCheck(fileName); + } + + @TestMetadata("superProperty.1.kt") + public void testSuperProperty() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/syntheticAccessors/superProperty.1.kt"); + doTestMultiFileWithInlineCheck(fileName); + } + } + @TestMetadata("compiler/testData/codegen/boxInline/trait") @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class) diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/generated/BlackBoxWithJavaCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/generated/BlackBoxWithJavaCodegenTestGenerated.java index 7a573832f0e..caf32f4fb89 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/generated/BlackBoxWithJavaCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/generated/BlackBoxWithJavaCodegenTestGenerated.java @@ -709,5 +709,11 @@ public class BlackBoxWithJavaCodegenTestGenerated extends AbstractBlackBoxCodege doTestWithJava(fileName); } + @TestMetadata("protectedStaticAndInline") + public void testProtectedStaticAndInline() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/boxWithJava/statics/protectedStaticAndInline/"); + doTestWithJava(fileName); + } + } } diff --git a/compiler/tests/org/jetbrains/kotlin/jvm/compiler/CompileKotlinAgainstInlineKotlinTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/jvm/compiler/CompileKotlinAgainstInlineKotlinTestGenerated.java index 24d99b97e6f..682aa6b23fe 100644 --- a/compiler/tests/org/jetbrains/kotlin/jvm/compiler/CompileKotlinAgainstInlineKotlinTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/jvm/compiler/CompileKotlinAgainstInlineKotlinTestGenerated.java @@ -500,27 +500,6 @@ public class CompileKotlinAgainstInlineKotlinTestGenerated extends AbstractCompi } } - @TestMetadata("compiler/testData/codegen/boxInline/modifiers") - @TestDataPath("$PROJECT_ROOT") - @RunWith(JUnit3RunnerWithInners.class) - public static class Modifiers extends AbstractCompileKotlinAgainstInlineKotlinTest { - public void testAllFilesPresentInModifiers() throws Exception { - KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/boxInline/modifiers"), Pattern.compile("^(.+)\\.1.kt$"), true); - } - - @TestMetadata("packagePrivateMembers.1.kt") - public void testPackagePrivateMembers() throws Exception { - String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/modifiers/packagePrivateMembers.1.kt"); - doBoxTestWithInlineCheck(fileName); - } - - @TestMetadata("propertyModifiers.1.kt") - public void testPropertyModifiers() throws Exception { - String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/modifiers/propertyModifiers.1.kt"); - doBoxTestWithInlineCheck(fileName); - } - } - @TestMetadata("compiler/testData/codegen/boxInline/multifileClasses") @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class) @@ -1382,6 +1361,39 @@ public class CompileKotlinAgainstInlineKotlinTestGenerated extends AbstractCompi } } + @TestMetadata("compiler/testData/codegen/boxInline/syntheticAccessors") + @TestDataPath("$PROJECT_ROOT") + @RunWith(JUnit3RunnerWithInners.class) + public static class SyntheticAccessors extends AbstractCompileKotlinAgainstInlineKotlinTest { + public void testAllFilesPresentInSyntheticAccessors() throws Exception { + KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/boxInline/syntheticAccessors"), Pattern.compile("^(.+)\\.1.kt$"), true); + } + + @TestMetadata("packagePrivateMembers.1.kt") + public void testPackagePrivateMembers() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/syntheticAccessors/packagePrivateMembers.1.kt"); + doBoxTestWithInlineCheck(fileName); + } + + @TestMetadata("propertyModifiers.1.kt") + public void testPropertyModifiers() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/syntheticAccessors/propertyModifiers.1.kt"); + doBoxTestWithInlineCheck(fileName); + } + + @TestMetadata("superCall.1.kt") + public void testSuperCall() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/syntheticAccessors/superCall.1.kt"); + doBoxTestWithInlineCheck(fileName); + } + + @TestMetadata("superProperty.1.kt") + public void testSuperProperty() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/syntheticAccessors/superProperty.1.kt"); + doBoxTestWithInlineCheck(fileName); + } + } + @TestMetadata("compiler/testData/codegen/boxInline/trait") @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class)