Skip to content

Commit 77e7a3b

Browse files
committed
Add Tasty.SyntheticBounds
To represent dotty TypeTrees containing type bounds
1 parent f84d017 commit 77e7a3b

File tree

7 files changed

+84
-10
lines changed

7 files changed

+84
-10
lines changed

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,7 @@ object TastyImpl extends scala.tasty.Tasty {
536536

537537
object Synthetic extends SyntheticExtractor {
538538
def unapply(x: TypeTree)(implicit ctx: Context): Boolean = x match {
539-
case Trees.TypeTree() => true
539+
case x @ Trees.TypeTree() => !x.tpe.isInstanceOf[Types.TypeBounds]
540540
case _ => false
541541
}
542542
}
@@ -608,10 +608,10 @@ object TastyImpl extends scala.tasty.Tasty {
608608

609609
// ----- TypeBoundsTrees ------------------------------------------------
610610

611-
type TypeBoundsTree = tpd.TypeBoundsTree
611+
type TypeBoundsTree = tpd.Tree
612612

613613
def TypeBoundsTreeDeco(x: TypeBoundsTree): TypeBoundsTreeAPI = new TypeBoundsTreeAPI {
614-
def tpe(implicit ctx: Context): TypeBounds = x.tpe.bounds
614+
def tpe(implicit ctx: Context): TypeBounds = x.tpe.asInstanceOf[Types.TypeBounds]
615615
}
616616

617617
def typeBoundsTreeClassTag: ClassTag[TypeBoundsTree] = implicitly[ClassTag[TypeBoundsTree]]
@@ -623,6 +623,13 @@ object TastyImpl extends scala.tasty.Tasty {
623623
}
624624
}
625625

626+
object SyntheticBounds extends SyntheticBoundsExtractor {
627+
def unapply(x: TypeBoundsTree)(implicit ctx: Context): Boolean = x match {
628+
case x @ Trees.TypeTree() => x.tpe.isInstanceOf[Types.TypeBounds]
629+
case _ => false
630+
}
631+
}
632+
626633
// ===== Types ====================================================
627634

628635
type TypeOrBounds = Types.Type

library/src/scala/tasty/Tasty.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,11 @@ abstract class Tasty { tasty =>
456456
def unapply(x: TypeBoundsTree)(implicit ctx: Context): Option[(TypeTree, TypeTree)]
457457
}
458458

459+
val SyntheticBounds: SyntheticBoundsExtractor
460+
abstract class SyntheticBoundsExtractor {
461+
def unapply(x: TypeBoundsTree)(implicit ctx: Context): Boolean
462+
}
463+
459464
// ===== Types ====================================================
460465

461466
type TypeOrBounds

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

Lines changed: 4 additions & 1 deletion
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 TypeBoundsTree(lo, hi) =>
114114
this += "TypeBoundsTree(" += lo += ", " += hi += ")"
115+
case SyntheticBounds() =>
116+
this += s"SyntheticBounds()"
115117
}
116118

117119
def visitCaseDef(x: CaseDef): Buffer = {
@@ -188,7 +190,8 @@ class ShowExtractors[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
188190
case Type.PolyType(argNames, argBounds, resType) =>
189191
this += "Type.PolyType(" ++= argNames += ", " ++= argBounds += ", " += resType += ")"
190192
case Type.TypeLambda(argNames, argBounds, resType) =>
191-
this += "Type.TypeLambda(" ++= argNames += ", " ++= argBounds += ", " += resType += ")"
193+
// resType is not printed to avoid cycles
194+
this += "Type.TypeLambda(" ++= argNames += ", " ++= argBounds += ", _)"
192195
case TypeBounds(lo, hi) =>
193196
this += "TypeBounds(" += lo += ", " += hi += ")"
194197
case NoPrefix() =>

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -483,9 +483,11 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
483483
this += " <: "
484484
printTypeTree(hi)
485485
}
486-
case tpt@TypeTree() =>
486+
case rhs @ SyntheticBounds() =>
487+
printTypeOrBound(rhs.tpe)
488+
case rhs @ TypeTree() =>
487489
this += " = "
488-
printTypeTree(tpt)
490+
printTypeTree(rhs)
489491
}
490492
}
491493

@@ -601,6 +603,8 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
601603
printTypeTree(lo)
602604
this += " <: "
603605
printTypeTree(hi)
606+
case tpt @ SyntheticBounds() =>
607+
printTypeOrBound(tpt.tpe)
604608
case tpt @ TypeTree() =>
605609
printTypeTree(tpt)
606610
}

tests/pos/i2104.decompiled

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/** Decompiled from out/posTestFromTasty/pos/i2104/Cons.class */
2+
class Cons[H, T]() extends java.lang.Object
3+
object Cons {
4+
def apply[H, T](h: H, t: T): Cons[H, T] = scala.Predef.???
5+
def unapply[H, T](t: Cons[H, T]): scala.Option[<empty>.Pair[H, T]] = scala.Predef.???
6+
}/** Decompiled from out/posTestFromTasty/pos/i2104/Pair.class */
7+
case class Pair[A, B](_1: A, _2: B) {
8+
def copy[A >: scala.Nothing <: scala.Any, B >: scala.Nothing <: scala.Any](_1: A, _2: B): Pair[A, B] = new Pair[A, B](_1, _2)
9+
def copy$default$1[A >: scala.Nothing <: scala.Any, B >: scala.Nothing <: scala.Any]: Pair.A = (Pair._1: Pair._1)
10+
def copy$default$2[A >: scala.Nothing <: scala.Any, B >: scala.Nothing <: scala.Any]: Pair.B = (Pair._2: Pair._2)
11+
override def hashCode(): scala.Int = {
12+
var acc: scala.Int = 2479866
13+
acc = scala.runtime.Statics.mix(acc, scala.runtime.Statics.anyHash(Pair._1))
14+
acc = scala.runtime.Statics.mix(acc, scala.runtime.Statics.anyHash(Pair._2))
15+
scala.runtime.Statics.finalizeHash(acc, 2)
16+
}
17+
override def equals(x$0: scala.Any): scala.Boolean = this.eq(x$0.asInstanceOf[java.lang.Object]).||(x$0 match {
18+
case x$0: Pair[Pair.A, Pair.B] =>
19+
this._1.==(x$0._1).&&(this._2.==(x$0._2))
20+
case _ =>
21+
false
22+
})
23+
override def toString(): java.lang.String = scala.runtime.ScalaRunTime._toString(this)
24+
override def canEqual(that: scala.Any): scala.Boolean = that.isInstanceOf[Pair[Pair.A, Pair.B]]
25+
override def productArity: scala.Int = 2
26+
override def productPrefix: java.lang.String = "Pair"
27+
override def productElement(n: scala.Int): scala.Any = n match {
28+
case 0 =>
29+
this._1
30+
case 1 =>
31+
this._2
32+
case _ =>
33+
throw new java.lang.IndexOutOfBoundsException(n.toString())
34+
}
35+
}
36+
object Pair extends scala.AnyRef {
37+
def apply[A >: scala.Nothing <: scala.Any, B >: scala.Nothing <: scala.Any](_1: A, _2: B): Pair[A, B] = new Pair[A, B](_1, _2)
38+
def unapply[A >: scala.Nothing <: scala.Any, B >: scala.Nothing <: scala.Any](x$1: Pair[A, B]): Pair[A, B] = x$1
39+
}/** Decompiled from out/posTestFromTasty/pos/i2104/Test.class */
40+
object Test {
41+
def main(args: scala.Array[scala.Predef.String]): scala.Unit = {
42+
<empty>.Cons.apply[scala.Option[scala.Int], scala.None](scala.Option.apply[scala.Int](1), scala.None) match {
43+
case <empty>.Cons.unapply[scala.Option[scala.Int], scala.None](, scala.None) =>
44+
{
45+
(i: scala.Int)
46+
dotty.DottyPredef.assert(i.==(1))
47+
}
48+
}
49+
}
50+
}

tests/pos/tasty/definitions.scala

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,16 @@ object definitions {
102102
case ByName(tpt: TypeTree)
103103
}
104104

105-
/** Trees denoting type bounds*/
105+
/** Trees denoting type bounds */
106106
case class TypeBoundsTree(loBound: TypeTree, hiBound: TypeTree) extends Tree {
107107
def tpe: Type.TypeBounds = ???
108108
}
109109

110+
/** Trees denoting type infered bounds */
111+
case class SyntheticBounds() extends Tree {
112+
def tpe: Type.TypeBounds = ???
113+
}
114+
110115
/** Trees denoting patterns */
111116
enum Pattern extends Positioned {
112117
def tpe: Type = ???

tests/run/tasty-extractors-owners.check

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ baz2
1717
ValDef("foo2", TypeTree.Synthetic(), None)
1818

1919
<init>
20-
ClassDef("A", DefDef("<init>", Nil, List(Nil), TypeTree.Synthetic(), None), List(Term.Apply(Term.Select(Term.New(TypeTree.Synthetic()), "<init>", Some(Signature(Nil, java.lang.Object))), Nil)), None, List(TypeDef("B", TypeTree.Synthetic()), DefDef("b", Nil, Nil, TypeTree.Synthetic(), None), ValDef("b2", TypeTree.Synthetic(), None)))
20+
ClassDef("A", DefDef("<init>", Nil, List(Nil), TypeTree.Synthetic(), None), List(Term.Apply(Term.Select(Term.New(TypeTree.Synthetic()), "<init>", Some(Signature(Nil, java.lang.Object))), Nil)), None, List(TypeDef("B", SyntheticBounds()), DefDef("b", Nil, Nil, TypeTree.Synthetic(), None), ValDef("b2", TypeTree.Synthetic(), None)))
2121

2222
b
23-
ClassDef("A", DefDef("<init>", Nil, List(Nil), TypeTree.Synthetic(), None), List(Term.Apply(Term.Select(Term.New(TypeTree.Synthetic()), "<init>", Some(Signature(Nil, java.lang.Object))), Nil)), None, List(TypeDef("B", TypeTree.Synthetic()), DefDef("b", Nil, Nil, TypeTree.Synthetic(), None), ValDef("b2", TypeTree.Synthetic(), None)))
23+
ClassDef("A", DefDef("<init>", Nil, List(Nil), TypeTree.Synthetic(), None), List(Term.Apply(Term.Select(Term.New(TypeTree.Synthetic()), "<init>", Some(Signature(Nil, java.lang.Object))), Nil)), None, List(TypeDef("B", SyntheticBounds()), DefDef("b", Nil, Nil, TypeTree.Synthetic(), None), ValDef("b2", TypeTree.Synthetic(), None)))
2424

2525
b2
26-
ClassDef("A", DefDef("<init>", Nil, List(Nil), TypeTree.Synthetic(), None), List(Term.Apply(Term.Select(Term.New(TypeTree.Synthetic()), "<init>", Some(Signature(Nil, java.lang.Object))), Nil)), None, List(TypeDef("B", TypeTree.Synthetic()), DefDef("b", Nil, Nil, TypeTree.Synthetic(), None), ValDef("b2", TypeTree.Synthetic(), None)))
26+
ClassDef("A", DefDef("<init>", Nil, List(Nil), TypeTree.Synthetic(), None), List(Term.Apply(Term.Select(Term.New(TypeTree.Synthetic()), "<init>", Some(Signature(Nil, java.lang.Object))), Nil)), None, List(TypeDef("B", SyntheticBounds()), DefDef("b", Nil, Nil, TypeTree.Synthetic(), None), ValDef("b2", TypeTree.Synthetic(), None)))
2727

0 commit comments

Comments
 (0)