Skip to content

Commit

Permalink
Fix WUnused with idents in derived code (#17095)
Browse files Browse the repository at this point in the history
Resolve #16679
  • Loading branch information
Kordyjan authored Mar 15, 2023
2 parents 18259db + 7fed80e commit 9d51be0
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 9 deletions.
18 changes: 10 additions & 8 deletions compiler/src/dotty/tools/dotc/transform/CheckUnused.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package dotty.tools.dotc.transform

import dotty.tools.dotc.ast.tpd
import dotty.tools.dotc.ast.tpd.TreeTraverser
import dotty.tools.dotc.ast.tpd.{Inlined, TreeTraverser}
import dotty.tools.dotc.ast.untpd
import dotty.tools.dotc.ast.untpd.ImportSelector
import dotty.tools.dotc.config.ScalaSettings
Expand All @@ -15,19 +15,14 @@ import dotty.tools.dotc.reporting.Message
import dotty.tools.dotc.typer.ImportInfo
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
import dotty.tools.dotc.core.Types.AnnotatedType
import dotty.tools.dotc.core.Types.{AnnotatedType, ConstantType, NoType, TermRef, Type, TypeTraverser}
import dotty.tools.dotc.core.Flags.flagsString
import dotty.tools.dotc.core.Flags
import dotty.tools.dotc.core.Names.Name
import dotty.tools.dotc.transform.MegaPhase.MiniPhase
import dotty.tools.dotc.core.Annotations
import dotty.tools.dotc.core.Definitions
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,8 +75,16 @@ class CheckUnused extends MiniPhase:
traverser.traverse(tree)
ctx

override def prepareForInlined(tree: tpd.Inlined)(using Context): Context =
traverser.traverse(tree.call)
ctx

override def prepareForIdent(tree: tpd.Ident)(using Context): Context =
if tree.symbol.exists then
val prefixes = LazyList.iterate(tree.typeOpt.normalizedPrefix)(_.normalizedPrefix).takeWhile(_ != NoType)
.take(10) // Failsafe for the odd case if there was an infinite cycle
for prefix <- prefixes do
unusedDataApply(_.registerUsed(prefix.classSymbol, None))
unusedDataApply(_.registerUsed(tree.symbol, Some(tree.name)))
else if tree.hasType then
unusedDataApply(_.registerUsed(tree.tpe.classSymbol, Some(tree.name)))
Expand Down Expand Up @@ -409,7 +412,6 @@ object CheckUnused:
val kept = used.filterNot { t =>
val (sym, isAccessible, optName) = t
// keep the symbol for outer scope, if it matches **no** import

// This is the first matching wildcard selector
var selWildCard: Option[ImportSelector] = None

Expand Down
39 changes: 38 additions & 1 deletion tests/neg-custom-args/fatal-warnings/i15503i.scala
Original file line number Diff line number Diff line change
Expand Up @@ -207,4 +207,41 @@ package foo.test.i16925:
for {
i <- 1 to 2 if true
_ = println(i) // OK
} yield ()
} yield ()

package foo.test.i16679a:
object myPackage:
trait CaseClassName[A]:
def name: String
object CaseClassName:
trait CaseClassByStringName[A] extends CaseClassName[A]
import scala.deriving.Mirror
object CaseClassByStringName:
inline final def derived[A](using inline A: Mirror.Of[A]): CaseClassByStringName[A] =
new CaseClassByStringName[A]:
def name: String = A.toString

object secondPackage:
import myPackage.CaseClassName // OK
case class CoolClass(i: Int) derives CaseClassName.CaseClassByStringName
println(summon[CaseClassName[CoolClass]].name)

package foo.test.i16679b:
object myPackage:
trait CaseClassName[A]:
def name: String

object CaseClassName:
import scala.deriving.Mirror
inline final def derived[A](using inline A: Mirror.Of[A]): CaseClassName[A] =
new CaseClassName[A]:
def name: String = A.toString

object Foo:
given x: myPackage.CaseClassName[secondPackage.CoolClass] = null

object secondPackage:
import myPackage.CaseClassName // OK
import Foo.x
case class CoolClass(i: Int)
println(summon[myPackage.CaseClassName[CoolClass]])

0 comments on commit 9d51be0

Please sign in to comment.