Skip to content

Commit bb0a1ed

Browse files
Merge pull request #5863 from dotty-staging/add-show
Support show on Type[T]
2 parents 036096a + 50cdb1e commit bb0a1ed

File tree

14 files changed

+131
-58
lines changed

14 files changed

+131
-58
lines changed

compiler/src/dotty/tools/dotc/quoted/QuoteDriver.scala

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,21 @@ class QuoteDriver extends Driver {
4141
method.invoke(inst).asInstanceOf[T]
4242
}
4343

44-
def show(expr: Expr[_], settings: Toolbox.Settings): String = {
45-
def show(tree: Tree, ctx: Context): String = {
46-
implicit val c: Context = ctx
47-
val tree1 =
48-
if (ctx.settings.YshowRawQuoteTrees.value) tree
49-
else (new TreeCleaner).transform(tree)
50-
ReflectionImpl.showTree(tree1)
51-
}
52-
withTree(expr, show, settings)
44+
private def doShow(tree: Tree, ctx: Context): String = {
45+
implicit val c: Context = ctx
46+
val tree1 =
47+
if (ctx.settings.YshowRawQuoteTrees.value) tree
48+
else (new TreeCleaner).transform(tree)
49+
ReflectionImpl.showTree(tree1)
5350
}
5451

52+
def show(expr: Expr[_], settings: Toolbox.Settings): String =
53+
withTree(expr, doShow, settings)
54+
55+
def show(tpe: Type[_], settings: Toolbox.Settings): String =
56+
withTypeTree(tpe, doShow, settings)
57+
58+
5559
def withTree[T](expr: Expr[_], f: (Tree, Context) => T, settings: Toolbox.Settings): T = {
5660
val ctx = setToolboxSettings(setup(settings.compilerArgs.toArray :+ "dummy.scala", initCtx.fresh)._2.fresh, settings)
5761

@@ -65,7 +69,7 @@ class QuoteDriver extends Driver {
6569
}
6670

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

7074
var output: Option[T] = None
7175
def registerTree(tree: tpd.Tree)(ctx: Context): Unit = {

compiler/src/dotty/tools/dotc/quoted/ToolboxImpl.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package dotty.tools.dotc.quoted
22

33
import dotty.tools.dotc.ast.tpd
44

5-
import scala.quoted.Expr
5+
import scala.quoted._
66
import scala.quoted.Exprs.{LiftedExpr, TastyTreeExpr}
77

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

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

27+
def show[T](tpe: Type[T]): String = synchronized(driver.show(tpe, settings))
2728
}
2829
}

library/src-bootstrapped/scala/quoted/Type.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ import scala.runtime.quoted.Unpickler.Pickled
66

77
sealed abstract class Type[T <: AnyKind] {
88
type `$splice` = T
9+
10+
/** Show a source code like representation of this type */
11+
final def show(implicit toolbox: Toolbox): String = toolbox.show(this.asInstanceOf[Type[Any]])
912
}
1013

1114
/** Some basic type tags, currently incomplete */

library/src-non-bootstrapped/scala/quoted/Type.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ import scala.runtime.quoted.Unpickler.Pickled
66

77
sealed abstract class Type[T] {
88
type `$splice` = T
9+
10+
/** Show a source code like representation of this type */
11+
final def show(implicit toolbox: Toolbox): String = toolbox.show(this)
912
}
1013

1114
/** Some basic type tags, currently incomplete */

library/src/scala/quoted/Toolbox.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import scala.annotation.implicitNotFound
66
trait Toolbox {
77
def run[T](expr: Expr[T]): T
88
def show[T](expr: Expr[T]): String
9+
def show[T](tpe: Type[T]): String
910
}
1011

1112
object Toolbox {
@@ -32,7 +33,7 @@ object Toolbox {
3233
}
3334

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

3738
object Settings {
3839

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -699,7 +699,7 @@ trait Kernel {
699699
//
700700
// PATTERNS
701701
//
702-
702+
703703
/** Pattern tree of the pattern part of a CaseDef */
704704
type Pattern <: AnyRef
705705

0 commit comments

Comments
 (0)