Skip to content

Support show on Type[T] #5863

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 8 commits into from
Mar 21, 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
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