Skip to content

Commit f7e9af9

Browse files
committed
Adapt RefinedPrinter to eliminate duplication in UserFacingPrinter
1 parent e0139bf commit f7e9af9

File tree

4 files changed

+18
-32
lines changed

4 files changed

+18
-32
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,11 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
662662
override protected def keyString(sym: Symbol): String = {
663663
val flags = sym.flagsUNSAFE
664664
if (sym.isType && sym.owner.isTerm) ""
665+
else if (sym.isPackageObject) "package object"
666+
else if (flags.is(Module) && flags.is(Case)) "case object"
667+
else if (sym.isClass && flags.is(Case)) "case class"
668+
else if (flags is Module) "object"
669+
else if (sym.isTerm && !flags.is(Param) && flags.is(Implicit)) "implicit val"
665670
else super.keyString(sym)
666671
}
667672

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

Lines changed: 10 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ import core._
55
import Constants.Constant, Contexts.Context, Denotations._, Flags._, Names._
66
import NameOps._, StdNames._, Decorators._, Scopes.Scope, Types._, Texts._
77
import SymDenotations.NoDenotation, Symbols.{ Symbol, ClassSymbol, defn }
8-
import typer.Implicits.SearchResult
9-
import typer.ImportInfo
108

119
class UserFacingPrinter(_ctx: Context) extends RefinedPrinter(_ctx) {
1210

@@ -29,17 +27,8 @@ class UserFacingPrinter(_ctx: Context) extends RefinedPrinter(_ctx) {
2927

3028
def wellKnownPkg(pkgSym: Symbol) = standardPkg(pkgSym) || wrappedName(pkgSym)
3129

32-
override protected def keyString(sym: Symbol): String = {
33-
val flags = sym.flagsUNSAFE
34-
if (flags is Package) ""
35-
else if (sym.isPackageObject) "package object"
36-
else if (flags.is(Module) && flags.is(Case)) "case object"
37-
else if (sym.isClass && flags.is(Case)) "case class"
38-
else if (flags.is(Lazy)) "lazy val"
39-
else if (flags is Module) "object"
40-
else if (sym.isTerm && !flags.is(Param) && flags.is(Implicit)) "implicit val"
41-
else super.keyString(sym)
42-
}
30+
override protected def keyString(sym: Symbol): String =
31+
if (sym.flagsUNSAFE is Package) "" else super.keyString(sym)
4332

4433
override def nameString(name: Name): String =
4534
if (name.isReplAssignName) name.decode.toString.takeWhile(_ != '$')
@@ -49,34 +38,24 @@ class UserFacingPrinter(_ctx: Context) extends RefinedPrinter(_ctx) {
4938
if (sym.name.isReplAssignName) nameString(sym.name)
5039
else keyString(sym) ~~ nameString(sym.name.stripModuleClassSuffix)
5140

52-
override def dclText(sym: Symbol): Text =
53-
toText(sym) ~ {
54-
if (sym.is(Method)) toText(sym.info)
55-
else if (sym.isClass) ""
56-
else if (sym.isType && sym.info.isInstanceOf[TypeAlias]) toText(sym.info)
57-
else if (sym.isType) ""
58-
else {
59-
":" ~~ toText(sym.info)
60-
}
61-
}
41+
override def dclText(sym: Symbol): Text = toText(sym) ~ {
42+
if (sym.is(Method)) toText(sym.info)
43+
else if (sym.isType && sym.info.isInstanceOf[TypeAlias]) toText(sym.info)
44+
else if (sym.isType || sym.isClass) ""
45+
else ":" ~~ toText(sym.info)
46+
}
6247

6348
override def toText(const: Constant): Text = Str(const.value.toString)
6449

6550
override def toText(tp: Type): Text = tp match {
66-
case tp: ConstantType => toText(tp.value)
6751
case ExprType(result) => ":" ~~ toText(result)
52+
case tp: ConstantType => toText(tp.value)
6853
case tp: TypeRef => tp.info match {
6954
case TypeAlias(alias) => toText(alias)
7055
case _ => toText(tp.info)
7156
}
72-
case tp: ParamRef => {
73-
val name = ParamRefNameString(tp.paramName)
74-
if (tp.isInstanceOf[TermParamRef]) name ~ ".type"
75-
else name
76-
}
7757
case tp: ClassInfo => {
78-
if (wellKnownPkg(tp.cls.owner))
79-
nameString(tp.cls.name)
58+
if (wellKnownPkg(tp.cls.owner)) nameString(tp.cls.name)
8059
else {
8160
def printPkg(sym: ClassSymbol): Text =
8261
if (sym.owner == defn.RootClass || wrappedName(sym.owner))
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
scala> val toInt: Any => Int = new { def apply(a: Any) = 1; override def toString() = "<func1>" }
2+
val toInt: Any => Int = <func1>

compiler/test/dotty/tools/dotc/reporting/ErrorMessagesTests.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ class ErrorMessagesTests extends ErrorMessagesTest {
365365
@Test def methodDoesNotTakePrameters =
366366
checkMessagesAfter("frontend") {
367367
"""
368-
|object Scope{
368+
|object Scope {
369369
| def foo = ()
370370
| foo()
371371
|}

0 commit comments

Comments
 (0)