Skip to content

Commit 0739710

Browse files
committed
Exclude synthetic this.m, Any.m from import lookup
1 parent e592b37 commit 0739710

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ class CheckUnused private (phaseMode: PhaseMode, suffix: String) extends MiniPha
6666
// import x.y; y may be rewritten x.y, also import x.z as y
6767
override def transformSelect(tree: Select)(using Context): tree.type =
6868
val name = tree.removeAttachment(OriginalName).getOrElse(nme.NO_NAME)
69+
inline def isImportable = tree.qualifier.srcPos.isSynthetic
70+
&& tree.qualifier.tpe.match
71+
case ThisType(_) | SuperType(_, _) => false
72+
case qualtpe => qualtpe.isStable
6973
if tree.srcPos.isSynthetic && tree.symbol == defn.TypeTest_unapply then
7074
tree.qualifier.tpe.underlying.finalResultType match
7175
case AppliedType(tycon, args) =>
@@ -76,7 +80,7 @@ class CheckUnused private (phaseMode: PhaseMode, suffix: String) extends MiniPha
7680
val target = res.dealias.typeSymbol
7781
resolveUsage(target, target.name, res.importPrefix.skipPackageObject) // case _: T =>
7882
case _ =>
79-
else if tree.qualifier.srcPos.isSynthetic && tree.qualifier.tpe.isStable || name.exists(_ != tree.symbol.name) then
83+
else if isImportable || name.exists(_ != tree.symbol.name) then
8084
if !ignoreTree(tree) then
8185
resolveUsage(tree.symbol, name, tree.qualifier.tpe)
8286
else
@@ -313,7 +317,8 @@ class CheckUnused private (phaseMode: PhaseMode, suffix: String) extends MiniPha
313317
case none =>
314318

315319
// Avoid spurious NoSymbol and also primary ctors which are never warned about.
316-
if !sym.exists || sym.isPrimaryConstructor then return
320+
// Selections C.this.toString should be already excluded, but backtopped here.
321+
if !sym.exists || sym.isPrimaryConstructor || defn.topClasses(sym.owner) then return
317322

318323
// Find the innermost, highest precedence. Contexts have no nesting levels but assume correctness.
319324
// If the sym is an enclosing definition (the owner of a context), it does not count toward usages.
@@ -451,7 +456,6 @@ object CheckUnused:
451456
if (tree.symbol ne NoSymbol) && !tree.name.isWildcard then
452457
defs.addOne((tree.symbol, tree.namePos))
453458
case _ =>
454-
//println(s"OTHER ${tree.symbol}")
455459
if tree.symbol ne NoSymbol then
456460
defs.addOne((tree.symbol, tree.srcPos))
457461

0 commit comments

Comments
 (0)