From 8198ac77cb94311c82d507fe08a09109af249ca7 Mon Sep 17 00:00:00 2001 From: Michael Bogdanov Date: Thu, 8 Oct 2015 16:53:01 +0300 Subject: [PATCH] Generate private constructor for object #KT-9510 Fixed --- .../src/org/jetbrains/kotlin/codegen/AsmUtil.java | 2 +- .../kotlin/codegen/context/CodegenContext.java | 3 +-- .../asJava/lightClasses/object/SimpleObject.java | 4 ++-- .../codegen/box/privateConstructors/captured.kt | 10 ++++++++++ .../codegen/bytecodeText/staticFields/classObject.kt | 2 +- .../staticFields/classObjectSyntheticAccessor.kt | 2 +- .../writeFlags/function/constructors/objectInClass.kt | 2 +- .../writeFlags/function/constructors/topLevelObject.kt | 2 +- .../generated/BlackBoxCodegenTestGenerated.java | 6 ++++++ 9 files changed, 24 insertions(+), 9 deletions(-) create mode 100644 compiler/testData/codegen/box/privateConstructors/captured.kt diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/AsmUtil.java b/compiler/backend/src/org/jetbrains/kotlin/codegen/AsmUtil.java index 48d5666d73f..1d80a957d5a 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/AsmUtil.java +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/AsmUtil.java @@ -365,7 +365,7 @@ public class AsmUtil { // the following code is only for PRIVATE visibility of member if (memberDescriptor instanceof ConstructorDescriptor) { - if (isNonCompanionObject(containingDeclaration) || isEnumEntry(containingDeclaration)) { + if (isEnumEntry(containingDeclaration)) { return NO_FLAG_PACKAGE_PRIVATE; } if (isEnumClass(containingDeclaration)) { 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 229d89882a2..e466c663063 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/context/CodegenContext.java +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/context/CodegenContext.java @@ -395,8 +395,7 @@ public abstract class CodegenContext { } public void recordSyntheticAccessorIfNeeded(@NotNull CallableMemberDescriptor descriptor, @NotNull BindingContext bindingContext) { - if (hasThisDescriptor() && - (descriptor instanceof ConstructorDescriptor || Boolean.TRUE.equals(bindingContext.get(NEED_SYNTHETIC_ACCESSOR, descriptor)))) { + 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, /* superCallExpression = */ null); } diff --git a/compiler/testData/asJava/lightClasses/object/SimpleObject.java b/compiler/testData/asJava/lightClasses/object/SimpleObject.java index 3807297a403..2f906f6095c 100644 --- a/compiler/testData/asJava/lightClasses/object/SimpleObject.java +++ b/compiler/testData/asJava/lightClasses/object/SimpleObject.java @@ -17,5 +17,5 @@ public final class A { public final int f() { /* compiled code */ } - A() { /* compiled code */ } -} + private A() { /* compiled code */ } +} \ No newline at end of file diff --git a/compiler/testData/codegen/box/privateConstructors/captured.kt b/compiler/testData/codegen/box/privateConstructors/captured.kt new file mode 100644 index 00000000000..83b38054d77 --- /dev/null +++ b/compiler/testData/codegen/box/privateConstructors/captured.kt @@ -0,0 +1,10 @@ +public open class Outer private constructor(val s: String) { + + companion object { + fun test () = { Outer("OK") }() + } +} + +fun box(): String { + return Outer.test().s +} \ No newline at end of file diff --git a/compiler/testData/codegen/bytecodeText/staticFields/classObject.kt b/compiler/testData/codegen/bytecodeText/staticFields/classObject.kt index 2bcf84150fa..069eefa79f0 100644 --- a/compiler/testData/codegen/bytecodeText/staticFields/classObject.kt +++ b/compiler/testData/codegen/bytecodeText/staticFields/classObject.kt @@ -4,4 +4,4 @@ class A { } } // A and companion object constructor call -// 4 ALOAD 0 \ No newline at end of file +// 3 ALOAD 0 \ No newline at end of file diff --git a/compiler/testData/codegen/bytecodeText/staticFields/classObjectSyntheticAccessor.kt b/compiler/testData/codegen/bytecodeText/staticFields/classObjectSyntheticAccessor.kt index 369ed0472e2..8ef75cec9be 100644 --- a/compiler/testData/codegen/bytecodeText/staticFields/classObjectSyntheticAccessor.kt +++ b/compiler/testData/codegen/bytecodeText/staticFields/classObjectSyntheticAccessor.kt @@ -4,5 +4,5 @@ class A { } } // A and companion object constructor call -// 4 ALOAD 0 +// 3 ALOAD 0 // 1 synthetic access\$getR diff --git a/compiler/testData/writeFlags/function/constructors/objectInClass.kt b/compiler/testData/writeFlags/function/constructors/objectInClass.kt index bf61f402f18..1590b53c70b 100644 --- a/compiler/testData/writeFlags/function/constructors/objectInClass.kt +++ b/compiler/testData/writeFlags/function/constructors/objectInClass.kt @@ -5,4 +5,4 @@ class Foo { // TESTED_OBJECT_KIND: function // TESTED_OBJECTS: Foo$Test, -// FLAGS: \ No newline at end of file +// FLAGS: ACC_PRIVATE \ No newline at end of file diff --git a/compiler/testData/writeFlags/function/constructors/topLevelObject.kt b/compiler/testData/writeFlags/function/constructors/topLevelObject.kt index 65bc75b1560..70895a8bde9 100644 --- a/compiler/testData/writeFlags/function/constructors/topLevelObject.kt +++ b/compiler/testData/writeFlags/function/constructors/topLevelObject.kt @@ -4,4 +4,4 @@ object Foo { // TESTED_OBJECT_KIND: function // TESTED_OBJECTS: Foo, -// FLAGS: \ No newline at end of file +// FLAGS: ACC_PRIVATE \ No newline at end of file diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/generated/BlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/generated/BlackBoxCodegenTestGenerated.java index 930065871f6..dcecb8902a4 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/generated/BlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/generated/BlackBoxCodegenTestGenerated.java @@ -6415,6 +6415,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { doTest(fileName); } + @TestMetadata("captured.kt") + public void testCaptured() throws Exception { + String fileName = JetTestUtils.navigationMetadata("compiler/testData/codegen/box/privateConstructors/captured.kt"); + doTest(fileName); + } + @TestMetadata("companion.kt") public void testCompanion() throws Exception { String fileName = JetTestUtils.navigationMetadata("compiler/testData/codegen/box/privateConstructors/companion.kt");