From 710a8f4e0f2d63cfc358a78aad2104d19d8bb3c2 Mon Sep 17 00:00:00 2001 From: Nikolay Lunyak Date: Wed, 17 May 2023 15:32:24 +0300 Subject: [PATCH] [FIR] KT-58719: Check all imported declarations for visibility Unfortunately, we have to check callables even if the class is inaccessible. ^KT-58719 Fixed --- .../checkers/declaration/FirImportsChecker.kt | 28 ++++++++++++------- .../tests/invisibleClassInsteadOfFun.fir.kt | 2 +- 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/declaration/FirImportsChecker.kt b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/declaration/FirImportsChecker.kt index 28d79729224..3547bb295b2 100644 --- a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/declaration/FirImportsChecker.kt +++ b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/declaration/FirImportsChecker.kt @@ -103,25 +103,33 @@ object FirImportsChecker : FirFileChecker() { return } - val resolvedClassSymbol = ClassId.topLevel(importedFqName).resolveToClass(context) + var resolvedDeclaration: FirMemberDeclaration? = null - if (resolvedClassSymbol != null) { - if (!resolvedClassSymbol.fir.isVisible(context)) { - reporter.reportOn(import.getSourceForImportSegment(0), FirErrors.INVISIBLE_REFERENCE, resolvedClassSymbol, context) + ClassId.topLevel(importedFqName).resolveToClass(context)?.let { + resolvedDeclaration = it.fir + + if (it.fir.isVisible(context)) { + return } - - 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) + + for (it in topLevelCallableSymbol) { + if (it.fir.isVisible(context)) { + return } + if (resolvedDeclaration == null) { + resolvedDeclaration = it.fir + } + } + + resolvedDeclaration?.let { + val source = import.getSourceForImportSegment(0) + reporter.reportOn(source, FirErrors.INVISIBLE_REFERENCE, it.symbol, context) return } diff --git a/compiler/testData/diagnostics/tests/invisibleClassInsteadOfFun.fir.kt b/compiler/testData/diagnostics/tests/invisibleClassInsteadOfFun.fir.kt index 6bfb90802bf..95a5b4cd941 100644 --- a/compiler/testData/diagnostics/tests/invisibleClassInsteadOfFun.fir.kt +++ b/compiler/testData/diagnostics/tests/invisibleClassInsteadOfFun.fir.kt @@ -14,7 +14,7 @@ fun QueryPagingSource(randomParam: Int) {} package main -import pagind.QueryPagingSource +import pagind.QueryPagingSource fun test() { QueryPagingSource(10)