Skip to content

Commit

Permalink
Merge pull request scala#5863 from dotty-staging/add-show
Browse files Browse the repository at this point in the history
Support show on Type[T]
  • Loading branch information
nicolasstucki authored Mar 21, 2019
2 parents 036096a + 50cdb1e commit bb0a1ed
Show file tree
Hide file tree
Showing 14 changed files with 131 additions and 58 deletions.
24 changes: 14 additions & 10 deletions compiler/src/dotty/tools/dotc/quoted/QuoteDriver.scala
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,21 @@ class QuoteDriver extends Driver {
method.invoke(inst).asInstanceOf[T]
}

def show(expr: Expr[_], settings: Toolbox.Settings): String = {
def show(tree: Tree, ctx: Context): String = {
implicit val c: Context = ctx
val tree1 =
if (ctx.settings.YshowRawQuoteTrees.value) tree
else (new TreeCleaner).transform(tree)
ReflectionImpl.showTree(tree1)
}
withTree(expr, show, settings)
private def doShow(tree: Tree, ctx: Context): String = {
implicit val c: Context = ctx
val tree1 =
if (ctx.settings.YshowRawQuoteTrees.value) tree
else (new TreeCleaner).transform(tree)
ReflectionImpl.showTree(tree1)
}

def show(expr: Expr[_], settings: Toolbox.Settings): String =
withTree(expr, doShow, settings)

def show(tpe: Type[_], settings: Toolbox.Settings): String =
withTypeTree(tpe, doShow, settings)


def withTree[T](expr: Expr[_], f: (Tree, Context) => T, settings: Toolbox.Settings): T = {
val ctx = setToolboxSettings(setup(settings.compilerArgs.toArray :+ "dummy.scala", initCtx.fresh)._2.fresh, settings)

Expand All @@ -65,7 +69,7 @@ class QuoteDriver extends Driver {
}

def withTypeTree[T](tpe: Type[_], f: (TypTree, Context) => T, settings: Toolbox.Settings): T = {
val (_, ctx: Context) = setup(settings.compilerArgs.toArray :+ "dummy.scala", initCtx.fresh)
val ctx = setToolboxSettings(setup(settings.compilerArgs.toArray :+ "dummy.scala", initCtx.fresh)._2.fresh, settings)

var output: Option[T] = None
def registerTree(tree: tpd.Tree)(ctx: Context): Unit = {
Expand Down
3 changes: 2 additions & 1 deletion compiler/src/dotty/tools/dotc/quoted/ToolboxImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package dotty.tools.dotc.quoted

import dotty.tools.dotc.ast.tpd

import scala.quoted.Expr
import scala.quoted._
import scala.quoted.Exprs.{LiftedExpr, TastyTreeExpr}

/** Default runners for quoted expressions */
Expand All @@ -24,5 +24,6 @@ object ToolboxImpl {

def show[T](expr: Expr[T]): String = synchronized(driver.show(expr, settings))

def show[T](tpe: Type[T]): String = synchronized(driver.show(tpe, settings))
}
}
3 changes: 3 additions & 0 deletions library/src-bootstrapped/scala/quoted/Type.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import scala.runtime.quoted.Unpickler.Pickled

sealed abstract class Type[T <: AnyKind] {
type `$splice` = T

/** Show a source code like representation of this type */
final def show(implicit toolbox: Toolbox): String = toolbox.show(this.asInstanceOf[Type[Any]])
}

/** Some basic type tags, currently incomplete */
Expand Down
3 changes: 3 additions & 0 deletions library/src-non-bootstrapped/scala/quoted/Type.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import scala.runtime.quoted.Unpickler.Pickled

sealed abstract class Type[T] {
type `$splice` = T

/** Show a source code like representation of this type */
final def show(implicit toolbox: Toolbox): String = toolbox.show(this)
}

/** Some basic type tags, currently incomplete */
Expand Down
3 changes: 2 additions & 1 deletion library/src/scala/quoted/Toolbox.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import scala.annotation.implicitNotFound
trait Toolbox {
def run[T](expr: Expr[T]): T
def show[T](expr: Expr[T]): String
def show[T](tpe: Type[T]): String
}

object Toolbox {
Expand All @@ -32,7 +33,7 @@ object Toolbox {
}

/** Setting of the Toolbox instance. */
class Settings private (val outDir: Option[String], val showRawTree: Boolean, val compilerArgs: List[String], val color: Boolean)
case class Settings private (outDir: Option[String], showRawTree: Boolean, compilerArgs: List[String], color: Boolean)

object Settings {

Expand Down
2 changes: 1 addition & 1 deletion library/src/scala/tasty/reflect/Kernel.scala
Original file line number Diff line number Diff line change
Expand Up @@ -699,7 +699,7 @@ trait Kernel {
//
// PATTERNS
//

/** Pattern tree of the pattern part of a CaseDef */
type Pattern <: AnyRef

Expand Down
Loading

0 comments on commit bb0a1ed

Please sign in to comment.