[FIR] Report INVISIBLE_REFERENCE in imports
#KT-55405 Fixed
This commit is contained in:
committed by
Space Team
parent
7dde3603b5
commit
38f0ac332d
@@ -9,6 +9,7 @@ import com.intellij.lang.LighterASTNode
|
||||
import com.intellij.psi.tree.IElementType
|
||||
import com.intellij.psi.tree.TokenSet
|
||||
import org.jetbrains.kotlin.*
|
||||
import org.jetbrains.kotlin.fir.declarations.FirImport
|
||||
|
||||
fun KtSourceElement.getChild(type: IElementType, index: Int = 0, depth: Int = -1, reverse: Boolean = false): KtSourceElement? {
|
||||
return getChild(setOf(type), index, depth, reverse)
|
||||
@@ -53,3 +54,18 @@ internal fun KtLightSourceElement.buildChildSourceElement(childNode: LighterASTN
|
||||
)
|
||||
}
|
||||
|
||||
private val IMPORT_PARENT_TOKEN_TYPES = TokenSet.create(KtNodeTypes.DOT_QUALIFIED_EXPRESSION, KtNodeTypes.REFERENCE_EXPRESSION)
|
||||
|
||||
/**
|
||||
* Returns a source element for the import segment that is [indexFromLast]th from last.
|
||||
*/
|
||||
fun FirImport.getSourceForImportSegment(indexFromLast: Int): KtSourceElement? {
|
||||
var segmentSource: KtSourceElement = source ?: return null
|
||||
|
||||
repeat(indexFromLast + 1) {
|
||||
segmentSource = segmentSource.getChild(IMPORT_PARENT_TOKEN_TYPES, depth = 1) ?: return null
|
||||
}
|
||||
|
||||
return segmentSource.takeIf { it.elementType == KtNodeTypes.REFERENCE_EXPRESSION }
|
||||
?: segmentSource.getChild(KtNodeTypes.REFERENCE_EXPRESSION, depth = 1, reverse = true)
|
||||
}
|
||||
|
||||
+109
-65
@@ -5,6 +5,7 @@
|
||||
|
||||
package org.jetbrains.kotlin.fir.analysis.checkers.declaration
|
||||
|
||||
import org.jetbrains.kotlin.descriptors.Visibilities
|
||||
import org.jetbrains.kotlin.diagnostics.DiagnosticReporter
|
||||
import org.jetbrains.kotlin.diagnostics.reportOn
|
||||
import org.jetbrains.kotlin.fir.analysis.checkers.context.CheckerContext
|
||||
@@ -12,19 +13,26 @@ import org.jetbrains.kotlin.fir.analysis.checkers.expression.FirDeprecationCheck
|
||||
import org.jetbrains.kotlin.fir.declarations.fullyExpandedClass
|
||||
import org.jetbrains.kotlin.fir.analysis.checkers.unsubstitutedScope
|
||||
import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors
|
||||
import org.jetbrains.kotlin.fir.analysis.getSourceForImportSegment
|
||||
import org.jetbrains.kotlin.fir.declarations.*
|
||||
import org.jetbrains.kotlin.fir.declarations.utils.isEnumClass
|
||||
import org.jetbrains.kotlin.fir.declarations.utils.isOperator
|
||||
import org.jetbrains.kotlin.fir.declarations.utils.isStatic
|
||||
import org.jetbrains.kotlin.fir.declarations.utils.visibility
|
||||
import org.jetbrains.kotlin.fir.resolve.providers.firProvider
|
||||
import org.jetbrains.kotlin.fir.resolve.providers.getContainingFile
|
||||
import org.jetbrains.kotlin.fir.resolve.providers.symbolProvider
|
||||
import org.jetbrains.kotlin.fir.scopes.FirContainingNamesAwareScope
|
||||
import org.jetbrains.kotlin.fir.scopes.impl.declaredMemberScope
|
||||
import org.jetbrains.kotlin.fir.symbols.SymbolInternals
|
||||
import org.jetbrains.kotlin.fir.symbols.impl.*
|
||||
import org.jetbrains.kotlin.fir.visibilityChecker
|
||||
import org.jetbrains.kotlin.name.ClassId
|
||||
import org.jetbrains.kotlin.name.Name
|
||||
import org.jetbrains.kotlin.types.expressions.OperatorConventions
|
||||
import org.jetbrains.kotlin.utils.addToStdlib.filterIsInstanceWithChecker
|
||||
|
||||
@OptIn(SymbolInternals::class)
|
||||
object FirImportsChecker : FirFileChecker() {
|
||||
override fun check(declaration: FirFile, context: CheckerContext, reporter: DiagnosticReporter) {
|
||||
declaration.imports.forEach { import ->
|
||||
@@ -65,12 +73,27 @@ object FirImportsChecker : FirFileChecker() {
|
||||
if (parentClassId != null) {
|
||||
val parentClassSymbol = parentClassId.resolveToClass(context) ?: return
|
||||
|
||||
fun reportInvisibleParentClasses(classSymbol: FirRegularClassSymbol, depth: Int) {
|
||||
if (!classSymbol.fir.isVisible(context)) {
|
||||
val source = import.getSourceForImportSegment(indexFromLast = depth)
|
||||
reporter.reportOn(source, FirErrors.INVISIBLE_REFERENCE, classSymbol, context)
|
||||
}
|
||||
|
||||
classSymbol.classId.outerClassId?.resolveToClass(context)?.let { reportInvisibleParentClasses(it, depth + 1) }
|
||||
}
|
||||
|
||||
reportInvisibleParentClasses(parentClassSymbol, 1)
|
||||
|
||||
when (val status = parentClassSymbol.getImportStatusOfCallableMembers(context, importedName)) {
|
||||
ImportStatus.OK -> return
|
||||
is ImportStatus.Invisible -> {
|
||||
val source = import.getSourceForImportSegment(0)
|
||||
reporter.reportOn(source, FirErrors.INVISIBLE_REFERENCE, status.symbol, context)
|
||||
}
|
||||
else -> {
|
||||
val classId = parentClassSymbol.classId.createNestedClassId(importedName)
|
||||
if (symbolProvider.getClassLikeSymbolByClassId(classId) != null) return
|
||||
if (status == ImportStatus.UNRESOLVED) {
|
||||
if (status == ImportStatus.Unresolved) {
|
||||
reporter.reportOn(import.source, FirErrors.UNRESOLVED_IMPORT, importedName.asString(), context)
|
||||
} else {
|
||||
reporter.reportOn(import.source, FirErrors.CANNOT_BE_IMPORTED, importedName, context)
|
||||
@@ -79,22 +102,54 @@ object FirImportsChecker : FirFileChecker() {
|
||||
}
|
||||
return
|
||||
}
|
||||
when {
|
||||
ClassId.topLevel(importedFqName).resolveToClass(context) != null -> return
|
||||
// Note: two checks below are both heavyweight, so we should do them lazily!
|
||||
symbolProvider.getTopLevelCallableSymbols(importedFqName.parent(), importedName).isNotEmpty() -> return
|
||||
symbolProvider.getPackage(importedFqName) != null -> reporter.reportOn(
|
||||
import.source,
|
||||
FirErrors.PACKAGE_CANNOT_BE_IMPORTED,
|
||||
context
|
||||
)
|
||||
else -> reporter.reportOn(
|
||||
import.source,
|
||||
FirErrors.UNRESOLVED_IMPORT,
|
||||
importedName.asString(),
|
||||
context,
|
||||
)
|
||||
|
||||
val resolvedClassSymbol = ClassId.topLevel(importedFqName).resolveToClass(context)
|
||||
|
||||
if (resolvedClassSymbol != null) {
|
||||
if (!resolvedClassSymbol.fir.isVisible(context)) {
|
||||
reporter.reportOn(import.getSourceForImportSegment(0), FirErrors.INVISIBLE_REFERENCE, resolvedClassSymbol, context)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// Note: two checks below are both heavyweight, so we should do them lazily!
|
||||
|
||||
val topLevelCallableSymbol = symbolProvider.getTopLevelCallableSymbols(importedFqName.parent(), importedName)
|
||||
if (topLevelCallableSymbol.isNotEmpty()) {
|
||||
if (topLevelCallableSymbol.none { it.fir.isVisible(context) }) {
|
||||
val source = import.getSourceForImportSegment(0)
|
||||
reporter.reportOn(source, FirErrors.INVISIBLE_REFERENCE, topLevelCallableSymbol.first(), context)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if (symbolProvider.getPackage(importedFqName) != null) {
|
||||
reporter.reportOn(import.source, FirErrors.PACKAGE_CANNOT_BE_IMPORTED, context)
|
||||
} else {
|
||||
reporter.reportOn(import.source, FirErrors.UNRESOLVED_IMPORT, importedName.asString(), context)
|
||||
}
|
||||
}
|
||||
|
||||
private fun FirMemberDeclaration.isVisible(context: CheckerContext): Boolean {
|
||||
val useSiteFile = context.containingFile ?: return false
|
||||
|
||||
val visibility = visibility
|
||||
|
||||
if (visibility != Visibilities.Unknown && !visibility.mustCheckInImports()) return true
|
||||
if (visibility == Visibilities.Private || visibility == Visibilities.PrivateToThis) {
|
||||
return useSiteFile == context.session.firProvider.getContainingFile(symbol)
|
||||
}
|
||||
|
||||
return context.session.visibilityChecker.isVisible(
|
||||
this,
|
||||
context.session,
|
||||
useSiteFile,
|
||||
emptyList(),
|
||||
null,
|
||||
skipCheckForContainingClassVisibility = true,
|
||||
)
|
||||
}
|
||||
|
||||
private fun checkConflictingImports(imports: List<FirImport>, context: CheckerContext, reporter: DiagnosticReporter) {
|
||||
@@ -172,74 +227,63 @@ object FirImportsChecker : FirFileChecker() {
|
||||
return result
|
||||
}
|
||||
|
||||
private enum class ImportStatus {
|
||||
OK,
|
||||
CANNOT_BE_IMPORTED,
|
||||
UNRESOLVED
|
||||
private sealed class ImportStatus {
|
||||
data object OK : ImportStatus()
|
||||
data class Invisible(val symbol: FirCallableSymbol<*>) : ImportStatus()
|
||||
data object CannotBeImported : ImportStatus()
|
||||
data object Unresolved : ImportStatus()
|
||||
}
|
||||
|
||||
@OptIn(SymbolInternals::class)
|
||||
private fun FirRegularClassSymbol.getImportStatusOfCallableMembers(context: CheckerContext, name: Name): ImportStatus {
|
||||
return if (classKind.isSingleton) {
|
||||
getImportStatusOfCallableMembersFromSingleton(context, name)
|
||||
// Use declaredMemberScope first because it's faster, and it's relatively rare to import members declared from super types.
|
||||
val scopes = listOf(context.session.declaredMemberScope(this), unsubstitutedScope(context))
|
||||
getImportStatus(scopes, context, name) { true }
|
||||
} else {
|
||||
getImportStatusOfCallableMembersFromNonSingleton(context, name)
|
||||
val scopes = listOfNotNull(
|
||||
// We first try resolution with declaredMemberScope because it's faster and typically imported members are not from
|
||||
// super types.
|
||||
context.session.declaredMemberScope(this),
|
||||
|
||||
// Next, we try static scope, which can provide static (Java) members from super classes. Note that it's not available
|
||||
// for pure Kotlin classes.
|
||||
fir.staticScope(context.sessionHolder),
|
||||
|
||||
// Finally, we fall back to unsubstitutedScope to catch all
|
||||
unsubstitutedScope(context)
|
||||
)
|
||||
getImportStatus(scopes, context, name) { it.isStatic }
|
||||
}
|
||||
}
|
||||
|
||||
private fun FirRegularClassSymbol.getImportStatusOfCallableMembersFromSingleton(
|
||||
private inline fun getImportStatus(
|
||||
scopes: List<FirContainingNamesAwareScope>,
|
||||
context: CheckerContext,
|
||||
name: Name,
|
||||
crossinline isApplicable: (FirCallableSymbol<*>) -> Boolean
|
||||
): ImportStatus {
|
||||
// Use declaredMemberScope first because it's faster and it's relatively rare to import members declared from super types.
|
||||
for (scope in listOf(context.session.declaredMemberScope(this), unsubstitutedScope(context))) {
|
||||
var found = false
|
||||
scope.processFunctionsByName(name) {
|
||||
found = true
|
||||
}
|
||||
if (found) return ImportStatus.OK
|
||||
|
||||
scope.processPropertiesByName(name) {
|
||||
found = true
|
||||
}
|
||||
if (found) return ImportStatus.OK
|
||||
}
|
||||
return ImportStatus.UNRESOLVED
|
||||
}
|
||||
|
||||
@OptIn(SymbolInternals::class)
|
||||
private fun FirRegularClassSymbol.getImportStatusOfCallableMembersFromNonSingleton(
|
||||
context: CheckerContext,
|
||||
name: Name,
|
||||
): ImportStatus {
|
||||
var hasStatic = false
|
||||
var found = false
|
||||
for (scope in listOfNotNull(
|
||||
// We first try resolution with declaredMemberScope because it's faster and typically imported members are not from
|
||||
// super types.
|
||||
context.session.declaredMemberScope(this),
|
||||
var symbol: FirCallableSymbol<*>? = null
|
||||
|
||||
// Next, we try static scope, which can provide static (Java) members from super classes. Note that it's not available
|
||||
// for pure Kotlin classes.
|
||||
fir.staticScope(context.sessionHolder),
|
||||
|
||||
// Finally, we fallback to unsubstitutedScope to catch all
|
||||
unsubstitutedScope(context)
|
||||
)) {
|
||||
for (scope in scopes) {
|
||||
scope.processFunctionsByName(name) { sym ->
|
||||
if (sym.isStatic) hasStatic = true
|
||||
found = true
|
||||
if (sym.fir.isVisible(context) && isApplicable(sym)) found = true
|
||||
symbol = sym
|
||||
}
|
||||
if (hasStatic) return ImportStatus.OK
|
||||
if (found) return ImportStatus.OK
|
||||
|
||||
scope.processPropertiesByName(name) { sym ->
|
||||
if (sym.isStatic) hasStatic = true
|
||||
found = true
|
||||
if (sym.fir.isVisible(context) && isApplicable(sym)) found = true
|
||||
symbol = sym
|
||||
}
|
||||
if (hasStatic) return ImportStatus.OK
|
||||
if (found) return ImportStatus.OK
|
||||
}
|
||||
|
||||
return when {
|
||||
symbol?.let(isApplicable) == true -> ImportStatus.Invisible(symbol!!)
|
||||
symbol != null -> ImportStatus.CannotBeImported
|
||||
else -> ImportStatus.Unresolved
|
||||
}
|
||||
return if (found) ImportStatus.CANNOT_BE_IMPORTED
|
||||
else ImportStatus.UNRESOLVED
|
||||
}
|
||||
|
||||
private fun checkImportApiStatus(import: FirImport, context: CheckerContext, reporter: DiagnosticReporter) {
|
||||
|
||||
+4
-16
@@ -5,14 +5,12 @@
|
||||
|
||||
package org.jetbrains.kotlin.fir.analysis.checkers.syntax
|
||||
|
||||
import com.intellij.psi.tree.TokenSet
|
||||
import org.jetbrains.kotlin.KtNodeTypes
|
||||
import org.jetbrains.kotlin.KtSourceElement
|
||||
import org.jetbrains.kotlin.diagnostics.DiagnosticReporter
|
||||
import org.jetbrains.kotlin.diagnostics.reportOn
|
||||
import org.jetbrains.kotlin.fir.analysis.checkers.context.CheckerContext
|
||||
import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors
|
||||
import org.jetbrains.kotlin.fir.analysis.getChild
|
||||
import org.jetbrains.kotlin.fir.analysis.getSourceForImportSegment
|
||||
import org.jetbrains.kotlin.fir.declarations.FirErrorImport
|
||||
import org.jetbrains.kotlin.fir.declarations.FirFile
|
||||
import org.jetbrains.kotlin.fir.declarations.collectEnumEntries
|
||||
@@ -35,7 +33,6 @@ object FirUnresolvedInMiddleOfImportChecker : FirDeclarationSyntaxChecker<FirFil
|
||||
when (val diagnostic = import.diagnostic) {
|
||||
is ConeUnresolvedParentInImport -> {
|
||||
val source = import.source ?: return
|
||||
var segmentSource: KtSourceElement? = source.dotQualifiedExpression() ?: return
|
||||
|
||||
val symbolProvider = context.session.symbolProvider
|
||||
val parentClassId = diagnostic.parentClassId
|
||||
@@ -60,13 +57,12 @@ object FirUnresolvedInMiddleOfImportChecker : FirDeclarationSyntaxChecker<FirFil
|
||||
currentClassId = currentClassId.parentClassId
|
||||
errorSegmentIndexFromLast++
|
||||
}
|
||||
|
||||
// Finds the right segment from the last that causes resolution to fail. Note that FirImportResolveTransformer always create
|
||||
// an error import with resolvable package segments. That is, the segment corresponding to the outermost class name is where
|
||||
// resolution failed.
|
||||
for (i in 1..errorSegmentIndexFromLast) {
|
||||
segmentSource = segmentSource?.dotQualifiedExpression()
|
||||
}
|
||||
val unresolvedSource = segmentSource?.selectorExpression() ?: return
|
||||
val unresolvedSource = import.getSourceForImportSegment(errorSegmentIndexFromLast) ?: return
|
||||
|
||||
reporter.reportOn(
|
||||
unresolvedSource,
|
||||
FirErrors.UNRESOLVED_IMPORT,
|
||||
@@ -90,12 +86,4 @@ object FirUnresolvedInMiddleOfImportChecker : FirDeclarationSyntaxChecker<FirFil
|
||||
(symbolProvider.getClassLikeSymbolByClassId(enumClassId) as? FirRegularClassSymbol)?.takeIf { it.isEnumClass } ?: return false
|
||||
return enumClass.collectEnumEntries().any { it.callableId.callableName == classId.shortClassName }
|
||||
}
|
||||
|
||||
private val IMPORT_PARENT_TOKEN_TYPES = TokenSet.create(KtNodeTypes.DOT_QUALIFIED_EXPRESSION, KtNodeTypes.REFERENCE_EXPRESSION)
|
||||
|
||||
private fun KtSourceElement.dotQualifiedExpression(): KtSourceElement? = getChild(IMPORT_PARENT_TOKEN_TYPES, depth = 1)
|
||||
|
||||
private fun KtSourceElement.selectorExpression(): KtSourceElement? =
|
||||
takeIf { it.elementType == KtNodeTypes.REFERENCE_EXPRESSION }
|
||||
?: getChild(KtNodeTypes.REFERENCE_EXPRESSION, depth = 1, reverse = true)
|
||||
}
|
||||
|
||||
@@ -22,7 +22,6 @@ import org.jetbrains.kotlin.fir.resolve.isSubclassOf
|
||||
import org.jetbrains.kotlin.fir.symbols.FirBasedSymbol
|
||||
import org.jetbrains.kotlin.fir.symbols.impl.FirCallableSymbol
|
||||
import org.jetbrains.kotlin.name.FqName
|
||||
import org.jetbrains.kotlin.utils.addToStdlib.lastIsInstanceOrNull
|
||||
|
||||
@NoMutableState
|
||||
object FirJavaVisibilityChecker : FirVisibilityChecker() {
|
||||
|
||||
@@ -17,16 +17,15 @@ 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.ImplicitReceiverValue
|
||||
import org.jetbrains.kotlin.fir.resolve.calls.ReceiverValue
|
||||
import org.jetbrains.kotlin.fir.resolve.providers.firProvider
|
||||
import org.jetbrains.kotlin.fir.resolve.providers.getContainingFile
|
||||
import org.jetbrains.kotlin.fir.resolve.providers.symbolProvider
|
||||
import org.jetbrains.kotlin.fir.symbols.ConeClassLikeLookupTag
|
||||
import org.jetbrains.kotlin.fir.symbols.FirBasedSymbol
|
||||
import org.jetbrains.kotlin.fir.symbols.impl.*
|
||||
import org.jetbrains.kotlin.fir.types.*
|
||||
import org.jetbrains.kotlin.name.ClassId
|
||||
import org.jetbrains.kotlin.name.FqName
|
||||
import org.jetbrains.kotlin.types.AbstractTypeChecker
|
||||
|
||||
@@ -237,18 +236,8 @@ abstract class FirVisibilityChecker : FirSessionComponent {
|
||||
if (declaration.moduleData == session.moduleData) {
|
||||
when {
|
||||
ownerLookupTag == null -> {
|
||||
val candidateFile = when (symbol) {
|
||||
is FirSyntheticFunctionSymbol -> {
|
||||
// SAM case
|
||||
val classId = ClassId(symbol.callableId.packageName, symbol.callableId.callableName)
|
||||
provider.getFirClassifierContainerFile(classId)
|
||||
}
|
||||
is FirClassLikeSymbol<*> -> provider.getFirClassifierContainerFileIfAny(symbol)
|
||||
is FirCallableSymbol<*> -> provider.getFirCallableContainerFile(symbol)
|
||||
else -> null
|
||||
}
|
||||
// Top-level: visible in file
|
||||
candidateFile == useSiteFile
|
||||
provider.getContainingFile(symbol) == useSiteFile
|
||||
}
|
||||
declaration is FirConstructor && declaration.isFromSealedClass -> {
|
||||
// Sealed class constructor: visible in same package
|
||||
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.fir.resolve.providers
|
||||
|
||||
import org.jetbrains.kotlin.fir.declarations.FirFile
|
||||
import org.jetbrains.kotlin.fir.resolve.calls.FirSyntheticFunctionSymbol
|
||||
import org.jetbrains.kotlin.fir.symbols.FirBasedSymbol
|
||||
import org.jetbrains.kotlin.fir.symbols.impl.FirCallableSymbol
|
||||
import org.jetbrains.kotlin.fir.symbols.impl.FirClassLikeSymbol
|
||||
import org.jetbrains.kotlin.name.ClassId
|
||||
|
||||
fun FirProvider.getContainingFile(symbol: FirBasedSymbol<*>): FirFile? = when (symbol) {
|
||||
is FirSyntheticFunctionSymbol -> {
|
||||
// SAM case
|
||||
val classId = ClassId(symbol.callableId.packageName, symbol.callableId.callableName)
|
||||
getFirClassifierContainerFile(classId)
|
||||
}
|
||||
is FirClassLikeSymbol<*> -> getFirClassifierContainerFileIfAny(symbol)
|
||||
is FirCallableSymbol<*> -> getFirCallableContainerFile(symbol)
|
||||
else -> null
|
||||
}
|
||||
Reference in New Issue
Block a user