Allow to import private members if they are in the same file

#KT-7724 Fixed
This commit is contained in:
Mikhail Zarechenskiy
2017-04-04 03:45:27 +03:00
parent f518e8ebb8
commit 9fdd5fe5e8
14 changed files with 380 additions and 11 deletions
@@ -643,7 +643,7 @@ internal fun isVisible(
val visibility = descriptor.visibility
if (position == QualifierPosition.IMPORT) {
if (Visibilities.isPrivate(visibility)) return false
if (Visibilities.isPrivate(visibility)) return Visibilities.inSameFile(descriptor, shouldBeVisibleFrom)
if (!visibility.mustCheckInImports()) return true
}
return Visibilities.isVisibleIgnoringReceiver(descriptor, shouldBeVisibleFrom)
@@ -0,0 +1,40 @@
package test
import test.C.E1
import test.A.B.*
import test.Obj.CInObj.Tt
import test.Obj.foo
private enum class C {
E1
}
class A {
private class B {
object C
class D
}
fun test() {
C
D()
}
}
private object Obj {
private class CInObj {
class Tt
}
fun foo() {
Tt()
}
}
fun box(): String {
E1
A().test()
foo()
return "OK"
}
@@ -0,0 +1,37 @@
package test
import test.TopLevelClass.NestedClass
import test.TopLevelClass.NestedClass.InNested
import test.TopLevelEnum.NestedEnum.NestedEntry
import test.TopLevelObject.inObject
import test.TopLevelEnum.E1
private class TopLevelClass {
private class NestedClass {
class InNested
}
fun test() {
InNested()
}
}
private enum class TopLevelEnum(private val e: NestedEnum) {
E1(NestedEntry);
private enum class NestedEnum {
NestedEntry;
}
}
private object TopLevelObject {
fun inObject() {}
}
fun testAccess() {
<!INVISIBLE_MEMBER!>NestedClass<!>()
E1
<!INVISIBLE_MEMBER!>InNested<!>()
<!INVISIBLE_MEMBER!>NestedEntry<!>
inObject()
}
@@ -0,0 +1,74 @@
package
package test {
public fun testAccess(): kotlin.Unit
private final class TopLevelClass {
public constructor TopLevelClass()
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public final fun test(): kotlin.Unit
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
private final class NestedClass {
public constructor NestedClass()
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
public final class InNested {
public constructor InNested()
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
}
}
private final enum class TopLevelEnum : kotlin.Enum<test.TopLevelEnum> {
enum entry E1
private constructor TopLevelEnum(/*0*/ e: test.TopLevelEnum.NestedEnum)
private final val e: test.TopLevelEnum.NestedEnum
public final override /*1*/ /*fake_override*/ val name: kotlin.String
public final override /*1*/ /*fake_override*/ val ordinal: kotlin.Int
protected final override /*1*/ /*fake_override*/ fun clone(): kotlin.Any
public final override /*1*/ /*fake_override*/ fun compareTo(/*0*/ other: test.TopLevelEnum): kotlin.Int
public final override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
protected/*protected and package*/ final override /*1*/ /*fake_override*/ /*isHiddenForResolutionEverywhereBesideSupercalls*/ fun finalize(): kotlin.Unit
public final override /*1*/ /*fake_override*/ /*isHiddenForResolutionEverywhereBesideSupercalls*/ fun getDeclaringClass(): java.lang.Class<test.TopLevelEnum!>!
public final override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
private final enum class NestedEnum : kotlin.Enum<test.TopLevelEnum.NestedEnum> {
enum entry NestedEntry
private constructor NestedEnum()
public final override /*1*/ /*fake_override*/ val name: kotlin.String
public final override /*1*/ /*fake_override*/ val ordinal: kotlin.Int
protected final override /*1*/ /*fake_override*/ fun clone(): kotlin.Any
public final override /*1*/ /*fake_override*/ fun compareTo(/*0*/ other: test.TopLevelEnum.NestedEnum): kotlin.Int
public final override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
protected/*protected and package*/ final override /*1*/ /*fake_override*/ /*isHiddenForResolutionEverywhereBesideSupercalls*/ fun finalize(): kotlin.Unit
public final override /*1*/ /*fake_override*/ /*isHiddenForResolutionEverywhereBesideSupercalls*/ fun getDeclaringClass(): java.lang.Class<test.TopLevelEnum.NestedEnum!>!
public final override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
// Static members
public final /*synthesized*/ fun valueOf(/*0*/ value: kotlin.String): test.TopLevelEnum.NestedEnum
public final /*synthesized*/ fun values(): kotlin.Array<test.TopLevelEnum.NestedEnum>
}
// Static members
public final /*synthesized*/ fun valueOf(/*0*/ value: kotlin.String): test.TopLevelEnum
public final /*synthesized*/ fun values(): kotlin.Array<test.TopLevelEnum>
}
private object TopLevelObject {
private constructor TopLevelObject()
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public final fun inObject(): kotlin.Unit
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
}
@@ -0,0 +1,26 @@
// FILE: A.kt
class A {
private class Nested {
object O1
}
}
// FILE: B.java
public class B {
private static class JC {
public static class JC1 {
}
}
}
// FILE: C.kt
import A.<!INVISIBLE_REFERENCE!>Nested<!>.*
import B.<!INVISIBLE_REFERENCE!>JC<!>.JC1
fun test() {
<!INVISIBLE_MEMBER!>O1<!>
<!INACCESSIBLE_TYPE!><!INVISIBLE_MEMBER!>JC1<!>()<!>
}
@@ -0,0 +1,45 @@
package
public fun test(): kotlin.Unit
public final class A {
public constructor A()
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
private final class Nested {
public constructor Nested()
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
public object O1 {
private constructor O1()
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
}
}
public open class B {
public constructor B()
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
private open class JC {
private constructor JC()
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
public open class JC1 {
public constructor JC1()
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
}
}
@@ -0,0 +1,32 @@
package test
import test.TopLevelClass.NestedClass.*
import test.TopLevelEnum.NestedEnum.*
import test.TopLevelEnum.*
private class TopLevelClass {
private class NestedClass {
class A1
object A2
}
fun test() {
A1()
A2
}
}
private enum class TopLevelEnum(private val e: NestedEnum) {
E1(NestedEntry);
private enum class NestedEnum {
NestedEntry;
}
}
fun testAccess() {
E1
<!INVISIBLE_MEMBER!>NestedEntry<!>
<!INVISIBLE_MEMBER!>A1<!>()
<!INVISIBLE_MEMBER!>A2<!>
}
@@ -0,0 +1,73 @@
package
package test {
public fun testAccess(): kotlin.Unit
private final class TopLevelClass {
public constructor TopLevelClass()
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public final fun test(): kotlin.Unit
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
private final class NestedClass {
public constructor NestedClass()
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
public final class A1 {
public constructor A1()
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
public object A2 {
private constructor A2()
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
}
}
private final enum class TopLevelEnum : kotlin.Enum<test.TopLevelEnum> {
enum entry E1
private constructor TopLevelEnum(/*0*/ e: test.TopLevelEnum.NestedEnum)
private final val e: test.TopLevelEnum.NestedEnum
public final override /*1*/ /*fake_override*/ val name: kotlin.String
public final override /*1*/ /*fake_override*/ val ordinal: kotlin.Int
protected final override /*1*/ /*fake_override*/ fun clone(): kotlin.Any
public final override /*1*/ /*fake_override*/ fun compareTo(/*0*/ other: test.TopLevelEnum): kotlin.Int
public final override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
protected/*protected and package*/ final override /*1*/ /*fake_override*/ /*isHiddenForResolutionEverywhereBesideSupercalls*/ fun finalize(): kotlin.Unit
public final override /*1*/ /*fake_override*/ /*isHiddenForResolutionEverywhereBesideSupercalls*/ fun getDeclaringClass(): java.lang.Class<test.TopLevelEnum!>!
public final override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
private final enum class NestedEnum : kotlin.Enum<test.TopLevelEnum.NestedEnum> {
enum entry NestedEntry
private constructor NestedEnum()
public final override /*1*/ /*fake_override*/ val name: kotlin.String
public final override /*1*/ /*fake_override*/ val ordinal: kotlin.Int
protected final override /*1*/ /*fake_override*/ fun clone(): kotlin.Any
public final override /*1*/ /*fake_override*/ fun compareTo(/*0*/ other: test.TopLevelEnum.NestedEnum): kotlin.Int
public final override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
protected/*protected and package*/ final override /*1*/ /*fake_override*/ /*isHiddenForResolutionEverywhereBesideSupercalls*/ fun finalize(): kotlin.Unit
public final override /*1*/ /*fake_override*/ /*isHiddenForResolutionEverywhereBesideSupercalls*/ fun getDeclaringClass(): java.lang.Class<test.TopLevelEnum.NestedEnum!>!
public final override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
// Static members
public final /*synthesized*/ fun valueOf(/*0*/ value: kotlin.String): test.TopLevelEnum.NestedEnum
public final /*synthesized*/ fun values(): kotlin.Array<test.TopLevelEnum.NestedEnum>
}
// Static members
public final /*synthesized*/ fun valueOf(/*0*/ value: kotlin.String): test.TopLevelEnum
public final /*synthesized*/ fun values(): kotlin.Array<test.TopLevelEnum>
}
}
@@ -11651,6 +11651,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
doTest(fileName);
}
@TestMetadata("privateMembersInImportList.kt")
public void testPrivateMembersInImportList() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/package/privateMembersInImportList.kt");
doTest(fileName);
}
@TestMetadata("privateTopLevelPropAndVarInInner.kt")
public void testPrivateTopLevelPropAndVarInInner() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/package/privateTopLevelPropAndVarInInner.kt");
@@ -9487,6 +9487,24 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest {
doTest(fileName);
}
@TestMetadata("ImportPrivateMember.kt")
public void testImportPrivateMember() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/imports/ImportPrivateMember.kt");
doTest(fileName);
}
@TestMetadata("ImportPrivateMemberFromOtherFile.kt")
public void testImportPrivateMemberFromOtherFile() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/imports/ImportPrivateMemberFromOtherFile.kt");
doTest(fileName);
}
@TestMetadata("ImportPrivateMembersWithStar.kt")
public void testImportPrivateMembersWithStar() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/imports/ImportPrivateMembersWithStar.kt");
doTest(fileName);
}
@TestMetadata("ImportProtectedClass.kt")
public void testImportProtectedClass() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/imports/ImportProtectedClass.kt");
@@ -11651,6 +11651,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
doTest(fileName);
}
@TestMetadata("privateMembersInImportList.kt")
public void testPrivateMembersInImportList() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/package/privateMembersInImportList.kt");
doTest(fileName);
}
@TestMetadata("privateTopLevelPropAndVarInInner.kt")
public void testPrivateTopLevelPropAndVarInInner() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/package/privateTopLevelPropAndVarInInner.kt");
@@ -11651,6 +11651,12 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes
doTest(fileName);
}
@TestMetadata("privateMembersInImportList.kt")
public void testPrivateMembersInImportList() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/package/privateMembersInImportList.kt");
doTest(fileName);
}
@TestMetadata("privateTopLevelPropAndVarInInner.kt")
public void testPrivateTopLevelPropAndVarInInner() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/package/privateTopLevelPropAndVarInInner.kt");
@@ -39,16 +39,6 @@ public class Visibilities {
return true;
}
// Note that this method returns false if `from` declaration is `init` initializer
// because initializer does not have source element
private boolean inSameFile(@NotNull DeclarationDescriptor what, @NotNull DeclarationDescriptor from) {
SourceFile fromContainingFile = DescriptorUtils.getContainingSourceFile(from);
if (fromContainingFile != SourceFile.NO_SOURCE_FILE) {
return fromContainingFile.equals(DescriptorUtils.getContainingSourceFile(what));
}
return false;
}
private boolean hasContainingSourceFile(@NotNull DeclarationDescriptor descriptor) {
return DescriptorUtils.getContainingSourceFile(descriptor) != SourceFile.NO_SOURCE_FILE;
}
@@ -318,6 +308,16 @@ public class Visibilities {
return findInvisibleMember(IRRELEVANT_RECEIVER, what, from) == null;
}
// Note that this method returns false if `from` declaration is `init` initializer
// because initializer does not have source element
public static boolean inSameFile(@NotNull DeclarationDescriptor what, @NotNull DeclarationDescriptor from) {
SourceFile fromContainingFile = DescriptorUtils.getContainingSourceFile(from);
if (fromContainingFile != SourceFile.NO_SOURCE_FILE) {
return fromContainingFile.equals(DescriptorUtils.getContainingSourceFile(what));
}
return false;
}
@Nullable
public static DeclarationDescriptorWithVisibility findInvisibleMember(
@Nullable ReceiverValue receiver,
@@ -13123,6 +13123,12 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest {
doTest(fileName);
}
@TestMetadata("privateMembersInImportList.kt")
public void testPrivateMembersInImportList() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/package/privateMembersInImportList.kt");
doTest(fileName);
}
@TestMetadata("privateTopLevelPropAndVarInInner.kt")
public void testPrivateTopLevelPropAndVarInInner() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/package/privateTopLevelPropAndVarInInner.kt");