Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions compiler/src/dotty/tools/dotc/transform/CheckUnused.scala
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,9 @@ class CheckUnused private (phaseMode: PhaseMode, suffix: String) extends MiniPha
override def prepareForValDef(tree: ValDef)(using Context): Context =
if !tree.symbol.is(Deferred) && tree.rhs.symbol != defn.Predef_undefined then
refInfos.register(tree)
tree.tpt match
case RefinedTypeTree(_, refinements) => relax(tree.rhs, refinements)
case _ =>
ctx
override def transformValDef(tree: ValDef)(using Context): tree.type =
traverseAnnotations(tree.symbol)
Expand All @@ -168,11 +171,15 @@ class CheckUnused private (phaseMode: PhaseMode, suffix: String) extends MiniPha
override def prepareForDefDef(tree: DefDef)(using Context): Context =
def trivial = tree.symbol.is(Deferred) || isUnconsuming(tree.rhs)
def nontrivial = tree.symbol.isConstructor || tree.symbol.isAnonymousFunction
if !nontrivial && trivial then refInfos.skip.addOne(tree.symbol)
if !nontrivial && trivial then
refInfos.skip.addOne(tree.symbol)
if tree.symbol.is(Inline) then
refInfos.inliners += 1
else if !tree.symbol.is(Deferred) && tree.rhs.symbol != defn.Predef_undefined then
refInfos.register(tree)
tree.tpt match
case RefinedTypeTree(_, refinements) => relax(tree.rhs, refinements)
case _ =>
ctx
override def transformDefDef(tree: DefDef)(using Context): tree.type =
traverseAnnotations(tree.symbol)
Expand Down Expand Up @@ -447,7 +454,7 @@ object CheckUnused:
if !tree.name.isInstanceOf[DerivedName] then
pats.addOne((tree.symbol, tree.namePos))
case tree: NamedDefTree =>
if (tree.symbol ne NoSymbol) && !tree.name.isWildcard then
if (tree.symbol ne NoSymbol) && !tree.name.isWildcard && !tree.hasAttachment(NoWarn) then
defs.addOne((tree.symbol, tree.namePos))
case _ =>
if tree.symbol ne NoSymbol then
Expand Down Expand Up @@ -843,6 +850,17 @@ object CheckUnused:
args.foreach(traverse)
case tree => traverseChildren(tree)

// NoWarn members in tree that correspond to refinements; currently uses only names.
def relax(tree: Tree, refinements: List[Tree])(using Context): Unit =
val names = refinements.collect { case named: NamedDefTree => named.name }.toSet
val relaxer = new TreeTraverser:
def traverse(tree: Tree)(using Context) =
tree match
case tree: NamedDefTree if names(tree.name) => tree.withAttachment(NoWarn, ())
case _ =>
traverseChildren(tree)
relaxer.traverse(tree)

extension (nm: Name)
inline def exists(p: Name => Boolean): Boolean = nm.ne(nme.NO_NAME) && p(nm)
inline def isWildcard: Boolean = nm == nme.WILDCARD || nm.is(WildcardParamName)
Expand Down
Loading