Skip to content

Commit 63a02dd

Browse files
authored
Exclude synthetic this.m, Any.m from import lookup (#22695)
Follow-up to #22633 which restricts import prefixes to stable prefixes. This commit further excludes `this` and `super`; these are arbitrary synthetic expressions introduced by macros. Noticed at #22690 (comment) which now warns correctly. Requires a unit test. Fixes #22690
2 parents 91985db + fb1fe22 commit 63a02dd

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

Diff for: compiler/src/dotty/tools/dotc/transform/CheckUnused.scala

+8-4
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,10 +80,10 @@ 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)
82-
else
86+
else if !ignoreTree(tree) then
8387
refUsage(tree.symbol)
8488
tree
8589

@@ -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 for eq, etc.
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)