Skip to content

Commit

Permalink
~~~ Attempt to write 'allowsToHaveFakeOverrideIn' properly
Browse files Browse the repository at this point in the history
  • Loading branch information
mglukhikh committed Mar 17, 2021
1 parent 787eb46 commit 9882e02
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import org.jetbrains.kotlin.descriptors.Modality
import org.jetbrains.kotlin.fir.*
import org.jetbrains.kotlin.fir.analysis.checkers.FirDeclarationPresenter
import org.jetbrains.kotlin.fir.analysis.checkers.context.CheckerContext
import org.jetbrains.kotlin.fir.analysis.checkers.findClosestFile
import org.jetbrains.kotlin.fir.analysis.checkers.getContainingClass
import org.jetbrains.kotlin.fir.analysis.checkers.unsubstitutedScope
import org.jetbrains.kotlin.fir.analysis.diagnostics.DiagnosticReporter
Expand Down Expand Up @@ -43,7 +42,7 @@ object FirNotImplementedOverrideChecker : FirClassChecker() {

val contributedMembers = collectCallableMembers(declaration, context)
val baseSymbolsForFakeOverrides = declaration.calcBaseSymbolsForFakeOverrides(
contributedMembers, context.findClosestFile()!!, context.session, context.sessionHolder.scopeSession
contributedMembers, context.session, context.sessionHolder.scopeSession
).filter {
val fir = it.fir
fir is FirMemberDeclaration &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import org.jetbrains.kotlin.fir.symbols.impl.FirCallableSymbol

fun FirClass<*>.calcBaseSymbolsForFakeOverrides(
contributedDeclarations: Collection<FirDeclaration>,
containerFile: FirFile,
session: FirSession,
scopeSession: ScopeSession,
): Collection<FirCallableSymbol<*>> {
Expand All @@ -31,9 +30,7 @@ fun FirClass<*>.calcBaseSymbolsForFakeOverrides(
contributedDeclarations,
result,
classScope,
FirTypeScope::getDirectOverriddenFunctions,
containerFile,
session
FirTypeScope::getDirectOverriddenFunctions
)
}

Expand All @@ -43,9 +40,7 @@ fun FirClass<*>.calcBaseSymbolsForFakeOverrides(
contributedDeclarations,
result,
classScope,
FirTypeScope::getDirectOverriddenProperties,
containerFile,
session
FirTypeScope::getDirectOverriddenProperties
)
}

Expand Down Expand Up @@ -87,8 +82,6 @@ private inline fun <reified D : FirCallableMemberDeclaration<D>, reified S : Fir
result: MutableList<FirCallableSymbol<*>>,
scope: FirTypeScope,
computeDirectOverridden: FirTypeScope.(S) -> List<S>,
containerFile: FirFile,
session: FirSession,
) {
if (originalSymbol !is S || originalSymbol.fir in contributedDeclarations) return
val classLookupTag = symbol.toLookupTag()
Expand All @@ -101,25 +94,18 @@ private inline fun <reified D : FirCallableMemberDeclaration<D>, reified S : Fir
// The current one is a FIR declaration for that fake override, and we can compute base symbols from it.
computeBaseSymbols(originalSymbol, computeDirectOverridden, scope, classLookupTag)
}
originalDeclaration.allowsToHaveFakeOverrideIn(firClass = this, containerFile, session) -> {
originalDeclaration.allowsToHaveFakeOverrideIn(firClass = this) -> {
// Trivial fake override case.
// FIR2IR will create a fake override in BE IR directly, and the current one _is_ the base declaration.
result.add(originalSymbol)
}
}
}

private fun FirCallableMemberDeclaration<*>.allowsToHaveFakeOverrideIn(
firClass: FirClass<*>,
containerFile: FirFile,
session: FirSession,
): Boolean {
private fun FirCallableMemberDeclaration<*>.allowsToHaveFakeOverrideIn(firClass: FirClass<*>): Boolean {
if (!allowsToHaveFakeOverride) {
return false
}
if (!session.visibilityChecker.isVisible(this, session, containerFile, listOf(), dispatchReceiver = null)) {
return false
}
return this.symbol.callableId.packageName == firClass.symbol.classId.packageFqName
return visibility.canSeePackage(symbol.callableId.packageName, firClass.symbol.classId.packageFqName)
}

Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,17 @@ package impl
import base.*
import intermediate.*

class ImplDirectFromBase : Base()
<!ABSTRACT_CLASS_MEMBER_NOT_IMPLEMENTED!>class ImplDirectFromBase<!> : Base()

object ImplObjDirectFromBase : Base()
<!ABSTRACT_CLASS_MEMBER_NOT_IMPLEMENTED!>object ImplObjDirectFromBase<!> : Base()

class ImplDirectFromBaseWithOverride : BaseWithOverride()

class ImplDirectFromBaseWithOverrid : Base() {
override fun internalFoo(): String = ""
}

class ImplViaIntermediate : Intermediate()
<!ABSTRACT_CLASS_MEMBER_NOT_IMPLEMENTED!>class ImplViaIntermediate<!> : Intermediate()

fun foo() {
ImplDirectFromBase().foo()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ abstract class Base {
package impl
import base.*

class Impl : Base()
<!ABSTRACT_CLASS_MEMBER_NOT_IMPLEMENTED!>class Impl<!> : Base()

fun foo() {
Impl().foo()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package org.jetbrains.kotlin.descriptors.java
import org.jetbrains.kotlin.descriptors.EffectiveVisibility
import org.jetbrains.kotlin.descriptors.Visibilities
import org.jetbrains.kotlin.descriptors.Visibility
import org.jetbrains.kotlin.name.FqName

object JavaVisibilities {
object PackageVisibility : Visibility("package", isPublicAPI = false) {
Expand All @@ -32,6 +33,10 @@ object JavaVisibilities {
override fun customEffectiveVisibility(): EffectiveVisibility? {
return EffectiveVisibility.PackagePrivate
}

override fun canSeePackage(fromPackage: FqName, toPackage: FqName): Boolean {
return fromPackage == toPackage
}
}

object ProtectedStaticVisibility : Visibility("protected_static", isPublicAPI = true) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

package org.jetbrains.kotlin.descriptors

import org.jetbrains.kotlin.name.FqName

abstract class Visibility protected constructor(
val name: String,
val isPublicAPI: Boolean
Expand All @@ -27,4 +29,6 @@ abstract class Visibility protected constructor(

// Should be overloaded in Java visibilities
open fun customEffectiveVisibility(): EffectiveVisibility? = null

open fun canSeePackage(fromPackage: FqName, toPackage: FqName): Boolean = true
}

0 comments on commit 9882e02

Please sign in to comment.