Skip to content

Commit

Permalink
Backport "WUnused: Fix unused warning in synthetic symbols" (#17272)
Browse files Browse the repository at this point in the history
Backports #17020
  • Loading branch information
Kordyjan authored Apr 17, 2023
2 parents 942476c + b050bda commit cf6da33
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
8 changes: 6 additions & 2 deletions compiler/src/dotty/tools/dotc/transform/CheckUnused.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import dotty.tools.dotc.ast.untpd.ImportSelector
import dotty.tools.dotc.config.ScalaSettings
import dotty.tools.dotc.core.Contexts.*
import dotty.tools.dotc.core.Decorators.{em, i}
import dotty.tools.dotc.core.Flags._
import dotty.tools.dotc.core.Flags.*
import dotty.tools.dotc.core.Phases.Phase
import dotty.tools.dotc.core.StdNames
import dotty.tools.dotc.report
import dotty.tools.dotc.reporting.Message
import dotty.tools.dotc.typer.ImportInfo
import dotty.tools.dotc.util.Property
import dotty.tools.dotc.util.{Property, SrcPos}
import dotty.tools.dotc.core.Mode
import dotty.tools.dotc.core.Types.TypeTraverser
import dotty.tools.dotc.core.Types.Type
Expand Down Expand Up @@ -302,6 +302,7 @@ object CheckUnused:
* See the `isAccessibleAsIdent` extension method below in the file
*/
private val usedInScope = MutStack(MutSet[(Symbol,Boolean, Option[Name])]())
private val usedInPosition = MutSet[(SrcPos, Name)]()
/* unused import collected during traversal */
private val unusedImport = MutSet[ImportSelector]()

Expand Down Expand Up @@ -351,6 +352,7 @@ object CheckUnused:
usedInScope.top += ((sym, sym.isAccessibleAsIdent, name))
usedInScope.top += ((sym.companionModule, sym.isAccessibleAsIdent, name))
usedInScope.top += ((sym.companionClass, sym.isAccessibleAsIdent, name))
name.map(n => usedInPosition += ((sym.sourcePos, n)))

/** Register a symbol that should be ignored */
def addIgnoredUsage(sym: Symbol)(using Context): Unit =
Expand Down Expand Up @@ -455,6 +457,7 @@ object CheckUnused:
if ctx.settings.WunusedHas.locals then
localDefInScope
.filterNot(d => d.symbol.usedDefContains)
.filterNot(d => usedInPosition.exists { case (pos, name) => d.span.contains(pos.span) && name == d.symbol.name})
.map(d => d.namePos -> WarnTypes.LocalDefs).toList
else
Nil
Expand Down Expand Up @@ -483,6 +486,7 @@ object CheckUnused:
if ctx.settings.WunusedHas.patvars then
patVarsInScope
.filterNot(d => d.symbol.usedDefContains)
.filterNot(d => usedInPosition.exists { case (pos, name) => d.span.contains(pos.span) && name == d.symbol.name})
.map(d => d.namePos -> WarnTypes.PatVars).toList
else
Nil
Expand Down
14 changes: 14 additions & 0 deletions tests/neg-custom-args/fatal-warnings/i15503i.scala
Original file line number Diff line number Diff line change
Expand Up @@ -194,3 +194,17 @@ package foo.test.i16877:

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

package foo.test.i16926:
def hello(): Unit =
for {
i <- (0 to 10).toList
(a, b) = "hello" -> "world" // OK
} yield println(s"$a $b")

package foo.test.i16925:
def hello =
for {
i <- 1 to 2 if true
_ = println(i) // OK
} yield ()

0 comments on commit cf6da33

Please sign in to comment.