Skip to content

Commit

Permalink
Traverse annotations instead of just registering, fixes #16877 (#16956)
Browse files Browse the repository at this point in the history
@szymon-rd  Fixes #16877 
- Traverse the tree of annotations
- Update test suits

The issue is that the annotation was registered but not traversed.
  • Loading branch information
szymon-rd authored Feb 18, 2023
2 parents f6c57d8 + 4750edc commit 7c9c72a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
18 changes: 9 additions & 9 deletions compiler/src/dotty/tools/dotc/transform/CheckUnused.scala
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import dotty.tools.dotc.core.Types.ConstantType
import dotty.tools.dotc.core.NameKinds.WildcardParamName
import dotty.tools.dotc.core.Types.TermRef
import dotty.tools.dotc.core.Types.NameFilter
import dotty.tools.dotc.core.Symbols.Symbol



Expand Down Expand Up @@ -102,6 +103,7 @@ class CheckUnused extends MiniPhase:
override def prepareForValDef(tree: tpd.ValDef)(using Context): Context =
unusedDataApply{ud =>
// do not register the ValDef generated for `object`
traverseAnnotations(tree.symbol)
if !tree.symbol.is(Module) then
ud.registerDef(tree)
ud.addIgnoredUsage(tree.symbol)
Expand All @@ -111,18 +113,21 @@ class CheckUnused extends MiniPhase:
unusedDataApply{ ud =>
import ud.registerTrivial
tree.registerTrivial
traverseAnnotations(tree.symbol)
ud.registerDef(tree)
ud.addIgnoredUsage(tree.symbol)
}

override def prepareForTypeDef(tree: tpd.TypeDef)(using Context): Context =
unusedDataApply{ ud =>
if !tree.symbol.is(Param) then // Ignore type parameter (as Scala 2)
traverseAnnotations(tree.symbol)
ud.registerDef(tree)
ud.addIgnoredUsage(tree.symbol)
}

override def prepareForBind(tree: tpd.Bind)(using Context): Context =
traverseAnnotations(tree.symbol)
unusedDataApply(_.registerPatVar(tree))

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

/** This traverse the annotations of the symbol */
private def traverseAnnotations(sym: Symbol)(using Context): Unit =
sym.denot.annotations.foreach(annot => traverser.traverse(annot.tree))

/** Do the actual reporting given the result of the anaylsis */
private def reportUnused(res: UnusedData.UnusedResult)(using Context): Unit =
import CheckUnused.WarnTypes
Expand Down Expand Up @@ -279,7 +288,6 @@ object CheckUnused:
private class UnusedData:
import dotty.tools.dotc.transform.CheckUnused.UnusedData.UnusedResult
import collection.mutable.{Set => MutSet, Map => MutMap, Stack => MutStack}
import dotty.tools.dotc.core.Symbols.Symbol
import UnusedData.ScopeType

/** The current scope during the tree traversal */
Expand Down Expand Up @@ -329,11 +337,6 @@ object CheckUnused:
execInNewScope
popScope()

/** Register all annotations of this symbol's denotation */
def registerUsedAnnotation(sym: Symbol)(using Context): Unit =
val annotSym = sym.denot.annotations.map(_.symbol)
annotSym.foreach(s => registerUsed(s, None))

/**
* Register a found (used) symbol along with its name
*
Expand Down Expand Up @@ -368,8 +371,6 @@ object CheckUnused:

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

/** Register pattern variable */
def registerPatVar(patvar: tpd.Bind)(using Context): Unit =
registerUsedAnnotation(patvar.symbol)
if !patvar.symbol.isUnusedAnnot then
patVarsInScope += patvar

Expand Down
9 changes: 9 additions & 0 deletions tests/neg-custom-args/fatal-warnings/i15503i.scala
Original file line number Diff line number Diff line change
Expand Up @@ -185,3 +185,12 @@ package foo.test.i16822:
val x = ExampleEnum.List // OK
println(x) // OK
}

package foo.test.i16877:
import scala.collection.immutable.HashMap // OK
import scala.annotation.StaticAnnotation // OK

class ExampleAnnotation(val a: Object) extends StaticAnnotation // OK

@ExampleAnnotation(new HashMap()) // OK
class Test //OK

0 comments on commit 7c9c72a

Please sign in to comment.