Skip to content

Commit 9ab6c67

Browse files
authored
Merge pull request #7105 from milessabin/topic/tasty-simplified
Expose Type#simplified via tasty reflect API
2 parents f8e1503 + a3d2026 commit 9ab6c67

File tree

6 files changed

+69
-0
lines changed

6 files changed

+69
-0
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1109,6 +1109,8 @@ class ReflectionCompilerInterface(val rootContext: core.Contexts.Context) extend
11091109

11101110
def Type_dealias(self: Type) given Context: Type = self.dealias
11111111

1112+
def Type_simplified(self: Type) given Context: Type = self.simplified
1113+
11121114
def Type_classSymbol(self: Type) given Context: Option[ClassDefSymbol] =
11131115
if (self.classSymbol.exists) Some(self.classSymbol.asClass) else None
11141116

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -876,6 +876,8 @@ trait CompilerInterface {
876876
*/
877877
def Type_dealias(self: Type) given (ctx: Context): Type
878878

879+
def Type_simplified(self: Type) given (ctx: Context): Type
880+
879881
def Type_classSymbol(self: Type) given (ctx: Context): Option[ClassDefSymbol] // TODO remove Option and use NoSymbol
880882

881883
def Type_typeSymbol(self: Type) given (ctx: Context): Symbol

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ trait TypeOrBoundsOps extends Core {
1818
*/
1919
def dealias given (ctx: Context): Type = internal.Type_dealias(self)
2020

21+
/** A simplified version of this type which is equivalent wrt =:= to this type.
22+
* Reduces typerefs, applied match types, and and or types.
23+
*/
24+
def simplified given (ctx: Context): Type = internal.Type_simplified(self)
25+
2126
def classSymbol given (ctx: Context): Option[ClassDefSymbol] = internal.Type_classSymbol(self)
2227
def typeSymbol given (ctx: Context): Symbol = internal.Type_typeSymbol(self)
2328
def termSymbol given (ctx: Context): Symbol = internal.Type_termSymbol(self)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Functor[[+A >: scala.Nothing <: scala.Any] => scala.collection.immutable.List[+A]]
2+
Functor[Const[scala.Int]]
3+
Functor[Id]
4+
Functor[[+A >: scala.Nothing <: scala.Any] => scala.Option[+A]]
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import scala.annotation.tailrec
2+
import scala.quoted._
3+
4+
object Macros {
5+
6+
inline def simplified[T <: Tuple]: Seq[String] = ${ impl[T] }
7+
8+
def impl[T: Type] given (qctx: QuoteContext): Expr[Seq[String]] = {
9+
import qctx.tasty._
10+
11+
def unpackTuple(tp: Type): List[Type] = {
12+
@tailrec
13+
def loop(tp: Type, acc: List[Type]): List[Type] = tp.dealias.simplified match {
14+
case Type.AppliedType(_, List(IsType(hd), IsType(tl))) =>
15+
loop(tl, hd.dealias.simplified :: acc)
16+
case other => acc
17+
}
18+
loop(tp, Nil).reverse
19+
}
20+
21+
val tps = unpackTuple(typeOf[T])
22+
tps.map(_.show.toExpr).toExprOfSeq
23+
}
24+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import Macros.simplified
2+
3+
object Test {
4+
5+
def main(args: Array[String]): Unit = {
6+
type Const[C] = [T] =>> C
7+
type Id[T] = T
8+
case class Wrap[T](t: T)
9+
10+
class Dummy
11+
type Apply[T[_]] = T[Dummy]
12+
type Unapply[F[_[_]], T] = T match {
13+
case Wrap[Apply[a]] => F[a]
14+
case Wrap[Dummy] => F[Id]
15+
case Wrap[c] => F[Const[c]]
16+
}
17+
18+
type LiftP[F[_[_]], T[_]] = LiftP0[F, Apply[T]]
19+
20+
type LiftP0[F[_[_]], T] <: Tuple = T match {
21+
case Unit => Unit
22+
case (a *: b) => Unapply[F, Wrap[a]] *: LiftP0[F, b]
23+
}
24+
25+
trait Functor[F[_]]
26+
27+
type T1[t] = (List[t], Int, t, Option[t])
28+
29+
val elems = simplified[LiftP[Functor, T1]]
30+
elems.foreach(println)
31+
}
32+
}

0 commit comments

Comments
 (0)