Skip to content

Add Reflection constructors for SuperType, RecursiveType and RecursiveThis #8204

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions compiler/src/dotty/tools/dotc/core/Types.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1165,7 +1165,7 @@ object Types {
* these types as a set, otherwise the empty set.
* Overridden and cached in OrType.
* @param widenOK If type proxies that are upperbounded by types with atoms
* have the same atoms.
* have the same atoms.
*/
def atoms(widenOK: Boolean = false)(implicit ctx: Context): Set[Type] = dealias match {
case tp: SingletonType =>
Expand Down Expand Up @@ -2544,7 +2544,7 @@ object Types {
final class CachedSuperType(thistpe: Type, supertpe: Type) extends SuperType(thistpe, supertpe)

object SuperType {
def apply(thistpe: Type, supertpe: Type)(implicit ctx: Context): Type = {
def apply(thistpe: Type, supertpe: Type)(implicit ctx: Context): SuperType = {
assert(thistpe != NoPrefix)
unique(new CachedSuperType(thistpe, supertpe))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1274,6 +1274,9 @@ class ReflectionCompilerInterface(val rootContext: core.Contexts.Context) extend
case _ => None
}

def SuperType_apply(thistpe: Type, supertpe: Type)(given ctx: Context): SuperType =
Types.SuperType(thistpe, supertpe)

def SuperType_thistpe(self: SuperType)(given Context): Type = self.thistpe
def SuperType_supertpe(self: SuperType)(given Context): Type = self.supertpe

Expand Down Expand Up @@ -1431,8 +1434,13 @@ class ReflectionCompilerInterface(val rootContext: core.Contexts.Context) extend
case _ => None
}

def RecursiveType_apply(parentExp: RecursiveType => Type)(given ctx: Context): RecursiveType =
Types.RecType(parentExp)

def RecursiveType_underlying(self: RecursiveType)(given Context): Type = self.underlying.stripTypeVar

def RecursiveThis_recThis(self: RecursiveType)(given Context): RecursiveThis = self.recThis

type LambdaType[ParamInfo] = Types.LambdaType { type PInfo = ParamInfo }

type MethodType = Types.MethodType
Expand Down
17 changes: 17 additions & 0 deletions library/src/scala/tasty/Reflection.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1738,6 +1738,9 @@ class Reflection(private[scala] val internal: CompilerInterface) { self =>
given (given Context): IsInstanceOf[SuperType] = internal.isInstanceOfSuperType

object SuperType {
def apply(thistpe: Type, supertpe: Type)(given ctx: Context): SuperType =
internal.SuperType_apply(thistpe, supertpe)

def unapply(x: SuperType)(given ctx: Context): Option[(Type, Type)] =
Some((x.thistpe, x.supertpe))
}
Expand Down Expand Up @@ -1887,11 +1890,25 @@ class Reflection(private[scala] val internal: CompilerInterface) { self =>
given (given Context): IsInstanceOf[RecursiveType] = internal.isInstanceOfRecursiveType

object RecursiveType {

/** Create a RecType, normalizing its contents. This means:
*
* 1. Nested Rec types on the type's spine are merged with the outer one.
* 2. Any refinement of the form `type T = z.T` on the spine of the type
* where `z` refers to the created rec-type is replaced by
* `type T`. This avoids infinite recursions later when we
* try to follow these references.
*/
def apply(parentExp: RecursiveType => Type)(given ctx: Context): RecursiveType =
internal.RecursiveType_apply(parentExp)

def unapply(x: RecursiveType)(given ctx: Context): Option[Type] = Some(x.underlying)

}

extension RecursiveTypeOps on (self: RecursiveType) {
def underlying(given ctx: Context): Type = internal.RecursiveType_underlying(self)
def recThis(given Context): RecursiveThis = internal.RecursiveThis_recThis(self)
}

given (given Context): IsInstanceOf[MethodType] = internal.isInstanceOfMethodType
Expand Down
14 changes: 14 additions & 0 deletions library/src/scala/tasty/reflect/CompilerInterface.scala
Original file line number Diff line number Diff line change
Expand Up @@ -923,6 +923,8 @@ trait CompilerInterface {

def isInstanceOfSuperType(given ctx: Context): IsInstanceOf[SuperType]

def SuperType_apply(thistpe: Type, supertpe: Type)(given ctx: Context): SuperType

def SuperType_thistpe(self: SuperType)(given ctx: Context): Type
def SuperType_supertpe(self: SuperType)(given ctx: Context): Type

Expand Down Expand Up @@ -1024,8 +1026,20 @@ trait CompilerInterface {

def isInstanceOfRecursiveType(given ctx: Context): IsInstanceOf[RecursiveType]

/** Create a RecType, normalizing its contents. This means:
*
* 1. Nested Rec types on the type's spine are merged with the outer one.
* 2. Any refinement of the form `type T = z.T` on the spine of the type
* where `z` refers to the created rec-type is replaced by
* `type T`. This avoids infinite recursions later when we
* try to follow these references.
*/
def RecursiveType_apply(parentExp: RecursiveType => Type)(given ctx: Context): RecursiveType
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering if there are macros that use recursive types? Scala doesn't have syntax for recursive types.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We will still get some types that are represented with recursive types in TASTy, hence we need to support it.


def RecursiveType_underlying(self: RecursiveType)(given ctx: Context): Type

def RecursiveThis_recThis(self: RecursiveType)(given Context): RecursiveThis

// 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). */
Expand Down