diff --git a/compiler/src/dotty/tools/dotc/printing/RefinedPrinter.scala b/compiler/src/dotty/tools/dotc/printing/RefinedPrinter.scala index f1cf87ad9c1f..b64ec3a5ad92 100644 --- a/compiler/src/dotty/tools/dotc/printing/RefinedPrinter.scala +++ b/compiler/src/dotty/tools/dotc/printing/RefinedPrinter.scala @@ -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 + kindString(sym) ~~ (nameString(name) + idString(sym)) + } else super.toText(sym) } diff --git a/compiler/test/dotty/tools/dotc/printing/PrinterTests.scala b/compiler/test/dotty/tools/dotc/printing/PrinterTests.scala new file mode 100644 index 000000000000..629f8529a143 --- /dev/null +++ b/compiler/test/dotty/tools/dotc/printing/PrinterTests.scala @@ -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) + } + } +}