Skip to content

Commit a13b244

Browse files
committed
Support show on Type[T]
1 parent 8a8f383 commit a13b244

File tree

5 files changed

+42
-17
lines changed

5 files changed

+42
-17
lines changed

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

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,25 @@ 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-
new ReflectionImpl(ctx).showSourceCode.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+
val printer = new ReflectionImpl(ctx).showSourceCode
50+
if (tree.isType)
51+
printer.showTypeOrBoundsTree(tree1)
52+
else
53+
printer.showTree(tree1)
5354
}
5455

56+
def show(expr: Expr[_], settings: Toolbox.Settings): String =
57+
withTree(expr, doShow, settings)
58+
59+
def show(tpe: Type[_], settings: Toolbox.Settings): String =
60+
withTypeTree(tpe, doShow, settings)
61+
62+
5563
def withTree[T](expr: Expr[_], f: (Tree, Context) => T, settings: Toolbox.Settings): T = {
5664
val ctx = setToolboxSettings(setup(settings.compilerArgs.toArray :+ "dummy.scala", initCtx.fresh)._2.fresh, settings)
5765

@@ -65,7 +73,7 @@ class QuoteDriver extends Driver {
6573
}
6674

6775
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)
76+
val ctx = setToolboxSettings(setup(settings.compilerArgs.toArray :+ "dummy.scala", initCtx.fresh)._2.fresh, settings)
6977

7078
var output: Option[T] = None
7179
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/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 (val outDir: Option[String], val showRawTree: Boolean, val compilerArgs: List[String], val color: Boolean)
3637

3738
object Settings {
3839

library/src/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 unary_~ = 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/tasty/reflect/Printers.scala

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1367,6 +1367,9 @@ trait Printers
13671367
case tpe @ Type.SymRef(IsClassSymbol(sym), _) if sym.name.endsWith("$") =>
13681368
printType(tpe)
13691369
this += ".type"
1370+
case tpe @ Type.SymRef(sym, _) if !IsTypeSymbol.unapply(sym).nonEmpty =>
1371+
printType(tpe)
1372+
this += ".type"
13701373
case tpe => printType(tpe)
13711374
}
13721375
printTypeAndAnnots(tree.tpe)
@@ -1459,20 +1462,29 @@ trait Printers
14591462
case Type.ConstantType(const) =>
14601463
printConstant(const)
14611464

1462-
case Type.SymRef(sym, prefix) =>
1465+
case Type.SymRef(sym, prefix) if IsTypeSymbol.unapply(sym).nonEmpty =>
14631466
prefix match {
14641467
case Types.EmptyPrefix() =>
14651468
case IsType(prefix @ Type.SymRef(IsClassSymbol(_), _)) =>
14661469
printType(prefix)
14671470
this += "#"
14681471
case IsType(prefix) =>
1469-
if (!sym.flags.is(Flags.Local)) {
1470-
printType(prefix)
1471-
this += "."
1472-
}
1472+
printType(prefix)
1473+
this += "."
14731474
}
14741475
this += highlightTypeDef(sym.name.stripSuffix("$"), color)
14751476

1477+
case Type.SymRef(sym, prefix) if !IsTypeSymbol.unapply(sym).nonEmpty =>
1478+
prefix match {
1479+
case Types.EmptyPrefix() =>
1480+
this += highlightTypeDef(sym.name, color)
1481+
case _ =>
1482+
printTypeOrBound(prefix)
1483+
if (sym.name != "package")
1484+
this += "." += highlightTypeDef(sym.name, color)
1485+
this
1486+
}
1487+
14761488
case Type.TermRef(name, prefix) =>
14771489
prefix match {
14781490
case Type.ThisType(Types.EmptyPackage()) =>

0 commit comments

Comments
 (0)