Skip to content

Commit 42f8900

Browse files
committed
Homogenize package ids and literals
The unpickled term in a package id is different from the original but the type is the same. In a literal prefer constants in the types over thsoe in the terms. With this change, we get identical typed tree output also for core/pickled.
1 parent 83ac2af commit 42f8900

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,10 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
252252
prefix ~ (" extends" provided !ofNew) ~~ parentsText ~~ bodyText
253253
}
254254

255+
def toTextPackageId(pid: Tree): Text =
256+
if (homogenizedView) toTextLocal(pid.tpe)
257+
else toTextLocal(pid)
258+
255259
var txt: Text = tree match {
256260
case id: Trees.BackquotedIdent[_] if !homogenizedView =>
257261
"`" ~ toText(id.name) ~ "`"
@@ -282,7 +286,10 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
282286
case TypeApply(fun, args) =>
283287
toTextLocal(fun) ~ "[" ~ toTextGlobal(args, ", ") ~ "]"
284288
case Literal(c) =>
285-
toText(c)
289+
tree.typeOpt match {
290+
case ConstantType(tc) => toText(tc)
291+
case _ => toText(c)
292+
}
286293
case New(tpt) =>
287294
"new " ~ {
288295
tpt match {
@@ -404,7 +411,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
404411
}
405412
val bodyText =
406413
if (currentPrecedence == TopLevelPrec) "\n" ~ statsText else " {" ~ statsText ~ "}"
407-
"package " ~ toTextLocal(pid) ~ bodyText
414+
"package " ~ toTextPackageId(pid) ~ bodyText
408415
case tree: Template =>
409416
toTextTemplate(tree)
410417
case Annotated(annot, arg) =>
@@ -509,7 +516,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
509516

510517
def optText[T >: Untyped](tree: List[Tree[T]])(encl: Text => Text): Text =
511518
if (tree.exists(!_.isEmpty)) encl(blockText(tree)) else ""
512-
519+
513520
override protected def polyParamName(name: TypeName): TypeName =
514521
name.unexpandedName
515522

test/dotc/tests.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class tests extends CompilerTest {
2222
val doEmitBytecode = List("-Ystop-before:terminal")
2323
val failedbyName = List("-Ystop-before:collectEntryPoints") // #288
2424
val failedUnderscore = List("-Ystop-before:collectEntryPoints") // #289
25-
val testPickling = List("-Ytest-pickler", "-Ystop-after:pickler")
25+
val testPickling = List("-Xprint-types", "-Ytest-pickler", "-Ystop-after:pickler")
2626

2727
val failedOther = List("-Ystop-before:collectEntryPoints") // some non-obvious reason. need to look deeper
2828
val twice = List("#runs", "2", "-YnoDoubleBindings")
@@ -35,8 +35,8 @@ class tests extends CompilerTest {
3535
val newDir = "./tests/new/"
3636
val dotcDir = "./src/dotty/"
3737

38-
@Test def pickle_pickleOK = compileDir(posDir + "pickleOK/", "-Xprint-types" :: testPickling)
39-
@Test def pickle_pickling = compileDir(dotcDir + "tools/dotc/core/pickling", testPickling)
38+
@Test def pickle_pickleOK = compileDir(posDir + "pickleOK/", testPickling)
39+
@Test def pickle_pickling = compileDir(dotcDir + "tools/dotc/core/pickling/", testPickling)
4040

4141
@Test def pos_t2168_pat = compileFile(posDir, "t2168")
4242
@Test def pos_erasure = compileFile(posDir, "erasure")

0 commit comments

Comments
 (0)