Skip to content

Commit 6d67d26

Browse files
committed
Add Tasty.SyntheticBounds
To represent dotty TypeTrees containing type bounds
1 parent 1a7144a commit 6d67d26

File tree

7 files changed

+86
-10
lines changed

7 files changed

+86
-10
lines changed

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

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

523523
object Synthetic extends SyntheticExtractor {
524524
def unapply(x: TypeTree)(implicit ctx: Context): Boolean = x match {
525-
case Trees.TypeTree() => true
525+
case x @ Trees.TypeTree() => !x.tpe.isInstanceOf[Types.TypeBounds]
526526
case _ => false
527527
}
528528
}
@@ -594,9 +594,11 @@ object TastyImpl extends scala.tasty.Tasty {
594594

595595
// ----- TypeBoundsTrees ------------------------------------------------
596596

597-
type TypeBoundsTree = tpd.TypeBoundsTree
597+
type TypeBoundsTree = tpd.Tree
598598

599-
def TypeBoundsTreeDeco(x: TypeBoundsTree): AbstractTypeBoundsTree = ???
599+
def TypeBoundsTreeDeco(x: TypeBoundsTree): AbstractTypeBoundsTree = new AbstractTypeBoundsTree {
600+
def tpe: TypeBounds = x.tpe.asInstanceOf[Types.TypeBounds]
601+
}
600602

601603
def typeBoundsTreeClassTag: ClassTag[TypeBoundsTree] = implicitly[ClassTag[TypeBoundsTree]]
602604

@@ -607,6 +609,13 @@ object TastyImpl extends scala.tasty.Tasty {
607609
}
608610
}
609611

612+
object SyntheticBounds extends SyntheticBoundsExtractor {
613+
def unapply(x: TypeBoundsTree)(implicit ctx: Context): Boolean = x match {
614+
case x @ Trees.TypeTree() => x.tpe.isInstanceOf[Types.TypeBounds]
615+
case _ => false
616+
}
617+
}
618+
610619
// ===== Types ====================================================
611620

612621
type TypeOrBounds = Types.Type

library/src/scala/tasty/Tasty.scala

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

445+
val SyntheticBounds: SyntheticBoundsExtractor
446+
abstract class SyntheticBoundsExtractor {
447+
def unapply(x: TypeBoundsTree)(implicit ctx: Context): Boolean
448+
}
449+
445450
// ===== Types ====================================================
446451

447452
type TypeOrBounds

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ class ShowExtractors[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
106106
this += "TypeTree.Annotated(" += arg += ", " += annot += ")"
107107
case TypeBoundsTree(lo, hi) =>
108108
this += "TypeBoundsTree(" += lo += ", " += hi += ")"
109+
case SyntheticBounds() =>
110+
this += s"SyntheticBounds()"
109111
}
110112

111113
def visitCaseDef(x: CaseDef): Buffer = {
@@ -182,7 +184,8 @@ class ShowExtractors[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
182184
case Type.PolyType(argNames, argBounds, resType) =>
183185
this += "Type.PolyType(" ++= argNames += ", " ++= argBounds += ", " += resType += ")"
184186
case Type.TypeLambda(argNames, argBounds, resType) =>
185-
this += "Type.TypeLambda(" ++= argNames += ", " ++= argBounds += ", " += resType += ")"
187+
// resType is not printed to avoid cycles
188+
this += "Type.TypeLambda(" ++= argNames += ", " ++= argBounds += ", _)"
186189
case TypeBounds(lo, hi) =>
187190
this += "TypeBounds(" += lo += ", " += hi += ")"
188191
case NoPrefix() =>

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -434,9 +434,11 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
434434
this += " <: "
435435
printTypeTree(hi)
436436
}
437-
case tpt@TypeTree() =>
437+
case rhs @ SyntheticBounds() =>
438+
printTypeOrBound(rhs.tpe)
439+
case rhs @ TypeTree() =>
438440
this += " = "
439-
printTypeTree(tpt)
441+
printTypeTree(rhs)
440442
}
441443
}
442444

@@ -538,6 +540,8 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
538540
printTypeTree(lo)
539541
this += " <: "
540542
printTypeTree(hi)
543+
case tpt @ SyntheticBounds() =>
544+
printTypeOrBound(tpt.tpe)
541545
case tpt@Type() =>
542546
printType(tpt)
543547
}

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
@@ -88,11 +88,16 @@ object definitions {
8888
case ByName(tpt: TypeTree)
8989
}
9090

91-
/** Trees denoting type bounds*/
91+
/** Trees denoting type bounds */
9292
case class TypeBoundsTree(loBound: TypeTree, hiBound: TypeTree) extends Tree {
9393
def tpe: Type.TypeBounds = ???
9494
}
9595

96+
/** Trees denoting type infered bounds */
97+
case class SyntheticBounds() extends Tree {
98+
def tpe: Type.TypeBounds = ???
99+
}
100+
96101
/** Trees denoting patterns */
97102
enum Pattern extends Positioned {
98103
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)