Skip to content

Commit 4cc4339

Browse files
committed
Traverse annotations instead of just registering
- Traverse the tree of annotations - Update test suits
1 parent 2507577 commit 4cc4339

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

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

Lines changed: 9 additions & 9 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

@@ -103,6 +104,7 @@ class CheckUnused extends MiniPhase:
103104
override def prepareForValDef(tree: tpd.ValDef)(using Context): Context =
104105
_key.unusedDataApply{ud =>
105106
// do not register the ValDef generated for `object`
107+
traverseAnnotations(tree.symbol)
106108
if !tree.symbol.is(Module) then
107109
ud.registerDef(tree)
108110
ud.addIgnoredUsage(tree.symbol)
@@ -112,18 +114,21 @@ class CheckUnused extends MiniPhase:
112114
_key.unusedDataApply{ ud =>
113115
import ud.registerTrivial
114116
tree.registerTrivial
117+
traverseAnnotations(tree.symbol)
115118
ud.registerDef(tree)
116119
ud.addIgnoredUsage(tree.symbol)
117120
}
118121

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

126130
override def prepareForBind(tree: tpd.Bind)(using Context): Context =
131+
traverseAnnotations(tree.symbol)
127132
_key.unusedDataApply(_.registerPatVar(tree))
128133

129134
override def prepareForTypeTree(tree: tpd.TypeTree)(using Context): Context =
@@ -232,6 +237,10 @@ class CheckUnused extends MiniPhase:
232237
case AnnotatedType(_, annot) => dt(_.registerUsed(annot.symbol, None))
233238
case _ => traverseChildren(tp)
234239

240+
/** This traverse the annotations of the symbol */
241+
private def traverseAnnotations(sym: Symbol)(using Context): Unit =
242+
sym.denot.annotations.foreach(annot => traverser.traverse(annot.tree))
243+
235244
/** Do the actual reporting given the result of the anaylsis */
236245
private def reportUnused(res: UnusedData.UnusedResult)(using Context): Unit =
237246
import CheckUnused.WarnTypes
@@ -274,7 +283,6 @@ object CheckUnused:
274283
private class UnusedData:
275284
import dotty.tools.dotc.transform.CheckUnused.UnusedData.UnusedResult
276285
import collection.mutable.{Set => MutSet, Map => MutMap, Stack => MutStack}
277-
import dotty.tools.dotc.core.Symbols.Symbol
278286
import UnusedData.ScopeType
279287

280288
/** The current scope during the tree traversal */
@@ -324,11 +332,6 @@ object CheckUnused:
324332
execInNewScope
325333
popScope()
326334

327-
/** Register all annotations of this symbol's denotation */
328-
def registerUsedAnnotation(sym: Symbol)(using Context): Unit =
329-
val annotSym = sym.denot.annotations.map(_.symbol)
330-
annotSym.foreach(s => registerUsed(s, None))
331-
332335
/**
333336
* Register a found (used) symbol along with its name
334337
*
@@ -363,8 +366,6 @@ object CheckUnused:
363366

364367
/** Register (or not) some `val` or `def` according to the context, scope and flags */
365368
def registerDef(memDef: tpd.MemberDef)(using Context): Unit =
366-
// register the annotations for usage
367-
registerUsedAnnotation(memDef.symbol)
368369
if memDef.isValidMemberDef then
369370
if memDef.isValidParam then
370371
if memDef.symbol.isOneOf(GivenOrImplicit) then
@@ -378,7 +379,6 @@ object CheckUnused:
378379

379380
/** Register pattern variable */
380381
def registerPatVar(patvar: tpd.Bind)(using Context): Unit =
381-
registerUsedAnnotation(patvar.symbol)
382382
if !patvar.symbol.isUnusedAnnot then
383383
patVarsInScope += patvar
384384

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)