Skip to content

Commit 0f233e3

Browse files
committed
Add TypeTree.TypeLambda to Tasty reflect
1 parent 7028127 commit 0f233e3

File tree

6 files changed

+76
-2
lines changed

6 files changed

+76
-2
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -606,6 +606,12 @@ object TastyImpl extends scala.tasty.Tasty {
606606
}
607607
}
608608

609+
object TypeLambdaTree extends TypeLambdaTreeExtractor {
610+
def unapply(x: TypeTree)(implicit ctx: Context): Option[(List[TypeDef], TypeOrBoundsTree)] = x match {
611+
case Trees.LambdaTypeTree(tparams, body) => Some((tparams, body))
612+
case _ => None
613+
}
614+
}
609615
}
610616

611617
// ----- TypeBoundsTrees ------------------------------------------------

library/src/scala/tasty/Tasty.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,10 @@ abstract class Tasty { tasty =>
440440
def unapply(x: TypeTree)(implicit ctx: Context): Option[TypeTree]
441441
}
442442

443+
val TypeLambdaTree: TypeLambdaTreeExtractor
444+
abstract class TypeLambdaTreeExtractor {
445+
def unapply(x: TypeTree)(implicit ctx: Context): Option[(List[TypeDef], TypeOrBoundsTree)]
446+
}
443447
}
444448

445449
// ----- TypeBoundsTrees ------------------------------------------------

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@ class ShowExtractors[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
110110
this += "TypeTree.ByName(" += result += ")"
111111
case TypeTree.Annotated(arg, annot) =>
112112
this += "TypeTree.Annotated(" += arg += ", " += annot += ")"
113+
case TypeTree.TypeLambdaTree(tparams, body) =>
114+
this += "LambdaTypeTree(" ++= tparams += ", " += body += ")"
113115
case TypeBoundsTree(lo, hi) =>
114116
this += "TypeBoundsTree(" += lo += ", " += hi += ")"
115117
case SyntheticBounds() =>
@@ -185,6 +187,8 @@ class ShowExtractors[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
185187
this += "Type.ThisType(" += tp += ")"
186188
case Type.RecursiveThis(binder) =>
187189
this += "Type.RecursiveThis(" += binder += ")"
190+
case Type.RecursiveType(underlying) =>
191+
this += "Type.RecursiveType(" += underlying += ")"
188192
case Type.MethodType(argNames, argTypes, resType) =>
189193
this += "Type.MethodType(" ++= argNames += ", " ++= argTypes += ", " += resType += ")"
190194
case Type.PolyType(argNames, argBounds, resType) =>

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

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
151151
case tdef @ TypeDef(name, rhs) =>
152152
printDefAnnotations(tdef)
153153
this += "type "
154-
printTargDef(tdef)
154+
printTargDef(tdef, isMember = true)
155155

156156
case vdef @ ValDef(name, tpt, rhs) =>
157157
printDefAnnotations(vdef)
@@ -494,7 +494,7 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
494494
}
495495
}
496496

497-
def printTargDef(arg: TypeDef): Buffer = {
497+
def printTargDef(arg: TypeDef, isMember: Boolean = false): Buffer = {
498498
val TypeDef(name, rhs) = arg
499499
this += name
500500
rhs match {
@@ -513,6 +513,28 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
513513
}
514514
case rhs @ SyntheticBounds() =>
515515
printTypeOrBound(rhs.tpe)
516+
case rhs @ TypeTree.TypeLambdaTree(tparams, body) =>
517+
def printSeparated(list: List[TypeDef]): Unit = list match {
518+
case Nil =>
519+
case x :: Nil =>
520+
val TypeDef(name, trhs) = x
521+
this += name
522+
printTypeOrBoundsTree(trhs)
523+
case x :: xs =>
524+
val TypeDef(name, trhs) = x
525+
this += name
526+
printTypeOrBoundsTree(trhs)
527+
this += ", "
528+
printSeparated(xs)
529+
}
530+
this += "["
531+
printSeparated(tparams)
532+
this += "]"
533+
if (isMember) {
534+
this += " = "
535+
printTypeOrBoundsTree(body)
536+
}
537+
else this
516538
case rhs @ TypeTree() =>
517539
this += " = "
518540
printTypeTree(rhs)
@@ -724,6 +746,11 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
724746
this += "=> "
725747
printTypeTree(result)
726748

749+
case TypeTree.TypeLambdaTree(tparams, body) =>
750+
printTargsDefs(tparams)
751+
this += " => "
752+
printTypeOrBoundsTree(body)
753+
727754
case _ =>
728755
throw new MatchError(tree.show)
729756

@@ -811,6 +838,30 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
811838
case _ => this
812839
}
813840

841+
case Type.TypeLambda(paramNames, tparams, body) =>
842+
this += "["
843+
def printSeparated(list: List[(String, TypeBounds)]): Unit = list match {
844+
case Nil =>
845+
case (name, bounds) :: Nil =>
846+
this += name
847+
printTypeOrBound(bounds)
848+
case (name, bounds) :: xs =>
849+
this += name
850+
printTypeOrBound(bounds)
851+
this += ", "
852+
printSeparated(xs)
853+
}
854+
printSeparated(paramNames.zip(tparams))
855+
this += "] => "
856+
printTypeOrBound(body)
857+
858+
case Type.ParamRef(lambda, idx) =>
859+
lambda match {
860+
case Type.MethodType(params, _, _) => this += params(idx)
861+
case Type.PolyType(params, _, _) => this += params(idx)
862+
case Type.TypeLambda(params, _, _) => this += params(idx)
863+
}
864+
814865
case _ =>
815866
throw new MatchError(tpe.show)
816867
}

tests/pos/i1181.decompiled

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/** Decompiled from out/posTestFromTasty/pos/i1181/Test.class */
2+
object Test {
3+
def foo[M[_$1 >: scala.Nothing <: scala.Any]](x: M[scala.Int]): M[scala.Int] = x
4+
type Alias[A >: scala.Nothing <: scala.Any] = scala.Tuple2[A, A]
5+
val x: Test.Alias[scala.Int] = scala.Tuple2.apply[scala.Int, scala.Int](1, 2)
6+
Test.foo[Test.Alias](Test.x)
7+
Test.foo[[A >: scala.Nothing <: scala.Any] => scala.Tuple2[A, A]](Test.x)
8+
}

tests/pos/tasty/definitions.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ object definitions {
100100
case And(left: TypeTree, right: TypeTree)
101101
case Or(left: TypeTree, right: TypeTree)
102102
case ByName(tpt: TypeTree)
103+
case TypeLambda(tparams: List[TypeDef], body: Type | TypeBoundsTree)
103104
}
104105

105106
/** Trees denoting type bounds */

0 commit comments

Comments
 (0)