Skip to content

Commit fb2e2d5

Browse files
committed
refactor printers
1 parent f0d0ac8 commit fb2e2d5

File tree

3 files changed

+20
-21
lines changed

3 files changed

+20
-21
lines changed

compiler/src/dotty/tools/dotc/printing/OutlinePrinter.scala

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,22 @@ class OutlinePrinter private (_ctx: Context) extends RefinedPrinter(_ctx) {
2222

2323
/* Typical patterns seen in output of typer for Java code, plus the output of unpickling an ELIDED tree */
2424
def isElidableExpr[T <: Untyped](tree: Tree[T]): Boolean = tree match {
25+
case tree if tree.isEmpty => false
2526
case tree: Ident[T] if tree.name == nme.WILDCARD => true // `ELIDED exprType`
2627
case tree: Literal[T] => true // e.g. `()`
2728
case tree: Select[T] if tree.symbol == defn.Predef_undefined => true // e.g. `Predef.???`
2829
case Apply(Select(tree: New[T], nme.CONSTRUCTOR), Nil)
2930
if tree.tpt.typeOpt.typeSymbol.is(Module) => true // e.g. `new foo.Foo$()` (rhs of a module val)
30-
case _ => false
31+
case _ =>
32+
sys.error(s"Unexpected tree in OutlinePrinter: ${tree.show}, $tree")
33+
false
3134
}
3235

33-
def elideExpr[T <: Untyped](tree: Tree[T], original: => Text): Text =
34-
if isElidableExpr(tree) then Str("_") else original
36+
override protected def rhsValDef[T <: Untyped](tree: ValDef[T]): Text =
37+
if isElidableExpr(tree.rhs) then " = " ~ "elided" ~ "[" ~ toText(tree.tpt) ~ "]"
38+
else super.rhsValDef(tree)
3539

36-
override protected def rhsValDef[T <: Untyped](rhs: Tree[T], original: => Text): Text =
37-
elideExpr(rhs, original)
38-
39-
override protected def rhsDefDef[T <: Untyped](rhs: Tree[T], original: => Text): Text =
40-
elideExpr(rhs, original)
40+
override protected def rhsDefDef[T <: Untyped](tree: DefDef[T]): Text =
41+
if isElidableExpr(tree.rhs) then " = " ~ "elided" ~ "[" ~ toText(tree.tpt) ~ "]"
42+
else super.rhsDefDef(tree)
4143
}

compiler/src/dotty/tools/dotc/printing/RefinedPrinter.scala

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ import dotty.tools.dotc.util.SourcePosition
2929
import dotty.tools.dotc.ast.untpd.{MemberDef, Modifiers, PackageDef, RefTree, Template, TypeDef, ValOrDefDef}
3030
import cc.{CaptureSet, CapturingType, toCaptureSet, IllegalCaptureRef}
3131

32-
import scala.annotation.unused
33-
3432
class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
3533

3634
/** A stack of enclosing DefDef, TypeDef, or ClassDef, or ModuleDefs nodes */
@@ -922,7 +920,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
922920
dclTextOr(tree) {
923921
modText(tree.mods, tree.symbol, keywordStr(if (tree.mods.is(Mutable)) "var" else "val"), isType = false) ~~
924922
valDefText(nameIdText(tree)) ~ optAscription(tree.tpt) ~
925-
withEnclosingDef(tree) { optText(tree.rhs)(rhs => " = " ~ rhsValDef(tree.rhs, rhs)) }
923+
withEnclosingDef(tree) { rhsValDef(tree) }
926924
}
927925
}
928926

@@ -979,19 +977,18 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
979977

980978
coreSig
981979
~ optAscription(tree.tpt)
982-
~ optText(tree.rhs)(rhs =>
983-
" = " ~ rhsDefDef(tree.rhs, keywordText("macro ").provided(tree.symbol.isScala2Macro) ~ rhs))
980+
~ rhsDefDef(tree)
984981
}
985982
}
986983
}
987984

988985
/** Inspect the rhs of a ValDef, overridden in OutlinePrinter */
989-
protected def rhsValDef[T <: Untyped](@unused("override may inspect rhs") rhs: Tree[T], original: => Text): Text =
990-
original
986+
protected def rhsValDef[T <: Untyped](tree: ValDef[T]): Text =
987+
optText(tree.rhs)(" = " ~ _)
991988

992989
/** Inspect the rhs of a DefDef, overridden in OutlinePrinter */
993-
protected def rhsDefDef[T <: Untyped](@unused("override may inspect rhs") rhs: Tree[T], original: => Text): Text =
994-
original
990+
protected def rhsDefDef[T <: Untyped](tree: DefDef[T]): Text =
991+
optText(tree.rhs)(" = " ~ keywordText("macro ").provided(tree.symbol.isScala2Macro) ~ _)
995992

996993
protected def toTextTemplate(impl: Template, ofNew: Boolean = false): Text = {
997994
val Template(constr @ DefDef(_, paramss, _, _), _, self, _) = impl

compiler/src/dotty/tools/dotc/transform/Pickler.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,8 @@ class Pickler extends Phase {
8787
Pickler.ParallelPickling && !ctx.settings.YtestPickler.value &&
8888
!ctx.settings.YjavaTasty.value // disable parallel pickling when `-Yjava-tasty` is set (internal testing only)
8989

90-
private def adjustPrinter(ictx: Context): Context =
91-
if ictx.compilationUnit.typedAsJava then
90+
private def adjustPrinter(ictx: Context, isOutline: Boolean): Context =
91+
if isOutline then
9292
// use special printer because Java parser will use `Predef.???` as rhs,
9393
// which conflicts with the unpickling of ELIDED as `Ident(nme.WILDCARD).withType(tpe)`
9494
// In the future we could modify the typer/parser to elide the rhs in the same way.
@@ -105,7 +105,7 @@ class Pickler extends Phase {
105105
tree <- sliceTopLevel(unit.tpdTree, cls)
106106
do
107107
if ctx.settings.YtestPickler.value then
108-
beforePickling(cls) = tree.show(using adjustPrinter(ctx))
108+
beforePickling(cls) = tree.show(using adjustPrinter(ctx, unit.typedAsJava))
109109

110110
val sourceRelativePath =
111111
val reference = ctx.settings.sourceroot.value
@@ -260,7 +260,7 @@ class Pickler extends Phase {
260260
val freshUnit = CompilationUnit(rootCtx.compilationUnit.source)
261261
freshUnit.needsCaptureChecking = unit.needsCaptureChecking
262262
freshUnit.knowsPureFuns = unit.knowsPureFuns
263-
inContext(adjustPrinter(rootCtx.fresh.setCompilationUnit(freshUnit))):
263+
inContext(adjustPrinter(rootCtx.fresh.setCompilationUnit(freshUnit), unit.typedAsJava)):
264264
testSame(i"$unpickled%\n%", beforePickling(cls), cls)
265265

266266
private def testSame(unpickled: String, previous: String, cls: ClassSymbol)(using Context) =

0 commit comments

Comments
 (0)