Skip to content

Commit 64db2cf

Browse files
committed
Add type bindings in Tasty reflect
1 parent 8c3f3b9 commit 64db2cf

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
@@ -623,6 +623,13 @@ object TastyImpl extends scala.tasty.Tasty {
623623
case _ => None
624624
}
625625
}
626+
627+
object Bind extends BindExtractor {
628+
def unapply(x: TypeTree)(implicit ctx: Context): Option[(String, TypeBoundsTree)] = x match {
629+
case x: tpd.Bind @unchecked if x.name.isInstanceOf[Names.TypeName] => Some((x.name.toString, x.body))
630+
case _ => None
631+
}
632+
}
626633
}
627634

628635
// ----- TypeBoundsTrees ------------------------------------------------
@@ -645,6 +652,7 @@ object TastyImpl extends scala.tasty.Tasty {
645652
object SyntheticBounds extends SyntheticBoundsExtractor {
646653
def unapply(x: TypeBoundsTree)(implicit ctx: Context): Boolean = x match {
647654
case x @ Trees.TypeTree() => x.tpe.isInstanceOf[Types.TypeBounds]
655+
case Trees.Ident(nme.WILDCARD) => x.tpe.isInstanceOf[Types.TypeBounds]
648656
case _ => false
649657
}
650658
}

library/src/scala/tasty/Tasty.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,11 @@ abstract class Tasty { tasty =>
482482
abstract class TypeLambdaTreeExtractor {
483483
def unapply(x: TypeTree)(implicit ctx: Context): Option[(List[TypeDef], TypeOrBoundsTree)]
484484
}
485+
486+
val Bind: BindExtractor
487+
abstract class BindExtractor{
488+
def unapply(x: TypeTree)(implicit ctx: Context): Option[(String, TypeBoundsTree)]
489+
}
485490
}
486491

487492
// ----- TypeBoundsTrees ------------------------------------------------

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

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

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

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

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

760763
}
761764

762765
def printTypeOrBound(tpe: TypeOrBounds): Buffer = tpe match {
763766
case tpe@TypeBounds(lo, hi) =>
764-
this += " >: "
767+
this += "_ >: "
765768
printType(lo)
766769
this += " <: "
767770
printType(hi)
@@ -843,14 +846,21 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
843846

844847
case Type.TypeLambda(paramNames, tparams, body) =>
845848
this += "["
849+
def printBounds(bounds: TypeBounds): Buffer = {
850+
val TypeBounds(lo, hi) = bounds
851+
this += " >: "
852+
printType(lo)
853+
this += " <: "
854+
printType(hi)
855+
}
846856
def printSeparated(list: List[(String, TypeBounds)]): Unit = list match {
847857
case Nil =>
848858
case (name, bounds) :: Nil =>
849859
this += name
850-
printTypeOrBound(bounds)
860+
printBounds(bounds)
851861
case (name, bounds) :: xs =>
852862
this += name
853-
printTypeOrBound(bounds)
863+
printBounds(bounds)
854864
this += ", "
855865
printSeparated(xs)
856866
}

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
@@ -102,6 +102,7 @@ object definitions {
102102
case Or(left: TypeTree, right: TypeTree)
103103
case ByName(tpt: TypeTree)
104104
case TypeLambda(tparams: List[TypeDef], body: Type | TypeBoundsTree)
105+
case Bind(name: String, bounds: TypeBoundsTree)
105106
}
106107

107108
/** Trees denoting type bounds */

0 commit comments

Comments
 (0)