Skip to content

Commit 1578f06

Browse files
noti0na1WojciechMazur
authored andcommitted
Refactor isInImport
[Cherry-picked e6b00c3]
1 parent b120b36 commit 1578f06

File tree

1 file changed

+22
-19
lines changed

1 file changed

+22
-19
lines changed

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

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -683,36 +683,39 @@ object CheckUnused:
683683
}
684684

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

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

712-
private def dealias(symbol: Symbol)(using Context): Symbol =
713-
if(symbol.isType && symbol.asType.denot.isAliasType) then
714-
symbol.asType.typeRef.dealias.typeSymbol
715-
else symbol
714+
private def dealias(using Context): Symbol =
715+
if sym.isType && sym.asType.denot.isAliasType then
716+
sym.asType.typeRef.dealias.typeSymbol
717+
else sym
718+
716719
/** Annotated with @unused */
717720
private def isUnusedAnnot(using Context): Boolean =
718721
sym.annotations.exists(a => a.symbol == ctx.definitions.UnusedAnnot)

0 commit comments

Comments
 (0)