Skip to content

Commit ba5cbd8

Browse files
Merge pull request #4706 from dotty-staging/decompile-fix-packages
Fix printing package objects and nested packages
2 parents fce7ef8 + 8778218 commit ba5cbd8

File tree

3 files changed

+49
-11
lines changed

3 files changed

+49
-11
lines changed

library/src/scala/tasty/util/ShowSourceCode.scala

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,22 +41,31 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
4141
def doubleLineBreak(): String = "\n\n" + (" " * indent)
4242

4343
def printTree(tree: Tree): Buffer = tree match {
44-
case tree @ PackageClause(Term.Ident(name), stats) =>
44+
case PackageObject(body)=>
45+
printTree(body) // Print package object
46+
47+
case PackageClause(Term.Ident(name), (inner @ PackageClause(_, _)) :: Nil) if name != "<empty>" && PackageObject.unapply(inner).isEmpty =>
48+
// print inner package as `package outer.inner { ... }`
49+
printTree(inner)
50+
51+
case tree @ PackageClause(name, stats) =>
4552
val stats1 = stats.collect {
4653
case stat @ PackageClause(_, _) => stat
4754
case stat @ Definition() if !(stat.flags.isObject && stat.flags.isLazy) => stat
4855
case stat @ Import(_, _) => stat
4956
}
50-
51-
if (name == "<empty>") {
52-
printTrees(stats1, lineBreak())
53-
} else {
54-
this += "package " += name += " {"
55-
indented {
56-
this += lineBreak()
57+
name match {
58+
case Term.Ident("<empty>") =>
5759
printTrees(stats1, lineBreak())
58-
}
59-
this += lineBreak() += "}"
60+
case _ =>
61+
this += "package "
62+
printType(name.tpe)
63+
this += " {"
64+
indented {
65+
this += lineBreak()
66+
printTrees(stats1, lineBreak())
67+
}
68+
this += lineBreak() += "}"
6069
}
6170

6271
case Import(expr, selectors) =>
@@ -74,7 +83,11 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
7483
if (flags.isFinal && !flags.isObject) this += "final "
7584
if (flags.isCase) this += "case "
7685

77-
if (flags.isObject) this += "object " += name.stripSuffix("$")
86+
if (name == "package$") {
87+
this += "package object "
88+
printDefinitionName(cdef.owner)
89+
}
90+
else if (flags.isObject) this += "object " += name.stripSuffix("$")
7891
else if (flags.isTrait) this += "trait " += name
7992
else if (flags.isAbstract) this += "abstract class " += name
8093
else this += "class " += name
@@ -1136,5 +1149,11 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
11361149
}
11371150
}
11381151

1152+
object PackageObject {
1153+
def unapply(tree: Tree)(implicit ctx: Context): Option[Tree] = tree match {
1154+
case PackageClause(_, ValDef("package", _, _) :: body :: Nil) => Some(body)
1155+
case _ => None
1156+
}
1157+
}
11391158

11401159
}

tests/pos/t6225b.decompiled

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/** Decompiled from out/posTestFromTasty/pos/t6225b/library/x/X.class */
2+
package library.x {
3+
class X() {
4+
class Foo()
5+
}
6+
}
7+
/** Decompiled from out/posTestFromTasty/pos/t6225b/library/y/package.class */
8+
package library {
9+
package object y extends library.x.X()
10+
}

tests/pos/t6225b.scala

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
package library.x {
3+
class X {
4+
class Foo
5+
}
6+
}
7+
package library {
8+
package object y extends library.x.X
9+
}

0 commit comments

Comments
 (0)