From 6872bdbbc9b8028d73f3115b0c325ac8a57ea718 Mon Sep 17 00:00:00 2001 From: Nicolas Stucki Date: Wed, 2 Sep 2020 13:32:31 +0200 Subject: [PATCH] Remove Reflection.TypeOrBounds abstraction * Align with TASTy format * Make it simpler to handle types with the reflection API * Remove ParamInfo from LambdaType (it was unused) This abstraction is there for historical reasons only, from the time when there was split between TypeTree, TypeBoundsTree and Tree. This distinction is no longer in the API and the types should follow the same design. Migration help: If there match on `Type` that matched agaist a `TypeOrBound` ```scala def f(t: TypeOrBound) = t match case t: Type => ... ``` The cases for `TypeBounds` and `NoPrefix` should be added before the `Type` test ```scala def f(t: Type) = t match case t: TypeBounds => case NoPrefix() => case t => // t: Type ... ``` --- .../community-projects/dotty-cps-async | 2 +- community-build/community-projects/shapeless | 2 +- .../reflect/ReflectionCompilerInterface.scala | 111 ++++++----- .../internal/tasty/CompilerInterface.scala | 58 +++--- library/src/scala/tasty/Reflection.scala | 174 +++++++++--------- .../tasty/reflect/ExtractorsPrinter.scala | 10 +- library/src/scala/tasty/reflect/Printer.scala | 4 +- .../tasty/reflect/SourceCodePrinter.scala | 57 +++--- library/src/scala/tasty/reflect/Types.scala | 67 +++---- .../dotty/tastydoc/TastyTypeConverter.scala | 58 +++--- .../src/dotty/tastydoc/representations.scala | 4 +- .../tasty-macro-assert-1/quoted_1.scala | 2 +- .../tasty-macro-assert-2/quoted_1.scala | 2 +- .../refined-selectable-macro/Macro_1.scala | 2 +- .../tasty-custom-show/quoted_1.scala | 2 +- .../tasty-indexed-map/quoted_1.scala | 4 +- .../tasty-macro-assert/quoted_1.scala | 2 +- 17 files changed, 265 insertions(+), 296 deletions(-) diff --git a/community-build/community-projects/dotty-cps-async b/community-build/community-projects/dotty-cps-async index cf5fd400643f..d7f4a49916d1 160000 --- a/community-build/community-projects/dotty-cps-async +++ b/community-build/community-projects/dotty-cps-async @@ -1 +1 @@ -Subproject commit cf5fd400643f6b61b2c81c49e080e59034a37fd9 +Subproject commit d7f4a49916d1a53be5587e0d76dae05ee6435676 diff --git a/community-build/community-projects/shapeless b/community-build/community-projects/shapeless index fcd7ad513a60..dcabf2858fb0 160000 --- a/community-build/community-projects/shapeless +++ b/community-build/community-projects/shapeless @@ -1 +1 @@ -Subproject commit fcd7ad513a603520bfaf1390a751bbc5bb7c6484 +Subproject commit dcabf2858fb08e0af0d59d4f1833c8f396125f50 diff --git a/compiler/src/dotty/tools/dotc/quoted/reflect/ReflectionCompilerInterface.scala b/compiler/src/dotty/tools/dotc/quoted/reflect/ReflectionCompilerInterface.scala index ebb34f2fb892..ef2184421e25 100644 --- a/compiler/src/dotty/tools/dotc/quoted/reflect/ReflectionCompilerInterface.scala +++ b/compiler/src/dotty/tools/dotc/quoted/reflect/ReflectionCompilerInterface.scala @@ -1015,7 +1015,7 @@ class ReflectionCompilerInterface(val rootContext: Context) extends CompilerInte case _ => None } - def WildcardTypeTree_tpe(self: WildcardTypeTree)(using Context): TypeOrBounds = self.tpe.stripTypeVar + def WildcardTypeTree_tpe(self: WildcardTypeTree)(using Context): Type = self.tpe.stripTypeVar type CaseDef = tpd.CaseDef @@ -1117,34 +1117,9 @@ class ReflectionCompilerInterface(val rootContext: Context) extends CompilerInte // TYPES // ///////////// - type TypeOrBounds = Types.Type - - type NoPrefix = Types.NoPrefix.type - - def NoPrefix_TypeTest(using Context): TypeTest[TypeOrBounds, NoPrefix] = new { - def runtimeClass: Class[?] = classOf[Types.NoPrefix.type] - override def unapply(x: Any): Option[NoPrefix] = - if (x == Types.NoPrefix) Some(Types.NoPrefix) else None - } - - type TypeBounds = Types.TypeBounds - - def TypeBounds_TypeTest(using Context): TypeTest[TypeOrBounds, TypeBounds] = new { - def runtimeClass: Class[?] = classOf[TypeBounds] - override def unapply(x: Any): Option[TypeBounds] = x match - case x: Types.TypeBounds => Some(x) - case _ => None - } - - def TypeBounds_apply(low: Type, hi: Type)(using Context): TypeBounds = - Types.TypeBounds(low, hi) - - def TypeBounds_low(self: TypeBounds)(using Context): Type = self.lo - def TypeBounds_hi(self: TypeBounds)(using Context): Type = self.hi - type Type = Types.Type - def Type_TypeTest(using Context): TypeTest[TypeOrBounds, Type] = new { + def Type_TypeTest(using Context): TypeTest[Type, Type] = new { def runtimeClass: Class[?] = classOf[Type] override def unapply(x: Any): Option[Type] = x match case x: TypeBounds => None @@ -1224,12 +1199,12 @@ class ReflectionCompilerInterface(val rootContext: Context) extends CompilerInte def Type_select(self: Type)(sym: Symbol)(using Context): Type = self.select(sym) - def Type_appliedTo(self: Type)(targs: List[TypeOrBounds]): Type = + def Type_appliedTo(self: Type)(targs: List[Type]): Type = self.appliedTo(targs) type ConstantType = Types.ConstantType - def ConstantType_TypeTest(using Context): TypeTest[TypeOrBounds, ConstantType] = new { + def ConstantType_TypeTest(using Context): TypeTest[Type, ConstantType] = new { def runtimeClass: Class[?] = classOf[ConstantType] override def unapply(x: Any): Option[ConstantType] = x match case tpe: Types.ConstantType => Some(tpe) @@ -1243,30 +1218,30 @@ class ReflectionCompilerInterface(val rootContext: Context) extends CompilerInte type TermRef = Types.NamedType - def TermRef_TypeTest(using Context): TypeTest[TypeOrBounds, TermRef] = new { + def TermRef_TypeTest(using Context): TypeTest[Type, TermRef] = new { def runtimeClass: Class[?] = classOf[TermRef] override def unapply(x: Any): Option[TermRef] = x match case tp: Types.TermRef => Some(tp) case _ => None } - def TermRef_apply(qual: TypeOrBounds, name: String)(using Context): TermRef = + def TermRef_apply(qual: Type, name: String)(using Context): TermRef = Types.TermRef(qual, name.toTermName) - def TermRef_qualifier(self: TermRef)(using Context): TypeOrBounds = self.prefix + def TermRef_qualifier(self: TermRef)(using Context): Type = self.prefix def TermRef_name(self: TermRef)(using Context): String = self.name.toString type TypeRef = Types.NamedType - def TypeRef_TypeTest(using Context): TypeTest[TypeOrBounds, TypeRef] = new { + def TypeRef_TypeTest(using Context): TypeTest[Type, TypeRef] = new { def runtimeClass: Class[?] = classOf[TypeRef] override def unapply(x: Any): Option[TypeRef] = x match case tp: Types.TypeRef => Some(tp) case _ => None } - def TypeRef_qualifier(self: TypeRef)(using Context): TypeOrBounds = self.prefix + def TypeRef_qualifier(self: TypeRef)(using Context): Type = self.prefix def TypeRef_name(self: TypeRef)(using Context): String = self.name.toString @@ -1276,7 +1251,7 @@ class ReflectionCompilerInterface(val rootContext: Context) extends CompilerInte type NamedTermRef = Types.NamedType - def NamedTermRef_TypeTest(using Context): TypeTest[TypeOrBounds, NamedTermRef] = new { + def NamedTermRef_TypeTest(using Context): TypeTest[Type, NamedTermRef] = new { def runtimeClass: Class[?] = classOf[NamedTermRef] override def unapply(x: Any): Option[NamedTermRef] = x match case tpe: Types.NamedType => @@ -1288,11 +1263,11 @@ class ReflectionCompilerInterface(val rootContext: Context) extends CompilerInte } def NamedTermRef_name(self: NamedTermRef)(using Context): String = self.name.toString - def NamedTermRef_qualifier(self: NamedTermRef)(using Context): TypeOrBounds = self.prefix + def NamedTermRef_qualifier(self: NamedTermRef)(using Context): Type = self.prefix type SuperType = Types.SuperType - def SuperType_TypeTest(using Context): TypeTest[TypeOrBounds, SuperType] = new { + def SuperType_TypeTest(using Context): TypeTest[Type, SuperType] = new { def runtimeClass: Class[?] = classOf[SuperType] override def unapply(x: Any): Option[SuperType] = x match case tpe: Types.SuperType => Some(tpe) @@ -1307,14 +1282,14 @@ class ReflectionCompilerInterface(val rootContext: Context) extends CompilerInte type Refinement = Types.RefinedType - def Refinement_TypeTest(using Context): TypeTest[TypeOrBounds, Refinement] = new { + def Refinement_TypeTest(using Context): TypeTest[Type, Refinement] = new { def runtimeClass: Class[?] = classOf[Refinement] override def unapply(x: Any): Option[Refinement] = x match case tpe: Types.RefinedType => Some(tpe) case _ => None } - def Refinement_apply(parent: Type, name: String, info: TypeOrBounds /* Type | TypeBounds */)(using Context): Refinement = { + def Refinement_apply(parent: Type, name: String, info: Type)(using Context): Refinement = { val name1 = info match case _: TypeBounds => name.toTypeName @@ -1324,11 +1299,11 @@ class ReflectionCompilerInterface(val rootContext: Context) extends CompilerInte def Refinement_parent(self: Refinement)(using Context): Type = self.parent def Refinement_name(self: Refinement)(using Context): String = self.refinedName.toString - def Refinement_info(self: Refinement)(using Context): TypeOrBounds = self.refinedInfo + def Refinement_info(self: Refinement)(using Context): Type = self.refinedInfo type AppliedType = Types.AppliedType - def AppliedType_TypeTest(using Context): TypeTest[TypeOrBounds, AppliedType] = new { + def AppliedType_TypeTest(using Context): TypeTest[Type, AppliedType] = new { def runtimeClass: Class[?] = classOf[AppliedType] override def unapply(x: Any): Option[AppliedType] = x match case tpe: Types.AppliedType => Some(tpe) @@ -1336,11 +1311,11 @@ class ReflectionCompilerInterface(val rootContext: Context) extends CompilerInte } def AppliedType_tycon(self: AppliedType)(using Context): Type = self.tycon - def AppliedType_args(self: AppliedType)(using Context): List[TypeOrBounds] = self.args + def AppliedType_args(self: AppliedType)(using Context): List[Type] = self.args type AnnotatedType = Types.AnnotatedType - def AnnotatedType_TypeTest(using Context): TypeTest[TypeOrBounds, AnnotatedType] = new { + def AnnotatedType_TypeTest(using Context): TypeTest[Type, AnnotatedType] = new { def runtimeClass: Class[?] = classOf[AnnotatedType] override def unapply(x: Any): Option[AnnotatedType] = x match case tpe: Types.AnnotatedType => Some(tpe) @@ -1355,7 +1330,7 @@ class ReflectionCompilerInterface(val rootContext: Context) extends CompilerInte type AndType = Types.AndType - def AndType_TypeTest(using Context): TypeTest[TypeOrBounds, AndType] = new { + def AndType_TypeTest(using Context): TypeTest[Type, AndType] = new { def runtimeClass: Class[?] = classOf[AndType] override def unapply(x: Any): Option[AndType] = x match case tpe: Types.AndType => Some(tpe) @@ -1370,7 +1345,7 @@ class ReflectionCompilerInterface(val rootContext: Context) extends CompilerInte type OrType = Types.OrType - def OrType_TypeTest(using Context): TypeTest[TypeOrBounds, OrType] = new { + def OrType_TypeTest(using Context): TypeTest[Type, OrType] = new { def runtimeClass: Class[?] = classOf[OrType] override def unapply(x: Any): Option[OrType] = x match case tpe: Types.OrType => Some(tpe) @@ -1385,7 +1360,7 @@ class ReflectionCompilerInterface(val rootContext: Context) extends CompilerInte type MatchType = Types.MatchType - def MatchType_TypeTest(using Context): TypeTest[TypeOrBounds, MatchType] = new { + def MatchType_TypeTest(using Context): TypeTest[Type, MatchType] = new { def runtimeClass: Class[?] = classOf[MatchType] override def unapply(x: Any): Option[MatchType] = x match case tpe: Types.MatchType => Some(tpe) @@ -1401,7 +1376,7 @@ class ReflectionCompilerInterface(val rootContext: Context) extends CompilerInte type ByNameType = Types.ExprType - def ByNameType_TypeTest(using Context): TypeTest[TypeOrBounds, ByNameType] = new { + def ByNameType_TypeTest(using Context): TypeTest[Type, ByNameType] = new { def runtimeClass: Class[?] = classOf[ByNameType] override def unapply(x: Any): Option[ByNameType] = x match case tpe: Types.ExprType => Some(tpe) @@ -1414,7 +1389,7 @@ class ReflectionCompilerInterface(val rootContext: Context) extends CompilerInte type ParamRef = Types.ParamRef - def ParamRef_TypeTest(using Context): TypeTest[TypeOrBounds, ParamRef] = new { + def ParamRef_TypeTest(using Context): TypeTest[Type, ParamRef] = new { def runtimeClass: Class[?] = classOf[ParamRef] override def unapply(x: Any): Option[ParamRef] = x match case tpe: Types.TypeParamRef => Some(tpe) @@ -1422,13 +1397,13 @@ class ReflectionCompilerInterface(val rootContext: Context) extends CompilerInte case _ => None } - def ParamRef_binder(self: ParamRef)(using Context): LambdaType[TypeOrBounds] = - self.binder.asInstanceOf[LambdaType[TypeOrBounds]] // Cast to tpd + def ParamRef_binder(self: ParamRef)(using Context): LambdaType = + self.binder.asInstanceOf[LambdaType] // Cast to tpd def ParamRef_paramNum(self: ParamRef)(using Context): Int = self.paramNum type ThisType = Types.ThisType - def ThisType_TypeTest(using Context): TypeTest[TypeOrBounds, ThisType] = new { + def ThisType_TypeTest(using Context): TypeTest[Type, ThisType] = new { def runtimeClass: Class[?] = classOf[ThisType] override def unapply(x: Any): Option[ThisType] = x match case tpe: Types.ThisType => Some(tpe) @@ -1439,7 +1414,7 @@ class ReflectionCompilerInterface(val rootContext: Context) extends CompilerInte type RecursiveThis = Types.RecThis - def RecursiveThis_TypeTest(using Context): TypeTest[TypeOrBounds, RecursiveThis] = new { + def RecursiveThis_TypeTest(using Context): TypeTest[Type, RecursiveThis] = new { def runtimeClass: Class[?] = classOf[RecursiveThis] override def unapply(x: Any): Option[RecursiveThis] = x match case tpe: Types.RecThis => Some(tpe) @@ -1450,7 +1425,7 @@ class ReflectionCompilerInterface(val rootContext: Context) extends CompilerInte type RecursiveType = Types.RecType - def RecursiveType_TypeTest(using Context): TypeTest[TypeOrBounds, RecursiveType] = new { + def RecursiveType_TypeTest(using Context): TypeTest[Type, RecursiveType] = new { def runtimeClass: Class[?] = classOf[RecursiveType] override def unapply(x: Any): Option[RecursiveType] = x match case tpe: Types.RecType => Some(tpe) @@ -1464,11 +1439,11 @@ class ReflectionCompilerInterface(val rootContext: Context) extends CompilerInte def RecursiveThis_recThis(self: RecursiveType)(using Context): RecursiveThis = self.recThis - type LambdaType[ParamInfo] = Types.LambdaType { type PInfo = ParamInfo } + type LambdaType = Types.LambdaType type MethodType = Types.MethodType - def MethodType_TypeTest(using Context): TypeTest[TypeOrBounds, MethodType] = new { + def MethodType_TypeTest(using Context): TypeTest[Type, MethodType] = new { def runtimeClass: Class[?] = classOf[MethodType] override def unapply(x: Any): Option[MethodType] = x match case tpe: Types.MethodType => Some(tpe) @@ -1487,7 +1462,7 @@ class ReflectionCompilerInterface(val rootContext: Context) extends CompilerInte type PolyType = Types.PolyType - def PolyType_TypeTest(using Context): TypeTest[TypeOrBounds, PolyType] = new { + def PolyType_TypeTest(using Context): TypeTest[Type, PolyType] = new { def runtimeClass: Class[?] = classOf[PolyType] override def unapply(x: Any): Option[PolyType] = x match case tpe: Types.PolyType => Some(tpe) @@ -1504,7 +1479,7 @@ class ReflectionCompilerInterface(val rootContext: Context) extends CompilerInte type TypeLambda = Types.TypeLambda - def TypeLambda_TypeTest(using Context): TypeTest[TypeOrBounds, TypeLambda] = new { + def TypeLambda_TypeTest(using Context): TypeTest[Type, TypeLambda] = new { def runtimeClass: Class[?] = classOf[TypeLambda] override def unapply(x: Any): Option[TypeLambda] = x match case tpe: Types.TypeLambda => Some(tpe) @@ -1520,6 +1495,28 @@ class ReflectionCompilerInterface(val rootContext: Context) extends CompilerInte self.newParamRef(idx) def TypeLambda_resType(self: TypeLambda)(using Context): Type = self.resType + type NoPrefix = Types.NoPrefix.type + + def NoPrefix_TypeTest(using Context): TypeTest[Type, NoPrefix] = new { + def runtimeClass: Class[?] = classOf[Types.NoPrefix.type] + override def unapply(x: Any): Option[NoPrefix] = + if (x == Types.NoPrefix) Some(Types.NoPrefix) else None + } + + type TypeBounds = Types.TypeBounds + + def TypeBounds_TypeTest(using Context): TypeTest[Type, TypeBounds] = new { + def runtimeClass: Class[?] = classOf[TypeBounds] + override def unapply(x: Any): Option[TypeBounds] = x match + case x: Types.TypeBounds => Some(x) + case _ => None + } + + def TypeBounds_apply(low: Type, hi: Type)(using Context): TypeBounds = + Types.TypeBounds(low, hi) + + def TypeBounds_low(self: TypeBounds)(using Context): Type = self.lo + def TypeBounds_hi(self: TypeBounds)(using Context): Type = self.hi ////////////////////// // IMPORT SELECTORS // diff --git a/library/src/scala/internal/tasty/CompilerInterface.scala b/library/src/scala/internal/tasty/CompilerInterface.scala index b3146276b0b2..4c1f572fb361 100644 --- a/library/src/scala/internal/tasty/CompilerInterface.scala +++ b/library/src/scala/internal/tasty/CompilerInterface.scala @@ -446,7 +446,7 @@ trait CompilerInterface extends scala.tasty.reflect.Types { def WildcardTypeTree_TypeTest(using ctx: Context): TypeTest[Tree, WildcardTypeTree] - def WildcardTypeTree_tpe(self: WildcardTypeTree)(using ctx: Context): TypeOrBounds + def WildcardTypeTree_tpe(self: WildcardTypeTree)(using ctx: Context): Type def CaseDef_TypeTest(using ctx: Context): TypeTest[Tree, CaseDef] @@ -503,14 +503,14 @@ trait CompilerInterface extends scala.tasty.reflect.Types { def NoPrefix_TypeTest(using ctx: Context): TypeTest[Tree, NoPrefix] - def TypeBounds_TypeTest(using ctx: Context): TypeTest[TypeOrBounds, TypeBounds] + def TypeBounds_TypeTest(using ctx: Context): TypeTest[Type, TypeBounds] def TypeBounds_apply(low: Type, hi: Type)(using ctx: Context): TypeBounds def TypeBounds_low(self: TypeBounds)(using ctx: Context): Type def TypeBounds_hi(self: TypeBounds)(using ctx: Context): Type - def Type_TypeTest(using ctx: Context): TypeTest[TypeOrBounds, Type] + def Type_TypeTest(using ctx: Context): TypeTest[Type, Type] def Type_apply(clazz: Class[_])(using ctx: Context): Type @@ -605,70 +605,70 @@ trait CompilerInterface extends scala.tasty.reflect.Types { def Type_select(self: Type)(sym: Symbol)(using ctx: Context): Type /** The current type applied to given type arguments: `this[targ0, ..., targN]` */ - def Type_appliedTo(self: Type)(targs: List[TypeOrBounds]): Type + def Type_appliedTo(self: Type)(targs: List[Type]): Type - def ConstantType_TypeTest(using ctx: Context): TypeTest[TypeOrBounds, ConstantType] + def ConstantType_TypeTest(using ctx: Context): TypeTest[Type, ConstantType] def ConstantType_apply(const : Constant)(using ctx : Context) : ConstantType def ConstantType_constant(self: ConstantType)(using ctx: Context): Constant - def TermRef_TypeTest(using ctx: Context): TypeTest[TypeOrBounds, TermRef] + def TermRef_TypeTest(using ctx: Context): TypeTest[Type, TermRef] - def TermRef_apply(qual: TypeOrBounds, name: String)(using ctx: Context): TermRef + def TermRef_apply(qual: Type, name: String)(using ctx: Context): TermRef - def TermRef_qualifier(self: TermRef)(using ctx: Context): TypeOrBounds + def TermRef_qualifier(self: TermRef)(using ctx: Context): Type def TermRef_name(self: TermRef)(using ctx: Context): String - def TypeRef_TypeTest(using ctx: Context): TypeTest[TypeOrBounds, TypeRef] + def TypeRef_TypeTest(using ctx: Context): TypeTest[Type, TypeRef] - def TypeRef_qualifier(self: TypeRef)(using ctx: Context): TypeOrBounds + def TypeRef_qualifier(self: TypeRef)(using ctx: Context): Type def TypeRef_name(self: TypeRef)(using ctx: Context): String def TypeRef_isOpaqueAlias(self: TypeRef)(using ctx: Context): Boolean def TypeRef_translucentSuperType(self: TypeRef)(using ctx: Context): Type - def SuperType_TypeTest(using ctx: Context): TypeTest[TypeOrBounds, SuperType] + def SuperType_TypeTest(using ctx: Context): TypeTest[Type, SuperType] def SuperType_apply(thistpe: Type, supertpe: Type)(using ctx: Context): SuperType def SuperType_thistpe(self: SuperType)(using ctx: Context): Type def SuperType_supertpe(self: SuperType)(using ctx: Context): Type - def Refinement_TypeTest(using ctx: Context): TypeTest[TypeOrBounds, Refinement] + def Refinement_TypeTest(using ctx: Context): TypeTest[Type, Refinement] - def Refinement_apply(parent: Type, name: String, info: TypeOrBounds /* Type | TypeBounds */)(using ctx: Context): Refinement + def Refinement_apply(parent: Type, name: String, info: Type)(using ctx: Context): Refinement def Refinement_parent(self: Refinement)(using ctx: Context): Type def Refinement_name(self: Refinement)(using ctx: Context): String - def Refinement_info(self: Refinement)(using ctx: Context): TypeOrBounds + def Refinement_info(self: Refinement)(using ctx: Context): Type - def AppliedType_TypeTest(using ctx: Context): TypeTest[TypeOrBounds, AppliedType] + def AppliedType_TypeTest(using ctx: Context): TypeTest[Type, AppliedType] def AppliedType_tycon(self: AppliedType)(using ctx: Context): Type - def AppliedType_args(self: AppliedType)(using ctx: Context): List[TypeOrBounds] + def AppliedType_args(self: AppliedType)(using ctx: Context): List[Type] - def AnnotatedType_TypeTest(using ctx: Context): TypeTest[TypeOrBounds, AnnotatedType] + def AnnotatedType_TypeTest(using ctx: Context): TypeTest[Type, AnnotatedType] def AnnotatedType_apply(underlying: Type, annot: Term)(using ctx: Context): AnnotatedType def AnnotatedType_underlying(self: AnnotatedType)(using ctx: Context): Type def AnnotatedType_annot(self: AnnotatedType)(using ctx: Context): Term - def AndType_TypeTest(using ctx: Context): TypeTest[TypeOrBounds, AndType] + def AndType_TypeTest(using ctx: Context): TypeTest[Type, AndType] def AndType_apply(lhs: Type, rhs: Type)(using ctx: Context): AndType def AndType_left(self: AndType)(using ctx: Context): Type def AndType_right(self: AndType)(using ctx: Context): Type - def OrType_TypeTest(using ctx: Context): TypeTest[TypeOrBounds, OrType] + def OrType_TypeTest(using ctx: Context): TypeTest[Type, OrType] def OrType_apply(lhs : Type, rhs : Type)(using ctx : Context): OrType def OrType_left(self: OrType)(using ctx: Context): Type def OrType_right(self: OrType)(using ctx: Context): Type - def MatchType_TypeTest(using ctx: Context): TypeTest[TypeOrBounds, MatchType] + def MatchType_TypeTest(using ctx: Context): TypeTest[Type, MatchType] def MatchType_apply(bound: Type, scrutinee: Type, cases: List[Type])(using ctx: Context): MatchType @@ -676,26 +676,26 @@ trait CompilerInterface extends scala.tasty.reflect.Types { def MatchType_scrutinee(self: MatchType)(using ctx: Context): Type def MatchType_cases(self: MatchType)(using ctx: Context): List[Type] - def ByNameType_TypeTest(using ctx: Context): TypeTest[TypeOrBounds, ByNameType] + def ByNameType_TypeTest(using ctx: Context): TypeTest[Type, ByNameType] def ByNameType_apply(underlying: Type)(using ctx: Context): Type def ByNameType_underlying(self: ByNameType)(using ctx: Context): Type - def ParamRef_TypeTest(using ctx: Context): TypeTest[TypeOrBounds, ParamRef] + def ParamRef_TypeTest(using ctx: Context): TypeTest[Type, ParamRef] - def ParamRef_binder(self: ParamRef)(using ctx: Context): LambdaType[TypeOrBounds] + def ParamRef_binder(self: ParamRef)(using ctx: Context): LambdaType def ParamRef_paramNum(self: ParamRef)(using ctx: Context): Int - def ThisType_TypeTest(using ctx: Context): TypeTest[TypeOrBounds, ThisType] + def ThisType_TypeTest(using ctx: Context): TypeTest[Type, ThisType] def ThisType_tref(self: ThisType)(using ctx: Context): Type - def RecursiveThis_TypeTest(using ctx: Context): TypeTest[TypeOrBounds, RecursiveThis] + def RecursiveThis_TypeTest(using ctx: Context): TypeTest[Type, RecursiveThis] def RecursiveThis_binder(self: RecursiveThis)(using ctx: Context): RecursiveType - def RecursiveType_TypeTest(using ctx: Context): TypeTest[TypeOrBounds, RecursiveType] + def RecursiveType_TypeTest(using ctx: Context): TypeTest[Type, RecursiveType] /** Create a RecType, normalizing its contents. This means: * @@ -711,7 +711,7 @@ trait CompilerInterface extends scala.tasty.reflect.Types { def RecursiveThis_recThis(self: RecursiveType)(using ctx: Context): RecursiveThis - def MethodType_TypeTest(using ctx: Context): TypeTest[TypeOrBounds, MethodType] + def MethodType_TypeTest(using ctx: Context): TypeTest[Type, MethodType] def MethodType_apply(paramNames: List[String])(paramInfosExp: MethodType => List[Type], resultTypeExp: MethodType => Type): MethodType @@ -722,7 +722,7 @@ trait CompilerInterface extends scala.tasty.reflect.Types { def MethodType_paramTypes(self: MethodType)(using ctx: Context): List[Type] def MethodType_resType(self: MethodType)(using ctx: Context): Type - def PolyType_TypeTest(using ctx: Context): TypeTest[TypeOrBounds, PolyType] + def PolyType_TypeTest(using ctx: Context): TypeTest[Type, PolyType] def PolyType_apply(paramNames: List[String])(paramBoundsExp: PolyType => List[TypeBounds], resultTypeExp: PolyType => Type)(using ctx: Context): PolyType @@ -731,7 +731,7 @@ trait CompilerInterface extends scala.tasty.reflect.Types { def PolyType_paramBounds(self: PolyType)(using ctx: Context): List[TypeBounds] def PolyType_resType(self: PolyType)(using ctx: Context): Type - def TypeLambda_TypeTest(using ctx: Context): TypeTest[TypeOrBounds, TypeLambda] + def TypeLambda_TypeTest(using ctx: Context): TypeTest[Type, TypeLambda] def TypeLambda_apply(paramNames: List[String], boundsFn: TypeLambda => List[TypeBounds], bodyFn: TypeLambda => Type): TypeLambda diff --git a/library/src/scala/tasty/Reflection.scala b/library/src/scala/tasty/Reflection.scala index ba6daf3fa3ca..135a89d75f3a 100644 --- a/library/src/scala/tasty/Reflection.scala +++ b/library/src/scala/tasty/Reflection.scala @@ -1185,7 +1185,7 @@ trait Reflection extends reflect.Types { reflectSelf: CompilerInterface => def unapply(x: WildcardTypeTree)(using ctx: Context): Boolean = true extension (self: WildcardTypeTree): - def tpe(using ctx: Context): TypeOrBounds = reflectSelf.WildcardTypeTree_tpe(self) + def tpe(using ctx: Context): Type = reflectSelf.WildcardTypeTree_tpe(self) end extension end WildcardTypeTree @@ -1340,25 +1340,10 @@ trait Reflection extends reflect.Types { reflectSelf: CompilerInterface => def typeOf[T](using qtype: scala.quoted.Type[T], ctx: Context): Type = qtype.asInstanceOf[scala.internal.quoted.Type[T]].typeTree.asInstanceOf[TypeTree].tpe - given TypeOrBoundsOps as AnyRef: - /** Members of `TypeOrBounds` */ - extension (tpe: TypeOrBounds): - /** Shows the tree as extractors */ - def showExtractors(using ctx: Context): String = - new ExtractorsPrinter[reflectSelf.type](reflectSelf).showTypeOrBounds(tpe) - - /** Shows the tree as fully typed source code */ - def show(using ctx: Context): String = - tpe.showWith(SyntaxHighlight.plain) - - /** Shows the tree as fully typed source code */ - def showWith(syntaxHighlight: SyntaxHighlight)(using ctx: Context): String = - new SourceCodePrinter[reflectSelf.type](reflectSelf)(syntaxHighlight).showTypeOrBounds(tpe) - end extension // ----- Types ---------------------------------------------------- - given (using ctx: Context) as TypeTest[TypeOrBounds, Type] = reflectSelf.Type_TypeTest + given (using ctx: Context) as TypeTest[Type, Type] = reflectSelf.Type_TypeTest given TypeOps as Type.type = Type object Type: @@ -1368,27 +1353,39 @@ trait Reflection extends reflect.Types { reflectSelf: CompilerInterface => extension (self: Type): + /** Shows the tree as extractors */ + def showExtractors(using ctx: Context): String = + new ExtractorsPrinter[reflectSelf.type](reflectSelf).showType(self) + + /** Shows the tree as fully typed source code */ + def show(using ctx: Context): String = + self.showWith(SyntaxHighlight.plain) + + /** Shows the tree as fully typed source code */ + def showWith(syntaxHighlight: SyntaxHighlight)(using ctx: Context): String = + new SourceCodePrinter[reflectSelf.type](reflectSelf)(syntaxHighlight).showType(self) + /** Convert `Type` to an `quoted.Type[_]` */ def seal(using ctx: Context): scala.quoted.Type[_] = new scala.internal.quoted.Type(Inferred(self), reflectSelf.compilerId) /** Is `self` type the same as `that` type? - * This is the case iff `self <:< that` and `that <:< self`. - */ + * This is the case iff `self <:< that` and `that <:< self`. + */ def =:=(that: Type)(using ctx: Context): Boolean = reflectSelf.Type_isTypeEq(self)(that) /** Is this type a subtype of that type? */ def <:<(that: Type)(using ctx: Context): Boolean = reflectSelf.Type_isSubType(self)(that) /** Widen from singleton type to its underlying non-singleton - * base type by applying one or more `underlying` dereferences, - * Also go from => T to T. - * Identity for all other types. Example: - * - * class Outer { class C ; val x: C } - * def o: Outer - * .widen = o.C - */ + * base type by applying one or more `underlying` dereferences, + * Also go from => T to T. + * Identity for all other types. Example: + * + * class Outer { class C ; val x: C } + * def o: Outer + * .widen = o.C + */ def widen(using ctx: Context): Type = reflectSelf.Type_widen(self) /** Widen from TermRef to its underlying non-termref @@ -1403,8 +1400,8 @@ trait Reflection extends reflect.Types { reflectSelf: CompilerInterface => def dealias(using ctx: Context): Type = reflectSelf.Type_dealias(self) /** A simplified version of this type which is equivalent wrt =:= to this type. - * Reduces typerefs, applied match types, and and or types. - */ + * Reduces typerefs, applied match types, and and or types. + */ def simplified(using ctx: Context): Type = reflectSelf.Type_simplified(self) def classSymbol(using ctx: Context): Option[Symbol] = reflectSelf.Type_classSymbol(self) @@ -1416,15 +1413,14 @@ trait Reflection extends reflect.Types { reflectSelf: CompilerInterface => /** The base classes of this type with the class itself as first element. */ def baseClasses(using ctx: Context): List[Symbol] = reflectSelf.Type_baseClasses(self) - - /** The least type instance of given class which is a super-type - * of this type. Example: - * {{{ - * class D[T] - * class C extends p.D[Int] - * ThisType(C).baseType(D) = p.D[Int] - * }}} - */ + /** The least type instance of given class which is a super-type + * of this type. Example: + * {{{ + * class D[T] + * class C extends p.D[Int] + * ThisType(C).baseType(D) = p.D[Int] + * }}} + */ def baseType(cls: Symbol)(using ctx: Context): Type = reflectSelf.Type_baseType(self)(cls) /** Is this type an instance of a non-bottom subclass of the given class `cls`? */ @@ -1432,47 +1428,47 @@ trait Reflection extends reflect.Types { reflectSelf: CompilerInterface => reflectSelf.Type_derivesFrom(self)(cls) /** Is this type a function type? - * - * @return true if the dealised type of `self` without refinement is `FunctionN[T1, T2, ..., Tn]` - * - * @note The function - * - * - returns true for `given Int => Int` and `erased Int => Int` - * - returns false for `List[Int]`, despite that `List[Int] <:< Int => Int`. - */ + * + * @return true if the dealised type of `self` without refinement is `FunctionN[T1, T2, ..., Tn]` + * + * @note The function + * + * - returns true for `given Int => Int` and `erased Int => Int` + * - returns false for `List[Int]`, despite that `List[Int] <:< Int => Int`. + */ def isFunctionType(using ctx: Context): Boolean = reflectSelf.Type_isFunctionType(self) /** Is this type an context function type? - * - * @see `isFunctionType` - */ + * + * @see `isFunctionType` + */ def isContextFunctionType(using ctx: Context): Boolean = reflectSelf.Type_isContextFunctionType(self) /** Is this type an erased function type? - * - * @see `isFunctionType` - */ + * + * @see `isFunctionType` + */ def isErasedFunctionType(using ctx: Context): Boolean = reflectSelf.Type_isErasedFunctionType(self) /** Is this type a dependent function type? - * - * @see `isFunctionType` - */ + * + * @see `isFunctionType` + */ def isDependentFunctionType(using ctx: Context): Boolean = reflectSelf.Type_isDependentFunctionType(self) /** The type , reduced if possible */ def select(sym: Symbol)(using ctx: Context): Type = reflectSelf.Type_select(self)(sym) /** The current type applied to given type arguments: `this[targ]` */ - def appliedTo(targ: TypeOrBounds): Type = reflectSelf.Type_appliedTo(self)(List(targ)) + def appliedTo(targ: Type): Type = reflectSelf.Type_appliedTo(self)(List(targ)) /** The current type applied to given type arguments: `this[targ0, ..., targN]` */ - def appliedTo(targs: List[TypeOrBounds]): Type = reflectSelf.Type_appliedTo(self)(targs) + def appliedTo(targs: List[Type]): Type = reflectSelf.Type_appliedTo(self)(targs) end extension end Type - given (using ctx: Context) as TypeTest[TypeOrBounds, ConstantType] = reflectSelf.ConstantType_TypeTest + given (using ctx: Context) as TypeTest[Type, ConstantType] = reflectSelf.ConstantType_TypeTest given ConstantTypeOps as ConstantType.type = ConstantType object ConstantType: @@ -1485,31 +1481,31 @@ trait Reflection extends reflect.Types { reflectSelf: CompilerInterface => end ConstantType - given (using ctx: Context) as TypeTest[TypeOrBounds, TermRef] = reflectSelf.TermRef_TypeTest + given (using ctx: Context) as TypeTest[Type, TermRef] = reflectSelf.TermRef_TypeTest given TermRefOps as TermRef.type = TermRef object TermRef: - def apply(qual: TypeOrBounds, name: String)(using ctx: Context): TermRef = + def apply(qual: Type, name: String)(using ctx: Context): TermRef = reflectSelf.TermRef_apply(qual, name) - def unapply(x: TermRef)(using ctx: Context): Option[(TypeOrBounds /* Type | NoPrefix */, String)] = + def unapply(x: TermRef)(using ctx: Context): Option[(Type, String)] = Some((x.qualifier, x.name)) extension (self: TermRef): - def qualifier(using ctx: Context): TypeOrBounds /* Type | NoPrefix */ = reflectSelf.TermRef_qualifier(self) + def qualifier(using ctx: Context): Type = reflectSelf.TermRef_qualifier(self) def name(using ctx: Context): String = reflectSelf.TermRef_name(self) end extension end TermRef - given (using ctx: Context) as TypeTest[TypeOrBounds, TypeRef] = reflectSelf.TypeRef_TypeTest + given (using ctx: Context) as TypeTest[Type, TypeRef] = reflectSelf.TypeRef_TypeTest given TypeRefOps as TypeRef.type = TypeRef object TypeRef: - def unapply(x: TypeRef)(using ctx: Context): Option[(TypeOrBounds /* Type | NoPrefix */, String)] = + def unapply(x: TypeRef)(using ctx: Context): Option[(Type, String)] = Some((x.qualifier, x.name)) extension (self: TypeRef): - def qualifier(using ctx: Context): TypeOrBounds /* Type | NoPrefix */ = reflectSelf.TypeRef_qualifier(self) + def qualifier(using ctx: Context): Type = reflectSelf.TypeRef_qualifier(self) def name(using ctx: Context): String = reflectSelf.TypeRef_name(self) def isOpaqueAlias(using ctx: Context): Boolean = reflectSelf.TypeRef_isOpaqueAlias(self) def translucentSuperType(using ctx: Context): Type = reflectSelf.TypeRef_translucentSuperType(self) @@ -1517,7 +1513,7 @@ trait Reflection extends reflect.Types { reflectSelf: CompilerInterface => end TypeRef - given (using ctx: Context) as TypeTest[TypeOrBounds, SuperType] = reflectSelf.SuperType_TypeTest + given (using ctx: Context) as TypeTest[Type, SuperType] = reflectSelf.SuperType_TypeTest given SuperTypeOps as SuperType.type = SuperType object SuperType: @@ -1534,39 +1530,39 @@ trait Reflection extends reflect.Types { reflectSelf: CompilerInterface => end SuperType - given (using ctx: Context) as TypeTest[TypeOrBounds, Refinement] = reflectSelf.Refinement_TypeTest + given (using ctx: Context) as TypeTest[Type, Refinement] = reflectSelf.Refinement_TypeTest given RefinementOps as Refinement.type = Refinement object Refinement: - def apply(parent: Type, name: String, info: TypeOrBounds /* Type | TypeBounds */)(using ctx: Context): Refinement = + def apply(parent: Type, name: String, info: Type)(using ctx: Context): Refinement = reflectSelf.Refinement_apply(parent, name, info) - def unapply(x: Refinement)(using ctx: Context): Option[(Type, String, TypeOrBounds /* Type | TypeBounds */)] = + def unapply(x: Refinement)(using ctx: Context): Option[(Type, String, Type)] = Some((x.parent, x.name, x.info)) extension (self: Refinement): def parent(using ctx: Context): Type = reflectSelf.Refinement_parent(self) def name(using ctx: Context): String = reflectSelf.Refinement_name(self) - def info(using ctx: Context): TypeOrBounds = reflectSelf.Refinement_info(self) + def info(using ctx: Context): Type = reflectSelf.Refinement_info(self) end extension end Refinement - given (using ctx: Context) as TypeTest[TypeOrBounds, AppliedType] = reflectSelf.AppliedType_TypeTest + given (using ctx: Context) as TypeTest[Type, AppliedType] = reflectSelf.AppliedType_TypeTest given AppliedTypeOps as AppliedType.type = AppliedType object AppliedType: - def unapply(x: AppliedType)(using ctx: Context): Option[(Type, List[TypeOrBounds])] = + def unapply(x: AppliedType)(using ctx: Context): Option[(Type, List[Type])] = Some((x.tycon, x.args)) extension (self: AppliedType): def tycon(using ctx: Context): Type = reflectSelf.AppliedType_tycon(self) - def args(using ctx: Context): List[TypeOrBounds] = reflectSelf.AppliedType_args(self) + def args(using ctx: Context): List[Type] = reflectSelf.AppliedType_args(self) end extension end AppliedType - given (using ctx: Context) as TypeTest[TypeOrBounds, AnnotatedType] = reflectSelf.AnnotatedType_TypeTest + given (using ctx: Context) as TypeTest[Type, AnnotatedType] = reflectSelf.AnnotatedType_TypeTest given AnnotatedTypeOps as AnnotatedType.type = AnnotatedType object AnnotatedType: @@ -1582,7 +1578,7 @@ trait Reflection extends reflect.Types { reflectSelf: CompilerInterface => end AnnotatedType - given (using ctx: Context) as TypeTest[TypeOrBounds, AndType] = reflectSelf.AndType_TypeTest + given (using ctx: Context) as TypeTest[Type, AndType] = reflectSelf.AndType_TypeTest given AndTypeOps as AndType.type = AndType object AndType: @@ -1598,7 +1594,7 @@ trait Reflection extends reflect.Types { reflectSelf: CompilerInterface => end AndType - given (using ctx: Context) as TypeTest[TypeOrBounds, OrType] = reflectSelf.OrType_TypeTest + given (using ctx: Context) as TypeTest[Type, OrType] = reflectSelf.OrType_TypeTest given OrTypeOps as OrType.type = OrType object OrType: @@ -1613,7 +1609,7 @@ trait Reflection extends reflect.Types { reflectSelf: CompilerInterface => end OrType - given (using ctx: Context) as TypeTest[TypeOrBounds, MatchType] = reflectSelf.MatchType_TypeTest + given (using ctx: Context) as TypeTest[Type, MatchType] = reflectSelf.MatchType_TypeTest given MatchTypeOps as MatchType.type = MatchType object MatchType: @@ -1638,7 +1634,7 @@ trait Reflection extends reflect.Types { reflectSelf: CompilerInterface => Type(classOf[MatchCase[_,_]]) } - given (using ctx: Context) as TypeTest[TypeOrBounds, ByNameType] = reflectSelf.ByNameType_TypeTest + given (using ctx: Context) as TypeTest[Type, ByNameType] = reflectSelf.ByNameType_TypeTest given ByNameTypeOps as ByNameType.type = ByNameType object ByNameType: @@ -1651,21 +1647,21 @@ trait Reflection extends reflect.Types { reflectSelf: CompilerInterface => end ByNameType - given (using ctx: Context) as TypeTest[TypeOrBounds, ParamRef] = reflectSelf.ParamRef_TypeTest + given (using ctx: Context) as TypeTest[Type, ParamRef] = reflectSelf.ParamRef_TypeTest given ParamRefOps as ParamRef.type = ParamRef object ParamRef: - def unapply(x: ParamRef)(using ctx: Context): Option[(LambdaType[TypeOrBounds], Int)] = + def unapply(x: ParamRef)(using ctx: Context): Option[(LambdaType, Int)] = Some((x.binder, x.paramNum)) extension (self: ParamRef): - def binder(using ctx: Context): LambdaType[TypeOrBounds] = reflectSelf.ParamRef_binder(self) + def binder(using ctx: Context): LambdaType = reflectSelf.ParamRef_binder(self) def paramNum(using ctx: Context): Int = reflectSelf.ParamRef_paramNum(self) end extension end ParamRef - given (using ctx: Context) as TypeTest[TypeOrBounds, ThisType] = reflectSelf.ThisType_TypeTest + given (using ctx: Context) as TypeTest[Type, ThisType] = reflectSelf.ThisType_TypeTest given ThisTypeOps as ThisType.type = ThisType object ThisType: @@ -1677,7 +1673,7 @@ trait Reflection extends reflect.Types { reflectSelf: CompilerInterface => end ThisType - given (using ctx: Context) as TypeTest[TypeOrBounds, RecursiveThis] = reflectSelf.RecursiveThis_TypeTest + given (using ctx: Context) as TypeTest[Type, RecursiveThis] = reflectSelf.RecursiveThis_TypeTest given RecursiveThisOps as RecursiveThis.type = RecursiveThis object RecursiveThis: @@ -1689,7 +1685,7 @@ trait Reflection extends reflect.Types { reflectSelf: CompilerInterface => end RecursiveThis - given (using ctx: Context) as TypeTest[TypeOrBounds, RecursiveType] = reflectSelf.RecursiveType_TypeTest + given (using ctx: Context) as TypeTest[Type, RecursiveType] = reflectSelf.RecursiveType_TypeTest given RecursiveTypeOps as RecursiveType.type = RecursiveType object RecursiveType: @@ -1714,7 +1710,7 @@ trait Reflection extends reflect.Types { reflectSelf: CompilerInterface => end RecursiveType - given (using ctx: Context) as TypeTest[TypeOrBounds, MethodType] = reflectSelf.MethodType_TypeTest + given (using ctx: Context) as TypeTest[Type, MethodType] = reflectSelf.MethodType_TypeTest given MethodTypeOps as MethodType.type = MethodType object MethodType: @@ -1735,7 +1731,7 @@ trait Reflection extends reflect.Types { reflectSelf: CompilerInterface => end MethodType - given (using ctx: Context) as TypeTest[TypeOrBounds, PolyType] = reflectSelf.PolyType_TypeTest + given (using ctx: Context) as TypeTest[Type, PolyType] = reflectSelf.PolyType_TypeTest given PolyTypeOps as PolyType.type = PolyType object PolyType: @@ -1753,7 +1749,7 @@ trait Reflection extends reflect.Types { reflectSelf: CompilerInterface => end PolyType - given (using ctx: Context) as TypeTest[TypeOrBounds, TypeLambda] = reflectSelf.TypeLambda_TypeTest + given (using ctx: Context) as TypeTest[Type, TypeLambda] = reflectSelf.TypeLambda_TypeTest given TypeLambdaOps as TypeLambda.type = TypeLambda object TypeLambda: @@ -1773,7 +1769,7 @@ trait Reflection extends reflect.Types { reflectSelf: CompilerInterface => // ----- TypeBounds ----------------------------------------------- - given (using ctx: Context) as TypeTest[TypeOrBounds, TypeBounds] = reflectSelf.TypeBounds_TypeTest + given (using ctx: Context) as TypeTest[Type, TypeBounds] = reflectSelf.TypeBounds_TypeTest given TypeBoundsOps as TypeBounds.type = TypeBounds object TypeBounds: @@ -1790,7 +1786,7 @@ trait Reflection extends reflect.Types { reflectSelf: CompilerInterface => // ----- NoPrefix ------------------------------------------------- - given (using ctx: Context) as TypeTest[TypeOrBounds, NoPrefix] = reflectSelf.NoPrefix_TypeTest + given (using ctx: Context) as TypeTest[Type, NoPrefix] = reflectSelf.NoPrefix_TypeTest object NoPrefix: def unapply(x: NoPrefix)(using ctx: Context): Boolean = true diff --git a/library/src/scala/tasty/reflect/ExtractorsPrinter.scala b/library/src/scala/tasty/reflect/ExtractorsPrinter.scala index 8eb832b7b1e7..87393bb78dd9 100644 --- a/library/src/scala/tasty/reflect/ExtractorsPrinter.scala +++ b/library/src/scala/tasty/reflect/ExtractorsPrinter.scala @@ -7,7 +7,7 @@ class ExtractorsPrinter[R <: Reflection & Singleton](val tasty: R) extends Print def showTree(tree: Tree)(using ctx: Context): String = new Buffer().visitTree(tree).result() - def showTypeOrBounds(tpe: TypeOrBounds)(using ctx: Context): String = + def showType(tpe: Type)(using ctx: Context): String = new Buffer().visitType(tpe).result() def showConstant(const: Constant)(using ctx: Context): String = @@ -183,7 +183,7 @@ class ExtractorsPrinter[R <: Reflection & Singleton](val tasty: R) extends Print visitType(value) += ")" } - def visitType(x: TypeOrBounds): Buffer = x match { + def visitType(x: Type): Buffer = x match { case ConstantType(value) => this += "ConstantType(" += value += ")" case TermRef(qual, name) => @@ -275,9 +275,9 @@ class ExtractorsPrinter[R <: Reflection & Singleton](val tasty: R) extends Print } private implicit class TypeOps(buff: Buffer) { - def +=(x: TypeOrBounds): Buffer = { visitType(x); buff } - def +=(x: Option[TypeOrBounds]): Buffer = { visitOption(x, visitType); buff } - def ++=(x: List[TypeOrBounds]): Buffer = { visitList(x, visitType); buff } + def +=(x: Type): Buffer = { visitType(x); buff } + def +=(x: Option[Type]): Buffer = { visitOption(x, visitType); buff } + def ++=(x: List[Type]): Buffer = { visitList(x, visitType); buff } } private implicit class IdOps(buff: Buffer) { diff --git a/library/src/scala/tasty/reflect/Printer.scala b/library/src/scala/tasty/reflect/Printer.scala index 1809e1883d76..0e95716b233d 100644 --- a/library/src/scala/tasty/reflect/Printer.scala +++ b/library/src/scala/tasty/reflect/Printer.scala @@ -9,8 +9,8 @@ trait Printer[R <: Reflection & Singleton] { /** Show a String representation of a tasty.Tree */ def showTree(tree: tasty.Tree)(using ctx: tasty.Context): String - /** Show a String representation of a tasty.TypeOrBounds */ - def showTypeOrBounds(tpe: tasty.TypeOrBounds)(using ctx: tasty.Context): String + /** Show a String representation of a tasty.Type */ + def showType(tpe: tasty.Type)(using ctx: tasty.Context): String /** Show a String representation of a tasty.Constant */ def showConstant(const: tasty.Constant)(using ctx: tasty.Context): String diff --git a/library/src/scala/tasty/reflect/SourceCodePrinter.scala b/library/src/scala/tasty/reflect/SourceCodePrinter.scala index 911d944528e9..8d8ba4aed8c6 100644 --- a/library/src/scala/tasty/reflect/SourceCodePrinter.scala +++ b/library/src/scala/tasty/reflect/SourceCodePrinter.scala @@ -12,8 +12,8 @@ class SourceCodePrinter[R <: Reflection & Singleton](val tasty: R)(syntaxHighlig def showTree(tree: Tree)(using ctx: Context): String = (new Buffer).printTree(tree).result() - def showTypeOrBounds(tpe: TypeOrBounds)(using ctx: Context): String = - (new Buffer).printTypeOrBound(tpe)(using None).result() + def showType(tpe: Type)(using ctx: Context): String = + (new Buffer).printType(tpe)(using None).result() def showConstant(const: Constant)(using ctx: Context): String = (new Buffer).printConstant(const).result() @@ -519,7 +519,7 @@ class SourceCodePrinter[R <: Reflection & Singleton](val tasty: R)(syntaxHighlig printTypeTree(hi) case tpt: WildcardTypeTree => - printTypeOrBound(tpt.tpe) + printType(tpt.tpe) case tpt: TypeTree => printTypeTree(tpt) @@ -717,12 +717,12 @@ class SourceCodePrinter[R <: Reflection & Singleton](val tasty: R)(syntaxHighlig this } - def printTypesOrBounds(types: List[TypeOrBounds], sep: String)(using elideThis: Option[Symbol]): Buffer = { - def printSeparated(list: List[TypeOrBounds]): Unit = list match { + def printTypesOrBounds(types: List[Type], sep: String)(using elideThis: Option[Symbol]): Buffer = { + def printSeparated(list: List[Type]): Unit = list match { case Nil => - case x :: Nil => printTypeOrBound(x) + case x :: Nil => printType(x) case x :: xs => - printTypeOrBound(x) + printType(x) this += sep printSeparated(xs) } @@ -760,7 +760,7 @@ class SourceCodePrinter[R <: Reflection & Singleton](val tasty: R)(syntaxHighlig argCons.rhs match { case rhs: TypeBoundsTree => printBoundsTree(rhs) case rhs: WildcardTypeTree => - printTypeOrBound(rhs.tpe) + printType(rhs.tpe) case rhs @ LambdaTypeTree(tparams, body) => def printParam(t: Tree /*TypeTree | TypeBoundsTree*/): Unit = t match { case t: TypeBoundsTree => printBoundsTree(t) @@ -923,7 +923,7 @@ class SourceCodePrinter[R <: Reflection & Singleton](val tasty: R)(syntaxHighlig case Typed(Ident("_"), tpt) => this += "_: " - printTypeOrBoundsTree(tpt) + printTypeTree(tpt) case v: Term => printTree(v) @@ -960,7 +960,7 @@ class SourceCodePrinter[R <: Reflection & Singleton](val tasty: R)(syntaxHighlig this += " <: " printTypeTree(hi) case tpt: WildcardTypeTree => - printTypeOrBound(tpt.tpe) + printType(tpt.tpe) case tpt: TypeTree => printTypeTree(tpt) } @@ -1059,15 +1059,6 @@ class SourceCodePrinter[R <: Reflection & Singleton](val tasty: R)(syntaxHighlig } - def printTypeOrBound(tpe: TypeOrBounds)(using elideThis: Option[Symbol]): Buffer = tpe match { - case tpe@TypeBounds(lo, hi) => - this += "_ >: " - printType(lo) - this += " <: " - printType(hi) - case tpe: Type => printType(tpe) - } - /** Print type * * @param elideThis Shoud printing elide `C.this` for the given class `C`? @@ -1112,7 +1103,7 @@ class SourceCodePrinter[R <: Reflection & Singleton](val tasty: R)(syntaxHighlig case ThisType(tp) if tp.typeSymbol == defn.RootClass || tp.typeSymbol == defn.EmptyPackageClass => this += highlightTypeDef(name) case _ => - printTypeOrBound(prefix) + printType(prefix) if (name != "package") this += "." += highlightTypeDef(name) this @@ -1170,7 +1161,7 @@ class SourceCodePrinter[R <: Reflection & Singleton](val tasty: R)(syntaxHighlig case NoPrefix() => case ThisType(tp) if tp.typeSymbol == defn.RootClass || tp.typeSymbol == defn.EmptyPackageClass => case _ => - printTypeOrBound(prefix) + printType(prefix) this += "." } this += highlightTypeDef(name.stripSuffix("$")) @@ -1185,7 +1176,7 @@ class SourceCodePrinter[R <: Reflection & Singleton](val tasty: R)(syntaxHighlig case TypeLambda(paramNames, tparams, body) => inSquare(printMethodicTypeParams(paramNames, tparams)) this += highlightTypeDef(" => ") - printTypeOrBound(body) + printType(body) case ParamRef(lambda, idx) => lambda match { @@ -1210,17 +1201,23 @@ class SourceCodePrinter[R <: Reflection & Singleton](val tasty: R)(syntaxHighlig case tpe: PolyType => this += "[" printList(tpe.paramNames.zip(tpe.paramBounds), ", ", - (x: (String, TypeBounds)) => (this += x._1 += " ").printTypeOrBound(x._2)) + (x: (String, TypeBounds)) => (this += x._1 += " ").printType(x._2)) this += "]" printType(tpe.resType) case tpe: TypeLambda => this += "[" printList(tpe.paramNames.zip(tpe.paramBounds), ", ", - (x: (String, TypeBounds)) => (this += x._1 += " ").printTypeOrBound(x._2)) + (x: (String, TypeBounds)) => (this += x._1 += " ").printType(x._2)) this += "] => " printType(tpe.resType) + case tpe@TypeBounds(lo, hi) => + this += "_ >: " + printType(lo) + this += " <: " + printType(hi) + case _ => throw new MatchError(tpe.showExtractors) } @@ -1266,7 +1263,7 @@ class SourceCodePrinter[R <: Reflection & Singleton](val tasty: R)(syntaxHighlig } def printRefinement(tpe: Type)(using elideThis: Option[Symbol]): Buffer = { - def printMethodicType(tp: TypeOrBounds): Unit = tp match { + def printMethodicType(tp: Type): Unit = tp match { case tp @ MethodType(paramNames, params, res) => inParens(printMethodicTypeParams(paramNames, params)) printMethodicType(res) @@ -1305,14 +1302,14 @@ class SourceCodePrinter[R <: Reflection & Singleton](val tasty: R)(syntaxHighlig this += lineBreak() += "}" } - def printMethodicTypeParams(paramNames: List[String], params: List[TypeOrBounds])(using elideThis: Option[Symbol]): Unit = { - def printInfo(info: TypeOrBounds) = info match { + def printMethodicTypeParams(paramNames: List[String], params: List[Type])(using elideThis: Option[Symbol]): Unit = { + def printInfo(info: Type) = info match { case info: TypeBounds => printBounds(info) case info: Type => this += ": " printType(info) } - def printSeparated(list: List[(String, TypeOrBounds)]): Unit = list match { + def printSeparated(list: List[(String, Type)]): Unit = list match { case Nil => case (name, info) :: Nil => this += name @@ -1376,8 +1373,8 @@ class SourceCodePrinter[R <: Reflection & Singleton](val tasty: R)(syntaxHighlig prefixWasPrinted } - def printFullClassName(tp: TypeOrBounds): Unit = { - def printClassPrefix(prefix: TypeOrBounds): Unit = prefix match { + def printFullClassName(tp: Type): Unit = { + def printClassPrefix(prefix: Type): Unit = prefix match { case TypeRef(prefix2, name) => printClassPrefix(prefix2) this += name += "." diff --git a/library/src/scala/tasty/reflect/Types.scala b/library/src/scala/tasty/reflect/Types.scala index b6334788750d..0f1b234dbd44 100644 --- a/library/src/scala/tasty/reflect/Types.scala +++ b/library/src/scala/tasty/reflect/Types.scala @@ -70,27 +70,26 @@ import scala.tasty.reflect._ * +- Alternatives * * - * +- NoPrefix - * +- TypeOrBounds -+- TypeBounds - * | - * +- Type -------+- ConstantType - * +- TermRef - * +- TypeRef - * +- SuperType - * +- Refinement - * +- AppliedType - * +- AnnotatedType - * +- AndType - * +- OrType - * +- MatchType - * +- ByNameType - * +- ParamRef - * +- ThisType - * +- RecursiveThis - * +- RecursiveType - * +- LambdaType[ParamInfo <: TypeOrBounds] -+- MethodType - * +- PolyType - * +- TypeLambda + * +- Type -+- ConstantType + * +- TermRef + * +- TypeRef + * +- SuperType + * +- Refinement + * +- AppliedType + * +- AnnotatedType + * +- AndType + * +- OrType + * +- MatchType + * +- ByNameType + * +- ParamRef + * +- ThisType + * +- RecursiveThis + * +- RecursiveType + * +- LambdaType -+- MethodType + * | +- PolyType + * | +- TypeLambda + * +- TypeBounds + * +- NoPrefix * * +- ImportSelector -+- SimpleSelector * +- RenameSelector @@ -293,17 +292,8 @@ trait Types { /** Pattern representing `X | Y | ...` alternatives. */ type Alternatives <: Tree - /** Type or bounds */ - type TypeOrBounds <: AnyRef - - /** NoPrefix for a type selection */ - type NoPrefix <: TypeOrBounds - - /** Type bounds */ - type TypeBounds <: TypeOrBounds - /** A type */ - type Type <: TypeOrBounds + type Type /** A singleton type representing a known constant value */ type ConstantType <: Type @@ -350,20 +340,23 @@ trait Types { /** A type that is recursively defined */ type RecursiveType <: Type - // TODO can we add the bound back without an cake? - // TODO is LambdaType really needed? ParamRefExtractor could be split into more precise extractors /** Common abstraction for lambda types (MethodType, PolyType and TypeLambda). */ - type LambdaType[ParamInfo /*<: TypeOrBounds*/] <: Type + type LambdaType <: Type /** Type of the definition of a method taking a single list of parameters. It's return type may be a MethodType. */ - type MethodType <: LambdaType[Type] + type MethodType <: LambdaType /** Type of the definition of a method taking a list of type parameters. It's return type may be a MethodType. */ - type PolyType <: LambdaType[TypeBounds] + type PolyType <: LambdaType /** Type of the definition of a type lambda taking a list of type parameters. It's return type may be a TypeLambda. */ - type TypeLambda <: LambdaType[TypeBounds] + type TypeLambda <: LambdaType + + /** NoPrefix for a type selection */ + type NoPrefix <: Type + /** Type bounds */ + type TypeBounds <: Type /** Import selectors: * * SimpleSelector: `.bar` in `import foo.bar` diff --git a/tastydoc/src/dotty/tastydoc/TastyTypeConverter.scala b/tastydoc/src/dotty/tastydoc/TastyTypeConverter.scala index 26ba050146cb..6159eca85f79 100644 --- a/tastydoc/src/dotty/tastydoc/TastyTypeConverter.scala +++ b/tastydoc/src/dotty/tastydoc/TastyTypeConverter.scala @@ -6,29 +6,6 @@ import dotty.tastydoc.references._ /** Trait containing methods for converting from Reflect types to References */ trait TastyTypeConverter { - def convertTypeOrBoundsToReference(using QuoteContext)(typeOrBounds: qctx.tasty.TypeOrBounds): Reference = { - import qctx.tasty._ - - def anyOrNothing(reference: Reference): Boolean = reference match { - case TypeReference("Any", "/scala", _, _) => true - case TypeReference("Nothing", "/scala", _, _) => true - case _ => false - } - - typeOrBounds match { - case tpe: Type => convertTypeToReference(tpe) - case TypeBounds(low, hi) => - val lowRef = convertTypeToReference(low) - val hiRef = convertTypeToReference(hi) - if(hiRef == lowRef){ - hiRef - }else{ - BoundsReference(lowRef, hiRef) - } - case NoPrefix() => EmptyReference - } - } - def convertTypeToReference(using QuoteContext)(tp: qctx.tasty.Type): Reference = { import qctx.tasty._ @@ -42,7 +19,7 @@ trait TastyTypeConverter { case AnnotatedType(tpe, _) => inner(tpe) case TypeLambda(paramNames, paramTypes, resType) => ConstantReference(tp.show) //TOFIX case Refinement(parent, name, info) => - val tuple = convertTypeOrBoundsToReference(info) match { + val tuple = convertTypeToReference(info) match { case r if (info match {case info: TypeBounds => true case _ => false}) => ("type", name, r) case r@TypeReference(_, _, _, _) => ("val", name, r) case ByNameReference(rChild) => ("def", name, rChild) @@ -58,52 +35,61 @@ trait TastyTypeConverter { case TypeReference(label, link, _, hasOwnFile) => if(link == "/scala"){ if(label.matches("Function[1-9]") || label.matches("Function[1-9][0-9]")){ - val argsAndReturn = typeOrBoundsList.map(convertTypeOrBoundsToReference(_)) + val argsAndReturn = typeOrBoundsList.map(convertTypeToReference(_)) FunctionReference(argsAndReturn.take(argsAndReturn.size - 1), argsAndReturn.last, false) }else if(label.matches("Tuple[1-9]") || label.matches("Tuple[1-9][0-9]")){ - TupleReference(typeOrBoundsList.map(convertTypeOrBoundsToReference(_))) + TupleReference(typeOrBoundsList.map(convertTypeToReference(_))) }else{ - TypeReference(label, link, typeOrBoundsList.map(convertTypeOrBoundsToReference(_)), hasOwnFile) + TypeReference(label, link, typeOrBoundsList.map(convertTypeToReference(_)), hasOwnFile) } }else{ - TypeReference(label, link, typeOrBoundsList.map(convertTypeOrBoundsToReference(_)), hasOwnFile) + TypeReference(label, link, typeOrBoundsList.map(convertTypeToReference(_)), hasOwnFile) } case _ => throw Exception("Match error in AppliedType. This should not happen, please open an issue. " + tp) } case tp @ TypeRef(qual, typeName) => - convertTypeOrBoundsToReference(qual) match { + convertTypeToReference(qual) match { case TypeReference(label, link, xs, _) => TypeReference(typeName, link + "/" + label, xs, true) case EmptyReference => TypeReference(typeName, "", Nil, true) case _ if tp.typeSymbol.exists => tp.typeSymbol match { // NOTE: Only TypeRefs can reference ClassDefSymbols case sym if sym.isClassDef => //Need to be split because these types have their own file - convertTypeOrBoundsToReference(qual) match { + convertTypeToReference(qual) match { case TypeReference(label, link, xs, _) => TypeReference(sym.name, link + "/" + label, xs, true) case EmptyReference if sym.name == "" | sym.name == "_root_" => EmptyReference case EmptyReference => TypeReference(sym.name, "", Nil, true) - case _ => throw Exception("Match error in SymRef/TypeOrBounds/ClassDef. This should not happen, please open an issue. " + convertTypeOrBoundsToReference(qual)) + case _ => throw Exception("Match error in SymRef/Types/ClassDef. This should not happen, please open an issue. " + convertTypeToReference(qual)) } // NOTE: This branch handles packages, which are now TypeRefs case sym if sym.isTerm || sym.isTypeDef => - convertTypeOrBoundsToReference(qual) match { + convertTypeToReference(qual) match { case TypeReference(label, link, xs, _) => TypeReference(sym.name, link + "/" + label, xs) case EmptyReference if sym.name == "" | sym.name == "_root_" => EmptyReference case EmptyReference => TypeReference(sym.name, "", Nil) - case _ => throw Exception("Match error in SymRef/TypeOrBounds/Other. This should not happen, please open an issue. " + convertTypeOrBoundsToReference(qual)) + case _ => throw Exception("Match error in SymRef/Types/Other. This should not happen, please open an issue. " + convertTypeToReference(qual)) } case sym => throw Exception("Match error in SymRef. This should not happen, please open an issue. " + sym) } case _ => - throw Exception("Match error in TypeRef. This should not happen, please open an issue. " + convertTypeOrBoundsToReference(qual)) + throw Exception("Match error in TypeRef. This should not happen, please open an issue. " + convertTypeToReference(qual)) } case TermRef(qual, typeName) => - convertTypeOrBoundsToReference(qual) match { + convertTypeToReference(qual) match { case TypeReference(label, link, xs, _) => TypeReference(typeName + "$", link + "/" + label, xs) case EmptyReference => TypeReference(typeName, "", Nil) - case _ => throw Exception("Match error in TermRef. This should not happen, please open an issue. " + convertTypeOrBoundsToReference(qual)) + case _ => throw Exception("Match error in TermRef. This should not happen, please open an issue. " + convertTypeToReference(qual)) + } + case TypeBounds(low, hi) => + val lowRef = convertTypeToReference(low) + val hiRef = convertTypeToReference(hi) + if(hiRef == lowRef){ + hiRef + }else{ + BoundsReference(lowRef, hiRef) } + case NoPrefix() => EmptyReference // NOTE: old SymRefs are now either TypeRefs or TermRefs - the logic here needs to be moved into above branches // NOTE: _.symbol on *Ref returns its symbol diff --git a/tastydoc/src/dotty/tastydoc/representations.scala b/tastydoc/src/dotty/tastydoc/representations.scala index 0bed42fb33e9..2e18ccdc47e0 100644 --- a/tastydoc/src/dotty/tastydoc/representations.scala +++ b/tastydoc/src/dotty/tastydoc/representations.scala @@ -183,8 +183,8 @@ object representations extends TastyExtractor { override val typeParams = Nil override val annotations = extractAnnotations(internal.symbol.annots) val alias: Option[Reference] = internal.rhs match{ - case t: TypeBoundsTree => Some(convertTypeOrBoundsToReference(t.tpe)) - case t: TypeTree => Some(convertTypeOrBoundsToReference(t.tpe.asInstanceOf[TypeOrBounds])) + case t: TypeBoundsTree => Some(convertTypeToReference(t.tpe)) + case t: TypeTree => Some(convertTypeToReference(t.tpe)) case _ => None } override def isAbstract: Boolean = !alias.isDefined diff --git a/tests/neg-macros/tasty-macro-assert-1/quoted_1.scala b/tests/neg-macros/tasty-macro-assert-1/quoted_1.scala index 6c5cb5c77e07..e0e5cc3e8cd8 100644 --- a/tests/neg-macros/tasty-macro-assert-1/quoted_1.scala +++ b/tests/neg-macros/tasty-macro-assert-1/quoted_1.scala @@ -17,7 +17,7 @@ object Asserts { val tree = cond.unseal - def isOps(tpe: TypeOrBounds): Boolean = tpe match { + def isOps(tpe: Type): Boolean = tpe match { case tpe: TermRef => tpe.termSymbol.isDefDef && tpe.name == "Ops"// TODO check that the parent is Asserts case _ => false } diff --git a/tests/neg-macros/tasty-macro-assert-2/quoted_1.scala b/tests/neg-macros/tasty-macro-assert-2/quoted_1.scala index 4f661fb3a8e5..5df817920c11 100644 --- a/tests/neg-macros/tasty-macro-assert-2/quoted_1.scala +++ b/tests/neg-macros/tasty-macro-assert-2/quoted_1.scala @@ -17,7 +17,7 @@ object Asserts { val tree = cond.unseal - def isOps(tpe: TypeOrBounds): Boolean = tpe match { + def isOps(tpe: Type): Boolean = tpe match { case tpe: TermRef => tpe.termSymbol.isDefDef && tpe.name == "Ops"// TODO check that the parent is Asserts case _ => false } diff --git a/tests/run-macros/refined-selectable-macro/Macro_1.scala b/tests/run-macros/refined-selectable-macro/Macro_1.scala index e97e6b50abe5..9e6725651e99 100644 --- a/tests/run-macros/refined-selectable-macro/Macro_1.scala +++ b/tests/run-macros/refined-selectable-macro/Macro_1.scala @@ -55,7 +55,7 @@ object Macro { def isTupleCons(sym: Symbol): Boolean = sym.owner == defn.ScalaPackageClass && sym.name == "*:" - def extractTuple(tpe: TypeOrBounds, seen: Set[String]): (Set[String], (String, Type)) = { + def extractTuple(tpe: Type, seen: Set[String]): (Set[String], (String, Type)) = { tpe match { // Tuple2(S, T) where S must be a constant string type case AppliedType(parent, ConstantType(Constant(name: String)) :: (info: Type) :: Nil) if (parent.typeSymbol == defn.TupleClass(2)) => diff --git a/tests/run-macros/tasty-custom-show/quoted_1.scala b/tests/run-macros/tasty-custom-show/quoted_1.scala index a5b80fa2d5aa..528c0302cb20 100644 --- a/tests/run-macros/tasty-custom-show/quoted_1.scala +++ b/tests/run-macros/tasty-custom-show/quoted_1.scala @@ -42,7 +42,7 @@ object Macros { val tasty = qctx.tasty import qctx.tasty._ def showTree(tree: Tree)(implicit ctx: Context): String = "Tree" - def showTypeOrBounds(tpe: TypeOrBounds)(implicit ctx: Context): String = "TypeOrBounds" + def showType(tpe: Type)(implicit ctx: Context): String = "Type" def showConstant(const: Constant)(implicit ctx: Context): String = "Constant" def showSymbol(symbol: Symbol)(implicit ctx: Context): String = "Symbol" def showFlags(flags: Flags)(implicit ctx: Context): String = "Flags" diff --git a/tests/run-macros/tasty-indexed-map/quoted_1.scala b/tests/run-macros/tasty-indexed-map/quoted_1.scala index 1a40645d3ffc..8c0fbc8fef54 100644 --- a/tests/run-macros/tasty-indexed-map/quoted_1.scala +++ b/tests/run-macros/tasty-indexed-map/quoted_1.scala @@ -27,11 +27,11 @@ object Index { def succImpl[K, H, T](implicit qctx: QuoteContext, k: Type[K], h: Type[H], t: Type[T]): Expr[Index[K, (H, T)]] = { import qctx.tasty._ - def name(tp: TypeOrBounds): String = tp match { + def name(tp: Type): String = tp match { case ConstantType(Constant(str: String)) => str } - def names(tp: TypeOrBounds): List[String] = tp match { + def names(tp: Type): List[String] = tp match { case AppliedType(_, x1 :: x2 :: Nil) => name(x1) :: names(x2) case _ => Nil } diff --git a/tests/run-macros/tasty-macro-assert/quoted_1.scala b/tests/run-macros/tasty-macro-assert/quoted_1.scala index 3f0291b49f61..aa156c6e0b59 100644 --- a/tests/run-macros/tasty-macro-assert/quoted_1.scala +++ b/tests/run-macros/tasty-macro-assert/quoted_1.scala @@ -17,7 +17,7 @@ object Asserts { val tree = cond.unseal - def isOps(tpe: TypeOrBounds): Boolean = tpe match { + def isOps(tpe: Type): Boolean = tpe match { case tpe: TermRef => tpe.termSymbol.isDefDef && tpe.name == "Ops"// TODO check that the parent is Asserts case _ => false }