@@ -14,7 +14,6 @@ import dotty.tools.dotc.report
14
14
import dotty .tools .dotc .reporting .Message
15
15
import dotty .tools .dotc .typer .ImportInfo
16
16
import dotty .tools .dotc .util .Property
17
- import dotty .tools .dotc .transform .CheckUnused .UnusedData .UnusedResult
18
17
import dotty .tools .dotc .core .Mode
19
18
import dotty .tools .dotc .core .Types .TypeTraverser
20
19
import dotty .tools .dotc .core .Types .Type
@@ -34,6 +33,10 @@ import dotty.tools.dotc.core.Names.Name
34
33
class CheckUnused extends Phase :
35
34
import CheckUnused .UnusedData
36
35
36
+ /**
37
+ * The key used to retrieve the "unused entity" analysis metadata,
38
+ * from the compilation `Context`
39
+ */
37
40
private val _key = Property .Key [UnusedData ]
38
41
39
42
override def phaseName : String = CheckUnused .phaseName
@@ -98,11 +101,13 @@ class CheckUnused extends Phase:
98
101
end traverse
99
102
end traverser
100
103
104
+ /** This is a type traverser which catch some special Types not traversed by the term traverser above */
101
105
private def typeTraverser (dt : (UnusedData => Any ) => Unit )(using Context ) = new TypeTraverser :
102
106
override def traverse (tp : Type ): Unit = tp match
103
107
case AnnotatedType (_, annot) => dt(_.registerUsed(annot.symbol, None ))
104
108
case _ => traverseChildren(tp)
105
109
110
+ /** Do the actual reporting given the result of the anaylsis */
106
111
private def reportUnused (res : UnusedData .UnusedResult )(using Context ): Unit =
107
112
import CheckUnused .WarnTypes
108
113
res.warnings.foreach { s =>
@@ -127,7 +132,7 @@ object CheckUnused:
127
132
val phaseName : String = " checkUnused"
128
133
val description : String = " check for unused elements"
129
134
130
- enum WarnTypes :
135
+ private enum WarnTypes :
131
136
case Imports
132
137
case LocalDefs
133
138
case ExplicitParams
@@ -142,6 +147,7 @@ object CheckUnused:
142
147
* - usage
143
148
*/
144
149
private class UnusedData :
150
+ import dotty .tools .dotc .transform .CheckUnused .UnusedData .UnusedResult
145
151
import collection .mutable .{Set => MutSet , Map => MutMap , Stack => MutStack }
146
152
import dotty .tools .dotc .core .Symbols .Symbol
147
153
import UnusedData .ScopeType
@@ -195,7 +201,12 @@ object CheckUnused:
195
201
val annotSym = sym.denot.annotations.map(_.symbol)
196
202
registerUsed(annotSym)
197
203
198
- /** Register a found (used) symbol */
204
+ /**
205
+ * Register a found (used) symbol along with its name
206
+ *
207
+ * The optional name will be used to target the right import
208
+ * as the same element can be imported with different renaming
209
+ */
199
210
def registerUsed (sym : Symbol , name : Option [Name ])(using Context ): Unit =
200
211
if ! isConstructorOfSynth(sym) then
201
212
usedInScope.top += ((sym, sym.isAccessibleAsIdent, name))
@@ -336,9 +347,16 @@ object CheckUnused:
336
347
private def isConstructorOfSynth (sym : Symbol )(using Context ): Boolean =
337
348
sym.exists && sym.isConstructor && sym.owner.isPackageObject && sym.owner.is(Synthetic )
338
349
350
+ /**
351
+ * This is used to avoid reporting the parameters of the synthetic main method
352
+ * generated by `@main`
353
+ */
339
354
private def isSyntheticMainParam (sym : Symbol )(using Context ): Boolean =
340
355
sym.exists && ctx.platform.isMainMethod(sym.owner) && sym.owner.is(Synthetic )
341
356
357
+ /**
358
+ * This is used to ignore exclusion imports (i.e. import `qual`.{`member` => _})
359
+ */
342
360
private def isImportExclusion (sel : ImportSelector ): Boolean = sel.renamed match
343
361
case untpd.Ident (name) => name == StdNames .nme.WILDCARD
344
362
case _ => false
@@ -376,21 +394,22 @@ object CheckUnused:
376
394
selector.orElse(wildcard) // selector with name or wildcard (or given)
377
395
else
378
396
None
379
-
380
397
end UnusedData
381
398
382
- object UnusedData :
399
+ private object UnusedData :
383
400
enum ScopeType :
384
401
case Local
385
402
case Template
386
403
case Other
387
404
388
405
object ScopeType :
406
+ /** return the scope corresponding to the enclosing scope of the given tree */
389
407
def fromTree (tree : tpd.Tree ): ScopeType = tree match
390
408
case _:tpd.Template => Template
391
409
case _:tpd.Block => Local
392
410
case _ => Other
393
411
412
+ /** A container for the results of the used elements analysis */
394
413
case class UnusedResult (warnings : List [(dotty.tools.dotc.util.SrcPos , WarnTypes )], usedImports : List [(tpd.Import , untpd.ImportSelector )])
395
414
end CheckUnused
396
415
0 commit comments