FE 1.0: introduce PrivateInFile effective visibility #KT-20094 Fixed

This commit is contained in:
Mikhail Glukhikh
2021-04-05 13:33:25 +03:00
parent addfa27980
commit 4bd44fa61b
16 changed files with 187 additions and 30 deletions

View File

@@ -9275,6 +9275,12 @@ public class FirOldFrontendDiagnosticsTestGenerated extends AbstractFirDiagnosti
runTest("compiler/testData/diagnostics/tests/exposed/privateFromLocal.kt");
}
@Test
@TestMetadata("privateInFile.kt")
public void testPrivateInFile() throws Exception {
runTest("compiler/testData/diagnostics/tests/exposed/privateInFile.kt");
}
@Test
@TestMetadata("privatePropertyInPrivateConstructor.kt")
public void testPrivatePropertyInPrivateConstructor() throws Exception {

View File

@@ -0,0 +1,28 @@
class Public {
private open class NestedPrivate
fun <!EXPOSED_FUNCTION_RETURN_TYPE!>test1<!>() = NestedPrivate()
fun test2(<!EXPOSED_PARAMETER_TYPE!>p: NestedPrivate<!>) {}
fun <!EXPOSED_RECEIVER_TYPE!>NestedPrivate<!>.test3() {}
val <!EXPOSED_PROPERTY_TYPE!>test4<!> = NestedPrivate()
class Test5 : <!EXPOSED_SUPER_CLASS!>NestedPrivate<!>()
}
private class PrivateInFileClass {
private open class NestedPrivate
fun test1() = NestedPrivate()
fun test2(p: NestedPrivate) {}
fun NestedPrivate.test3() {}
val test4 = NestedPrivate()
class Test5 : NestedPrivate()
}
private interface PrivateInFile {
private class Private
fun expose() = Private()
}
// Exposes 'PrivateInFile$Private' via 'expose'
class Derived : PrivateInFile

View File

@@ -0,0 +1,28 @@
class Public {
private open class NestedPrivate
fun <!EXPOSED_FUNCTION_RETURN_TYPE!>test1<!>() = NestedPrivate()
fun test2(<!EXPOSED_PARAMETER_TYPE!>p: NestedPrivate<!>) {}
fun <!EXPOSED_RECEIVER_TYPE!>NestedPrivate<!>.test3() {}
val <!EXPOSED_PROPERTY_TYPE!>test4<!> = NestedPrivate()
class Test5 : <!EXPOSED_SUPER_CLASS!>NestedPrivate()<!>
}
private class PrivateInFileClass {
private open class NestedPrivate
fun <!EXPOSED_FUNCTION_RETURN_TYPE!>test1<!>() = NestedPrivate()
fun test2(<!EXPOSED_PARAMETER_TYPE!>p: NestedPrivate<!>) {}
fun <!EXPOSED_RECEIVER_TYPE!>NestedPrivate<!>.test3() {}
val <!EXPOSED_PROPERTY_TYPE!>test4<!> = NestedPrivate()
class Test5 : <!EXPOSED_SUPER_CLASS!>NestedPrivate()<!>
}
private interface PrivateInFile {
private class Private
fun <!EXPOSED_FUNCTION_RETURN_TYPE!>expose<!>() = Private()
}
// Exposes 'PrivateInFile$Private' via 'expose'
class Derived : PrivateInFile

View File

@@ -0,0 +1,73 @@
package
public final class Derived : PrivateInFile {
public constructor Derived()
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun expose(): PrivateInFile.Private
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
private interface PrivateInFile {
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open fun expose(): PrivateInFile.Private
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
private final class Private {
public constructor Private()
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 PrivateInFileClass {
public constructor PrivateInFileClass()
public final val test4: PrivateInFileClass.NestedPrivate
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 test1(): PrivateInFileClass.NestedPrivate
public final fun test2(/*0*/ p: PrivateInFileClass.NestedPrivate): kotlin.Unit
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
public final fun PrivateInFileClass.NestedPrivate.test3(): kotlin.Unit
private open class NestedPrivate {
public constructor NestedPrivate()
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 Test5 : PrivateInFileClass.NestedPrivate {
public constructor Test5()
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 Public {
public constructor Public()
public final val test4: Public.NestedPrivate
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 test1(): Public.NestedPrivate
public final fun test2(/*0*/ p: Public.NestedPrivate): kotlin.Unit
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
public final fun Public.NestedPrivate.test3(): kotlin.Unit
private open class NestedPrivate {
public constructor NestedPrivate()
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 Test5 : Public.NestedPrivate {
public constructor Test5()
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
}
}

View File

@@ -11,4 +11,4 @@ package a
import pack1.SomeClass.*
private class X : <!FINAL_SUPERTYPE, INVISIBLE_MEMBER, INVISIBLE_REFERENCE!>N<!>()
private class X : <!EXPOSED_SUPER_CLASS!><!FINAL_SUPERTYPE, INVISIBLE_MEMBER, INVISIBLE_REFERENCE!>N<!>()<!>

View File

@@ -9281,6 +9281,12 @@ public class DiagnosticTestGenerated extends AbstractDiagnosticTest {
runTest("compiler/testData/diagnostics/tests/exposed/privateFromLocal.kt");
}
@Test
@TestMetadata("privateInFile.kt")
public void testPrivateInFile() throws Exception {
runTest("compiler/testData/diagnostics/tests/exposed/privateInFile.kt");
}
@Test
@TestMetadata("privatePropertyInPrivateConstructor.kt")
public void testPrivatePropertyInPrivateConstructor() throws Exception {

View File

@@ -24,6 +24,8 @@ sealed class EffectiveVisibility(val name: String, val publicApi: Boolean = fals
// \ / /InternalProtected(Derived)
// \InternalProtectedBound/
// |
// PrivateInFile
// |
// Private = Local
@@ -42,6 +44,17 @@ sealed class EffectiveVisibility(val name: String, val publicApi: Boolean = fals
override fun toVisibility(): Visibility = Visibilities.Local
}
object PrivateInFile : EffectiveVisibility("private-in-file", privateApi = true) {
override fun relation(other: EffectiveVisibility, typeCheckerContextProvider: TypeCheckerProviderContext): Permissiveness =
when (other) {
this -> Permissiveness.SAME
Private, Local -> Permissiveness.MORE
else -> Permissiveness.LESS
}
override fun toVisibility(): Visibility = Visibilities.Private
}
object Public : EffectiveVisibility("public", publicApi = true) {
override fun relation(other: EffectiveVisibility, typeCheckerContextProvider: TypeCheckerProviderContext): Permissiveness =
if (this == other) Permissiveness.SAME else Permissiveness.MORE
@@ -55,7 +68,7 @@ sealed class EffectiveVisibility(val name: String, val publicApi: Boolean = fals
override fun relation(other: EffectiveVisibility, typeCheckerContextProvider: TypeCheckerProviderContext): Permissiveness =
when (other) {
Public -> Permissiveness.LESS
Private, Local, InternalProtectedBound, is InternalProtected -> Permissiveness.MORE
Private, PrivateInFile, Local, InternalProtectedBound, is InternalProtected -> Permissiveness.MORE
is InternalOrPackage -> Permissiveness.SAME
ProtectedBound, is Protected -> Permissiveness.UNKNOWN
}
@@ -63,7 +76,7 @@ sealed class EffectiveVisibility(val name: String, val publicApi: Boolean = fals
override fun lowerBound(other: EffectiveVisibility, typeCheckerContextProvider: TypeCheckerProviderContext): EffectiveVisibility =
when (other) {
Public -> this
Private, Local, InternalProtectedBound, is InternalOrPackage, is InternalProtected -> other
Private, PrivateInFile, Local, InternalProtectedBound, is InternalOrPackage, is InternalProtected -> other
is Protected -> InternalProtected(other.containerTypeConstructor)
ProtectedBound -> InternalProtectedBound
}
@@ -88,7 +101,7 @@ sealed class EffectiveVisibility(val name: String, val publicApi: Boolean = fals
override fun relation(other: EffectiveVisibility, typeCheckerContextProvider: TypeCheckerProviderContext): Permissiveness =
when (other) {
Public -> Permissiveness.LESS
Private, Local, ProtectedBound, InternalProtectedBound -> Permissiveness.MORE
Private, PrivateInFile, Local, ProtectedBound, InternalProtectedBound -> Permissiveness.MORE
is Protected -> containerRelation(containerTypeConstructor, other.containerTypeConstructor, typeCheckerContextProvider)
is InternalProtected -> when (containerRelation(
containerTypeConstructor,
@@ -105,7 +118,7 @@ sealed class EffectiveVisibility(val name: String, val publicApi: Boolean = fals
override fun lowerBound(other: EffectiveVisibility, typeCheckerContextProvider: TypeCheckerProviderContext): EffectiveVisibility =
when (other) {
Public -> this
Private, Local, ProtectedBound, InternalProtectedBound -> other
Private, PrivateInFile, Local, ProtectedBound, InternalProtectedBound -> other
is Protected -> when (relation(other, typeCheckerContextProvider)) {
Permissiveness.SAME, Permissiveness.MORE -> this
Permissiveness.LESS -> other
@@ -126,7 +139,7 @@ sealed class EffectiveVisibility(val name: String, val publicApi: Boolean = fals
override fun relation(other: EffectiveVisibility, typeCheckerContextProvider: TypeCheckerProviderContext): Permissiveness =
when (other) {
Public, is Protected -> Permissiveness.LESS
Private, Local, InternalProtectedBound -> Permissiveness.MORE
Private, PrivateInFile, Local, InternalProtectedBound -> Permissiveness.MORE
ProtectedBound -> Permissiveness.SAME
is InternalOrPackage, is InternalProtected -> Permissiveness.UNKNOWN
}
@@ -134,7 +147,7 @@ sealed class EffectiveVisibility(val name: String, val publicApi: Boolean = fals
override fun lowerBound(other: EffectiveVisibility, typeCheckerContextProvider: TypeCheckerProviderContext): EffectiveVisibility =
when (other) {
Public, is Protected -> this
Private, Local, ProtectedBound, InternalProtectedBound -> other
Private, PrivateInFile, Local, ProtectedBound, InternalProtectedBound -> other
is InternalOrPackage, is InternalProtected -> InternalProtectedBound
}
@@ -155,7 +168,7 @@ sealed class EffectiveVisibility(val name: String, val publicApi: Boolean = fals
override fun relation(other: EffectiveVisibility, typeCheckerContextProvider: TypeCheckerProviderContext): Permissiveness =
when (other) {
Public, is InternalOrPackage -> Permissiveness.LESS
Private, Local, InternalProtectedBound -> Permissiveness.MORE
Private, PrivateInFile, Local, InternalProtectedBound -> Permissiveness.MORE
is InternalProtected -> containerRelation(
containerTypeConstructor,
other.containerTypeConstructor,
@@ -176,7 +189,7 @@ sealed class EffectiveVisibility(val name: String, val publicApi: Boolean = fals
override fun lowerBound(other: EffectiveVisibility, typeCheckerContextProvider: TypeCheckerProviderContext): EffectiveVisibility =
when (other) {
Public, is InternalOrPackage -> this
Private, Local, InternalProtectedBound -> other
Private, PrivateInFile, Local, InternalProtectedBound -> other
is Protected, is InternalProtected -> when (relation(other, typeCheckerContextProvider)) {
Permissiveness.SAME, Permissiveness.MORE -> this
Permissiveness.LESS -> other
@@ -193,7 +206,7 @@ sealed class EffectiveVisibility(val name: String, val publicApi: Boolean = fals
override fun relation(other: EffectiveVisibility, typeCheckerContextProvider: TypeCheckerProviderContext): Permissiveness =
when (other) {
Public, is Protected, is InternalProtected, ProtectedBound, is InternalOrPackage -> Permissiveness.LESS
Private, Local -> Permissiveness.MORE
Private, PrivateInFile, Local -> Permissiveness.MORE
InternalProtectedBound -> Permissiveness.SAME
}

View File

@@ -20,7 +20,10 @@ fun DescriptorVisibility.effectiveVisibility(
private fun DescriptorVisibility.forVisibility(descriptor: DeclarationDescriptor, checkPublishedApi: Boolean = false): EffectiveVisibility =
when (this) {
DescriptorVisibilities.PRIVATE, DescriptorVisibilities.PRIVATE_TO_THIS, DescriptorVisibilities.INVISIBLE_FAKE -> EffectiveVisibility.Private
DescriptorVisibilities.PRIVATE_TO_THIS, DescriptorVisibilities.INVISIBLE_FAKE -> EffectiveVisibility.Private
DescriptorVisibilities.PRIVATE -> if (descriptor is ClassDescriptor &&
descriptor.containingDeclaration is PackageFragmentDescriptor
) EffectiveVisibility.PrivateInFile else EffectiveVisibility.Private
DescriptorVisibilities.PROTECTED -> EffectiveVisibility.Protected(
(descriptor.containingDeclaration as? ClassDescriptor)?.defaultType?.constructor
)

View File

@@ -86,7 +86,7 @@ class MemberVisibilityCanBePrivateInspection : AbstractKotlinInspection() {
val descriptor = (declaration.toDescriptor() as? DeclarationDescriptorWithVisibility) ?: return false
when (descriptor.effectiveVisibility()) {
EffectiveVisibility.Private, EffectiveVisibility.Local -> return false
EffectiveVisibility.Private, EffectiveVisibility.PrivateInFile, EffectiveVisibility.Local -> return false
}
val entryPointsManager = EntryPointsManager.getInstance(declaration.project) as EntryPointsManagerBase

View File

@@ -2,18 +2,18 @@ private class A
open class B<T>(val x: T)
class C(<error descr="[EXPOSED_PARAMETER_TYPE] 'public' function exposes its 'private' parameter type A">x: A</error>): <error descr="[EXPOSED_SUPER_CLASS] 'public' subclass exposes its 'private' supertype argument A">B<A>(x)</error>
class C(<error descr="[EXPOSED_PARAMETER_TYPE] 'public' function exposes its 'private-in-file' parameter type A">x: A</error>): <error descr="[EXPOSED_SUPER_CLASS] 'public' subclass exposes its 'private-in-file' supertype argument A">B<A>(x)</error>
private class D {
class E
}
fun <error descr="[EXPOSED_FUNCTION_RETURN_TYPE] 'public' function exposes its 'private' return type A">create</error>() = A()
fun <error descr="[EXPOSED_FUNCTION_RETURN_TYPE] 'public' function exposes its 'private-in-file' return type A">create</error>() = A()
fun <error descr="[EXPOSED_FUNCTION_RETURN_TYPE] 'public' function exposes its 'private' return type argument A">create</error>(<error descr="[EXPOSED_PARAMETER_TYPE] 'public' function exposes its 'private' parameter type A">a: A</error>) = B(a)
fun <error descr="[EXPOSED_FUNCTION_RETURN_TYPE] 'public' function exposes its 'private-in-file' return type argument A">create</error>(<error descr="[EXPOSED_PARAMETER_TYPE] 'public' function exposes its 'private-in-file' parameter type A">a: A</error>) = B(a)
val <error descr="[EXPOSED_PROPERTY_TYPE] 'public' property exposes its 'private' type A">x</error> = create()
val <error descr="[EXPOSED_PROPERTY_TYPE] 'public' property exposes its 'private-in-file' type A">x</error> = create()
val <error descr="[EXPOSED_PROPERTY_TYPE] 'public' property exposes its 'private' type argument A">y</error> = create(x)
val <error descr="[EXPOSED_PROPERTY_TYPE] 'public' property exposes its 'private-in-file' type argument A">y</error> = create(x)
val <error descr="[EXPOSED_PROPERTY_TYPE] 'public' property exposes its 'private' type argument containing declaration D">z</error>: B<D.E>? = null
val <error descr="[EXPOSED_PROPERTY_TYPE] 'public' property exposes its 'private-in-file' type argument containing declaration D">z</error>: B<D.E>? = null

View File

@@ -53,22 +53,22 @@ Exit code: ABORT
------------------------------------------
COMPILATION FAILED
Cannot access 'A': it is private in file
'public' subclass exposes its 'private' supertype A
'public' generic exposes its 'private' parameter bound type A
'public' subclass exposes its 'private-in-file' supertype A
'public' generic exposes its 'private-in-file' parameter bound type A
Cannot access 'A': it is private in file
Cannot access 'A': it is private in file
'public' function exposes its 'private' parameter type A
'public' function exposes its 'private-in-file' parameter type A
Cannot access 'A': it is private in file
Cannot access 'A': it is private in file
'public' function exposes its 'private' return type A
'public' function exposes its 'private-in-file' return type A
Cannot access 'A': it is private in file
Cannot access 'A': it is private in file
Cannot access 'A': it is private in file
Cannot access 'A': it is private in file
'public' function exposes its 'private' return type A
'public' function exposes its 'private-in-file' return type A
Cannot access 'A': it is private in file
Cannot access 'A': it is private in file
'public' function exposes its 'private' return type A
'public' function exposes its 'private-in-file' return type A
================ Step #2 =================

View File

@@ -35,8 +35,8 @@ Exit code: ABORT
------------------------------------------
COMPILATION FAILED
Cannot access 'A': it is private in file
'public' subclass exposes its 'private' supertype A
'public' function exposes its 'private' return type A
'public' subclass exposes its 'private-in-file' supertype A
'public' function exposes its 'private-in-file' return type A
Cannot access 'A': it is private in file
Cannot access 'A': it is private in file
Only safe (?.) or non-null asserted (!!.) calls are allowed on a nullable receiver of type Any?

View File

@@ -29,7 +29,7 @@ Exit code: ABORT
------------------------------------------
COMPILATION FAILED
Cannot access 'A': it is private in file
'public' subclass exposes its 'private' supertype A
'public' subclass exposes its 'private-in-file' supertype A
================ Step #2 =================

View File

@@ -31,7 +31,7 @@ Exit code: ABORT
------------------------------------------
COMPILATION FAILED
Cannot access 'A': it is private in file
'public' subclass exposes its 'private' supertype A
'public' subclass exposes its 'private-in-file' supertype A
================ Step #2 =================

View File

@@ -29,7 +29,7 @@ Exit code: ABORT
------------------------------------------
COMPILATION FAILED
Cannot access 'A': it is private in file
'public' subclass exposes its 'private' supertype A
'public' subclass exposes its 'private-in-file' supertype A
Cannot access 'A': it is private in file
================ Step #2 =================

View File

@@ -31,7 +31,7 @@ Exit code: ABORT
------------------------------------------
COMPILATION FAILED
Cannot access 'A': it is private in file
'public' subclass exposes its 'private' supertype A
'public' subclass exposes its 'private-in-file' supertype A
Cannot access 'A': it is private in file
================ Step #2 =================