Skip to content

Commit ab28b09

Browse files
PaulCoralKordyjan
authored andcommitted
Traverse annotations instead of just registering
- Traverse the tree of annotations - Update test suits
1 parent 85fa542 commit ab28b09

File tree

2 files changed

+19
-10
lines changed

2 files changed

+19
-10
lines changed

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import dotty.tools.dotc.core.Types.ConstantType
2828
import dotty.tools.dotc.core.NameKinds.WildcardParamName
2929
import dotty.tools.dotc.core.Types.TermRef
3030
import dotty.tools.dotc.core.Types.NameFilter
31+
import dotty.tools.dotc.core.Symbols.Symbol
3132

3233

3334

@@ -80,7 +81,7 @@ class CheckUnused extends MiniPhase:
8081
ctx
8182

8283
override def prepareForIdent(tree: tpd.Ident)(using Context): Context =
83-
if tree.symbol.exists then
84+
if tree.symbol.exists then
8485
unusedDataApply(_.registerUsed(tree.symbol, Some(tree.name)))
8586
else if tree.hasType then
8687
unusedDataApply(_.registerUsed(tree.tpe.classSymbol, Some(tree.name)))
@@ -102,6 +103,7 @@ class CheckUnused extends MiniPhase:
102103
override def prepareForValDef(tree: tpd.ValDef)(using Context): Context =
103104
unusedDataApply{ud =>
104105
// do not register the ValDef generated for `object`
106+
traverseAnnotations(tree.symbol)
105107
if !tree.symbol.is(Module) then
106108
ud.registerDef(tree)
107109
ud.addIgnoredUsage(tree.symbol)
@@ -111,18 +113,21 @@ class CheckUnused extends MiniPhase:
111113
unusedDataApply{ ud =>
112114
import ud.registerTrivial
113115
tree.registerTrivial
116+
traverseAnnotations(tree.symbol)
114117
ud.registerDef(tree)
115118
ud.addIgnoredUsage(tree.symbol)
116119
}
117120

118121
override def prepareForTypeDef(tree: tpd.TypeDef)(using Context): Context =
119122
unusedDataApply{ ud =>
120123
if !tree.symbol.is(Param) then // Ignore type parameter (as Scala 2)
124+
traverseAnnotations(tree.symbol)
121125
ud.registerDef(tree)
122126
ud.addIgnoredUsage(tree.symbol)
123127
}
124128

125129
override def prepareForBind(tree: tpd.Bind)(using Context): Context =
130+
traverseAnnotations(tree.symbol)
126131
unusedDataApply(_.registerPatVar(tree))
127132

128133
override def prepareForTypeTree(tree: tpd.TypeTree)(using Context): Context =
@@ -237,6 +242,10 @@ class CheckUnused extends MiniPhase:
237242
case _ =>
238243
traverseChildren(tp)
239244

245+
/** This traverse the annotations of the symbol */
246+
private def traverseAnnotations(sym: Symbol)(using Context): Unit =
247+
sym.denot.annotations.foreach(annot => traverser.traverse(annot.tree))
248+
240249
/** Do the actual reporting given the result of the anaylsis */
241250
private def reportUnused(res: UnusedData.UnusedResult)(using Context): Unit =
242251
import CheckUnused.WarnTypes
@@ -279,7 +288,6 @@ object CheckUnused:
279288
private class UnusedData:
280289
import dotty.tools.dotc.transform.CheckUnused.UnusedData.UnusedResult
281290
import collection.mutable.{Set => MutSet, Map => MutMap, Stack => MutStack}
282-
import dotty.tools.dotc.core.Symbols.Symbol
283291
import UnusedData.ScopeType
284292

285293
/** The current scope during the tree traversal */
@@ -329,11 +337,6 @@ object CheckUnused:
329337
execInNewScope
330338
popScope()
331339

332-
/** Register all annotations of this symbol's denotation */
333-
def registerUsedAnnotation(sym: Symbol)(using Context): Unit =
334-
val annotSym = sym.denot.annotations.map(_.symbol)
335-
annotSym.foreach(s => registerUsed(s, None))
336-
337340
/**
338341
* Register a found (used) symbol along with its name
339342
*
@@ -368,8 +371,6 @@ object CheckUnused:
368371

369372
/** Register (or not) some `val` or `def` according to the context, scope and flags */
370373
def registerDef(memDef: tpd.MemberDef)(using Context): Unit =
371-
// register the annotations for usage
372-
registerUsedAnnotation(memDef.symbol)
373374
if memDef.isValidMemberDef then
374375
if memDef.isValidParam then
375376
if memDef.symbol.isOneOf(GivenOrImplicit) then
@@ -383,7 +384,6 @@ object CheckUnused:
383384

384385
/** Register pattern variable */
385386
def registerPatVar(patvar: tpd.Bind)(using Context): Unit =
386-
registerUsedAnnotation(patvar.symbol)
387387
if !patvar.symbol.isUnusedAnnot then
388388
patVarsInScope += patvar
389389

tests/neg-custom-args/fatal-warnings/i15503i.scala

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,3 +142,12 @@ package foo.test.i16822:
142142
val x = ExampleEnum.List // OK
143143
println(x) // OK
144144
}
145+
146+
package foo.test.i16877:
147+
import scala.collection.immutable.HashMap // OK
148+
import scala.annotation.StaticAnnotation // OK
149+
150+
class ExampleAnnotation(val a: Object) extends StaticAnnotation // OK
151+
152+
@ExampleAnnotation(new HashMap()) // OK
153+
class Test //OK

0 commit comments

Comments
 (0)