@@ -21,7 +21,7 @@ import dotty.tools.dotc.core.NameOps.isReplWrapperName
21
21
import dotty .tools .dotc .core .Annotations
22
22
import dotty .tools .dotc .core .Definitions
23
23
import dotty .tools .dotc .core .NameKinds .WildcardParamName
24
- import dotty .tools .dotc .core .Symbols .Symbol
24
+ import dotty .tools .dotc .core .Symbols .{ Symbol , isDeprecated }
25
25
import dotty .tools .dotc .report
26
26
import dotty .tools .dotc .reporting .{Message , UnusedSymbol as UnusedSymbolMessage }
27
27
import dotty .tools .dotc .transform .MegaPhase .MiniPhase
@@ -76,7 +76,6 @@ class CheckUnused private (phaseMode: CheckUnused.PhaseMode, suffix: String, _ke
76
76
77
77
// ========== MiniPhase Prepare ==========
78
78
override def prepareForOther (tree : tpd.Tree )(using Context ): Context =
79
- // A standard tree traverser covers cases not handled by the Mega/MiniPhase
80
79
traverser.traverse(tree)
81
80
ctx
82
81
@@ -104,13 +103,13 @@ class CheckUnused private (phaseMode: CheckUnused.PhaseMode, suffix: String, _ke
104
103
ud.registerUsed(tree.symbol, name, tree.qualifier.tpe, includeForImport = tree.qualifier.span.isSynthetic)
105
104
106
105
override def prepareForBlock (tree : tpd.Block )(using Context ): Context =
107
- pushInBlockTemplatePackageDef (tree)
106
+ pushScope (tree)
108
107
109
108
override def prepareForTemplate (tree : tpd.Template )(using Context ): Context =
110
- pushInBlockTemplatePackageDef (tree)
109
+ pushScope (tree)
111
110
112
111
override def prepareForPackageDef (tree : tpd.PackageDef )(using Context ): Context =
113
- pushInBlockTemplatePackageDef (tree)
112
+ pushScope (tree)
114
113
115
114
override def prepareForValDef (tree : tpd.ValDef )(using Context ): Context =
116
115
preparing :
@@ -156,15 +155,15 @@ class CheckUnused private (phaseMode: CheckUnused.PhaseMode, suffix: String, _ke
156
155
// ========== MiniPhase Transform ==========
157
156
158
157
override def transformBlock (tree : tpd.Block )(using Context ): tpd.Tree =
159
- popOutBlockTemplatePackageDef (tree)
158
+ popScope (tree)
160
159
tree
161
160
162
161
override def transformTemplate (tree : tpd.Template )(using Context ): tpd.Tree =
163
- popOutBlockTemplatePackageDef (tree)
162
+ popScope (tree)
164
163
tree
165
164
166
165
override def transformPackageDef (tree : tpd.PackageDef )(using Context ): tpd.Tree =
167
- popOutBlockTemplatePackageDef (tree)
166
+ popScope (tree)
168
167
tree
169
168
170
169
override def transformValDef (tree : tpd.ValDef )(using Context ): tpd.Tree =
@@ -185,11 +184,11 @@ class CheckUnused private (phaseMode: CheckUnused.PhaseMode, suffix: String, _ke
185
184
186
185
// ---------- MiniPhase HELPERS -----------
187
186
188
- private def pushInBlockTemplatePackageDef (tree : tpd.Block | tpd.Template | tpd.PackageDef )(using Context ): Context =
187
+ private def pushScope (tree : tpd.Block | tpd.Template | tpd.PackageDef )(using Context ): Context =
189
188
preparing :
190
189
ud.pushScope(UnusedData .ScopeType .fromTree(tree))
191
190
192
- private def popOutBlockTemplatePackageDef (tree : tpd.Block | tpd.Template | tpd.PackageDef )(using Context ): Context =
191
+ private def popScope (tree : tpd.Block | tpd.Template | tpd.PackageDef )(using Context ): Context =
193
192
preparing :
194
193
ud.popScope(UnusedData .ScopeType .fromTree(tree))
195
194
@@ -198,6 +197,8 @@ class CheckUnused private (phaseMode: CheckUnused.PhaseMode, suffix: String, _ke
198
197
*
199
198
* It traverse the tree the tree and gather the data in the
200
199
* corresponding context property
200
+ *
201
+ * A standard tree traverser covers cases not handled by the Mega/MiniPhase
201
202
*/
202
203
private def traverser = new TreeTraverser :
203
204
@@ -220,9 +221,9 @@ class CheckUnused private (phaseMode: CheckUnused.PhaseMode, suffix: String, _ke
220
221
traverseChildren(tree)(using newCtx)
221
222
case tree : (tpd.Block | tpd.Template | tpd.PackageDef ) =>
222
223
// ! DIFFERS FROM MINIPHASE
223
- pushInBlockTemplatePackageDef (tree)
224
+ pushScope (tree)
224
225
traverseChildren(tree)(using newCtx)
225
- popOutBlockTemplatePackageDef (tree)
226
+ popScope (tree)
226
227
case t : tpd.ValDef =>
227
228
prepareForValDef(t)
228
229
traverseChildren(tree)(using newCtx)
@@ -335,6 +336,7 @@ object CheckUnused:
335
336
336
337
/** The current scope during the tree traversal */
337
338
val currScopeType : Stack [ScopeType ] = Stack (ScopeType .Other )
339
+ inline def peekScopeType = currScopeType.top
338
340
339
341
var unusedAggregate : Option [UnusedResult ] = None
340
342
@@ -427,7 +429,7 @@ object CheckUnused:
427
429
! tpd.languageImport(imp.expr).nonEmpty
428
430
&& ! imp.isGeneratedByEnum
429
431
&& ! isTransparentAndInline(imp)
430
- && currScopeType.top != ScopeType .ReplWrapper // #18383 Do not report top-level import's in the repl as unused
432
+ && peekScopeType != ScopeType .ReplWrapper // #18383 Do not report top-level import's in the repl as unused
431
433
then
432
434
val qualTpe = imp.expr.tpe
433
435
@@ -455,7 +457,7 @@ object CheckUnused:
455
457
implicitParamInScope += memDef
456
458
else if ! paramsToSkip.contains(memDef.symbol) then
457
459
explicitParamInScope += memDef
458
- else if currScopeType.top == ScopeType .Local then
460
+ else if peekScopeType == ScopeType .Local then
459
461
localDefInScope += memDef
460
462
else if memDef.shouldReportPrivateDef then
461
463
privateDefInScope += memDef
@@ -653,14 +655,11 @@ object CheckUnused:
653
655
if altName.exists(explicitName => selector.rename != explicitName.toTermName) then
654
656
// if there is an explicit name, it must match
655
657
false
656
- else
657
- (isDerived || prefix.typeSymbol.isPackageObject || selData.qualTpe =:= prefix) && (
658
- if isDerived then
659
- // See i15503i.scala, grep for "package foo.test.i17156"
660
- selData.allSymbolsDealiasedForNamed.contains(sym.dealiasAsType)
661
- else
662
- selData.allSymbolsForNamed.contains(sym)
663
- )
658
+ else if isDerived then
659
+ // See i15503i.scala, grep for "package foo.test.i17156"
660
+ selData.allSymbolsDealiasedForNamed.contains(sym.dealiasAsType)
661
+ else (prefix.typeSymbol.isPackageObject || selData.qualTpe =:= prefix) &&
662
+ selData.allSymbolsForNamed.contains(sym)
664
663
else
665
664
// Wildcard
666
665
if ! selData.qualTpe.member(sym.name).hasAltWith(_.symbol == sym) then
@@ -687,9 +686,7 @@ object CheckUnused:
687
686
val owner = sym.owner
688
687
trivialDefs(owner) || // is a trivial def
689
688
owner.isPrimaryConstructor ||
690
- owner.annotations.exists ( // @depreacated
691
- _.symbol == ctx.definitions.DeprecatedAnnot
692
- ) ||
689
+ owner.isDeprecated ||
693
690
owner.isAllOf(Synthetic | PrivateLocal ) ||
694
691
owner.is(Accessor ) ||
695
692
owner.isOverridden
@@ -743,7 +740,7 @@ object CheckUnused:
743
740
! sym.shouldNotReportParamOwner
744
741
745
742
private def shouldReportPrivateDef (using Context ): Boolean =
746
- currScopeType.top == ScopeType .Template && ! memDef.symbol.isConstructor && memDef.symbol.is(Private , butNot = SelfName | Synthetic | CaseAccessor )
743
+ peekScopeType == ScopeType .Template && ! memDef.symbol.isConstructor && memDef.symbol.is(Private , butNot = SelfName | Synthetic | CaseAccessor )
747
744
748
745
private def isUnsetVarDef (using Context ): Boolean =
749
746
val sym = memDef.symbol
@@ -770,7 +767,7 @@ object CheckUnused:
770
767
object ScopeType :
771
768
/** return the scope corresponding to the enclosing scope of the given tree */
772
769
def fromTree (tree : tpd.Tree )(using Context ): ScopeType = tree match
773
- case tree : tpd.Template => if tree.symbol.name.isReplWrapperName then ReplWrapper else Template
770
+ case _ : tpd.Template => if tree.symbol.name.isReplWrapperName then ReplWrapper else Template
774
771
case _ : tpd.Block => Local
775
772
case _ => Other
776
773
@@ -827,5 +824,5 @@ object CheckUnused:
827
824
case tp : NamedType => tp.prefix
828
825
case tp : ClassInfo => tp.prefix
829
826
case tp : TypeProxy => tp.superType.normalizedPrefix
830
- case _ => tp
827
+ case _ => NoType
831
828
end CheckUnused
0 commit comments