Skip to content

Commit b699650

Browse files
committed
Support show on Type[T]
1 parent beea2b5 commit b699650

File tree

5 files changed

+38
-17
lines changed

5 files changed

+38
-17
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-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 (val outDir: Option[String], val showRawTree: Boolean, val compilerArgs: List[String], val color: Boolean)
3637

3738
object Settings {
3839

library/src/scala/tasty/reflect/Printers.scala

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1360,6 +1360,9 @@ trait Printers
13601360
case tpe @ Type.SymRef(IsClassSymbol(sym), _) if sym.name.endsWith("$") =>
13611361
printType(tpe)
13621362
this += ".type"
1363+
case tpe @ Type.SymRef(sym, _) if !IsTypeSymbol.unapply(sym).nonEmpty =>
1364+
printType(tpe)
1365+
this += ".type"
13631366
case tpe => printType(tpe)
13641367
}
13651368
printTypeAndAnnots(tree.tpe)
@@ -1442,7 +1445,7 @@ trait Printers
14421445
case Type.ConstantType(const) =>
14431446
printConstant(const)
14441447

1445-
case Type.SymRef(sym, prefix) =>
1448+
case Type.SymRef(sym, prefix) if IsTypeSymbol.unapply(sym).nonEmpty =>
14461449
prefix match {
14471450
case Type.ThisType(Types.EmptyPackage() | Types.RootPackage()) =>
14481451
case NoPrefix() =>
@@ -1456,13 +1459,22 @@ trait Printers
14561459
printType(prefix)
14571460
this += "#"
14581461
case IsType(prefix) =>
1459-
if (!sym.flags.is(Flags.Local)) {
1460-
printType(prefix)
1461-
this += "."
1462-
}
1462+
printType(prefix)
1463+
this += "."
14631464
}
14641465
this += highlightTypeDef(sym.name.stripSuffix("$"), color)
14651466

1467+
case Type.SymRef(sym, prefix) if !IsTypeSymbol.unapply(sym).nonEmpty =>
1468+
prefix match {
1469+
case NoPrefix() =>
1470+
this += highlightTypeDef(sym.name, color)
1471+
case _ =>
1472+
printTypeOrBound(prefix)
1473+
if (sym.name != "package")
1474+
this += "." += highlightTypeDef(sym.name, color)
1475+
this
1476+
}
1477+
14661478
case Type.TermRef(name, prefix) =>
14671479
prefix match {
14681480
case Type.ThisType(Types.EmptyPackage()) =>

0 commit comments

Comments
 (0)