fixup! [FIR] KT-58719: Check all imported declarations for visibility
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 21e3d09..2cbaaaf 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
@@ -28,7 +28,6 @@
 import org.jetbrains.kotlin.fir.symbols.impl.*
 import org.jetbrains.kotlin.fir.visibilityChecker
 import org.jetbrains.kotlin.name.ClassId
-import org.jetbrains.kotlin.name.FqName
 import org.jetbrains.kotlin.name.Name
 import org.jetbrains.kotlin.types.expressions.OperatorConventions
 import org.jetbrains.kotlin.utils.addToStdlib.filterIsInstanceWithChecker
@@ -104,14 +103,33 @@
             return
         }
 
-        val possibleDeclarations = importedFqName.resolveToDeclarations(context)
+        var resolvedDeclaration: FirMemberDeclaration? = null
 
-        if (possibleDeclarations.isNotEmpty()) {
-            if (possibleDeclarations.none { it.isVisible(context) }) {
-                val source = import.getSourceForImportSegment(0)
-                reporter.reportOn(source, FirErrors.INVISIBLE_REFERENCE, possibleDeclarations.first().symbol, context)
+        ClassId.topLevel(importedFqName).resolveToClass(context)?.let {
+            resolvedDeclaration = it.fir
+
+            if (it.fir.isVisible(context)) {
+                return
+            }
+        }
+
+        // Note: two checks below are both heavyweight, so we should do them lazily!
+
+        val topLevelCallableSymbol = symbolProvider.getTopLevelCallableSymbols(importedFqName.parent(), importedName)
+
+        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
         }
 
@@ -203,12 +221,6 @@
         }
     }
 
-    private fun FqName.resolveToDeclarations(context: CheckerContext): List<FirMemberDeclaration> {
-        val resolvedClassSymbol = ClassId.topLevel(this).resolveToClass(context)?.fir
-        val resolvedCallableSymbols = context.session.symbolProvider.getTopLevelCallableSymbols(parent(), shortName())
-        return listOfNotNull(resolvedClassSymbol) + resolvedCallableSymbols.map { it.fir }
-    }
-
     private fun FirRegularClassSymbol.hasFunction(
         context: CheckerContext,
         name: Name,