Skip to content

Commit

Permalink
Fix false positive in WUnused for renamed path-dependent imports (2) (#…
Browse files Browse the repository at this point in the history
…18617)

Resolve #18366
  • Loading branch information
odersky authored Oct 3, 2023
2 parents 9464bf0 + cde97d8 commit ebb3a11
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
12 changes: 7 additions & 5 deletions compiler/src/dotty/tools/dotc/transform/CheckUnused.scala
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ object CheckUnused:
* - usage
*/
private class UnusedData:
import collection.mutable.{Set => MutSet, Map => MutMap, Stack => MutStack}
import collection.mutable.{Set => MutSet, Map => MutMap, Stack => MutStack, ListBuffer => MutList}
import UnusedData.*

/** The current scope during the tree traversal */
Expand All @@ -346,7 +346,7 @@ object CheckUnused:
var unusedAggregate: Option[UnusedResult] = None

/* IMPORTS */
private val impInScope = MutStack(MutSet[tpd.Import]())
private val impInScope = MutStack(MutList[tpd.Import]())
/**
* We store the symbol along with their accessibility without import.
* Accessibility to their definition in outer context/scope
Expand Down Expand Up @@ -449,7 +449,7 @@ object CheckUnused:
def pushScope(newScopeType: ScopeType): Unit =
// unused imports :
currScopeType.push(newScopeType)
impInScope.push(MutSet())
impInScope.push(MutList())
usedInScope.push(MutSet())

def registerSetVar(sym: Symbol): Unit =
Expand Down Expand Up @@ -671,8 +671,10 @@ object CheckUnused:
val simpleSelections = qual.tpe.member(sym.name).alternatives
val typeSelections = sels.flatMap(n => qual.tpe.member(n.name.toTypeName).alternatives)
val termSelections = sels.flatMap(n => qual.tpe.member(n.name.toTermName).alternatives)
val sameTermPath = qual.isTerm && sym.exists && sym.owner.isType && qual.tpe.typeSymbol == sym.owner.asType
val selectionsToDealias = typeSelections ::: termSelections
val qualHasSymbol = simpleSelections.map(_.symbol).contains(sym) || (simpleSelections ::: selectionsToDealias).map(_.symbol).map(dealias).contains(dealiasedSym)
val renamedSelection = if sameTermPath then sels.find(sel => sel.imported.name == sym.name) else None
val qualHasSymbol = simpleSelections.map(_.symbol).contains(sym) || (simpleSelections ::: selectionsToDealias).map(_.symbol).map(dealias).contains(dealiasedSym) || renamedSelection.isDefined
def selector = sels.find(sel => (sel.name.toTermName == sym.name || sel.name.toTypeName == sym.name) && symName.map(n => n.toTermName == sel.rename).getOrElse(true))
def dealiasedSelector = if(isDerived) sels.flatMap(sel => selectionsToDealias.map(m => (sel, m.symbol))).collect {
case (sel, sym) if dealias(sym) == dealiasedSym => sel
Expand All @@ -682,7 +684,7 @@ object CheckUnused:
else None
def wildcard = sels.find(sel => sel.isWildcard && ((sym.is(Given) == sel.isGiven && sel.bound.isEmpty) || sym.is(Implicit)))
if qualHasSymbol && (!isAccessible || sym.isRenamedSymbol(symName)) && sym.exists then
selector.orElse(dealiasedSelector).orElse(givenSelector).orElse(wildcard) // selector with name or wildcard (or given)
selector.orElse(dealiasedSelector).orElse(givenSelector).orElse(wildcard).orElse(renamedSelection) // selector with name or wildcard (or given)
else
None

Expand Down
10 changes: 10 additions & 0 deletions tests/pos/i18366.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//> using options -Xfatal-warnings -Wunused:all

trait Builder {
def foo(): Unit
}

def repro =
val builder: Builder = ???
import builder.{foo => bar}
bar()

0 comments on commit ebb3a11

Please sign in to comment.