[FIR] Fix visibility issues with private members within inner and anonymous scopes ^KT-49992 Fixed

This commit is contained in:
Ivan Kochurkin
2021-12-01 16:57:27 +03:00
committed by Space
parent 800d594a4f
commit f68c8f8f01
11 changed files with 390 additions and 35 deletions
@@ -32625,6 +32625,12 @@ public class DiagnosisCompilerTestFE10TestdataTestGenerated extends AbstractDiag
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/diagnostics/tests/visibility"), Pattern.compile("^(.+)\\.kt$"), Pattern.compile("^(.+)\\.fir\\.kts?$"), true);
}
@Test
@TestMetadata("innerNestedAndAnonymousClasses.kt")
public void testInnerNestedAndAnonymousClasses() throws Exception {
runTest("compiler/testData/diagnostics/tests/visibility/innerNestedAndAnonymousClasses.kt");
}
@Test
@TestMetadata("invisibleSetterOfJavaClass.kt")
public void testInvisibleSetterOfJavaClass() throws Exception {
@@ -32625,6 +32625,12 @@ public class FirOldFrontendDiagnosticsTestGenerated extends AbstractFirDiagnosti
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/diagnostics/tests/visibility"), Pattern.compile("^(.+)\\.kt$"), Pattern.compile("^(.+)\\.fir\\.kts?$"), true);
}
@Test
@TestMetadata("innerNestedAndAnonymousClasses.kt")
public void testInnerNestedAndAnonymousClasses() throws Exception {
runTest("compiler/testData/diagnostics/tests/visibility/innerNestedAndAnonymousClasses.kt");
}
@Test
@TestMetadata("invisibleSetterOfJavaClass.kt")
public void testInvisibleSetterOfJavaClass() throws Exception {
@@ -32625,6 +32625,12 @@ public class FirOldFrontendDiagnosticsWithLightTreeTestGenerated extends Abstrac
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/diagnostics/tests/visibility"), Pattern.compile("^(.+)\\.kt$"), Pattern.compile("^(.+)\\.fir\\.kts?$"), true);
}
@Test
@TestMetadata("innerNestedAndAnonymousClasses.kt")
public void testInnerNestedAndAnonymousClasses() throws Exception {
runTest("compiler/testData/diagnostics/tests/visibility/innerNestedAndAnonymousClasses.kt");
}
@Test
@TestMetadata("invisibleSetterOfJavaClass.kt")
public void testInvisibleSetterOfJavaClass() throws Exception {
@@ -41,7 +41,7 @@ object FirJavaVisibilityChecker : FirVisibilityChecker() {
val ownerLookupTag = symbol.getOwnerLookupTag() ?: return false
if (canSeeProtectedMemberOf(
containingDeclarations, dispatchReceiver, ownerLookupTag, session,
isVariableOrNamedFunction = symbol is FirVariableSymbol || symbol is FirNamedFunctionSymbol || symbol is FirPropertyAccessorSymbol,
isVariableOrNamedFunction = symbol.isVariableOrNamedFunction(),
isSyntheticProperty = symbol.fir is FirSyntheticPropertyAccessor,
supertypeSupplier
)
@@ -6,17 +6,17 @@
package org.jetbrains.kotlin.fir
import org.jetbrains.kotlin.config.LanguageFeature
import org.jetbrains.kotlin.descriptors.ClassKind
import org.jetbrains.kotlin.descriptors.Visibilities
import org.jetbrains.kotlin.descriptors.Visibility
import org.jetbrains.kotlin.fir.declarations.*
import org.jetbrains.kotlin.fir.declarations.synthetic.FirSyntheticPropertyAccessor
import org.jetbrains.kotlin.fir.declarations.utils.*
import org.jetbrains.kotlin.fir.expressions.FirPropertyAccessExpression
import org.jetbrains.kotlin.fir.expressions.FirThisReceiverExpression
import org.jetbrains.kotlin.fir.references.FirSuperReference
import org.jetbrains.kotlin.fir.resolve.*
import org.jetbrains.kotlin.fir.resolve.calls.ExpressionReceiverValue
import org.jetbrains.kotlin.fir.resolve.calls.FirSyntheticFunctionSymbol
import org.jetbrains.kotlin.fir.resolve.calls.ReceiverValue
import org.jetbrains.kotlin.fir.resolve.calls.*
import org.jetbrains.kotlin.fir.resolve.providers.firProvider
import org.jetbrains.kotlin.fir.resolve.providers.symbolProvider
import org.jetbrains.kotlin.fir.symbols.ConeClassLikeLookupTag
@@ -140,6 +140,7 @@ abstract class FirVisibilityChecker : FirSessionComponent {
): Boolean {
val symbol = declaration.symbol
val provider = session.firProvider
return when (declaration.visibility) {
Visibilities.Internal -> {
declaration.moduleData == session.moduleData || session.moduleVisibilityChecker?.isInFriendModule(declaration) == true
@@ -168,7 +169,13 @@ abstract class FirVisibilityChecker : FirSessionComponent {
}
else -> {
// Member: visible inside parent class, including all its member classes
canSeePrivateMemberOf(containingDeclarations, ownerLookupTag, session)
canSeePrivateMemberOf(
containingDeclarations,
ownerLookupTag,
dispatchReceiver,
isVariableOrNamedFunction = symbol.isVariableOrNamedFunction(),
session
)
}
}
} else {
@@ -180,7 +187,7 @@ abstract class FirVisibilityChecker : FirSessionComponent {
val ownerId = symbol.getOwnerLookupTag()
ownerId != null && canSeeProtectedMemberOf(
containingDeclarations, dispatchReceiver, ownerId, session,
isVariableOrNamedFunction = symbol is FirVariableSymbol || symbol is FirNamedFunctionSymbol || symbol is FirPropertyAccessorSymbol,
isVariableOrNamedFunction = symbol.isVariableOrNamedFunction(),
symbol.fir is FirSyntheticPropertyAccessor,
supertypeSupplier
)
@@ -213,21 +220,76 @@ abstract class FirVisibilityChecker : FirSessionComponent {
private fun canSeePrivateMemberOf(
containingDeclarationOfUseSite: List<FirDeclaration>,
ownerLookupTag: ConeClassLikeLookupTag,
dispatchReceiver: ReceiverValue?,
isVariableOrNamedFunction: Boolean,
session: FirSession
): Boolean {
ownerLookupTag.ownerIfCompanion(session)?.let { companionOwnerLookupTag ->
return canSeePrivateMemberOf(containingDeclarationOfUseSite, companionOwnerLookupTag, session)
return canSeePrivateMemberOf(
containingDeclarationOfUseSite,
companionOwnerLookupTag,
dispatchReceiver,
isVariableOrNamedFunction,
session
)
}
for (declaration in containingDeclarationOfUseSite) {
if (declaration !is FirClass) continue
val boundSymbol = declaration.symbol
if (boundSymbol.classId.isSame(ownerLookupTag.classId)) {
return true
fun matchWithContainingDeclarations(): Boolean {
for (declaration in containingDeclarationOfUseSite) {
if (declaration !is FirClass) continue
val boundSymbol = declaration.symbol
if (boundSymbol.classId.isSame(ownerLookupTag.classId)) {
return true
}
}
return false
}
return false
return if (isVariableOrNamedFunction && dispatchReceiver?.type is ConeClassLikeType) {
if (dispatchReceiver is ImplicitDispatchReceiverValue) {
val dispatchReceiverOwnerClassId = dispatchReceiver.ownerIfCompanion(session)?.classId ?: dispatchReceiver.type.classId
var isMemberFound = false
for (declaration in containingDeclarationOfUseSite) {
if (declaration !is FirClass) continue
val boundSymbol = declaration.symbol
var isSameClasses = false
if (!isMemberFound) {
isSameClasses = boundSymbol.classId.isSame(ownerLookupTag.classId)
if (isSameClasses) {
isMemberFound = true
}
}
if (isMemberFound) {
if (!isSameClasses && !(
declaration.isInner ||
(declaration.containingNonLocalClass(session) as? FirRegularClass)?.classKind == ClassKind.OBJECT)
) {
// It should not be ordinary classes between use site and declaration
return false
}
if (boundSymbol.classId.isSame(dispatchReceiverOwnerClassId!!)) {
return true
}
}
}
isMemberFound
} else {
val receiverExpression = dispatchReceiver.receiverExpression
if (receiverExpression is FirThisReceiverExpression &&
receiverExpression.source != null &&
(containingDeclarationOfUseSite.lastOrNull() as? FirFunction)?.symbol?.isExtension != true
) {
// Processing of explicit no extension `this`
val dispatchReceiverOwnerClassId = dispatchReceiver.ownerIfCompanion(session)?.classId ?: dispatchReceiver.type.classId
dispatchReceiverOwnerClassId?.isSame(ownerLookupTag.classId) == true
} else {
matchWithContainingDeclarations()
}
}
} else {
matchWithContainingDeclarations()
}
}
// 'local' isn't taken into account here
@@ -350,7 +412,14 @@ abstract class FirVisibilityChecker : FirSessionComponent {
isSyntheticProperty: Boolean,
supertypeSupplier: SupertypeSupplier
): Boolean {
if (canSeePrivateMemberOf(containingDeclarationOfUseSite, ownerLookupTag, session)) return true
if (canSeePrivateMemberOf(
containingDeclarationOfUseSite,
ownerLookupTag,
dispatchReceiver,
isVariableOrNamedFunction,
session
)
) return true
for (containingDeclaration in containingDeclarationOfUseSite) {
if (containingDeclaration is FirClass) {
@@ -405,3 +474,7 @@ fun FirBasedSymbol<*>.getOwnerLookupTag(): ConeClassLikeLookupTag? {
else -> error("Unsupported owner search for ${fir.javaClass}: ${fir.render()}")
}
}
fun FirBasedSymbol<*>.isVariableOrNamedFunction(): Boolean {
return this is FirVariableSymbol || this is FirNamedFunctionSymbol || this is FirPropertyAccessorSymbol
}
@@ -27,17 +27,17 @@ fun FirVisibilityChecker.isVisible(
return isVisible(declaration.originalIfFakeOverride() as FirMemberDeclaration, candidate)
}
val callInfo = candidate.callInfo
val useSiteFile = callInfo.containingFile
val containingDeclarations = callInfo.containingDeclarations
val session = callInfo.session
// We won't resolve into the backing field
// in the first place, if it's not accessible.
if (declaration is FirBackingField) {
return true
}
val callInfo = candidate.callInfo
val useSiteFile = callInfo.containingFile
val containingDeclarations = callInfo.containingDeclarations
val session = callInfo.session
val visible = isVisible(
declaration,
session,
@@ -46,17 +46,19 @@ fun FirVisibilityChecker.isVisible(
candidate.dispatchReceiverValue,
candidate.callInfo.callSite is FirVariableAssignment
)
val backingField = declaration.getBackingFieldIfApplicable()
if (visible && backingField != null) {
candidate.hasVisibleBackingField = isVisible(
backingField,
session,
useSiteFile,
containingDeclarations,
candidate.dispatchReceiverValue,
candidate.callInfo.callSite is FirVariableAssignment,
)
if (visible) {
val backingField = declaration.getBackingFieldIfApplicable()
if (backingField != null) {
candidate.hasVisibleBackingField = isVisible(
backingField,
session,
useSiteFile,
containingDeclarations,
candidate.dispatchReceiverValue,
candidate.callInfo.callSite is FirVariableAssignment,
)
}
}
return visible
@@ -67,10 +69,11 @@ private fun FirMemberDeclaration.getBackingFieldIfApplicable(): FirBackingField?
// This check prevents resolving protected and
// public fields.
val visibility = field.visibility
if (
field.visibility == Visibilities.PrivateToThis ||
field.visibility == Visibilities.Private ||
field.visibility == Visibilities.Internal
visibility == Visibilities.PrivateToThis ||
visibility == Visibilities.Private ||
visibility == Visibilities.Internal
) {
return field
}
-3
View File
@@ -1,6 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
// FIR status: NPE: null cannot be cast to not-null type IrPropertySymbol
// while translating this@R|/<anonymous>|.R|/X.n|
open class X(private val n: String) {
fun foo(): String {
@@ -0,0 +1,76 @@
// KT-49992
import kotlin.reflect.KFunction0
open class A {
private val x: String? = null
fun test0() {
x
this.x
}
open class Nested : A() {
private val y: String? = null
fun test1(): String? = <!INVISIBLE_REFERENCE!>x<!>
fun test2(): String? = this.<!INVISIBLE_REFERENCE!>x<!>
class NestedInNested : Nested() {
fun test20(): String? = <!INVISIBLE_REFERENCE!>y<!>
fun test21(): String? = this.<!INVISIBLE_REFERENCE!>y<!>
}
inner class InnerInNested : Nested() {
fun test23(): String? = y
fun test24(): String? = this.<!INVISIBLE_REFERENCE!>y<!>
}
}
interface I {
fun test401(): KFunction0<Unit>
}
open inner class Inner : A(), I {
private val y: String? = null
fun test3(): String? = x
fun test4(): String? = this.<!INVISIBLE_REFERENCE!>x<!>
inner class InnerInInner : Inner() {
fun test40(): String? = x
fun test41(): String? = y
}
private fun test400() {
}
override fun test401(): KFunction0<Unit> {
return this::test400
}
}
fun test5() {
object : A() {
fun local() {
x
this.<!INVISIBLE_REFERENCE!>x<!>
}
inner class NestedInAnonymous() {
fun test50(): String? = x
}
}
}
}
fun A.extensionFun(): String? = this.<!INVISIBLE_REFERENCE!>x<!>
abstract class B<T: B<T>> {
protected abstract val thisBuilder: T
private val x: String? = null
fun test6(obj: Any?) = thisBuilder.apply {
obj?.let { this.x }
}
}
@@ -0,0 +1,76 @@
// KT-49992
import kotlin.reflect.KFunction0
open class A {
private val x: String? = null
fun test0() {
x
this.x
}
open class Nested : A() {
private val y: String? = null
fun test1(): String? = <!INVISIBLE_MEMBER!>x<!>
fun test2(): String? = this.<!INVISIBLE_MEMBER!>x<!>
class NestedInNested : Nested() {
fun test20(): String? = <!INVISIBLE_MEMBER!>y<!>
fun test21(): String? = this.<!INVISIBLE_MEMBER!>y<!>
}
inner class InnerInNested : Nested() {
fun test23(): String? = y
fun test24(): String? = this.<!INVISIBLE_MEMBER!>y<!>
}
}
interface I {
fun test401(): KFunction0<Unit>
}
open inner class Inner : A(), I {
private val y: String? = null
fun test3(): String? = x
fun test4(): String? = this.<!INVISIBLE_MEMBER!>x<!>
inner class InnerInInner : Inner() {
fun test40(): String? = x
fun test41(): String? = y
}
private fun test400() {
}
override fun test401(): KFunction0<Unit> {
return this::test400
}
}
fun test5() {
object : A() {
fun local() {
x
this.<!INVISIBLE_MEMBER!>x<!>
}
inner class NestedInAnonymous() {
fun test50(): String? = x
}
}
}
}
fun A.extensionFun(): String? = this.<!INVISIBLE_MEMBER!>x<!>
abstract class B<T: B<T>> {
protected abstract val thisBuilder: T
private val x: String? = null
fun test6(obj: Any?) = thisBuilder.apply {
obj?.let { this.x }
}
}
@@ -0,0 +1,106 @@
package
public fun A.extensionFun(): kotlin.String?
public open class A {
public constructor A()
private final val x: kotlin.String? = null
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 test0(): kotlin.Unit
public final fun test5(): kotlin.Unit
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
public interface I {
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 abstract fun test401(): kotlin.reflect.KFunction0<kotlin.Unit>
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
public open inner class Inner : A, A.I {
public constructor Inner()
invisible_fake final override /*1*/ /*fake_override*/ val x: kotlin.String?
private final val y: kotlin.String? = null
public open override /*2*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*2*/ /*fake_override*/ fun hashCode(): kotlin.Int
public final override /*1*/ /*fake_override*/ fun test0(): kotlin.Unit
public final fun test3(): kotlin.String?
public final fun test4(): kotlin.String?
private final fun test400(): kotlin.Unit
public open override /*1*/ fun test401(): kotlin.reflect.KFunction0<kotlin.Unit>
public final override /*1*/ /*fake_override*/ fun test5(): kotlin.Unit
public open override /*2*/ /*fake_override*/ fun toString(): kotlin.String
public final inner class InnerInInner : A.Inner {
public constructor InnerInInner()
invisible_fake final override /*1*/ /*fake_override*/ val x: kotlin.String?
invisible_fake final override /*1*/ /*fake_override*/ val y: kotlin.String?
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 override /*1*/ /*fake_override*/ fun test0(): kotlin.Unit
public final override /*1*/ /*fake_override*/ fun test3(): kotlin.String?
public final override /*1*/ /*fake_override*/ fun test4(): kotlin.String?
public final fun test40(): kotlin.String?
invisible_fake final override /*1*/ /*fake_override*/ fun test400(): kotlin.Unit
public open override /*1*/ /*fake_override*/ fun test401(): kotlin.reflect.KFunction0<kotlin.Unit>
public final fun test41(): kotlin.String?
public final override /*1*/ /*fake_override*/ fun test5(): kotlin.Unit
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
}
public open class Nested : A {
public constructor Nested()
invisible_fake final override /*1*/ /*fake_override*/ val x: kotlin.String?
private final val y: kotlin.String? = null
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 override /*1*/ /*fake_override*/ fun test0(): kotlin.Unit
public final fun test1(): kotlin.String?
public final fun test2(): kotlin.String?
public final override /*1*/ /*fake_override*/ fun test5(): kotlin.Unit
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
public final inner class InnerInNested : A.Nested {
public constructor InnerInNested()
invisible_fake final override /*1*/ /*fake_override*/ val x: kotlin.String?
invisible_fake final override /*1*/ /*fake_override*/ val y: kotlin.String?
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 override /*1*/ /*fake_override*/ fun test0(): kotlin.Unit
public final override /*1*/ /*fake_override*/ fun test1(): kotlin.String?
public final override /*1*/ /*fake_override*/ fun test2(): kotlin.String?
public final fun test23(): kotlin.String?
public final fun test24(): kotlin.String?
public final override /*1*/ /*fake_override*/ fun test5(): kotlin.Unit
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
public final class NestedInNested : A.Nested {
public constructor NestedInNested()
invisible_fake final override /*1*/ /*fake_override*/ val x: kotlin.String?
invisible_fake final override /*1*/ /*fake_override*/ val y: kotlin.String?
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 override /*1*/ /*fake_override*/ fun test0(): kotlin.Unit
public final override /*1*/ /*fake_override*/ fun test1(): kotlin.String?
public final override /*1*/ /*fake_override*/ fun test2(): kotlin.String?
public final fun test20(): kotlin.String?
public final fun test21(): kotlin.String?
public final override /*1*/ /*fake_override*/ fun test5(): kotlin.Unit
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
}
}
public abstract class B</*0*/ T : B<T>> {
public constructor B</*0*/ T : B<T>>()
protected abstract val thisBuilder: T
private final val x: kotlin.String? = null
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 test6(/*0*/ obj: kotlin.Any?): T
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
@@ -32721,6 +32721,12 @@ public class DiagnosticTestGenerated extends AbstractDiagnosticTest {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/diagnostics/tests/visibility"), Pattern.compile("^(.*)\\.kts?$"), Pattern.compile("^(.+)\\.fir\\.kts?$"), true);
}
@Test
@TestMetadata("innerNestedAndAnonymousClasses.kt")
public void testInnerNestedAndAnonymousClasses() throws Exception {
runTest("compiler/testData/diagnostics/tests/visibility/innerNestedAndAnonymousClasses.kt");
}
@Test
@TestMetadata("invisibleSetterOfJavaClass.kt")
public void testInvisibleSetterOfJavaClass() throws Exception {