@@ -7,7 +7,7 @@ import dotty.tools.dotc.ast.untpd.ImportSelector
7
7
import dotty .tools .dotc .config .ScalaSettings
8
8
import dotty .tools .dotc .core .Contexts .*
9
9
import dotty .tools .dotc .core .Decorators .{em , i }
10
- import dotty .tools .dotc .core .Flags .{Given , GivenVal , Param , Private }
10
+ import dotty .tools .dotc .core .Flags .{Given , GivenVal , Param , Private , SelfName }
11
11
import dotty .tools .dotc .core .Phases .Phase
12
12
import dotty .tools .dotc .core .StdNames
13
13
import dotty .tools .dotc .report
@@ -55,29 +55,32 @@ class CheckUnused extends Phase:
55
55
import tpd ._
56
56
import UnusedData .ScopeType
57
57
58
- override def traverse (tree : tpd.Tree )(using Context ): Unit = tree match
59
- case imp: tpd.Import =>
60
- ctx.property(_key).foreach(_.registerImport(imp))
61
- case ident : Ident =>
62
- ctx.property(_key).foreach(_.registerUsed(ident.symbol))
63
- traverseChildren(tree)
64
- case sel : Select =>
65
- ctx.property(_key).foreach(_.registerUsed(sel.symbol))
66
- traverseChildren(tree)
67
- case _ : (tpd.Block | tpd.Template ) =>
68
- ctx.property(_key).foreach { ud =>
69
- ud.inNewScope(ScopeType .fromTree(tree))(traverseChildren(tree))
70
- }
71
- case t: tpd.ValDef =>
72
- ctx.property(_key).foreach(_.registerDef(t))
73
- traverseChildren(tree)
74
- case t: tpd.DefDef =>
75
- ctx.property(_key).foreach(_.registerDef(t))
76
- traverseChildren(tree)
77
- case t : tpd.Bind =>
78
- ctx.property(_key).foreach(_.registerPatVar(t))
79
- traverseChildren(tree)
80
- case _ => traverseChildren(tree)
58
+ override def traverse (tree : tpd.Tree )(using Context ): Unit =
59
+ if tree.isDef then // register the annotations for usage
60
+ ctx.property(_key).foreach(_.registerUsedAnnotation(tree.symbol))
61
+ tree match
62
+ case imp: tpd.Import =>
63
+ ctx.property(_key).foreach(_.registerImport(imp))
64
+ case ident : Ident =>
65
+ ctx.property(_key).foreach(_.registerUsed(ident.symbol))
66
+ traverseChildren(tree)
67
+ case sel : Select =>
68
+ ctx.property(_key).foreach(_.registerUsed(sel.symbol))
69
+ traverseChildren(tree)
70
+ case _ : (tpd.Block | tpd.Template ) =>
71
+ ctx.property(_key).foreach { ud =>
72
+ ud.inNewScope(ScopeType .fromTree(tree))(traverseChildren(tree))
73
+ }
74
+ case t: tpd.ValDef =>
75
+ ctx.property(_key).foreach(_.registerDef(t))
76
+ traverseChildren(tree)
77
+ case t: tpd.DefDef =>
78
+ ctx.property(_key).foreach(_.registerDef(t))
79
+ traverseChildren(tree)
80
+ case t : tpd.Bind =>
81
+ ctx.property(_key).foreach(_.registerPatVar(t))
82
+ traverseChildren(tree)
83
+ case _ => traverseChildren(tree)
81
84
82
85
}
83
86
@@ -162,10 +165,21 @@ object CheckUnused:
162
165
case untpd.Ident (name) => name == StdNames .nme.WILDCARD
163
166
case _ => false
164
167
165
- /** Register the id of a found (used) symbol */
166
- def registerUsed (id : Symbol ): Unit =
167
- usedImports.top += id
168
- usedDef += id
168
+
169
+ /** Register all annotations of this symbol's denotation */
170
+ def registerUsedAnnotation (sym : Symbol )(using Context ): Unit =
171
+ val annotSym = sym.denot.annotations.map(_.symbol)
172
+ registerUsed(annotSym)
173
+
174
+ /** Register a found (used) symbol */
175
+ def registerUsed (sym : Symbol ): Unit =
176
+ usedImports.top += sym
177
+ usedDef += sym
178
+
179
+ /** Register a list of found (used) symbols */
180
+ def registerUsed (sym : Seq [Symbol ]): Unit =
181
+ usedImports.top ++= sym
182
+ usedDef ++= sym
169
183
170
184
/** Register an import */
171
185
def registerImport (imp : tpd.Import )(using Context ): Unit =
@@ -183,9 +197,9 @@ object CheckUnused:
183
197
val typeSymbol = tree.tpe.member(s.name.toTypeName).symbol
184
198
List (termSymbol -> s, typeSymbol -> s)
185
199
}
186
- entries.foreach{(id , sel) =>
187
- map.get(id ) match
188
- case None => map.put(id , ListBuffer (sel))
200
+ entries.foreach{(sym , sel) =>
201
+ map.get(sym ) match
202
+ case None => map.put(sym , ListBuffer (sel))
189
203
case Some (value) => value += sel
190
204
}
191
205
@@ -197,7 +211,7 @@ object CheckUnused:
197
211
explicitParamInScope.top += valOrDef
198
212
else if currScopeType == ScopeType .Local then
199
213
localDefInScope.top += valOrDef
200
- else if currScopeType == ScopeType .Template && valOrDef.symbol.is(Private ) then
214
+ else if currScopeType == ScopeType .Template && valOrDef.symbol.is(Private , butNot = SelfName ) then
201
215
privateDefInScope.top += valOrDef
202
216
203
217
def registerPatVar (patvar : tpd.Bind )(using Context ): Unit =
@@ -227,8 +241,8 @@ object CheckUnused:
227
241
def popScopeImport (): Unit =
228
242
val usedImp = MutSet [ImportSelector ]()
229
243
val poppedImp = impInScope.pop()
230
- val notDefined = usedImports.pop.filter{id =>
231
- poppedImp.remove(id ) match
244
+ val notDefined = usedImports.pop.filter{sym =>
245
+ poppedImp.remove(sym ) match
232
246
case None => true
233
247
case Some (value) =>
234
248
usedImp.addAll(value)
0 commit comments