Skip to content

Commit ff93bb1

Browse files
Merge pull request #8204 from dotty-staging/add-missing-type-constructors
Add Reflection constructors for SuperType, RecursiveType and RecursiveThis
2 parents c5a0e10 + a7b51e2 commit ff93bb1

File tree

4 files changed

+41
-2
lines changed

4 files changed

+41
-2
lines changed

compiler/src/dotty/tools/dotc/core/Types.scala

+2-2
Original file line numberDiff line numberDiff line change
@@ -1165,7 +1165,7 @@ object Types {
11651165
* these types as a set, otherwise the empty set.
11661166
* Overridden and cached in OrType.
11671167
* @param widenOK If type proxies that are upperbounded by types with atoms
1168-
* have the same atoms.
1168+
* have the same atoms.
11691169
*/
11701170
def atoms(widenOK: Boolean = false)(implicit ctx: Context): Set[Type] = dealias match {
11711171
case tp: SingletonType =>
@@ -2544,7 +2544,7 @@ object Types {
25442544
final class CachedSuperType(thistpe: Type, supertpe: Type) extends SuperType(thistpe, supertpe)
25452545

25462546
object SuperType {
2547-
def apply(thistpe: Type, supertpe: Type)(implicit ctx: Context): Type = {
2547+
def apply(thistpe: Type, supertpe: Type)(implicit ctx: Context): SuperType = {
25482548
assert(thistpe != NoPrefix)
25492549
unique(new CachedSuperType(thistpe, supertpe))
25502550
}

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

+8
Original file line numberDiff line numberDiff line change
@@ -1274,6 +1274,9 @@ class ReflectionCompilerInterface(val rootContext: core.Contexts.Context) extend
12741274
case _ => None
12751275
}
12761276

1277+
def SuperType_apply(thistpe: Type, supertpe: Type)(given ctx: Context): SuperType =
1278+
Types.SuperType(thistpe, supertpe)
1279+
12771280
def SuperType_thistpe(self: SuperType)(given Context): Type = self.thistpe
12781281
def SuperType_supertpe(self: SuperType)(given Context): Type = self.supertpe
12791282

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

1437+
def RecursiveType_apply(parentExp: RecursiveType => Type)(given ctx: Context): RecursiveType =
1438+
Types.RecType(parentExp)
1439+
14341440
def RecursiveType_underlying(self: RecursiveType)(given Context): Type = self.underlying.stripTypeVar
14351441

1442+
def RecursiveThis_recThis(self: RecursiveType)(given Context): RecursiveThis = self.recThis
1443+
14361444
type LambdaType[ParamInfo] = Types.LambdaType { type PInfo = ParamInfo }
14371445

14381446
type MethodType = Types.MethodType

library/src/scala/tasty/Reflection.scala

+17
Original file line numberDiff line numberDiff line change
@@ -1738,6 +1738,9 @@ class Reflection(private[scala] val internal: CompilerInterface) { self =>
17381738
given (given Context): IsInstanceOf[SuperType] = internal.isInstanceOfSuperType
17391739

17401740
object SuperType {
1741+
def apply(thistpe: Type, supertpe: Type)(given ctx: Context): SuperType =
1742+
internal.SuperType_apply(thistpe, supertpe)
1743+
17411744
def unapply(x: SuperType)(given ctx: Context): Option[(Type, Type)] =
17421745
Some((x.thistpe, x.supertpe))
17431746
}
@@ -1887,11 +1890,25 @@ class Reflection(private[scala] val internal: CompilerInterface) { self =>
18871890
given (given Context): IsInstanceOf[RecursiveType] = internal.isInstanceOfRecursiveType
18881891

18891892
object RecursiveType {
1893+
1894+
/** Create a RecType, normalizing its contents. This means:
1895+
*
1896+
* 1. Nested Rec types on the type's spine are merged with the outer one.
1897+
* 2. Any refinement of the form `type T = z.T` on the spine of the type
1898+
* where `z` refers to the created rec-type is replaced by
1899+
* `type T`. This avoids infinite recursions later when we
1900+
* try to follow these references.
1901+
*/
1902+
def apply(parentExp: RecursiveType => Type)(given ctx: Context): RecursiveType =
1903+
internal.RecursiveType_apply(parentExp)
1904+
18901905
def unapply(x: RecursiveType)(given ctx: Context): Option[Type] = Some(x.underlying)
1906+
18911907
}
18921908

18931909
extension RecursiveTypeOps on (self: RecursiveType) {
18941910
def underlying(given ctx: Context): Type = internal.RecursiveType_underlying(self)
1911+
def recThis(given Context): RecursiveThis = internal.RecursiveThis_recThis(self)
18951912
}
18961913

18971914
given (given Context): IsInstanceOf[MethodType] = internal.isInstanceOfMethodType

library/src/scala/tasty/reflect/CompilerInterface.scala

+14
Original file line numberDiff line numberDiff line change
@@ -923,6 +923,8 @@ trait CompilerInterface {
923923

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

926+
def SuperType_apply(thistpe: Type, supertpe: Type)(given ctx: Context): SuperType
927+
926928
def SuperType_thistpe(self: SuperType)(given ctx: Context): Type
927929
def SuperType_supertpe(self: SuperType)(given ctx: Context): Type
928930

@@ -1024,8 +1026,20 @@ trait CompilerInterface {
10241026

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

1029+
/** Create a RecType, normalizing its contents. This means:
1030+
*
1031+
* 1. Nested Rec types on the type's spine are merged with the outer one.
1032+
* 2. Any refinement of the form `type T = z.T` on the spine of the type
1033+
* where `z` refers to the created rec-type is replaced by
1034+
* `type T`. This avoids infinite recursions later when we
1035+
* try to follow these references.
1036+
*/
1037+
def RecursiveType_apply(parentExp: RecursiveType => Type)(given ctx: Context): RecursiveType
1038+
10271039
def RecursiveType_underlying(self: RecursiveType)(given ctx: Context): Type
10281040

1041+
def RecursiveThis_recThis(self: RecursiveType)(given Context): RecursiveThis
1042+
10291043
// TODO can we add the bound back without an cake?
10301044
// TODO is LambdaType really needed? ParamRefExtractor could be split into more precise extractors
10311045
/** Common abstraction for lambda types (MethodType, PolyType and TypeLambda). */

0 commit comments

Comments
 (0)