Skip to content

Add type bindings in Tasty reflect #4683

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 2 commits into from
Jun 20, 2018
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
10 changes: 9 additions & 1 deletion compiler/src/dotty/tools/dotc/tastyreflect/TastyImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@ object TastyImpl extends scala.tasty.Tasty {

object Bind extends BindExtractor {
def unapply(x: Pattern)(implicit ctx: Context): Option[(String, Pattern)] = x match {
case x: tpd.Bind @unchecked if x.name.isInstanceOf[Names.TermName] => Some(x.name.toString, x.body)
case x: tpd.Bind @unchecked if x.name.isTermName => Some(x.name.toString, x.body)
case _ => None
}
}
Expand Down Expand Up @@ -623,6 +623,13 @@ object TastyImpl extends scala.tasty.Tasty {
case _ => None
}
}

object Bind extends BindExtractor {
def unapply(x: TypeTree)(implicit ctx: Context): Option[(String, TypeBoundsTree)] = x match {
case x: tpd.Bind @unchecked if x.name.isTypeName => Some((x.name.toString, x.body))
case _ => None
}
}
}

// ----- TypeBoundsTrees ------------------------------------------------
Expand All @@ -645,6 +652,7 @@ object TastyImpl extends scala.tasty.Tasty {
object SyntheticBounds extends SyntheticBoundsExtractor {
def unapply(x: TypeBoundsTree)(implicit ctx: Context): Boolean = x match {
case x @ Trees.TypeTree() => x.tpe.isInstanceOf[Types.TypeBounds]
case Trees.Ident(nme.WILDCARD) => x.tpe.isInstanceOf[Types.TypeBounds]
case _ => false
}
}
Expand Down
5 changes: 5 additions & 0 deletions library/src/scala/tasty/Tasty.scala
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,11 @@ abstract class Tasty { tasty =>
abstract class TypeLambdaTreeExtractor {
def unapply(x: TypeTree)(implicit ctx: Context): Option[(List[TypeDef], TypeOrBoundsTree)]
}

val Bind: BindExtractor
abstract class BindExtractor{
def unapply(x: TypeTree)(implicit ctx: Context): Option[(String, TypeBoundsTree)]
}
}

// ----- TypeBoundsTrees ------------------------------------------------
Expand Down
2 changes: 2 additions & 0 deletions library/src/scala/tasty/util/ShowExtractors.scala
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ class ShowExtractors[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
this += "TypeTree.Annotated(" += arg += ", " += annot += ")"
case TypeTree.TypeLambdaTree(tparams, body) =>
this += "LambdaTypeTree(" ++= tparams += ", " += body += ")"
case TypeTree.Bind(name, bounds) =>
this += "Bind(" += name += ", " += bounds += ")"
case TypeBoundsTree(lo, hi) =>
this += "TypeBoundsTree(" += lo += ", " += hi += ")"
case SyntheticBounds() =>
Expand Down
16 changes: 13 additions & 3 deletions library/src/scala/tasty/util/ShowSourceCode.scala
Original file line number Diff line number Diff line change
Expand Up @@ -754,14 +754,17 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
this += " => "
printTypeOrBoundsTree(body)

case TypeTree.Bind(name, _) =>
this += name

case _ =>
throw new MatchError(tree.show)

}

def printTypeOrBound(tpe: TypeOrBounds): Buffer = tpe match {
case tpe@TypeBounds(lo, hi) =>
this += " >: "
this += "_ >: "
printType(lo)
this += " <: "
printType(hi)
Expand Down Expand Up @@ -843,14 +846,21 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty

case Type.TypeLambda(paramNames, tparams, body) =>
this += "["
def printBounds(bounds: TypeBounds): Buffer = {
val TypeBounds(lo, hi) = bounds
this += " >: "
printType(lo)
this += " <: "
printType(hi)
}
def printSeparated(list: List[(String, TypeBounds)]): Unit = list match {
case Nil =>
case (name, bounds) :: Nil =>
this += name
printTypeOrBound(bounds)
printBounds(bounds)
case (name, bounds) :: xs =>
this += name
printTypeOrBound(bounds)
printBounds(bounds)
this += ", "
printSeparated(xs)
}
Expand Down
14 changes: 14 additions & 0 deletions tests/pos/i0306.decompiled
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/** Decompiled from out/posTestFromTasty/pos/i0306/bar.class */
object bar {
class C[T <: scala.Seq[_ >: scala.Nothing <: scala.Any]]()
val x: scala.AnyRef = new bar.C[scala.collection.Seq[_ >: scala.Nothing <: scala.Any]]()
val y: scala.collection.Seq[_ >: scala.Nothing <: scala.Any] = bar.x match {
case x: bar.C[u] =>
def xx: u = xx
((xx: u): scala.collection.Seq[_ >: scala.Nothing <: scala.Any])
}
val z: java.lang.String = {
def xx: scala.Predef.String = xx
(xx: java.lang.String)
}
}
1 change: 1 addition & 0 deletions tests/pos/tasty/definitions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ object definitions {
case Or(left: TypeTree, right: TypeTree)
case ByName(tpt: TypeTree)
case TypeLambda(tparams: List[TypeDef], body: Type | TypeBoundsTree)
case Bind(name: String, bounds: TypeBoundsTree)
}

/** Trees denoting type bounds */
Expand Down