Skip to content

Backport "Handle Typeable" to 3.3 LTS #273

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 23, 2025
Merged
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
1 change: 1 addition & 0 deletions compiler/src/dotty/tools/dotc/core/Definitions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -811,6 +811,7 @@ class Definitions {

@tu lazy val ReflectSelectableTypeRef: TypeRef = requiredClassRef("scala.reflect.Selectable")

@tu lazy val TypeableType: TypeSymbol = requiredPackage("scala.reflect.Typeable$package").moduleClass.requiredType("Typeable")
@tu lazy val TypeTestClass: ClassSymbol = requiredClass("scala.reflect.TypeTest")
@tu lazy val TypeTest_unapply: Symbol = TypeTestClass.requiredMethod(nme.unapply)
@tu lazy val TypeTestModule_identity: Symbol = TypeTestClass.companionModule.requiredMethod(nme.identity)
Expand Down
7 changes: 5 additions & 2 deletions compiler/src/dotty/tools/dotc/transform/CheckUnused.scala
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,11 @@ class CheckUnused private (phaseMode: PhaseMode, suffix: String) extends MiniPha
val name = tree.removeAttachment(OriginalName).getOrElse(nme.NO_NAME)
if tree.srcPos.isSynthetic && tree.symbol == defn.TypeTest_unapply then
tree.qualifier.tpe.underlying.finalResultType match
case AppliedType(_, args) => // tycon.typeSymbol == defn.TypeTestClass
val res = args(1) // T in TypeTest[-S, T]
case AppliedType(tycon, args) =>
val res =
if tycon.typeSymbol == defn.TypeTestClass then args(1) // T in TypeTest[-S, T]
else if tycon.typeSymbol == defn.TypeableType then args(0) // T in Typeable[T]
else return tree
val target = res.dealias.typeSymbol
resolveUsage(target, target.name, res.importPrefix.skipPackageObject) // case _: T =>
case _ =>
Expand Down
9 changes: 0 additions & 9 deletions tests/warn/i15503d.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
//> using options -Wunused:patvars

import scala.reflect.Typeable

sealed trait Calc
sealed trait Const extends Calc
case class Sum(a: Calc, b: Calc) extends Calc
Expand Down Expand Up @@ -74,13 +72,6 @@ class C(c0: Option[Int], k0: K):
for case Some(value) <- List(Option(42))
yield 27

/*
def tester[A](a: A)(using Typeable[K]) =
a match
case S(i, j) => i + j
case _ => 0
*/

class Wild:
def f(x: Any) =
x match
Expand Down
18 changes: 17 additions & 1 deletion tests/warn/i21525.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//> using options -Werror -Wunused:imports

import scala.reflect.TypeTest
import scala.reflect.{Typeable, TypeTest}
import compiletime.*

trait A {
type B
Expand All @@ -18,3 +19,18 @@ def f(a: A, b: a.B): Boolean = {
false
}
}

trait T:
type X
given Typeable[X] = deferred

def g(t: T, x: Any) =
import t.X
x match
case _: X => true
case _ => false

def typer[T: Typeable](x: Any) =
x match
case _: T => 1
case _ => 0