Skip to content

Commit 5ff7cf4

Browse files
committed
Fix: printing modifiers
When printing trees, modifiers were always taken from the tree. This is wrong for typed trees, since their flags and annotations come from the symbol, the tree modifiers are no longer relevant.
1 parent 3268106 commit 5ff7cf4

File tree

2 files changed

+17
-15
lines changed

2 files changed

+17
-15
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ class DecompilerPrinter(_ctx: Context) extends RefinedPrinter(_ctx) {
4949

5050
override protected def templateText(tree: TypeDef, impl: Template): Text = {
5151
val decl =
52-
if (!tree.mods.is(Module)) modText(tree.mods, keywordStr(if ((tree).mods is Trait) "trait" else "class"))
53-
else modText(tree.mods &~ (Final | Module), keywordStr("object"))
52+
if (!tree.mods.is(Module)) modText(tree.mods, tree.symbol, keywordStr(if ((tree).mods is Trait) "trait" else "class"))
53+
else modText(tree.mods, tree.symbol, keywordStr("object"), suppress = Final | Module)
5454
decl ~~ typeText(nameIdText(tree)) ~ withEnclosingDef(tree) { toTextTemplate(impl) } ~ ""
5555
}
5656

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

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
411411
case tree @ TypeDef(name, rhs) =>
412412
def typeDefText(tparamsText: => Text, rhsText: => Text) =
413413
dclTextOr(tree) {
414-
modText(tree.mods, keywordStr("type")) ~~ (varianceText(tree.mods) ~ typeText(nameIdText(tree))) ~
414+
modText(tree.mods, tree.symbol, keywordStr("type")) ~~ (varianceText(tree.mods) ~ typeText(nameIdText(tree))) ~
415415
withEnclosingDef(tree) { tparamsText ~ rhsText }
416416
}
417417
def recur(rhs: Tree, tparamsTxt: => Text): Text = rhs match {
@@ -449,7 +449,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
449449
toText(t)
450450
case tree @ ModuleDef(name, impl) =>
451451
withEnclosingDef(tree) {
452-
modText(tree.mods, keywordStr("object")) ~~ nameIdText(tree) ~ toTextTemplate(impl)
452+
modText(tree.mods, NoSymbol, keywordStr("object")) ~~ nameIdText(tree) ~ toTextTemplate(impl)
453453
}
454454
case SymbolLit(str) =>
455455
"'" + str
@@ -506,7 +506,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
506506
t ~ cxBoundToText(cxb)
507507
}
508508
case PatDef(mods, pats, tpt, rhs) =>
509-
modText(mods, keywordStr("val")) ~~ toText(pats, ", ") ~ optAscription(tpt) ~
509+
modText(mods, NoSymbol, keywordStr("val")) ~~ toText(pats, ", ") ~ optAscription(tpt) ~
510510
optText(rhs)(" = " ~ _)
511511
case ParsedTry(expr, handler, finalizer) =>
512512
changePrec(GlobalPrec) {
@@ -618,7 +618,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
618618
protected def valDefToText[T >: Untyped](tree: ValDef[T]): Text = {
619619
import untpd.{modsDeco => _, _}
620620
dclTextOr(tree) {
621-
modText(tree.mods, keywordStr(if (tree.mods is Mutable) "var" else "val")) ~~
621+
modText(tree.mods, tree.symbol, keywordStr(if (tree.mods is Mutable) "var" else "val")) ~~
622622
valDefText(nameIdText(tree)) ~ optAscription(tree.tpt) ~
623623
withEnclosingDef(tree) { optText(tree.rhs)(" = " ~ _) }
624624
}
@@ -627,7 +627,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
627627
protected def defDefToText[T >: Untyped](tree: DefDef[T]): Text = {
628628
import untpd.{modsDeco => _, _}
629629
dclTextOr(tree) {
630-
val prefix = modText(tree.mods, keywordStr("def")) ~~ valDefText(nameIdText(tree))
630+
val prefix = modText(tree.mods, tree.symbol, keywordStr("def")) ~~ valDefText(nameIdText(tree))
631631
withEnclosingDef(tree) {
632632
addVparamssText(prefix ~ tparamsText(tree.tparams), tree.vparamss) ~ optAscription(tree.tpt) ~
633633
optText(tree.rhs)(" = " ~ _)
@@ -642,7 +642,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
642642
val prefix: Text =
643643
if (vparamss.isEmpty || primaryConstrs.nonEmpty) tparamsTxt
644644
else {
645-
var modsText = modText(constr.mods, "")
645+
var modsText = modText(constr.mods, constr.symbol, "")
646646
if (!modsText.isEmpty) modsText = " " ~ modsText
647647
if (constr.mods.hasAnnotations && !constr.mods.hasFlags) modsText = modsText ~~ " this"
648648
withEnclosingDef(constr) { addVparamssText(tparamsTxt ~~ modsText, vparamss) }
@@ -670,7 +670,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
670670
}
671671

672672
protected def templateText(tree: TypeDef, impl: Template): Text = {
673-
val decl = modText(tree.mods, keywordStr(if ((tree).mods is Trait) "trait" else "class"))
673+
val decl = modText(tree.mods, tree.symbol, keywordStr(if ((tree).mods is Trait) "trait" else "class"))
674674
decl ~~ typeText(nameIdText(tree)) ~ withEnclosingDef(tree) { toTextTemplate(impl) } ~
675675
(if (tree.hasType && ctx.settings.verbose.value) i"[decls = ${tree.symbol.info.decls}]" else "")
676676
}
@@ -693,16 +693,18 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
693693

694694
protected def annotText(tree: untpd.Tree): Text = "@" ~ constrText(tree) // DD
695695

696-
protected def modText(mods: untpd.Modifiers, kw: String): Text = { // DD
696+
protected def modText(mods: untpd.Modifiers, sym: Symbol, kw: String, suppress: FlagSet = EmptyFlags): Text = { // DD
697697
val suppressKw = if (enclDefIsClass) mods is ParamAndLocal else mods is Param
698698
var flagMask =
699699
if (ctx.settings.YdebugFlags.value) AnyFlags
700-
else if (suppressKw) PrintableFlags &~ Private
701-
else PrintableFlags
700+
else if (suppressKw) PrintableFlags &~ Private &~ suppress
701+
else PrintableFlags &~ suppress
702702
if (homogenizedView && mods.flags.isTypeFlags) flagMask &~= Implicit // drop implicit from classes
703-
val flags = mods.flags & flagMask
704-
val flagsText = if (flags.isEmpty) "" else keywordStr((mods.flags & flagMask).toString)
705-
val annotations = filterModTextAnnots(mods.annotations)
703+
val flags = (if (sym.exists) sym.flags else (mods.flags)) & flagMask
704+
val flagsText = if (flags.isEmpty) "" else keywordStr(flags.toString)
705+
val annotations = filterModTextAnnots(
706+
if (sym.exists) sym.annotations.filterNot(_.isInstanceOf[Annotations.BodyAnnotation]).map(_.tree)
707+
else mods.annotations)
706708
Text(annotations.map(annotText), " ") ~~ flagsText ~~ (Str(kw) provided !suppressKw)
707709
}
708710

0 commit comments

Comments
 (0)