Skip to content

Commit af178fd

Browse files
committed
Add Tasty.SyntheticBounds
To represent dotty TypeTrees containing type bounds
1 parent df3c9b5 commit af178fd

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
@@ -538,7 +538,7 @@ object TastyImpl extends scala.tasty.Tasty {
538538

539539
object Synthetic extends SyntheticExtractor {
540540
def unapply(x: TypeTree)(implicit ctx: Context): Boolean = x match {
541-
case Trees.TypeTree() => true
541+
case x @ Trees.TypeTree() => !x.tpe.isInstanceOf[Types.TypeBounds]
542542
case _ => false
543543
}
544544
}
@@ -610,10 +610,10 @@ object TastyImpl extends scala.tasty.Tasty {
610610

611611
// ----- TypeBoundsTrees ------------------------------------------------
612612

613-
type TypeBoundsTree = tpd.TypeBoundsTree
613+
type TypeBoundsTree = tpd.Tree
614614

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

619619
def typeBoundsTreeClassTag: ClassTag[TypeBoundsTree] = implicitly[ClassTag[TypeBoundsTree]]
@@ -625,6 +625,13 @@ object TastyImpl extends scala.tasty.Tasty {
625625
}
626626
}
627627

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

630637
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
@@ -486,9 +486,11 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
486486
this += " <: "
487487
printTypeTree(hi)
488488
}
489-
case tpt@TypeTree() =>
489+
case rhs @ SyntheticBounds() =>
490+
printTypeOrBound(rhs.tpe)
491+
case rhs @ TypeTree() =>
490492
this += " = "
491-
printTypeTree(tpt)
493+
printTypeTree(rhs)
492494
}
493495
}
494496

@@ -604,6 +606,8 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
604606
printTypeTree(lo)
605607
this += " <: "
606608
printTypeTree(hi)
609+
case tpt @ SyntheticBounds() =>
610+
printTypeOrBound(tpt.tpe)
607611
case tpt @ TypeTree() =>
608612
printTypeTree(tpt)
609613
}

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)