From 3651ec9294a9fb15e1681a6bd2433e04e04dba54 Mon Sep 17 00:00:00 2001 From: Michael Bogdanov Date: Wed, 11 Nov 2015 11:54:15 +0300 Subject: [PATCH] Support access to protected members within inline functions --- .../codegen/context/CodegenContext.java | 18 ++++++++++-------- .../syntheticAccessors/protectedMembers.1.kt | 15 +++++++++++++++ .../syntheticAccessors/protectedMembers.2.kt | 16 ++++++++++++++++ .../protectedMembersFromSuper.1.kt | 15 +++++++++++++++ .../protectedMembersFromSuper.2.kt | 19 +++++++++++++++++++ .../BlackBoxInlineCodegenTestGenerated.java | 12 ++++++++++++ ...otlinAgainstInlineKotlinTestGenerated.java | 12 ++++++++++++ 7 files changed, 99 insertions(+), 8 deletions(-) create mode 100644 compiler/testData/codegen/boxInline/syntheticAccessors/protectedMembers.1.kt create mode 100644 compiler/testData/codegen/boxInline/syntheticAccessors/protectedMembers.2.kt create mode 100644 compiler/testData/codegen/boxInline/syntheticAccessors/protectedMembersFromSuper.1.kt create mode 100644 compiler/testData/codegen/boxInline/syntheticAccessors/protectedMembersFromSuper.2.kt 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 e07b4a79778..5070cb6ac9e 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/context/CodegenContext.java +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/context/CodegenContext.java @@ -556,14 +556,13 @@ public abstract class CodegenContext { } if (descriptorContext == null && withinInliningContext && superCallTarget != null) { - //generate super callы within inline function through synthetic accessors + //generate super calls 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); @@ -572,13 +571,13 @@ public abstract class CodegenContext { int getterAccessFlag = getter == null ? propertyAccessFlag : propertyAccessFlag | getVisibilityAccessFlag(getter); boolean getterAccessorRequired = isAccessorRequired(getterAccessFlag, unwrappedDescriptor, descriptorContext, - isSuperCallInsideInline); + withinInliningContext, superCallTarget != null); PropertySetterDescriptor setter = propertyDescriptor.getSetter(); int setterAccessFlag = setter == null ? propertyAccessFlag : propertyAccessFlag | getVisibilityAccessFlag(setter); boolean setterAccessorRequired = isAccessorRequired(setterAccessFlag, unwrappedDescriptor, descriptorContext, - isSuperCallInsideInline); + withinInliningContext, superCallTarget != null); if (!getterAccessorRequired && !setterAccessorRequired) { return descriptor; @@ -587,7 +586,7 @@ public abstract class CodegenContext { } else { int flag = getVisibilityAccessFlag(unwrappedDescriptor); - if (!isAccessorRequired(flag, unwrappedDescriptor, descriptorContext, isSuperCallInsideInline)) { + if (!isAccessorRequired(flag, unwrappedDescriptor, descriptorContext, withinInliningContext, superCallTarget != null)) { return descriptor; } return (D) descriptorContext.getAccessor(descriptor, superCallTarget); @@ -598,10 +597,13 @@ public abstract class CodegenContext { int accessFlag, @NotNull CallableMemberDescriptor unwrappedDescriptor, @NotNull CodegenContext descriptorContext, - boolean isSuperCallInsideInline + boolean withinInline, + boolean isSuperCall ) { - return isSuperCallInsideInline || (accessFlag & ACC_PRIVATE) != 0 || - ((accessFlag & ACC_PROTECTED) != 0 && !isInSamePackage(unwrappedDescriptor, descriptorContext.getContextDescriptor())); + return isSuperCall && withinInline || + (accessFlag & ACC_PRIVATE) != 0 || + ((accessFlag & ACC_PROTECTED) != 0 && + (withinInline || !isInSamePackage(unwrappedDescriptor, descriptorContext.getContextDescriptor()))); } private static boolean isInSamePackage(DeclarationDescriptor descriptor1, DeclarationDescriptor descriptor2) { diff --git a/compiler/testData/codegen/boxInline/syntheticAccessors/protectedMembers.1.kt b/compiler/testData/codegen/boxInline/syntheticAccessors/protectedMembers.1.kt new file mode 100644 index 00000000000..796d276d2c6 --- /dev/null +++ b/compiler/testData/codegen/boxInline/syntheticAccessors/protectedMembers.1.kt @@ -0,0 +1,15 @@ +import test.* + +class A: P() { + override val FOO: String + get() = "fail" + + override fun test(): String { + return "fail" + } +} + +fun box() : String { + val p = P() + return p.protectedProp() + p.protectedFun() +} \ No newline at end of file diff --git a/compiler/testData/codegen/boxInline/syntheticAccessors/protectedMembers.2.kt b/compiler/testData/codegen/boxInline/syntheticAccessors/protectedMembers.2.kt new file mode 100644 index 00000000000..d446994d72f --- /dev/null +++ b/compiler/testData/codegen/boxInline/syntheticAccessors/protectedMembers.2.kt @@ -0,0 +1,16 @@ +package test + +open class P { + protected open val FOO = "O" + + protected open fun test() = "K" + + inline fun protectedProp(): String { + return FOO + } + + inline fun protectedFun(): String { + return test() + } +} + diff --git a/compiler/testData/codegen/boxInline/syntheticAccessors/protectedMembersFromSuper.1.kt b/compiler/testData/codegen/boxInline/syntheticAccessors/protectedMembersFromSuper.1.kt new file mode 100644 index 00000000000..796d276d2c6 --- /dev/null +++ b/compiler/testData/codegen/boxInline/syntheticAccessors/protectedMembersFromSuper.1.kt @@ -0,0 +1,15 @@ +import test.* + +class A: P() { + override val FOO: String + get() = "fail" + + override fun test(): String { + return "fail" + } +} + +fun box() : String { + val p = P() + return p.protectedProp() + p.protectedFun() +} \ No newline at end of file diff --git a/compiler/testData/codegen/boxInline/syntheticAccessors/protectedMembersFromSuper.2.kt b/compiler/testData/codegen/boxInline/syntheticAccessors/protectedMembersFromSuper.2.kt new file mode 100644 index 00000000000..31d5b865209 --- /dev/null +++ b/compiler/testData/codegen/boxInline/syntheticAccessors/protectedMembersFromSuper.2.kt @@ -0,0 +1,19 @@ +package test + +open class Base { + protected open val FOO = "O" + + protected open fun test() = "K" +} + +open class P : Base() { + + inline fun protectedProp(): String { + return FOO + } + + inline fun protectedFun(): String { + return test() + } +} + diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/generated/BlackBoxInlineCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/generated/BlackBoxInlineCodegenTestGenerated.java index 4be71e10e98..ab200cec52a 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/generated/BlackBoxInlineCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/generated/BlackBoxInlineCodegenTestGenerated.java @@ -1381,6 +1381,18 @@ public class BlackBoxInlineCodegenTestGenerated extends AbstractBlackBoxInlineCo doTestMultiFileWithInlineCheck(fileName); } + @TestMetadata("protectedMembers.1.kt") + public void testProtectedMembers() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/syntheticAccessors/protectedMembers.1.kt"); + doTestMultiFileWithInlineCheck(fileName); + } + + @TestMetadata("protectedMembersFromSuper.1.kt") + public void testProtectedMembersFromSuper() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/syntheticAccessors/protectedMembersFromSuper.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"); diff --git a/compiler/tests/org/jetbrains/kotlin/jvm/compiler/CompileKotlinAgainstInlineKotlinTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/jvm/compiler/CompileKotlinAgainstInlineKotlinTestGenerated.java index 682aa6b23fe..3e327b64420 100644 --- a/compiler/tests/org/jetbrains/kotlin/jvm/compiler/CompileKotlinAgainstInlineKotlinTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/jvm/compiler/CompileKotlinAgainstInlineKotlinTestGenerated.java @@ -1381,6 +1381,18 @@ public class CompileKotlinAgainstInlineKotlinTestGenerated extends AbstractCompi doBoxTestWithInlineCheck(fileName); } + @TestMetadata("protectedMembers.1.kt") + public void testProtectedMembers() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/syntheticAccessors/protectedMembers.1.kt"); + doBoxTestWithInlineCheck(fileName); + } + + @TestMetadata("protectedMembersFromSuper.1.kt") + public void testProtectedMembersFromSuper() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/syntheticAccessors/protectedMembersFromSuper.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");