Skip to content

Commit

Permalink
Backport "Traverse annotations instead of just registering, fixes #16877
Browse files Browse the repository at this point in the history
" (#17269)

Backports #16956
  • Loading branch information
Kordyjan authored Apr 17, 2023
2 parents 5d76f39 + ab28b09 commit ebba1cc
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
20 changes: 10 additions & 10 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 @@ -80,7 +81,7 @@ class CheckUnused extends MiniPhase:
ctx

override def prepareForIdent(tree: tpd.Ident)(using Context): Context =
if tree.symbol.exists then
if tree.symbol.exists then
unusedDataApply(_.registerUsed(tree.symbol, Some(tree.name)))
else if tree.hasType then
unusedDataApply(_.registerUsed(tree.tpe.classSymbol, Some(tree.name)))
Expand All @@ -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 @@ -142,3 +142,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 ebba1cc

Please sign in to comment.