Skip to content

Commit 176c787

Browse files
committed
Move more logic from RefinedPrinter to DecompilerPrinter
RefinedPrinter cannot make use of symbols in order to be able to print after all phases (e.g. Parser).
1 parent 6971c0d commit 176c787

File tree

2 files changed

+11
-16
lines changed

2 files changed

+11
-16
lines changed

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,16 @@ class DecompilerPrinter(_ctx: Context) extends RefinedPrinter(_ctx) {
5454
decl ~~ typeText(nameIdText(tree)) ~ withEnclosingDef(tree) { toTextTemplate(impl) } ~ ""
5555
}
5656

57-
override protected def selfToText(impl: Template): Text =
58-
super.selfToText(impl).provided(!impl.constr.symbol.owner.is(Module))
59-
6057
override protected def toTextTemplate(impl: Template, ofNew: Boolean = false): Text = {
6158
def isSyntheticParent(sym: Symbol): Boolean = {
6259
sym.maybeOwner == defn.ObjectClass ||
6360
(sym == defn.ProductClass && impl.symbol.owner.is(Case))
6461
}
65-
val impl1 = impl.copy(parents = impl.parents.filterNot(p => isSyntheticParent(p.symbol)))
62+
val impl1 = impl.copy(
63+
constr = impl.constr.copy(vparamss = Nil), // don't print constructor for objects
64+
parents = impl.parents.filterNot(p => isSyntheticParent(p.symbol)),
65+
self = theEmptyValDef // don't print self type for objects
66+
)
6667
super.toTextTemplate(impl1, ofNew)
6768
}
6869

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

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -640,16 +640,18 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
640640
val tparamsTxt = withEnclosingDef(constr) { tparamsText(tparams) }
641641
val primaryConstrs = if (constr.rhs.isEmpty) Nil else constr :: Nil
642642
val prefix: Text =
643-
if (constr.symbol.owner.is(Module)) ""
644-
else if (vparamss.isEmpty || primaryConstrs.nonEmpty) tparamsTxt
643+
if (vparamss.isEmpty || primaryConstrs.nonEmpty) tparamsTxt
645644
else {
646645
var modsText = modText(constr.mods, "")
647646
if (!modsText.isEmpty) modsText = " " ~ modsText
648647
if (constr.mods.hasAnnotations && !constr.mods.hasFlags) modsText = modsText ~~ " this"
649648
withEnclosingDef(constr) { addVparamssText(tparamsTxt ~~ modsText, vparamss) }
650649
}
651650
val parentsText = Text(parents map constrText, keywordStr(" with "))
652-
val selfText = selfToText(impl)
651+
val selfText = {
652+
val selfName = if (self.name == nme.WILDCARD) keywordStr("this") else self.name.toString
653+
(selfName ~ optText(self.tpt)(": " ~ _) ~ " =>").close
654+
}.provided(!self.isEmpty)
653655
val body = if (ctx.settings.YtestPickler.value) {
654656
// Pickling/unpickling reorders the body members, so we need to homogenize
655657
val (params, rest) = impl.body partition {
@@ -661,19 +663,11 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
661663
params ::: rest
662664
} else impl.body
663665

664-
val bodyText =
665-
(if (constr.symbol.owner.is(Module)) " {" else "{") ~~
666-
selfText ~~ toTextGlobal(primaryConstrs ::: body, "\n") ~ "}"
666+
val bodyText = "{" ~~ selfText ~~ toTextGlobal(primaryConstrs ::: body, "\n") ~ "}"
667667

668668
prefix ~ (keywordText(" extends") provided (!ofNew && parents.nonEmpty)) ~~ parentsText ~~ bodyText
669669
}
670670

671-
protected def selfToText(impl: Template): Text = {
672-
val self = impl.self
673-
val selfName = if (self.name == nme.WILDCARD) keywordStr("this") else self.name.toString
674-
(selfName ~ optText(self.tpt)(": " ~ _) ~ " =>").close provided (!self.isEmpty)
675-
}
676-
677671
protected def templateText(tree: TypeDef, impl: Template): Text = {
678672
val decl = modText(tree.mods, keywordStr(if ((tree).mods is Trait) "trait" else "class"))
679673
decl ~~ typeText(nameIdText(tree)) ~ withEnclosingDef(tree) { toTextTemplate(impl) } ~

0 commit comments

Comments
 (0)