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 5dcde3893ba5..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 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] case _ => false } diff --git a/library/src/scala/tasty/reflect/Core.scala b/library/src/scala/tasty/reflect/Core.scala index a3186be0dbe9..c792f4005f94 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,12 @@ 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. + * 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 */ 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..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 @@ -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 }