Skip to content

Commit 6f77fe2

Browse files
Merge pull request #4683 from dotty-staging/fix-tasty-type-bind
Add type bindings in Tasty reflect
2 parents 10aeb0e + 34d593b commit 6f77fe2

File tree

6 files changed

+44
-4
lines changed

6 files changed

+44
-4
lines changed

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,7 @@ object TastyImpl extends scala.tasty.Tasty {
485485

486486
object Bind extends BindExtractor {
487487
def unapply(x: Pattern)(implicit ctx: Context): Option[(String, Pattern)] = x match {
488-
case x: tpd.Bind @unchecked if x.name.isInstanceOf[Names.TermName] => Some(x.name.toString, x.body)
488+
case x: tpd.Bind @unchecked if x.name.isTermName => Some(x.name.toString, x.body)
489489
case _ => None
490490
}
491491
}
@@ -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.isTypeName => 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
@@ -758,14 +758,17 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
758758
this += " => "
759759
printTypeOrBoundsTree(body)
760760

761+
case TypeTree.Bind(name, _) =>
762+
this += name
763+
761764
case _ =>
762765
throw new MatchError(tree.show)
763766

764767
}
765768

766769
def printTypeOrBound(tpe: TypeOrBounds): Buffer = tpe match {
767770
case tpe@TypeBounds(lo, hi) =>
768-
this += " >: "
771+
this += "_ >: "
769772
printType(lo)
770773
this += " <: "
771774
printType(hi)
@@ -847,14 +850,21 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
847850

848851
case Type.TypeLambda(paramNames, tparams, body) =>
849852
this += "["
853+
def printBounds(bounds: TypeBounds): Buffer = {
854+
val TypeBounds(lo, hi) = bounds
855+
this += " >: "
856+
printType(lo)
857+
this += " <: "
858+
printType(hi)
859+
}
850860
def printSeparated(list: List[(String, TypeBounds)]): Unit = list match {
851861
case Nil =>
852862
case (name, bounds) :: Nil =>
853863
this += name
854-
printTypeOrBound(bounds)
864+
printBounds(bounds)
855865
case (name, bounds) :: xs =>
856866
this += name
857-
printTypeOrBound(bounds)
867+
printBounds(bounds)
858868
this += ", "
859869
printSeparated(xs)
860870
}

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)