Skip to content

Expose Type#simplified via tasty reflect API #7105

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 1 commit into from
Aug 27, 2019
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
Original file line number Diff line number Diff line change
Expand Up @@ -1109,6 +1109,8 @@ class ReflectionCompilerInterface(val rootContext: core.Contexts.Context) extend

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

def Type_simplified(self: Type) given Context: Type = self.simplified

def Type_classSymbol(self: Type) given Context: Option[ClassDefSymbol] =
if (self.classSymbol.exists) Some(self.classSymbol.asClass) else None

Expand Down
2 changes: 2 additions & 0 deletions library/src/scala/tasty/reflect/CompilerInterface.scala
Original file line number Diff line number Diff line change
Expand Up @@ -876,6 +876,8 @@ trait CompilerInterface {
*/
def Type_dealias(self: Type) given (ctx: Context): Type

def Type_simplified(self: Type) given (ctx: Context): Type

def Type_classSymbol(self: Type) given (ctx: Context): Option[ClassDefSymbol] // TODO remove Option and use NoSymbol

def Type_typeSymbol(self: Type) given (ctx: Context): Symbol
Expand Down
5 changes: 5 additions & 0 deletions library/src/scala/tasty/reflect/TypeOrBoundsOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ trait TypeOrBoundsOps extends Core {
*/
def dealias given (ctx: Context): Type = internal.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.
*/
def simplified given (ctx: Context): Type = internal.Type_simplified(self)

def classSymbol given (ctx: Context): Option[ClassDefSymbol] = internal.Type_classSymbol(self)
def typeSymbol given (ctx: Context): Symbol = internal.Type_typeSymbol(self)
def termSymbol given (ctx: Context): Symbol = internal.Type_termSymbol(self)
Expand Down
4 changes: 4 additions & 0 deletions tests/run-macros/tasty-simplified.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Functor[[+A >: scala.Nothing <: scala.Any] => scala.collection.immutable.List[+A]]
Functor[Const[scala.Int]]
Functor[Id]
Functor[[+A >: scala.Nothing <: scala.Any] => scala.Option[+A]]
24 changes: 24 additions & 0 deletions tests/run-macros/tasty-simplified/quoted_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import scala.annotation.tailrec
import scala.quoted._

object Macros {

inline def simplified[T <: Tuple]: Seq[String] = ${ impl[T] }

def impl[T: Type] given (qctx: QuoteContext): Expr[Seq[String]] = {
import qctx.tasty._

def unpackTuple(tp: Type): List[Type] = {
@tailrec
def loop(tp: Type, acc: List[Type]): List[Type] = tp.dealias.simplified match {
case Type.AppliedType(_, List(IsType(hd), IsType(tl))) =>
loop(tl, hd.dealias.simplified :: acc)
case other => acc
}
loop(tp, Nil).reverse
}

val tps = unpackTuple(typeOf[T])
tps.map(_.show.toExpr).toExprOfSeq
}
}
32 changes: 32 additions & 0 deletions tests/run-macros/tasty-simplified/quoted_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import Macros.simplified

object Test {

def main(args: Array[String]): Unit = {
type Const[C] = [T] =>> C
type Id[T] = T
case class Wrap[T](t: T)

class Dummy
type Apply[T[_]] = T[Dummy]
type Unapply[F[_[_]], T] = T match {
case Wrap[Apply[a]] => F[a]
case Wrap[Dummy] => F[Id]
case Wrap[c] => F[Const[c]]
}

type LiftP[F[_[_]], T[_]] = LiftP0[F, Apply[T]]

type LiftP0[F[_[_]], T] <: Tuple = T match {
case Unit => Unit
case (a *: b) => Unapply[F, Wrap[a]] *: LiftP0[F, b]
}

trait Functor[F[_]]

type T1[t] = (List[t], Int, t, Option[t])

val elems = simplified[LiftP[Functor, T1]]
elems.foreach(println)
}
}