Skip to content

Fix printing of package object symbol #4731

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions compiler/src/dotty/tools/dotc/printing/RefinedPrinter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -745,8 +745,12 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
case info: ImportType => return s"import $info.expr.show"
case _ =>
}
if (sym.is(ModuleClass))
kindString(sym) ~~ (nameString(sym.name.stripModuleClassSuffix) + idString(sym))
if (sym.is(ModuleClass)) {
val name =
if (sym.isPackageObject) sym.owner.name
else sym.name.stripModuleClassSuffix
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sym.name is package$ for package object

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense to me, but is this testable without too much effort? Otherwise I'll need to figure out an example to try this out... (that is, write a test.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added a test case

kindString(sym) ~~ (nameString(name) + idString(sym))
}
else
super.toText(sym)
}
Expand Down
27 changes: 27 additions & 0 deletions compiler/test/dotty/tools/dotc/printing/PrinterTests.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package dotty.tools.dotc.printing

import dotty.tools.DottyTest
import dotty.tools.dotc.ast.tpd
import dotty.tools.dotc.core.Names._
import dotty.tools.dotc.core.Symbols._
import org.junit.Assert.assertEquals
import org.junit.Test

class PrinterTests extends DottyTest {
import tpd._

@Test
def packageObject: Unit = {
val source = """
package object foo {
def bar: Int = 1
}
"""

checkCompile("frontend", source) { (tree, context) =>
implicit val ctx = context
val bar = tree.find(tree => tree.symbol.name == termName("bar")).get
assertEquals("package object foo", bar.symbol.owner.show)
}
}
}