Skip to content

Backport "Append instead of prepending import selectors for the current scope when collecting them in CheckUnused" to 3.3 LTS #182

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions compiler/src/dotty/tools/dotc/transform/CheckUnused.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dotty.tools.dotc.transform

import scala.annotation.tailrec
import scala.collection.mutable

import dotty.tools.uncheckedNN
import dotty.tools.dotc.ast.tpd
Expand All @@ -24,7 +25,7 @@ import dotty.tools.dotc.core.Mode
import dotty.tools.dotc.core.Types.{AnnotatedType, ConstantType, NoType, TermRef, Type, TypeTraverser}
import dotty.tools.dotc.core.Flags.flagsString
import dotty.tools.dotc.core.Flags
import dotty.tools.dotc.core.Names.Name
import dotty.tools.dotc.core.Names.{Name, TermName}
import dotty.tools.dotc.core.NameOps.isReplWrapperName
import dotty.tools.dotc.transform.MegaPhase.MiniPhase
import dotty.tools.dotc.core.Annotations
Expand Down Expand Up @@ -211,7 +212,7 @@ class CheckUnused private (phaseMode: CheckUnused.PhaseMode, suffix: String, _ke
/**
* This traverse is the **main** component of this phase
*
* It traverse the tree the tree and gather the data in the
* It traverses the tree and gathers the data in the
* corresponding context property
*/
private def traverser = new TreeTraverser:
Expand Down Expand Up @@ -456,14 +457,21 @@ object CheckUnused:
val (wildcardSels, nonWildcardSels) = imp.selectors.partition(_.isWildcard)
nonWildcardSels ::: wildcardSels

val excludedMembers: mutable.Set[TermName] = mutable.Set.empty

val newDataInScope =
for sel <- reorderdSelectors yield
val data = new ImportSelectorData(qualTpe, sel)
if shouldSelectorBeReported(imp, sel) || isImportExclusion(sel) || isImportIgnored(imp, sel) then
// Immediately mark the selector as used
data.markUsed()
if isImportExclusion(sel) then
excludedMembers += sel.name
if sel.isWildcard && excludedMembers.nonEmpty then
// mark excluded members for the wildcard import
data.markExcluded(excludedMembers.toSet)
data
impInScope.top.prependAll(newDataInScope)
impInScope.top.appendAll(newDataInScope)
end registerImport

/** Register (or not) some `val` or `def` according to the context, scope and flags */
Expand Down Expand Up @@ -703,7 +711,7 @@ object CheckUnused:

/** Given an import and accessibility, return selector that matches import<->symbol */
private def isInImport(selData: ImportSelectorData, altName: Option[Name], isDerived: Boolean)(using Context): Boolean =
assert(sym.exists)
assert(sym.exists, s"Symbol $sym does not exist")

val selector = selData.selector

Expand All @@ -719,7 +727,10 @@ object CheckUnused:
selData.allSymbolsForNamed.contains(sym)
else
// Wildcard
if !selData.qualTpe.member(sym.name).hasAltWith(_.symbol == sym) then
if selData.excludedMembers.contains(altName.getOrElse(sym.name).toTermName) then
// Wildcard with exclusions that match the symbol
false
else if !selData.qualTpe.member(sym.name).hasAltWith(_.symbol == sym) then
// The qualifier does not have the target symbol as a member
false
else
Expand Down Expand Up @@ -832,11 +843,14 @@ object CheckUnused:

final class ImportSelectorData(val qualTpe: Type, val selector: ImportSelector):
private var myUsed: Boolean = false
var excludedMembers: Set[TermName] = Set.empty

def markUsed(): Unit = myUsed = true

def isUsed: Boolean = myUsed

def markExcluded(excluded: Set[TermName]): Unit = excludedMembers ++= excluded

private var myAllSymbols: Set[Symbol] | Null = null

def allSymbolsForNamed(using Context): Set[Symbol] =
Expand Down
13 changes: 13 additions & 0 deletions tests/warn/i21420.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//> using options -Wunused:imports

object decisions4s{
trait HKD
trait DecisionTable
}

object DiagnosticsExample {
import decisions4s.HKD
val _ = new HKD {}
import decisions4s.*
val _ = new DecisionTable {}
}