Skip to content

Commit e2b6b61

Browse files
committed
Clean, refine scope, add comments
- Tidy the code (space, newline,...) - Refine the scope by making more things private - Add explaination and doc comments (mostly to help reviewer)
1 parent e158a9f commit e2b6b61

File tree

1 file changed

+24
-5
lines changed

1 file changed

+24
-5
lines changed

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

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import dotty.tools.dotc.report
1414
import dotty.tools.dotc.reporting.Message
1515
import dotty.tools.dotc.typer.ImportInfo
1616
import dotty.tools.dotc.util.Property
17-
import dotty.tools.dotc.transform.CheckUnused.UnusedData.UnusedResult
1817
import dotty.tools.dotc.core.Mode
1918
import dotty.tools.dotc.core.Types.TypeTraverser
2019
import dotty.tools.dotc.core.Types.Type
@@ -34,6 +33,10 @@ import dotty.tools.dotc.core.Names.Name
3433
class CheckUnused extends Phase:
3534
import CheckUnused.UnusedData
3635

36+
/**
37+
* The key used to retrieve the "unused entity" analysis metadata,
38+
* from the compilation `Context`
39+
*/
3740
private val _key = Property.Key[UnusedData]
3841

3942
override def phaseName: String = CheckUnused.phaseName
@@ -98,11 +101,13 @@ class CheckUnused extends Phase:
98101
end traverse
99102
end traverser
100103

104+
/** This is a type traverser which catch some special Types not traversed by the term traverser above */
101105
private def typeTraverser(dt: (UnusedData => Any) => Unit)(using Context) = new TypeTraverser:
102106
override def traverse(tp: Type): Unit = tp match
103107
case AnnotatedType(_, annot) => dt(_.registerUsed(annot.symbol, None))
104108
case _ => traverseChildren(tp)
105109

110+
/** Do the actual reporting given the result of the anaylsis */
106111
private def reportUnused(res: UnusedData.UnusedResult)(using Context): Unit =
107112
import CheckUnused.WarnTypes
108113
res.warnings.foreach { s =>
@@ -127,7 +132,7 @@ object CheckUnused:
127132
val phaseName: String = "checkUnused"
128133
val description: String = "check for unused elements"
129134

130-
enum WarnTypes:
135+
private enum WarnTypes:
131136
case Imports
132137
case LocalDefs
133138
case ExplicitParams
@@ -142,6 +147,7 @@ object CheckUnused:
142147
* - usage
143148
*/
144149
private class UnusedData:
150+
import dotty.tools.dotc.transform.CheckUnused.UnusedData.UnusedResult
145151
import collection.mutable.{Set => MutSet, Map => MutMap, Stack => MutStack}
146152
import dotty.tools.dotc.core.Symbols.Symbol
147153
import UnusedData.ScopeType
@@ -195,7 +201,12 @@ object CheckUnused:
195201
val annotSym = sym.denot.annotations.map(_.symbol)
196202
registerUsed(annotSym)
197203

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+
*/
199210
def registerUsed(sym: Symbol, name: Option[Name])(using Context): Unit =
200211
if !isConstructorOfSynth(sym) then
201212
usedInScope.top += ((sym, sym.isAccessibleAsIdent, name))
@@ -336,9 +347,16 @@ object CheckUnused:
336347
private def isConstructorOfSynth(sym: Symbol)(using Context): Boolean =
337348
sym.exists && sym.isConstructor && sym.owner.isPackageObject && sym.owner.is(Synthetic)
338349

350+
/**
351+
* This is used to avoid reporting the parameters of the synthetic main method
352+
* generated by `@main`
353+
*/
339354
private def isSyntheticMainParam(sym: Symbol)(using Context): Boolean =
340355
sym.exists && ctx.platform.isMainMethod(sym.owner) && sym.owner.is(Synthetic)
341356

357+
/**
358+
* This is used to ignore exclusion imports (i.e. import `qual`.{`member` => _})
359+
*/
342360
private def isImportExclusion(sel: ImportSelector): Boolean = sel.renamed match
343361
case untpd.Ident(name) => name == StdNames.nme.WILDCARD
344362
case _ => false
@@ -376,21 +394,22 @@ object CheckUnused:
376394
selector.orElse(wildcard) // selector with name or wildcard (or given)
377395
else
378396
None
379-
380397
end UnusedData
381398

382-
object UnusedData:
399+
private object UnusedData:
383400
enum ScopeType:
384401
case Local
385402
case Template
386403
case Other
387404

388405
object ScopeType:
406+
/** return the scope corresponding to the enclosing scope of the given tree */
389407
def fromTree(tree: tpd.Tree): ScopeType = tree match
390408
case _:tpd.Template => Template
391409
case _:tpd.Block => Local
392410
case _ => Other
393411

412+
/** A container for the results of the used elements analysis */
394413
case class UnusedResult(warnings: List[(dotty.tools.dotc.util.SrcPos, WarnTypes)], usedImports: List[(tpd.Import, untpd.ImportSelector)])
395414
end CheckUnused
396415

0 commit comments

Comments
 (0)