Skip to content

Commit a127ed0

Browse files
committed
Refactor isInImport
1 parent ea2ed3c commit a127ed0

File tree

1 file changed

+22
-19
lines changed

1 file changed

+22
-19
lines changed

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

+22-19
Original file line numberDiff line numberDiff line change
@@ -680,36 +680,39 @@ object CheckUnused:
680680
}
681681

682682
/** Given an import and accessibility, return selector that matches import<->symbol */
683-
private def isInImport(imp: tpd.Import, isAccessible: Boolean, symName: Option[Name], isDerived: Boolean)(using Context): Option[ImportSelector] =
683+
private def isInImport(imp: tpd.Import, isAccessible: Boolean, altName: Option[Name], isDerived: Boolean)(using Context): Option[ImportSelector] =
684684
val tpd.Import(qual, sels) = imp
685-
val dealiasedSym = dealias(sym)
686-
val simpleSelections = qual.tpe.member(sym.name).alternatives
687-
val typeSelections = sels.flatMap(n => qual.tpe.member(n.name.toTypeName).alternatives)
688-
val termSelections = sels.flatMap(n => qual.tpe.member(n.name.toTermName).alternatives)
689-
val sameTermPath = qual.isTerm && sym.exists && sym.owner.isType && qual.tpe.typeSymbol == sym.owner.asType
690-
val selectionsToDealias = typeSelections ::: termSelections
691-
val renamedSelection = if sameTermPath then sels.find(sel => sel.imported.name == sym.name) else None
692-
val qualHasSymbol = simpleSelections.map(_.symbol).contains(sym) || (simpleSelections ::: selectionsToDealias).map(_.symbol).map(dealias).contains(dealiasedSym) || renamedSelection.isDefined
693-
def selector = sels.find(sel => (sel.name.toTermName == sym.name || sel.name.toTypeName == sym.name) && symName.map(n => n.toTermName == sel.rename).getOrElse(true))
694-
def dealiasedSelector = if(isDerived) sels.flatMap(sel => selectionsToDealias.map(m => (sel, m.symbol))).collect {
695-
case (sel, sym) if dealias(sym) == dealiasedSym => sel
696-
}.headOption else None
685+
val qualTpe = qual.tpe
686+
val dealiasedSym = sym.dealias
687+
val simpleSelections = qualTpe.member(sym.name).alternatives
688+
val selectionsToDealias = sels.flatMap(sel =>
689+
qualTpe.member(sel.name.toTypeName).alternatives
690+
::: qualTpe.member(sel.name.toTermName).alternatives)
691+
def qualHasSymbol = simpleSelections.map(_.symbol).contains(sym) || (simpleSelections ::: selectionsToDealias).map(_.symbol).map(_.dealias).contains(dealiasedSym)
692+
def selector = sels.find(sel => (sel.name.toTermName == sym.name || sel.name.toTypeName == sym.name) && altName.map(n => n.toTermName == sel.rename).getOrElse(true))
693+
def dealiasedSelector =
694+
if isDerived then
695+
sels.flatMap(sel => selectionsToDealias.map(m => (sel, m.symbol))).collect {
696+
case (sel, sym) if sym.dealias == dealiasedSym => sel
697+
}.headOption
698+
else None
697699
def givenSelector = if sym.is(Given) || sym.is(Implicit)
698700
then sels.filter(sel => sel.isGiven && !sel.bound.isEmpty).find(sel => sel.boundTpe =:= sym.info)
699701
else None
700702
def wildcard = sels.find(sel => sel.isWildcard && ((sym.is(Given) == sel.isGiven && sel.bound.isEmpty) || sym.is(Implicit)))
701-
if qualHasSymbol && (!isAccessible || sym.isRenamedSymbol(symName)) && sym.exists then
702-
selector.orElse(dealiasedSelector).orElse(givenSelector).orElse(wildcard).orElse(renamedSelection) // selector with name or wildcard (or given)
703+
if sym.exists && qualHasSymbol && (!isAccessible || sym.isRenamedSymbol(altName)) then
704+
selector.orElse(dealiasedSelector).orElse(givenSelector).orElse(wildcard) // selector with name or wildcard (or given)
703705
else
704706
None
705707

706708
private def isRenamedSymbol(symNameInScope: Option[Name])(using Context) =
707709
sym.name != nme.NO_NAME && symNameInScope.exists(_.toSimpleName != sym.name.toSimpleName)
708710

709-
private def dealias(symbol: Symbol)(using Context): Symbol =
710-
if(symbol.isType && symbol.asType.denot.isAliasType) then
711-
symbol.asType.typeRef.dealias.typeSymbol
712-
else symbol
711+
private def dealias(using Context): Symbol =
712+
if sym.isType && sym.asType.denot.isAliasType then
713+
sym.asType.typeRef.dealias.typeSymbol
714+
else sym
715+
713716
/** Annotated with @unused */
714717
private def isUnusedAnnot(using Context): Boolean =
715718
sym.annotations.exists(a => a.symbol == ctx.definitions.UnusedAnnot)

0 commit comments

Comments
 (0)