From 252cc722d4fbb833389c6d73de9b4b839a3eb6a0 Mon Sep 17 00:00:00 2001 From: Nicolas Stucki Date: Tue, 20 Nov 2018 16:51:25 +0100 Subject: [PATCH 1/3] Rename SyntheticBounds to WildcardTypeTree --- .../dotc/tastyreflect/TypeOrBoundsTreesOpsImpl.scala | 2 +- library/src/scala/tasty/reflect/Core.scala | 5 ++++- library/src/scala/tasty/reflect/Printers.scala | 8 ++++---- library/src/scala/tasty/reflect/TreeUtils.scala | 2 +- library/src/scala/tasty/reflect/TypeOrBoundsTreeOps.scala | 8 ++++---- 5 files changed, 14 insertions(+), 11 deletions(-) diff --git a/compiler/src/dotty/tools/dotc/tastyreflect/TypeOrBoundsTreesOpsImpl.scala b/compiler/src/dotty/tools/dotc/tastyreflect/TypeOrBoundsTreesOpsImpl.scala index 5dcde3893ba5..567ff2b74fe4 100644 --- a/compiler/src/dotty/tools/dotc/tastyreflect/TypeOrBoundsTreesOpsImpl.scala +++ b/compiler/src/dotty/tools/dotc/tastyreflect/TypeOrBoundsTreesOpsImpl.scala @@ -159,7 +159,7 @@ trait TypeOrBoundsTreesOpsImpl extends scala.tasty.reflect.TypeOrBoundsTreeOps w } } - object SyntheticBounds extends SyntheticBoundsExtractor { + object WildcardTypeTree extends WildcardTypeTreeExtractor { def unapply(x: TypeOrBoundsTree)(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] diff --git a/library/src/scala/tasty/reflect/Core.scala b/library/src/scala/tasty/reflect/Core.scala index a3186be0dbe9..b9674308d364 100644 --- a/library/src/scala/tasty/reflect/Core.scala +++ b/library/src/scala/tasty/reflect/Core.scala @@ -52,7 +52,7 @@ package scala.tasty.reflect * | +- Bind * | * +- TypeBoundsTree - * +- SyntheticBounds + * +- WildcardTypeTree * * +- CaseDef * +- TypeCaseDef @@ -311,6 +311,9 @@ trait Core { /** Type tree representing a type bound written in the source */ type TypeBoundsTree <: TypeOrBoundsTree + /** Type tree representing wildcard type bounds written in the source */ + type WildcardType <: TypeOrBoundsTree + /** Type or bounds */ type TypeOrBounds <: AnyRef diff --git a/library/src/scala/tasty/reflect/Printers.scala b/library/src/scala/tasty/reflect/Printers.scala index a5674bf82c0a..01c376f6f19f 100644 --- a/library/src/scala/tasty/reflect/Printers.scala +++ b/library/src/scala/tasty/reflect/Printers.scala @@ -190,8 +190,8 @@ trait Printers this += "TypeTree.Block(" ++= aliases += ", " += tpt += ")" case TypeBoundsTree(lo, hi) => this += "TypeBoundsTree(" += lo += ", " += hi += ")" - case SyntheticBounds() => - this += s"SyntheticBounds()" + case WildcardTypeTree() => + this += s"WildcardTypeTree()" case TypeTree.MatchType(bound, selector, cases) => this += "TypeTree.MatchType(" += bound += ", " += selector += ", " ++= cases += ")" } @@ -996,7 +996,7 @@ trait Printers this += arg.name arg.rhs match { case IsTypeBoundsTree(rhs) => printBoundsTree(rhs) - case rhs @ SyntheticBounds() => + case rhs @ WildcardTypeTree() => printTypeOrBound(rhs.tpe) case rhs @ TypeTree.TypeLambdaTree(tparams, body) => def printParam(t: TypeOrBoundsTree): Unit = t match { @@ -1184,7 +1184,7 @@ trait Printers printTypeTree(lo) this += " <: " printTypeTree(hi) - case tpt @ SyntheticBounds() => + case tpt @ WildcardTypeTree() => printTypeOrBound(tpt.tpe) case IsTypeTree(tpt) => printTypeTree(tpt) diff --git a/library/src/scala/tasty/reflect/TreeUtils.scala b/library/src/scala/tasty/reflect/TreeUtils.scala index 279675c56d8a..8a18cfcdfbdc 100644 --- a/library/src/scala/tasty/reflect/TreeUtils.scala +++ b/library/src/scala/tasty/reflect/TreeUtils.scala @@ -104,7 +104,7 @@ trait TreeUtils case TypeTree.Block(typedefs, tpt) => foldTypeTree(foldTrees(x, typedefs), tpt) case TypeTree.MatchType(boundopt, selector, cases) => foldTypeCaseDefs(foldTypeTree(boundopt.fold(x)(foldTypeTree(x, _)), selector), cases) - case SyntheticBounds() => x + case WildcardTypeTree() => x case TypeBoundsTree(lo, hi) => foldTypeTree(foldTypeTree(x, lo), hi) } diff --git a/library/src/scala/tasty/reflect/TypeOrBoundsTreeOps.scala b/library/src/scala/tasty/reflect/TypeOrBoundsTreeOps.scala index 0c632099273a..280ced6c4286 100644 --- a/library/src/scala/tasty/reflect/TypeOrBoundsTreeOps.scala +++ b/library/src/scala/tasty/reflect/TypeOrBoundsTreeOps.scala @@ -126,10 +126,10 @@ trait TypeOrBoundsTreeOps extends Core { def unapply(typeOrBoundsTree: TypeOrBoundsTree)(implicit ctx: Context): Option[(TypeTree, TypeTree)] } - /** TypeBoundsTree containing an inferred type bounds */ - val SyntheticBounds: SyntheticBoundsExtractor - abstract class SyntheticBoundsExtractor { - /** Matches a TypeBoundsTree containing inferred type bounds */ + /** TypeBoundsTree containing wildcard type bounds */ + val WildcardTypeTree: WildcardTypeTreeExtractor + abstract class WildcardTypeTreeExtractor { + /** Matches a TypeBoundsTree containing wildcard type bounds */ def unapply(typeOrBoundsTree: TypeOrBoundsTree)(implicit ctx: Context): Boolean } From 12e0e3e7d3fd6b52b416b90dd381fd31fc603f94 Mon Sep 17 00:00:00 2001 From: Nicolas Stucki Date: Thu, 22 Nov 2018 07:29:43 +0100 Subject: [PATCH 2/3] Move synthetic type bounds to TypeBoundsTree --- .../dotty/tools/dotc/tastyreflect/CoreImpl.scala | 3 ++- .../tastyreflect/TypeOrBoundsTreesOpsImpl.scala | 16 +++++++++++----- .../tasty/reflect/TypeOrBoundsTreeOps.scala | 2 +- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/compiler/src/dotty/tools/dotc/tastyreflect/CoreImpl.scala b/compiler/src/dotty/tools/dotc/tastyreflect/CoreImpl.scala index 4d883aa18103..19d16181886d 100644 --- a/compiler/src/dotty/tools/dotc/tastyreflect/CoreImpl.scala +++ b/compiler/src/dotty/tools/dotc/tastyreflect/CoreImpl.scala @@ -79,7 +79,8 @@ trait CoreImpl extends scala.tasty.reflect.Core { type LambdaTypeTree = tpd.LambdaTypeTree type Bind = tpd.Bind } - type TypeBoundsTree = tpd.Tree + type TypeBoundsTree = tpd.TypeBoundsTree + type WildcardType = tpd.TypeTree type TypeOrBounds = Types.Type type NoPrefix = Types.NoPrefix.type diff --git a/compiler/src/dotty/tools/dotc/tastyreflect/TypeOrBoundsTreesOpsImpl.scala b/compiler/src/dotty/tools/dotc/tastyreflect/TypeOrBoundsTreesOpsImpl.scala index 567ff2b74fe4..3ffeaaf44484 100644 --- a/compiler/src/dotty/tools/dotc/tastyreflect/TypeOrBoundsTreesOpsImpl.scala +++ b/compiler/src/dotty/tools/dotc/tastyreflect/TypeOrBoundsTreesOpsImpl.scala @@ -123,7 +123,7 @@ trait TypeOrBoundsTreesOpsImpl extends scala.tasty.reflect.TypeOrBoundsTreeOps w } object Bind extends BindExtractor { - def unapply(x: TypeTree)(implicit ctx: Context): Option[(String, TypeBoundsTree)] = x match { + def unapply(x: TypeTree)(implicit ctx: Context): Option[(String, TypeOrBoundsTree)] = x match { case x: tpd.Bind if x.name.isTypeName => Some((x.name.toString, x.body)) case _ => None } @@ -141,27 +141,33 @@ trait TypeOrBoundsTreesOpsImpl extends scala.tasty.reflect.TypeOrBoundsTreeOps w def TypeBoundsTreeDeco(bounds: TypeBoundsTree): TypeBoundsTreeAPI = new TypeBoundsTreeAPI { def tpe(implicit ctx: Context): TypeBounds = bounds.tpe.asInstanceOf[Types.TypeBounds] - def low(implicit ctx: Context): TypeTree = bounds.asInstanceOf[tpd.TypeBoundsTree].lo - def hi(implicit ctx: Context): TypeTree = bounds.asInstanceOf[tpd.TypeBoundsTree].hi + def low(implicit ctx: Context): TypeTree = bounds.lo + def hi(implicit ctx: Context): TypeTree = bounds.hi } object IsTypeBoundsTree extends IsTypeBoundsTreeExtractor { def unapply(x: TypeOrBoundsTree)(implicit ctx: Context): Option[TypeBoundsTree] = x match { case x: tpd.TypeBoundsTree => Some(x) + case x @ Trees.TypeTree() => + // TODO only enums generate this kind of type bounds. Is this possible without enums? If not generate tpd.TypeBoundsTree for enums instead + x.tpe match { + case tpe: Types.TypeBounds => + Some(tpd.TypeBoundsTree(tpd.TypeTree(tpe.lo).withPos(x.pos), tpd.TypeTree(tpe.hi).withPos(x.pos))) + case _ => None + } case _ => None } } object TypeBoundsTree extends TypeBoundsTreeExtractor { def unapply(x: TypeOrBoundsTree)(implicit ctx: Context): Option[(TypeTree, TypeTree)] = x match { - case x: tpd.TypeBoundsTree => Some(x.lo, x.hi) + case IsTypeBoundsTree(x) => Some((x.lo, x.hi)) case _ => None } } object WildcardTypeTree extends WildcardTypeTreeExtractor { def unapply(x: TypeOrBoundsTree)(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 } diff --git a/library/src/scala/tasty/reflect/TypeOrBoundsTreeOps.scala b/library/src/scala/tasty/reflect/TypeOrBoundsTreeOps.scala index 280ced6c4286..56580bb05ed2 100644 --- a/library/src/scala/tasty/reflect/TypeOrBoundsTreeOps.scala +++ b/library/src/scala/tasty/reflect/TypeOrBoundsTreeOps.scala @@ -98,7 +98,7 @@ trait TypeOrBoundsTreeOps extends Core { val Bind: BindExtractor abstract class BindExtractor{ - def unapply(typeOrBoundsTree: TypeOrBoundsTree)(implicit ctx: Context): Option[(String, TypeBoundsTree)] + def unapply(typeOrBoundsTree: TypeOrBoundsTree)(implicit ctx: Context): Option[(String, TypeOrBoundsTree)] } val Block: BlockExtractor From 114a5ba7afb6e17df5ff3ca7c037dc0feea517c7 Mon Sep 17 00:00:00 2001 From: Nicolas Stucki Date: Fri, 23 Nov 2018 10:56:29 +0100 Subject: [PATCH 3/3] Add more doc --- library/src/scala/tasty/reflect/Core.scala | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/library/src/scala/tasty/reflect/Core.scala b/library/src/scala/tasty/reflect/Core.scala index b9674308d364..c792f4005f94 100644 --- a/library/src/scala/tasty/reflect/Core.scala +++ b/library/src/scala/tasty/reflect/Core.scala @@ -311,7 +311,10 @@ trait Core { /** Type tree representing a type bound written in the source */ type TypeBoundsTree <: TypeOrBoundsTree - /** Type tree representing wildcard type bounds written in the source */ + /** Type tree representing wildcard type bounds written in the source. + * The wildcard type `_` (for example in in `List[_]`) will be a type tree that + * represents a type but has `TypeBound`a inside. + */ type WildcardType <: TypeOrBoundsTree /** Type or bounds */