Skip to content

Commit ec6d350

Browse files
committed
Add type bindings in Tasty reflect
1 parent 1b73ba6 commit ec6d350

File tree

6 files changed

+43
-3
lines changed

6 files changed

+43
-3
lines changed

compiler/src/dotty/tools/dotc/tastyreflect/TastyImpl.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -616,6 +616,13 @@ object TastyImpl extends scala.tasty.Tasty {
616616
case _ => None
617617
}
618618
}
619+
620+
object Bind extends BindExtractor {
621+
def unapply(x: TypeTree)(implicit ctx: Context): Option[(String, TypeBoundsTree)] = x match {
622+
case x: tpd.Bind @unchecked if x.name.isInstanceOf[Names.TypeName] => Some((x.name.toString, x.body))
623+
case _ => None
624+
}
625+
}
619626
}
620627

621628
// ----- TypeBoundsTrees ------------------------------------------------
@@ -638,6 +645,7 @@ object TastyImpl extends scala.tasty.Tasty {
638645
object SyntheticBounds extends SyntheticBoundsExtractor {
639646
def unapply(x: TypeBoundsTree)(implicit ctx: Context): Boolean = x match {
640647
case x @ Trees.TypeTree() => x.tpe.isInstanceOf[Types.TypeBounds]
648+
case Trees.Ident(nme.WILDCARD) => x.tpe.isInstanceOf[Types.TypeBounds]
641649
case _ => false
642650
}
643651
}

library/src/scala/tasty/Tasty.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,11 @@ abstract class Tasty { tasty =>
477477
abstract class TypeLambdaTreeExtractor {
478478
def unapply(x: TypeTree)(implicit ctx: Context): Option[(List[TypeDef], TypeOrBoundsTree)]
479479
}
480+
481+
val Bind: BindExtractor
482+
abstract class BindExtractor{
483+
def unapply(x: TypeTree)(implicit ctx: Context): Option[(String, TypeBoundsTree)]
484+
}
480485
}
481486

482487
// ----- TypeBoundsTrees ------------------------------------------------

library/src/scala/tasty/util/ShowExtractors.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@ class ShowExtractors[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
112112
this += "TypeTree.Annotated(" += arg += ", " += annot += ")"
113113
case TypeTree.TypeLambdaTree(tparams, body) =>
114114
this += "LambdaTypeTree(" ++= tparams += ", " += body += ")"
115+
case TypeTree.Bind(name, bounds) =>
116+
this += "Bind(" += name += ", " += bounds += ")"
115117
case TypeBoundsTree(lo, hi) =>
116118
this += "TypeBoundsTree(" += lo += ", " += hi += ")"
117119
case SyntheticBounds() =>

library/src/scala/tasty/util/ShowSourceCode.scala

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -755,14 +755,17 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
755755
this += " => "
756756
printTypeOrBoundsTree(body)
757757

758+
case TypeTree.Bind(name, _) =>
759+
this += name
760+
758761
case _ =>
759762
throw new MatchError(tree.show)
760763

761764
}
762765

763766
def printTypeOrBound(tpe: TypeOrBounds): Buffer = tpe match {
764767
case tpe@TypeBounds(lo, hi) =>
765-
this += " >: "
768+
this += "_ >: "
766769
printType(lo)
767770
this += " <: "
768771
printType(hi)
@@ -844,14 +847,21 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
844847

845848
case Type.TypeLambda(paramNames, tparams, body) =>
846849
this += "["
850+
def printBounds(bounds: TypeBounds): Buffer = {
851+
val TypeBounds(lo, hi) = bounds
852+
this += " >: "
853+
printType(lo)
854+
this += " <: "
855+
printType(hi)
856+
}
847857
def printSeparated(list: List[(String, TypeBounds)]): Unit = list match {
848858
case Nil =>
849859
case (name, bounds) :: Nil =>
850860
this += name
851-
printTypeOrBound(bounds)
861+
printBounds(bounds)
852862
case (name, bounds) :: xs =>
853863
this += name
854-
printTypeOrBound(bounds)
864+
printBounds(bounds)
855865
this += ", "
856866
printSeparated(xs)
857867
}

tests/pos/i0306.decompiled

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/** Decompiled from out/posTestFromTasty/pos/i0306/bar.class */
2+
object bar {
3+
class C[T <: scala.Seq[_ >: scala.Nothing <: scala.Any]]()
4+
val x: scala.AnyRef = new bar.C[scala.collection.Seq[_ >: scala.Nothing <: scala.Any]]()
5+
val y: scala.collection.Seq[_ >: scala.Nothing <: scala.Any] = bar.x match {
6+
case x: bar.C[u] =>
7+
def xx: u = xx
8+
((xx: u): scala.collection.Seq[_ >: scala.Nothing <: scala.Any])
9+
}
10+
val z: java.lang.String = {
11+
def xx: scala.Predef.String = xx
12+
(xx: java.lang.String)
13+
}
14+
}

tests/pos/tasty/definitions.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ object definitions {
101101
case Or(left: TypeTree, right: TypeTree)
102102
case ByName(tpt: TypeTree)
103103
case TypeLambda(tparams: List[TypeDef], body: Type | TypeBoundsTree)
104+
case Bind(name: String, bounds: TypeBoundsTree)
104105
}
105106

106107
/** Trees denoting type bounds */

0 commit comments

Comments
 (0)