Skip to content

Commit fc13caa

Browse files
sjrdWojciechMazur
authored andcommitted
Move the isAccessible test up to registerSym, instead of isInImport.
That test does not rely on any information dependent on the import selectors. It only relies on information at the use site. Eagerly checking it means we do not put as many symbols into the `usedInScope` set, which is good because it is one of the complexity factors of the unused-imports analysis. [Cherry-picked 8553bfc]
1 parent a6d3114 commit fc13caa

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

compiler/src/dotty/tools/dotc/transform/CheckUnused.scala

+12-11
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ object CheckUnused:
367367
*
368368
* See the `isAccessibleAsIdent` extension method below in the file
369369
*/
370-
private val usedInScope = MutStack(MutSet[(Symbol,Boolean, Option[Name], Boolean)]())
370+
private val usedInScope = MutStack(MutSet[(Symbol, Option[Name], Boolean)]())
371371
private val usedInPosition = MutMap.empty[Name, MutSet[Symbol]]
372372
/* unused import collected during traversal */
373373
private val unusedImport = MutList.empty[ImportSelectorData]
@@ -415,12 +415,16 @@ object CheckUnused:
415415
if sym.isConstructor then
416416
registerUsed(sym.owner, None, includeForImport) // constructor are "implicitly" imported with the class
417417
else
418-
val accessibleAsIdent = sym.isAccessibleAsIdent
418+
// If the symbol is accessible in this scope without an import, do not register it for unused import analysis
419+
val includeForImport1 =
420+
includeForImport
421+
&& (name.exists(_.toTermName != sym.name.toTermName) || !sym.isAccessibleAsIdent)
422+
419423
def addIfExists(sym: Symbol): Unit =
420424
if sym.exists then
421425
usedDef += sym
422-
if includeForImport then
423-
usedInScope.top += ((sym, accessibleAsIdent, name, isDerived))
426+
if includeForImport1 then
427+
usedInScope.top += ((sym, name, isDerived))
424428
addIfExists(sym)
425429
addIfExists(sym.companionModule)
426430
addIfExists(sym.companionClass)
@@ -504,9 +508,9 @@ object CheckUnused:
504508
val selDatas = impInScope.pop()
505509

506510
for usedInfo <- usedInfos do
507-
val (sym, isAccessible, optName, isDerived) = usedInfo
511+
val (sym, optName, isDerived) = usedInfo
508512
val usedData = selDatas.find { selData =>
509-
sym.isInImport(selData, isAccessible, optName, isDerived)
513+
sym.isInImport(selData, optName, isDerived)
510514
}
511515
usedData match
512516
case Some(data) =>
@@ -700,15 +704,12 @@ object CheckUnused:
700704
}
701705

702706
/** Given an import and accessibility, return selector that matches import<->symbol */
703-
private def isInImport(selData: ImportSelectorData, isAccessible: Boolean, altName: Option[Name], isDerived: Boolean)(using Context): Boolean =
707+
private def isInImport(selData: ImportSelectorData, altName: Option[Name], isDerived: Boolean)(using Context): Boolean =
704708
assert(sym.exists)
705709

706710
val selector = selData.selector
707711

708-
if isAccessible && !altName.exists(_.toTermName != sym.name.toTermName) then
709-
// Even if this import matches, it is pointless because the symbol would be accessible anyway
710-
false
711-
else if !selector.isWildcard then
712+
if !selector.isWildcard then
712713
if altName.exists(explicitName => selector.rename != explicitName.toTermName) then
713714
// if there is an explicit name, it must match
714715
false

0 commit comments

Comments
 (0)