Skip to content

Commit 44e6d0f

Browse files
Merge pull request #6112 from dotty-staging/add-tasty-type-dealias
Add TASYTy Reflect type dealias
2 parents b13c235 + 94b8cb8 commit 44e6d0f

File tree

6 files changed

+46
-0
lines changed

6 files changed

+46
-0
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -994,6 +994,8 @@ class KernelImpl(val rootContext: core.Contexts.Context, val rootPosition: util.
994994
*/
995995
def Type_widen(self: Type)(implicit ctx: Context): Type = self.widen
996996

997+
def Type_dealias(self: Type)(implicit ctx: Context): Type = self.dealias
998+
997999
def Type_classSymbol(self: Type)(implicit ctx: Context): Option[ClassSymbol] =
9981000
if (self.classSymbol.exists) Some(self.classSymbol.asClass) else None
9991001

library/src/scala/tasty/reflect/Kernel.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -796,6 +796,12 @@ trait Kernel {
796796
*/
797797
def Type_widen(self: Type)(implicit ctx: Context): Type
798798

799+
/** Follow aliases and dereferences LazyRefs, annotated types and instantiated
800+
* TypeVars until type is no longer alias type, annotated type, LazyRef,
801+
* or instantiated type variable.
802+
*/
803+
def Type_dealias(self: Type)(implicit ctx: Context): Type
804+
799805
def Type_classSymbol(self: Type)(implicit ctx: Context): Option[ClassSymbol]
800806

801807
def Type_typeSymbol(self: Type)(implicit ctx: Context): Symbol

library/src/scala/tasty/reflect/TypeOrBoundsOps.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@ trait TypeOrBoundsOps extends Core {
1111
def =:=(that: Type)(implicit ctx: Context): Boolean = kernel.`Type_=:=`(self)(that)
1212
def <:<(that: Type)(implicit ctx: Context): Boolean = kernel.`Type_<:<`(self)(that)
1313
def widen(implicit ctx: Context): Type = kernel.Type_widen(self)
14+
15+
/** Follow aliases and dereferences LazyRefs, annotated types and instantiated
16+
* TypeVars until type is no longer alias type, annotated type, LazyRef,
17+
* or instantiated type variable.
18+
*/
19+
def dealias(implicit ctx: Context): Type = kernel.Type_dealias(self)
20+
1421
def classSymbol(implicit ctx: Context): Option[ClassSymbol] = kernel.Type_classSymbol(self)
1522
def typeSymbol(implicit ctx: Context): Symbol = kernel.Type_typeSymbol(self)
1623
def isSingleton(implicit ctx: Context): Boolean = kernel.Type_isSingleton(self)

tests/run/tasty-dealias.check

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
lang.String
2+
lang.String
3+
immutable.List[A]
4+
immutable.List[scala.Int]
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import scala.quoted._
2+
import scala.tasty._
3+
4+
object Macros {
5+
6+
inline def dealias[T]: String = ${ impl('[T]) }
7+
8+
def impl[T](x: quoted.Type[T])(implicit reflect: Reflection): Expr[String] = {
9+
import reflect._
10+
x.unseal.tpe.dealias.showCode.toExpr
11+
}
12+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
import Macros.dealias
3+
4+
object Test {
5+
6+
def main(args: Array[String]): Unit = {
7+
type A = String
8+
type B = List[A]
9+
type C[X] = List[X]
10+
println(dealias[String])
11+
println(dealias[A])
12+
println(dealias[B])
13+
println(dealias[C[Int]])
14+
}
15+
}

0 commit comments

Comments
 (0)